...

View Full Version : checking giant list of numbers for close mirrors

Dubz
01-22-2012, 08:25 PM
I have a giant array of 9 digit numbers (about 6300). I want to perform a check that takes each number and flips it and sees which still match (perfect mirror) and which are close to a perfect match (one digit is off by 1-2 increments)

so far this is a shortened down version of what i attempted but failed:

<?php
\$numberArray = array(
123456789,
234567890,
123456543,
624352454,
123454322, //Should be a possible close match, only of by one incrememnt (first/last digit)
318454813, //Should be a perfect match
829034505
);

\$count = 0;
foreach(\$numberArray as \$number){
\$number2 = strrev(\$number);//Flip the number
\$newNumber1 = array(
\$id+1,
\$id+10,
\$id+100,
\$id+1000,
\$id+10000,
\$id+100000,
\$id+1000000,
\$id+10000000,
\$id+100000000,
\$id-1,
\$id-10,
\$id-100,
\$id-1000,
\$id-10000,
\$id-100000,
\$id-1000000,
\$id-10000000,
\$id-100000000
);
\$newNumber2 = array(
\$id+2,
\$id+20,
\$id+200,
\$id+2000,
\$id+20000,
\$id+200000,
\$id+2000000,
\$id+20000000,
\$id+200000000,
\$id-2,
\$id-20,
\$id-200,
\$id-2000,
\$id-20000,
\$id-200000,
\$id-2000000,
\$id-20000000,
\$id-200000000
);
if(\$number==\$number2 || in_array(\$id2,\$newNumber1) || in_array(\$id2,\$newNumber2)){
print \$id."<br />\r\n";
\$count++;
}
}
print \$count." numbers's are closely mirrored";

I got the perfect mirror check to work but the 1-2 increment's off check wont work. Also if you have an easier way to check if its off by 1 or 2 increments that would be nice also.

Fou-Lu
01-22-2012, 08:51 PM
Don't waste your time treating them as numbers. Cast them to a string, reverse the string and compare them as strings. If strcmp is 0, they match.

Dubz
01-22-2012, 09:15 PM
Don't waste your time treating them as numbers. Cast them to a string, reverse the string and compare them as strings. If strcmp is 0, they match.

This is still not what i need. Say I have the number 102. Because 102 is not an exact mirror but 101 is, it would be partly true because one of the digits (the 2 or 1) is off by 1 increment meaning if it were either 202 or 101 it would be exact. Not sure if theirs some way to do this but it would be nice

Fou-Lu
01-22-2012, 10:33 PM
I don't understand what you mean then. From what you have just replied with, string handling seems to be exactly what you want.

Dubz
01-23-2012, 08:40 PM
I don't understand what you mean then. From what you have just replied with, string handling seems to be exactly what you want.

It seemed to work with either way that i did, but the problem is, it doesn't check the way i want it to (being one increment off for one number in either direction). Thinking about the way I attempted again, this is probably a harder thing to check without getting extreme. For example, the number 13209231 is off by one, but if you were to flip it, 13290231, subtracting one from the zero or adding one to the 9 would cause the next number to round up one. Basically the way I was attempting seems to be useless looking at it again now.

Fou-Lu
01-23-2012, 09:04 PM
I still don't know what you are looking to do. Detecting a palindrome is merely a matter of comparing a reverse to itself:

\$str = 101;
if (strcmp(\$str, strrev(\$str)) == 0)
{
printf("%s is a palindrome.", \$str);
}

Treating it as a string is much easier than calculating the reverse value of the numbers.