Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-22-2012, 08:25 PM   PM User | #1
Dubz
Regular Coder

 
Join Date: Sep 2011
Posts: 206
Thanks: 15
Thanked 5 Times in 5 Posts
Dubz has a little shameless behaviour in the past
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.
Dubz is offline   Reply With Quote
Old 01-22-2012, 08:51 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 01-22-2012, 09:15 PM   PM User | #3
Dubz
Regular Coder

 
Join Date: Sep 2011
Posts: 206
Thanks: 15
Thanked 5 Times in 5 Posts
Dubz has a little shameless behaviour in the past
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
Dubz is offline   Reply With Quote
Old 01-22-2012, 10:33 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
I don't understand what you mean then. From what you have just replied with, string handling seems to be exactly what you want.
Fou-Lu is offline   Reply With Quote
Old 01-23-2012, 08:40 PM   PM User | #5
Dubz
Regular Coder

 
Join Date: Sep 2011
Posts: 206
Thanks: 15
Thanked 5 Times in 5 Posts
Dubz has a little shameless behaviour in the past
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.
Dubz is offline   Reply With Quote
Old 01-23-2012, 09:04 PM   PM User | #6
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:47 PM.


Advertisement
Log in to turn off these ads.