Palindromes In PHP

Tuesday, June 1, 2010 - 13:17

Richard Wiseman is a psychologist, magician, and author who runs a little blog over at http://richardwiseman.wordpress.com/. His blog talks about all sorts of things, but every Friday he posts a little puzzle that you can have a go at solving.

The last puzzle posted talked about palindromic numbers and speed, here is the puzzle in full.

The other day I went for a bike ride. My favourite route has signs every meter saying how far you have travelled. I came across the sign saying '15951 meters' and thought 'Oh, that's interesting, it is a number palindrome because it reads the same from left to right as right to left'. Then I rode on. Two hours later I came across the next palindromic number sign. How fast was I going?

I usually like to sit down for a few minutes and go over these puzzles in my head, especially if they are number based. However, this weeks puzzle got me thinking in terms of an algorithm that could work out the answer. All it would need is a counter for the start and finish, a simple palindromic detector, and a bit of maths at the end to work out the speed based on the start and end values.

1
2
3
4
5
6
7
8
9
10
11
12
13
$start = 15951;
$end = 0;
 
for ($i = $start + 1; $i < 20000; ++$i) {
    if ($i == strrev($i)) {
        print "Palindrome = " . $i . "\n";
        $end = $i;
        break;
    }
}
$result = $end - $start;
// Work out speed.
print  $result . ' meters, or ' . $result / 1000 . 'km, travelled in 2 hours makes the speed ' . (($result) / 2) / 1000 . ' kph';

This prints out the following result.

1
2
Palindrome = 16061
110 meters, or 0.11km, travelled in 2 hours makes the speed 0.055 kph

The answer to the Friday puzzle can be seen on Richard Wiseman's blog post from Monday. The answer to this puzzle was 55 meters per hour, which fits in with the answer this code created, although I worked it out in kilometres per hour.

Finally, if you are looking at string instead of a number you might need to improve the palindromic detector slightly by removing all punctuation and spaces.

1
2
3
4
$string = preg_replace( '/[^\sa-zA-Z0-9]/', '', $string);
if ($string == strrev($string)) {
    echo 'Panindrome detected.';
}

It might also be worth using the strtolower() function to make sure that the string has no characters that might cause this palindrome check to fail.

Category: 
philipnorton42's picture

Philip Norton

Phil is the founder and administrator of #! code and is an IT professional working in the North West of the UK.
Google+ | Twitter

Comments

Using strrev() is not the proper way to do plaindrome. please provide a nice one to othres........

 

<?php

$str="1574751";

$ln=strlen($str);

for($i=$ln-1;$i>=0; $i--)

{

$rstr[]=$str[$i];

}

$rstr=implode($rstr);

if($str==$rstr){

echo $str." is palindrome.<br>";

}

else{

echo $str." is not a palindrome.";

}

?>

 

 

<?php

$a  = "414";

$check = strlen($a);

 

for($j=0; $j<$check; $j++)

{

$fstr = $a[$j];

$sec =  $check - $j-1;

$lstr = $a[$check - $j-1];

if($fstr == $lstr)

{

$flag = "ok";

continue;

}

else

{

$flag = "No";

break; /* No need of further operation */

}

}

echo "<br>";

if($flag == 'ok')

{

echo $a." == this is palindrome number";

}

else

{

echo $a."this is not a palindrome number";

}

?>

 

Add new comment