[SOLVED]comparrisons
Moderator: General Moderators
- dull1554
- Forum Regular
- Posts: 680
- Joined: Sat Nov 22, 2003 11:26 am
- Location: 42:21:35.359N, 76:02:20.688W
[SOLVED]comparrisons
ok i have some combinations that i want to compare three random nubmbers against to see if the 3 random #s match any of the combos, i was wondering if anyone had any suggestions as to how to do this
the combinations are:
412
456
123
666
555
444
333
222
111
66
55
44
33
22
11
if the three random numbers say 124, (order does not matter)
i would want it to match the 412 combo then stop
if my question needs to be cleared up some just ask,
i honestly have no idea where to start so any help would be appreciated
thanks
~~Dull1554~~
the combinations are:
412
456
123
666
555
444
333
222
111
66
55
44
33
22
11
if the three random numbers say 124, (order does not matter)
i would want it to match the 412 combo then stop
if my question needs to be cleared up some just ask,
i honestly have no idea where to start so any help would be appreciated
thanks
~~Dull1554~~
Last edited by dull1554 on Mon Nov 22, 2004 9:10 pm, edited 1 time in total.
i didnt test it extensively, but it seems to work
this will only match a complete match, it wont give you the "closest" match
ex: if the combo is 123
and the rnd nums are 1 and 2
this wont work.
also if you have 2 combos
123
321
given rand num 213
it will match the first combo it checks
the big problem i see w/ this is that
given the combo 123
and the rand number 111
this WILL match it, which, might not be ok for you purpose.
if you can explain more what is, and is not ok, i can work out a solution.
theres gotta be a better way to do this....prob something like splitting all nums into an array and using array_intersect or something like that
this will only match a complete match, it wont give you the "closest" match
ex: if the combo is 123
and the rnd nums are 1 and 2
this wont work.
also if you have 2 combos
123
321
given rand num 213
it will match the first combo it checks
the big problem i see w/ this is that
given the combo 123
and the rand number 111
this WILL match it, which, might not be ok for you purpose.
if you can explain more what is, and is not ok, i can work out a solution.
theres gotta be a better way to do this....prob something like splitting all nums into an array and using array_intersect or something like that
Code: Select all
<?php
$combos = array(
'412',
'456',
'123',
'666',
'555',
);
$rand_nums = array(
'5',
'4',
'6',
);
foreach ($combos as $combo) {
$found = true;
foreach ($rand_nums as $num) {
if (false === strpos($combo, $num)) {
$found = false;
break;
}
}
if ($found) {
break;
}
}
if ($found) {
echo $combo;
}
// im guessing the rand nums prob come from user input through $_POST or somthing,
// so if you need a way to turn the string 214 into an array,
// so you can use it for the $rand_nums array,
// and dont have str_split() (php5), use this
function str_2array($string) {
$array = array();
$num_chars = strlen($string);
for ($i=0; $i < $num_chars; $i++) {
$array[] = $string[$i];
}
return $array;
}
// then
$rand_nums = str_2array($_POST['guess']);
?>rehfeld: it's a good solution, and in order to prevent matching "111" in "123" you can throw out the found element. then the code would have to look like:
- is there an "1" in "123"?
- no -> next combo
- yes -> throw out the "1" out of the "123" and look for the next figure (then the next "1" would not match with the rest "23")
- is there an "1" in "123"?
- no -> next combo
- yes -> throw out the "1" out of the "123" and look for the next figure (then the next "1" would not match with the rest "23")
good idea
here it is
here it is
Code: Select all
<?php
function str_replace_once($search, $replace, $haystack) {
if (false !== ($start_pos = strpos($haystack, $search))) {
$haystack = substr_replace($haystack, $replace, $start_pos, strlen($search));
}
return $haystack;
}
function str_2array($string) {
$array = array();
$num_chars = strlen($string);
for ($i=0; $i < $num_chars; $i++) {
$arrayї] = $stringї$i];
}
return $array;
}
$rand_nums = str_2array('645');
$combos = array(
'412',
'456',
'123',
'666',
'555',
);
foreach ($combos as $combo) {
$found = true;
$tmp_combo = $combo; // make a copy to modify
foreach ($rand_nums as $num) {
if (false === strpos($tmp_combo, $num)) {
$found = false;
break;
} else {
$tmp_combo = str_replace_once($num, '', $tmp_combo); // remove 1 occurance of the $num we just found
}
}
if ($found) {
break;
}
}
if ($found) {
echo $combo;
}
?>- dull1554
- Forum Regular
- Posts: 680
- Joined: Sat Nov 22, 2003 11:26 am
- Location: 42:21:35.359N, 76:02:20.688W
ok, there is only one thing, if i make the array containing the possiable combos to this
and if the random numbers are 4 4 6
it wont match the combo 44
i looked at it and tried to find a soultion but i dont really understand all of it
so if there is a quick fix i would be so greatful
Code: Select all
$combos = array(
'412',
'456',
'123',
'666',
'555',
'444',
'333',
'222',
'111',
'66',
'55',
'44',
'33',
'22',
'11',
);it wont match the combo 44
i looked at it and tried to find a soultion but i dont really understand all of it
so if there is a quick fix i would be so greatful
- dull1554
- Forum Regular
- Posts: 680
- Joined: Sat Nov 22, 2003 11:26 am
- Location: 42:21:35.359N, 76:02:20.688W
ok so ive tried tweaking the code so that it can match 2 or 3 digit combos because before if the random number array consisted of 3 elements it would only match a three lenght combo so heres the new code that i tried and failed horriably
note - there are no errors but that it does not match anything
anythoughts as to why it fails
note - there are no errors but that it does not match anything
Code: Select all
<?php
function str_replace_once($search, $replace, $haystack) {
if (false !== ($start_pos = strpos($haystack, $search))) {
$haystack = substr_replace($haystack, $replace, $start_pos, strlen($search));
}
return $haystack;
}
function str_2array($string) {
$array = array();
$num_chars = strlen($string);
for ($i=0; $i < $num_chars; $i++) {
$array[] = $string[$i];
}
return $array;
}
$rand_nums = str_2array('232');
print'<div>';
print_r($rand_nums);
print'</div>';
$combos = array(
'412',
'456',
'123',
'666',
'555',
'444',
'333',
'222',
'111',
'66',
'55',
'44',
'33',
'22',//note - 232 should match this combo
'11',
);
foreach ($combos as $combo) {
$found = true;
$tmp_combo = $combo; // make a copy to modify
for($i = strlen($tmp_combo); $i > 0; $i--) {//this should make it check for a match of 3 numbers if the combo is 3 in length
//and it should check for a match of 2 if the combo is 2 in lenght...maybe my logic is flawed.....
$num = $rand_nums;
for($x = count($num); $i > 0; $i--) {//checks each of the random numbers against the combo
if (false === strpos($tmp_combo, $num)) {
$found = false;
break;
} else {
$tmp_combo = str_replace_once($num, '', $tmp_combo); // remove 1 occurance of the $num we just found
}
}
}
if ($found) {
break;
}
}
if ($found) {
echo $combo;
}
?>
Last edited by dull1554 on Mon Nov 22, 2004 6:30 pm, edited 1 time in total.
- dull1554
- Forum Regular
- Posts: 680
- Joined: Sat Nov 22, 2003 11:26 am
- Location: 42:21:35.359N, 76:02:20.688W
ok heres another shot that i apperantly missed
see anything wrong, i yet again with this test get no errors but it does not return what it should
thanks in advance....
Code: Select all
<?php
function str_replace_once($search, $replace, $haystack) {
if (false !== ($start_pos = strpos($haystack, $search))) {
$haystack = substr_replace($haystack, $replace, $start_pos, strlen($search));
}
return $haystack;
}
$combos = array(
'412',
'456',
'123',
'666',
'555',
'444',
'333',
'222',
'111',
'66',
'55',
'44',
'33',
'22',
'11',);
$num = array();
$num[1] = 2;//mt_rand(1,6);
$num[2] = 3;//mt_rand(1,6);
$num[3] = 2;//mt_rand(1,6);
foreach($combos as $combo) {
$found = true;
$combo1 = $combo;
for($i = strlen($combo1); $i > 0; $i--) {
if(strpos($combo1,$num[$i]) == false){$found = false;}
else{str_replace_once($num[$i], '', $combo1);}
}
if($found == true){$match = $combo; break;}
}
if(isset($match)){echo $match;}
?>thanks in advance....
think i got it
this scales to larger combos as well.
you will need to make sure that the $combos is sorted descending from longest to shortest (just like you have been). otherwise it may fail
also, if you use a rand_num like '11'
this will still match combo '11',
(but if you want it to rather match combo '111', or '21412', let me know i think i have an easy fix for it, but it seems like that would be contrary to what your doing). im just not sure the rand_num will always be at least as long as the longest combo
so do i win the game? i want my prize. lol j/k
this scales to larger combos as well.
you will need to make sure that the $combos is sorted descending from longest to shortest (just like you have been). otherwise it may fail
also, if you use a rand_num like '11'
this will still match combo '11',
(but if you want it to rather match combo '111', or '21412', let me know i think i have an easy fix for it, but it seems like that would be contrary to what your doing). im just not sure the rand_num will always be at least as long as the longest combo
Code: Select all
<?php
function str_replace_once($search, $replace, $haystack) {
if (false !== ($start_pos = strpos($haystack, $search))) {
$haystack = substr_replace($haystack, $replace, $start_pos, strlen($search));
}
return $haystack;
}
function str_2array($string) {
$array = array();
$num_chars = strlen($string);
for ($i=0; $i < $num_chars; $i++) {
$array[] = $string[$i];
}
return $array;
}
$rand_nums = str_2array('4211');
$combos = array(
'21412',
'14456',
'15123',
'1412',
'1456',
'1123',
'412',
'456',
'123',
'666',
'555',
'444',
'333',
'222',
'111',
'66',
'55',
'44',
'33',
'22',//note - 232 should match this combo
'11',
);
foreach ($combos as $combo) {
$tmp_combo = $combo; // make a copy to modify
$unique_occurances = 0;
foreach ($rand_nums as $num) {
if (false !== strpos($tmp_combo, $num)) {
$tmp_combo = str_replace_once($num, '', $tmp_combo);
$unique_occurances++;
}
}
if ($unique_occurances === strlen($combo)) {
$found = true;
break;
}
}
if (isSet($found)) {
echo $combo;
}
?>so do i win the game? i want my prize. lol j/k