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 Code:
<?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
);
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.
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
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.