Jump to content

Find if two digits in same place are the same


tjc19999

Recommended Posts

Basically if I have the number 1234 I do not want to have anything like 1334 OR 2234, OR 1235 but if i have 1245 it is fine.

 

Here is what I currently have 0000-0099,1100-1199, 2200-2299 etc.

 

I'm not sure if I can add any other numbers otherwise there will be more than 2 numbers in the same place in common.

 

Does that make sense?

Yea I'm not sure I get it either but here's my guess at a (crude) stating point for you to check the numbers.

You should be able to do it with modulus checks also.

function numPlacesMatch($a, $b){
$matches = 0;
$a = zerofill($a, 4);
$b = zerofill($b, 4);
for($i = 0; $i < 4; $i++){
	if($a{$i} == $b{$i}){
		$matches++;
	}
}
return $matches;
}

function zerofill($num, $places){
$str = (string)$num;
while(strlen($str) < $places){
	$str = '0' . $str;
}
return $str;
}

my curiosity got the better of me, this one is way faster than messing with all the strings.

function numPlacesMatch2($a, $b){
$matches = 0;
if($a % 10 == $b % 10){
	$matches++;
}
if(floor($a/10) % 10 == floor($b/10) % 10){
	$matches++;
}
if(floor($a/100) % 10 == floor($b/100) % 10){
	$matches++;
}
if(floor($a/1000) % 10 == floor($b/1000) % 10){
	$matches++;
}
return $matches;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.