Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Sep 2011
    Posts
    410
    Thanks
    18
    Thanked 26 Times in 26 Posts

    Question checking giant list of numbers for close mirrors

    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
    );

    $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.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.

  • #3
    Regular Coder
    Join Date
    Sep 2011
    Posts
    410
    Thanks
    18
    Thanked 26 Times in 26 Posts
    Quote Originally Posted by Fou-Lu View Post
    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

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    I don't understand what you mean then. From what you have just replied with, string handling seems to be exactly what you want.

  • #5
    Regular Coder
    Join Date
    Sep 2011
    Posts
    410
    Thanks
    18
    Thanked 26 Times in 26 Posts
    Quote Originally Posted by Fou-Lu View Post
    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.

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    I still don't know what you are looking to do. Detecting a palindrome is merely a matter of comparing a reverse to itself:
    PHP Code:
    $str 101;
    if (
    strcmp($strstrrev($str)) == 0)
    {
        
    printf("%s is a palindrome."$str);

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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •