Palindrome checking function

Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
14
down vote
favorite
I recently did a little task for fun that required me to create a class to determine if a word is a palindrome or not. I know this is quite simple, but I'd be interested to know if a real developer (I'm not a programmer) would approach the task in the same way, or if I make any amateur mistakes/do anything stupid.
My code passed the automated tests on the website, but I understand those aren't perfect.
class Palindrome
public static function isPalindrome($word)
$word = strtolower($word);
$wordLength = strlen($word);
$wordSplitPoint = ceil($wordLength / 2);
if ($wordLength % 2 == 0)
$firstHalf = substr($word, 0, $wordSplitPoint);
$secondHalf = substr($word, $wordSplitPoint, $wordSplitPoint);
else
$firstHalf = substr($word, 0, $wordSplitPoint-1);
$secondHalf = substr($word, $wordSplitPoint, $wordSplitPoint);
$secondHalfReversed = strrev($secondHalf);
if ($firstHalf == $secondHalfReversed)
return TRUE;
else
return FALSE;
echo Palindrome::isPalindrome('Deleveled');
php strings palindrome
add a comment |Â
up vote
14
down vote
favorite
I recently did a little task for fun that required me to create a class to determine if a word is a palindrome or not. I know this is quite simple, but I'd be interested to know if a real developer (I'm not a programmer) would approach the task in the same way, or if I make any amateur mistakes/do anything stupid.
My code passed the automated tests on the website, but I understand those aren't perfect.
class Palindrome
public static function isPalindrome($word)
$word = strtolower($word);
$wordLength = strlen($word);
$wordSplitPoint = ceil($wordLength / 2);
if ($wordLength % 2 == 0)
$firstHalf = substr($word, 0, $wordSplitPoint);
$secondHalf = substr($word, $wordSplitPoint, $wordSplitPoint);
else
$firstHalf = substr($word, 0, $wordSplitPoint-1);
$secondHalf = substr($word, $wordSplitPoint, $wordSplitPoint);
$secondHalfReversed = strrev($secondHalf);
if ($firstHalf == $secondHalfReversed)
return TRUE;
else
return FALSE;
echo Palindrome::isPalindrome('Deleveled');
php strings palindrome
5
Perhaps this is "outside" of the question/problem, but: Why is it in a class? Why not just make a standalone function? I don't know PHP, but to my understanding, it doesn't mandate top-level classes like Java does.
â Quelklef
May 22 at 2:49
1
@Quelklef The task specifically asks for it to be in a class and only allows you to modify what is contained within theisPalindromefunction
â Matadeleo
May 22 at 9:07
add a comment |Â
up vote
14
down vote
favorite
up vote
14
down vote
favorite
I recently did a little task for fun that required me to create a class to determine if a word is a palindrome or not. I know this is quite simple, but I'd be interested to know if a real developer (I'm not a programmer) would approach the task in the same way, or if I make any amateur mistakes/do anything stupid.
My code passed the automated tests on the website, but I understand those aren't perfect.
class Palindrome
public static function isPalindrome($word)
$word = strtolower($word);
$wordLength = strlen($word);
$wordSplitPoint = ceil($wordLength / 2);
if ($wordLength % 2 == 0)
$firstHalf = substr($word, 0, $wordSplitPoint);
$secondHalf = substr($word, $wordSplitPoint, $wordSplitPoint);
else
$firstHalf = substr($word, 0, $wordSplitPoint-1);
$secondHalf = substr($word, $wordSplitPoint, $wordSplitPoint);
$secondHalfReversed = strrev($secondHalf);
if ($firstHalf == $secondHalfReversed)
return TRUE;
else
return FALSE;
echo Palindrome::isPalindrome('Deleveled');
php strings palindrome
I recently did a little task for fun that required me to create a class to determine if a word is a palindrome or not. I know this is quite simple, but I'd be interested to know if a real developer (I'm not a programmer) would approach the task in the same way, or if I make any amateur mistakes/do anything stupid.
My code passed the automated tests on the website, but I understand those aren't perfect.
class Palindrome
public static function isPalindrome($word)
$word = strtolower($word);
$wordLength = strlen($word);
$wordSplitPoint = ceil($wordLength / 2);
if ($wordLength % 2 == 0)
$firstHalf = substr($word, 0, $wordSplitPoint);
$secondHalf = substr($word, $wordSplitPoint, $wordSplitPoint);
else
$firstHalf = substr($word, 0, $wordSplitPoint-1);
$secondHalf = substr($word, $wordSplitPoint, $wordSplitPoint);
$secondHalfReversed = strrev($secondHalf);
if ($firstHalf == $secondHalfReversed)
return TRUE;
else
return FALSE;
echo Palindrome::isPalindrome('Deleveled');
php strings palindrome
edited May 21 at 17:27
Sam Onela
5,75961543
5,75961543
asked May 21 at 17:16
Matadeleo
18317
18317
5
Perhaps this is "outside" of the question/problem, but: Why is it in a class? Why not just make a standalone function? I don't know PHP, but to my understanding, it doesn't mandate top-level classes like Java does.
â Quelklef
May 22 at 2:49
1
@Quelklef The task specifically asks for it to be in a class and only allows you to modify what is contained within theisPalindromefunction
â Matadeleo
May 22 at 9:07
add a comment |Â
5
Perhaps this is "outside" of the question/problem, but: Why is it in a class? Why not just make a standalone function? I don't know PHP, but to my understanding, it doesn't mandate top-level classes like Java does.
â Quelklef
May 22 at 2:49
1
@Quelklef The task specifically asks for it to be in a class and only allows you to modify what is contained within theisPalindromefunction
â Matadeleo
May 22 at 9:07
5
5
Perhaps this is "outside" of the question/problem, but: Why is it in a class? Why not just make a standalone function? I don't know PHP, but to my understanding, it doesn't mandate top-level classes like Java does.
â Quelklef
May 22 at 2:49
Perhaps this is "outside" of the question/problem, but: Why is it in a class? Why not just make a standalone function? I don't know PHP, but to my understanding, it doesn't mandate top-level classes like Java does.
â Quelklef
May 22 at 2:49
1
1
@Quelklef The task specifically asks for it to be in a class and only allows you to modify what is contained within the
isPalindrome functionâ Matadeleo
May 22 at 9:07
@Quelklef The task specifically asks for it to be in a class and only allows you to modify what is contained within the
isPalindrome functionâ Matadeleo
May 22 at 9:07
add a comment |Â
5 Answers
5
active
oldest
votes
up vote
33
down vote
accepted
Others have pointed out a fix you could make to your implementation. But there is a way to write this which is much simpler. That takes advantage of a simplified definition of palindrome: "a word which reads the same forwards and backwards".
Now I haven't done PHP in years, and don't have a test environment, so this might have issues, but the basic idea should be clear.
public static function isPalindrome($word)
return $word == strrev($word);
Update
Based on suggestions in the comments, this would be nicer:
function isPalindrome($word)
$word = preg_replace('/[^a-zA-Z]/', '', $word);
$word = strtolower($word);
return $word == strrev($word);
That will now correctly identify "Level" (ignoring capitalization) and "Madam, I'm Adam." (ignoring spaces and punctuation.)
Smart :) I like it!
â Matadeleo
May 21 at 22:12
And what is the reason of the divorce?
â Billal BEGUERADJ
May 22 at 10:53
3
You should make $word lowercase before you do the return. Else "Level" will not be valid as a palindrome.
â Tonny
May 22 at 11:26
1
this is exactly what i was thinking :) I agree with Tonny, but would also add something to strip white spaces so that "taco cat" evaluates as true
â Doug
May 22 at 12:58
2
@BillalBEGUERADJ: I never did much with it. But I absolutely prefer functional languages: ML, Haskell, OCaml, Erlang, various LISPs, and usually program by day in JavaScript.
â Scott Sauyet
May 22 at 14:13
 |Â
show 2 more comments
up vote
7
down vote
To start with id remove the code that you written twice by re-arranging the if statement into this
if ($wordLength % 2 == 0)
$firstHalf = substr($word, 0, $wordSplitPoint);
else
$firstHalf = substr($word, 0, $wordSplitPoint-1);
$secondHalf = substr($word, $wordSplitPoint, $wordSplitPoint);
Then for the return value I would use;
return $firstHalf == $secondHalfReversed;
This is about all i would change
Thanks, no reason why the$secondHalfshould be repeated, and that return is much cleaner
â Matadeleo
May 21 at 18:35
add a comment |Â
up vote
6
down vote
This is a very good program. There is only one thing I'd change:
if ($firstHalf == $secondHalfReversed)
return TRUE;
else
return FALSE;
You don't really need that if because the top part only returns TRUE when the condition also is TRUE. Otherwise, both the condition and the return is FALSE. Change it to:
return $firstHalf == $secondHalfReversed
add a comment |Â
up vote
2
down vote
I'd write this using a traditional for loop as follows:
class Palindrome
public static function isPalindrome($word)
$wordLength = strlen($word)-1;
for ($i = 0; $i < $wordLength/2; $i++)
if (strtolower($word[$i]) != strtolower($word[$wordLength-$i]))
return FALSE;
return TRUE;
echo Palindrome::isPalindrome('Deleveled');
1
Pedantically, this approach would be less computing power than the other approaches.
â Kenneth K.
May 22 at 13:17
add a comment |Â
up vote
0
down vote
echo Palindrome::isPalindrome('Deleveled');
You need to test corner cases and try to break your code. For example, you might try with:
- a string of odd size
- a string of even size
- the empty string
- a string with one character
How is size defined anyway? What about encodings? For example, your code works for '101', but not 'é_é'?
The task I completed on the website wanted a function that could detect single-word palindromes of varying lengths. But you're right. Edge cases are something I need to consider more. I'm trying hard to leave the world of "duck-tape" php behind. "It only broke because you aren't using it correctly" is a terrible philosophy :)
â Matadeleo
May 23 at 9:14
add a comment |Â
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
33
down vote
accepted
Others have pointed out a fix you could make to your implementation. But there is a way to write this which is much simpler. That takes advantage of a simplified definition of palindrome: "a word which reads the same forwards and backwards".
Now I haven't done PHP in years, and don't have a test environment, so this might have issues, but the basic idea should be clear.
public static function isPalindrome($word)
return $word == strrev($word);
Update
Based on suggestions in the comments, this would be nicer:
function isPalindrome($word)
$word = preg_replace('/[^a-zA-Z]/', '', $word);
$word = strtolower($word);
return $word == strrev($word);
That will now correctly identify "Level" (ignoring capitalization) and "Madam, I'm Adam." (ignoring spaces and punctuation.)
Smart :) I like it!
â Matadeleo
May 21 at 22:12
And what is the reason of the divorce?
â Billal BEGUERADJ
May 22 at 10:53
3
You should make $word lowercase before you do the return. Else "Level" will not be valid as a palindrome.
â Tonny
May 22 at 11:26
1
this is exactly what i was thinking :) I agree with Tonny, but would also add something to strip white spaces so that "taco cat" evaluates as true
â Doug
May 22 at 12:58
2
@BillalBEGUERADJ: I never did much with it. But I absolutely prefer functional languages: ML, Haskell, OCaml, Erlang, various LISPs, and usually program by day in JavaScript.
â Scott Sauyet
May 22 at 14:13
 |Â
show 2 more comments
up vote
33
down vote
accepted
Others have pointed out a fix you could make to your implementation. But there is a way to write this which is much simpler. That takes advantage of a simplified definition of palindrome: "a word which reads the same forwards and backwards".
Now I haven't done PHP in years, and don't have a test environment, so this might have issues, but the basic idea should be clear.
public static function isPalindrome($word)
return $word == strrev($word);
Update
Based on suggestions in the comments, this would be nicer:
function isPalindrome($word)
$word = preg_replace('/[^a-zA-Z]/', '', $word);
$word = strtolower($word);
return $word == strrev($word);
That will now correctly identify "Level" (ignoring capitalization) and "Madam, I'm Adam." (ignoring spaces and punctuation.)
Smart :) I like it!
â Matadeleo
May 21 at 22:12
And what is the reason of the divorce?
â Billal BEGUERADJ
May 22 at 10:53
3
You should make $word lowercase before you do the return. Else "Level" will not be valid as a palindrome.
â Tonny
May 22 at 11:26
1
this is exactly what i was thinking :) I agree with Tonny, but would also add something to strip white spaces so that "taco cat" evaluates as true
â Doug
May 22 at 12:58
2
@BillalBEGUERADJ: I never did much with it. But I absolutely prefer functional languages: ML, Haskell, OCaml, Erlang, various LISPs, and usually program by day in JavaScript.
â Scott Sauyet
May 22 at 14:13
 |Â
show 2 more comments
up vote
33
down vote
accepted
up vote
33
down vote
accepted
Others have pointed out a fix you could make to your implementation. But there is a way to write this which is much simpler. That takes advantage of a simplified definition of palindrome: "a word which reads the same forwards and backwards".
Now I haven't done PHP in years, and don't have a test environment, so this might have issues, but the basic idea should be clear.
public static function isPalindrome($word)
return $word == strrev($word);
Update
Based on suggestions in the comments, this would be nicer:
function isPalindrome($word)
$word = preg_replace('/[^a-zA-Z]/', '', $word);
$word = strtolower($word);
return $word == strrev($word);
That will now correctly identify "Level" (ignoring capitalization) and "Madam, I'm Adam." (ignoring spaces and punctuation.)
Others have pointed out a fix you could make to your implementation. But there is a way to write this which is much simpler. That takes advantage of a simplified definition of palindrome: "a word which reads the same forwards and backwards".
Now I haven't done PHP in years, and don't have a test environment, so this might have issues, but the basic idea should be clear.
public static function isPalindrome($word)
return $word == strrev($word);
Update
Based on suggestions in the comments, this would be nicer:
function isPalindrome($word)
$word = preg_replace('/[^a-zA-Z]/', '', $word);
$word = strtolower($word);
return $word == strrev($word);
That will now correctly identify "Level" (ignoring capitalization) and "Madam, I'm Adam." (ignoring spaces and punctuation.)
edited May 22 at 14:10
answered May 21 at 20:46
Scott Sauyet
59438
59438
Smart :) I like it!
â Matadeleo
May 21 at 22:12
And what is the reason of the divorce?
â Billal BEGUERADJ
May 22 at 10:53
3
You should make $word lowercase before you do the return. Else "Level" will not be valid as a palindrome.
â Tonny
May 22 at 11:26
1
this is exactly what i was thinking :) I agree with Tonny, but would also add something to strip white spaces so that "taco cat" evaluates as true
â Doug
May 22 at 12:58
2
@BillalBEGUERADJ: I never did much with it. But I absolutely prefer functional languages: ML, Haskell, OCaml, Erlang, various LISPs, and usually program by day in JavaScript.
â Scott Sauyet
May 22 at 14:13
 |Â
show 2 more comments
Smart :) I like it!
â Matadeleo
May 21 at 22:12
And what is the reason of the divorce?
â Billal BEGUERADJ
May 22 at 10:53
3
You should make $word lowercase before you do the return. Else "Level" will not be valid as a palindrome.
â Tonny
May 22 at 11:26
1
this is exactly what i was thinking :) I agree with Tonny, but would also add something to strip white spaces so that "taco cat" evaluates as true
â Doug
May 22 at 12:58
2
@BillalBEGUERADJ: I never did much with it. But I absolutely prefer functional languages: ML, Haskell, OCaml, Erlang, various LISPs, and usually program by day in JavaScript.
â Scott Sauyet
May 22 at 14:13
Smart :) I like it!
â Matadeleo
May 21 at 22:12
Smart :) I like it!
â Matadeleo
May 21 at 22:12
And what is the reason of the divorce?
â Billal BEGUERADJ
May 22 at 10:53
And what is the reason of the divorce?
â Billal BEGUERADJ
May 22 at 10:53
3
3
You should make $word lowercase before you do the return. Else "Level" will not be valid as a palindrome.
â Tonny
May 22 at 11:26
You should make $word lowercase before you do the return. Else "Level" will not be valid as a palindrome.
â Tonny
May 22 at 11:26
1
1
this is exactly what i was thinking :) I agree with Tonny, but would also add something to strip white spaces so that "taco cat" evaluates as true
â Doug
May 22 at 12:58
this is exactly what i was thinking :) I agree with Tonny, but would also add something to strip white spaces so that "taco cat" evaluates as true
â Doug
May 22 at 12:58
2
2
@BillalBEGUERADJ: I never did much with it. But I absolutely prefer functional languages: ML, Haskell, OCaml, Erlang, various LISPs, and usually program by day in JavaScript.
â Scott Sauyet
May 22 at 14:13
@BillalBEGUERADJ: I never did much with it. But I absolutely prefer functional languages: ML, Haskell, OCaml, Erlang, various LISPs, and usually program by day in JavaScript.
â Scott Sauyet
May 22 at 14:13
 |Â
show 2 more comments
up vote
7
down vote
To start with id remove the code that you written twice by re-arranging the if statement into this
if ($wordLength % 2 == 0)
$firstHalf = substr($word, 0, $wordSplitPoint);
else
$firstHalf = substr($word, 0, $wordSplitPoint-1);
$secondHalf = substr($word, $wordSplitPoint, $wordSplitPoint);
Then for the return value I would use;
return $firstHalf == $secondHalfReversed;
This is about all i would change
Thanks, no reason why the$secondHalfshould be repeated, and that return is much cleaner
â Matadeleo
May 21 at 18:35
add a comment |Â
up vote
7
down vote
To start with id remove the code that you written twice by re-arranging the if statement into this
if ($wordLength % 2 == 0)
$firstHalf = substr($word, 0, $wordSplitPoint);
else
$firstHalf = substr($word, 0, $wordSplitPoint-1);
$secondHalf = substr($word, $wordSplitPoint, $wordSplitPoint);
Then for the return value I would use;
return $firstHalf == $secondHalfReversed;
This is about all i would change
Thanks, no reason why the$secondHalfshould be repeated, and that return is much cleaner
â Matadeleo
May 21 at 18:35
add a comment |Â
up vote
7
down vote
up vote
7
down vote
To start with id remove the code that you written twice by re-arranging the if statement into this
if ($wordLength % 2 == 0)
$firstHalf = substr($word, 0, $wordSplitPoint);
else
$firstHalf = substr($word, 0, $wordSplitPoint-1);
$secondHalf = substr($word, $wordSplitPoint, $wordSplitPoint);
Then for the return value I would use;
return $firstHalf == $secondHalfReversed;
This is about all i would change
To start with id remove the code that you written twice by re-arranging the if statement into this
if ($wordLength % 2 == 0)
$firstHalf = substr($word, 0, $wordSplitPoint);
else
$firstHalf = substr($word, 0, $wordSplitPoint-1);
$secondHalf = substr($word, $wordSplitPoint, $wordSplitPoint);
Then for the return value I would use;
return $firstHalf == $secondHalfReversed;
This is about all i would change
answered May 21 at 18:11
Dan
373211
373211
Thanks, no reason why the$secondHalfshould be repeated, and that return is much cleaner
â Matadeleo
May 21 at 18:35
add a comment |Â
Thanks, no reason why the$secondHalfshould be repeated, and that return is much cleaner
â Matadeleo
May 21 at 18:35
Thanks, no reason why the
$secondHalf should be repeated, and that return is much cleanerâ Matadeleo
May 21 at 18:35
Thanks, no reason why the
$secondHalf should be repeated, and that return is much cleanerâ Matadeleo
May 21 at 18:35
add a comment |Â
up vote
6
down vote
This is a very good program. There is only one thing I'd change:
if ($firstHalf == $secondHalfReversed)
return TRUE;
else
return FALSE;
You don't really need that if because the top part only returns TRUE when the condition also is TRUE. Otherwise, both the condition and the return is FALSE. Change it to:
return $firstHalf == $secondHalfReversed
add a comment |Â
up vote
6
down vote
This is a very good program. There is only one thing I'd change:
if ($firstHalf == $secondHalfReversed)
return TRUE;
else
return FALSE;
You don't really need that if because the top part only returns TRUE when the condition also is TRUE. Otherwise, both the condition and the return is FALSE. Change it to:
return $firstHalf == $secondHalfReversed
add a comment |Â
up vote
6
down vote
up vote
6
down vote
This is a very good program. There is only one thing I'd change:
if ($firstHalf == $secondHalfReversed)
return TRUE;
else
return FALSE;
You don't really need that if because the top part only returns TRUE when the condition also is TRUE. Otherwise, both the condition and the return is FALSE. Change it to:
return $firstHalf == $secondHalfReversed
This is a very good program. There is only one thing I'd change:
if ($firstHalf == $secondHalfReversed)
return TRUE;
else
return FALSE;
You don't really need that if because the top part only returns TRUE when the condition also is TRUE. Otherwise, both the condition and the return is FALSE. Change it to:
return $firstHalf == $secondHalfReversed
answered May 21 at 18:09
Hosch250
16.9k561153
16.9k561153
add a comment |Â
add a comment |Â
up vote
2
down vote
I'd write this using a traditional for loop as follows:
class Palindrome
public static function isPalindrome($word)
$wordLength = strlen($word)-1;
for ($i = 0; $i < $wordLength/2; $i++)
if (strtolower($word[$i]) != strtolower($word[$wordLength-$i]))
return FALSE;
return TRUE;
echo Palindrome::isPalindrome('Deleveled');
1
Pedantically, this approach would be less computing power than the other approaches.
â Kenneth K.
May 22 at 13:17
add a comment |Â
up vote
2
down vote
I'd write this using a traditional for loop as follows:
class Palindrome
public static function isPalindrome($word)
$wordLength = strlen($word)-1;
for ($i = 0; $i < $wordLength/2; $i++)
if (strtolower($word[$i]) != strtolower($word[$wordLength-$i]))
return FALSE;
return TRUE;
echo Palindrome::isPalindrome('Deleveled');
1
Pedantically, this approach would be less computing power than the other approaches.
â Kenneth K.
May 22 at 13:17
add a comment |Â
up vote
2
down vote
up vote
2
down vote
I'd write this using a traditional for loop as follows:
class Palindrome
public static function isPalindrome($word)
$wordLength = strlen($word)-1;
for ($i = 0; $i < $wordLength/2; $i++)
if (strtolower($word[$i]) != strtolower($word[$wordLength-$i]))
return FALSE;
return TRUE;
echo Palindrome::isPalindrome('Deleveled');
I'd write this using a traditional for loop as follows:
class Palindrome
public static function isPalindrome($word)
$wordLength = strlen($word)-1;
for ($i = 0; $i < $wordLength/2; $i++)
if (strtolower($word[$i]) != strtolower($word[$wordLength-$i]))
return FALSE;
return TRUE;
echo Palindrome::isPalindrome('Deleveled');
answered May 21 at 22:30
aidanharris
734
734
1
Pedantically, this approach would be less computing power than the other approaches.
â Kenneth K.
May 22 at 13:17
add a comment |Â
1
Pedantically, this approach would be less computing power than the other approaches.
â Kenneth K.
May 22 at 13:17
1
1
Pedantically, this approach would be less computing power than the other approaches.
â Kenneth K.
May 22 at 13:17
Pedantically, this approach would be less computing power than the other approaches.
â Kenneth K.
May 22 at 13:17
add a comment |Â
up vote
0
down vote
echo Palindrome::isPalindrome('Deleveled');
You need to test corner cases and try to break your code. For example, you might try with:
- a string of odd size
- a string of even size
- the empty string
- a string with one character
How is size defined anyway? What about encodings? For example, your code works for '101', but not 'é_é'?
The task I completed on the website wanted a function that could detect single-word palindromes of varying lengths. But you're right. Edge cases are something I need to consider more. I'm trying hard to leave the world of "duck-tape" php behind. "It only broke because you aren't using it correctly" is a terrible philosophy :)
â Matadeleo
May 23 at 9:14
add a comment |Â
up vote
0
down vote
echo Palindrome::isPalindrome('Deleveled');
You need to test corner cases and try to break your code. For example, you might try with:
- a string of odd size
- a string of even size
- the empty string
- a string with one character
How is size defined anyway? What about encodings? For example, your code works for '101', but not 'é_é'?
The task I completed on the website wanted a function that could detect single-word palindromes of varying lengths. But you're right. Edge cases are something I need to consider more. I'm trying hard to leave the world of "duck-tape" php behind. "It only broke because you aren't using it correctly" is a terrible philosophy :)
â Matadeleo
May 23 at 9:14
add a comment |Â
up vote
0
down vote
up vote
0
down vote
echo Palindrome::isPalindrome('Deleveled');
You need to test corner cases and try to break your code. For example, you might try with:
- a string of odd size
- a string of even size
- the empty string
- a string with one character
How is size defined anyway? What about encodings? For example, your code works for '101', but not 'é_é'?
echo Palindrome::isPalindrome('Deleveled');
You need to test corner cases and try to break your code. For example, you might try with:
- a string of odd size
- a string of even size
- the empty string
- a string with one character
How is size defined anyway? What about encodings? For example, your code works for '101', but not 'é_é'?
answered May 23 at 8:02
coredump
678311
678311
The task I completed on the website wanted a function that could detect single-word palindromes of varying lengths. But you're right. Edge cases are something I need to consider more. I'm trying hard to leave the world of "duck-tape" php behind. "It only broke because you aren't using it correctly" is a terrible philosophy :)
â Matadeleo
May 23 at 9:14
add a comment |Â
The task I completed on the website wanted a function that could detect single-word palindromes of varying lengths. But you're right. Edge cases are something I need to consider more. I'm trying hard to leave the world of "duck-tape" php behind. "It only broke because you aren't using it correctly" is a terrible philosophy :)
â Matadeleo
May 23 at 9:14
The task I completed on the website wanted a function that could detect single-word palindromes of varying lengths. But you're right. Edge cases are something I need to consider more. I'm trying hard to leave the world of "duck-tape" php behind. "It only broke because you aren't using it correctly" is a terrible philosophy :)
â Matadeleo
May 23 at 9:14
The task I completed on the website wanted a function that could detect single-word palindromes of varying lengths. But you're right. Edge cases are something I need to consider more. I'm trying hard to leave the world of "duck-tape" php behind. "It only broke because you aren't using it correctly" is a terrible philosophy :)
â Matadeleo
May 23 at 9:14
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f194879%2fpalindrome-checking-function%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
5
Perhaps this is "outside" of the question/problem, but: Why is it in a class? Why not just make a standalone function? I don't know PHP, but to my understanding, it doesn't mandate top-level classes like Java does.
â Quelklef
May 22 at 2:49
1
@Quelklef The task specifically asks for it to be in a class and only allows you to modify what is contained within the
isPalindromefunctionâ Matadeleo
May 22 at 9:07