Alicia Posted November 18, 2009 Share Posted November 18, 2009 Hi, Can some guru please advise how can I accomplish this with a simple for loop ? I want to use for loop to echo all number from 000 to 999 for($i=0;$i<1000;$i++) { echo "$i<br>"; } My question is how can I skip the values that have the the same digits from displaying more than once ? e.g 1) 001 displayed and 101, 100 should not be displayed in the page anymore since it is the same value with different permutation e.g 2) 127 displayed and 217, 712, 172 and etc with the same digits should not be displayed. Please advise how can I accomplish the above. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/181984-for-loop-without-same-i-digits/ Share on other sites More sharing options...
cags Posted November 18, 2009 Share Posted November 18, 2009 Couple of questions/comments. a.) Your current code wouldn't display 001, is that what you want it to do? b.) 101, 001 and 100 don't hold the same numbers, should 101 have been 010? c.) You say list all numbers between 000 and 999, I assume that means it's always the permutation closest to 001 that you wish to display? Quote Link to comment https://forums.phpfreaks.com/topic/181984-for-loop-without-same-i-digits/#findComment-959889 Share on other sites More sharing options...
Mark Baker Posted November 18, 2009 Share Posted November 18, 2009 for($i=0; $i<1000; ++$i) { $formattedi = str_split(str_pad($i,4,'0',STR_PAD_LEFT)); sort($formattedi); $formattedi = implode('',$formattedi); if ($formattedi == $i) { echo $formattedi.'<br />'; } } Quote Link to comment https://forums.phpfreaks.com/topic/181984-for-loop-without-same-i-digits/#findComment-959892 Share on other sites More sharing options...
Alicia Posted November 18, 2009 Author Share Posted November 18, 2009 Yes. 101 is a typo. SOrry... I dont have problem getting extra 0 for 001 or 010.. i just have a big problem getting the same digits display only once. I have the permutation script but it doesnt seem to fit into the for loop. Any gurus have any advice? Quote Link to comment https://forums.phpfreaks.com/topic/181984-for-loop-without-same-i-digits/#findComment-959908 Share on other sites More sharing options...
cags Posted November 18, 2009 Share Posted November 18, 2009 As far as I can tell Mark Baker's suggestion fits the requirements you laid out (with the slight exception he put 4 in string pad, whereas your original requirements specified 3 digits). It pads the string out to 3 digits, splits it up and orders them into numeric order. It then implodes them back into a string in numeric order and compares it to the starting number. Basically that means that only the first permatation will match. What part of that isn't a suitable solution? Quote Link to comment https://forums.phpfreaks.com/topic/181984-for-loop-without-same-i-digits/#findComment-960068 Share on other sites More sharing options...
sasa Posted November 18, 2009 Share Posted November 18, 2009 try <?php for ($i = 1; $i < 1000; $i++){ $a = str_pad($i,3,'0',STR_PAD_LEFT); if (count(array_unique(str_split($a)))==3) echo $a, "<br />\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/181984-for-loop-without-same-i-digits/#findComment-960113 Share on other sites More sharing options...
Alicia Posted November 23, 2009 Author Share Posted November 23, 2009 Thanks guys, that one is solved and now follow up : May I how can I actually create a function that can perform the following : Assuming that we have 4 digits in ABCD, ABCD can be any numbers from 0000 to 9999. I need to filter and arrange the numbers in the following way without recurring numbers (permuted) ABCD - all different numbers AABB - same numbers in pair AAAB - 3 same numbers AABC - 2 same numbers only I do have the permutation code and used strstr all to array but I think it works because I have spent some time performing the matches and it failed to work as I wanted. Please advise and thanks. Quote Link to comment https://forums.phpfreaks.com/topic/181984-for-loop-without-same-i-digits/#findComment-963771 Share on other sites More sharing options...
sasa Posted November 23, 2009 Share Posted November 23, 2009 try <?php for($i = 0; $i < 10000; $i++){ $formattedi = str_split(str_pad($i,4,'0',STR_PAD_LEFT)); $count = array_count_values($formattedi); $min = min($count); $max = max($count); if($max == 1) $group = 'all different numbers'; if($max == 2 and $min == 2) $group = 'same numbers in pair'; if ($max == 3) $group = '3 same numbers'; if ($max == 2 and $min ==1) $group = '2 same numbers only'; if($max == 4) $group = '4 same numbers'; $out[$group][] = implode('', $formattedi); } echo '<pre>', print_r($out), '</pre>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/181984-for-loop-without-same-i-digits/#findComment-963905 Share on other sites More sharing options...
Alicia Posted November 23, 2009 Author Share Posted November 23, 2009 Thanks it works fine if I test your full code but I tried to filter and only display 2 same digits as below, it doesn't work anymore. Numbers will less or more than 2 same digits are all displayed. Can you please advise whether I have updated the code wrongly as below? Thank you. // show 2 same numbers digit for($i = 0; $i < 10000; $i++){ $formattedi = str_split(str_pad($i,4,'0',STR_PAD_LEFT)); $count = array_count_values($formattedi); $min = 2; $max = 2; $out[$group][] = implode('', $formattedi); } echo '<pre>', print_r($out), '</pre>'; // show 3 same numbers for($i = 0; $i < 10000; $i++){ $formattedi = str_split(str_pad($i,4,'0',STR_PAD_LEFT)); $count = array_count_values($formattedi); $min = 3; $max = 3; $out[$group][] = implode('', $formattedi); } echo '<pre>', print_r($out), '</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/181984-for-loop-without-same-i-digits/#findComment-964078 Share on other sites More sharing options...
sasa Posted November 23, 2009 Share Posted November 23, 2009 <?php for($i = 0; $i < 10000; $i++){ $formattedi = str_split(str_pad($i,4,'0',STR_PAD_LEFT)); $count = array_count_values($formattedi); $min = min($count); $max = max($count); if ($max == 2 and $min == 2) $out[] = implode('', $formattedi); } echo '<pre>', print_r($out), '</pre>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/181984-for-loop-without-same-i-digits/#findComment-964135 Share on other sites More sharing options...
Alicia Posted November 23, 2009 Author Share Posted November 23, 2009 Thanks. It is working now. Last question, how can I remove the [xxx] => from the output ? I just need the numbers in the page. Please advise and thanks again Quote Link to comment https://forums.phpfreaks.com/topic/181984-for-loop-without-same-i-digits/#findComment-964166 Share on other sites More sharing options...
sasa Posted November 23, 2009 Share Posted November 23, 2009 implode $out Quote Link to comment https://forums.phpfreaks.com/topic/181984-for-loop-without-same-i-digits/#findComment-964178 Share on other sites More sharing options...
Alicia Posted November 23, 2009 Author Share Posted November 23, 2009 syntax error occur after I added this line. implode $out; Please advise and thanks. <?php for($i = 0; $i < 10000; $i++){ $formattedi = str_split(str_pad($i,4,'0',STR_PAD_LEFT)); $count = array_count_values($formattedi); $min = min($count); $max = max($count); if ($max == 3) $out[] = implode('', $formattedi); } implode $out; ?> Quote Link to comment https://forums.phpfreaks.com/topic/181984-for-loop-without-same-i-digits/#findComment-964205 Share on other sites More sharing options...
cags Posted November 23, 2009 Share Posted November 23, 2009 implode is a function and as such requires parentheses. implode($out); Quote Link to comment https://forums.phpfreaks.com/topic/181984-for-loop-without-same-i-digits/#findComment-964210 Share on other sites More sharing options...
Alicia Posted November 23, 2009 Author Share Posted November 23, 2009 Actually I tried that too before I posted that to you and it returned empty page. =( Quote Link to comment https://forums.phpfreaks.com/topic/181984-for-loop-without-same-i-digits/#findComment-964215 Share on other sites More sharing options...
sasa Posted November 23, 2009 Share Posted November 23, 2009 http://php.net/manual/en/function.implode.php Quote Link to comment https://forums.phpfreaks.com/topic/181984-for-loop-without-same-i-digits/#findComment-964219 Share on other sites More sharing options...
Alicia Posted November 23, 2009 Author Share Posted November 23, 2009 It is working now but it is not in line break. I tried this but not working . echo implode($out)."<br />"; Quote Link to comment https://forums.phpfreaks.com/topic/181984-for-loop-without-same-i-digits/#findComment-964225 Share on other sites More sharing options...
sasa Posted November 23, 2009 Share Posted November 23, 2009 echo implode("<br />", $out); Quote Link to comment https://forums.phpfreaks.com/topic/181984-for-loop-without-same-i-digits/#findComment-964230 Share on other sites More sharing options...
Alicia Posted November 23, 2009 Author Share Posted November 23, 2009 Thank you very much. It is working now. May I know why it is not working when I placed the br behind ? Quote Link to comment https://forums.phpfreaks.com/topic/181984-for-loop-without-same-i-digits/#findComment-964233 Share on other sites More sharing options...
sasa Posted November 23, 2009 Share Posted November 23, 2009 http://php.net/manual/en/function.implode.php read manual Quote Link to comment https://forums.phpfreaks.com/topic/181984-for-loop-without-same-i-digits/#findComment-964234 Share on other sites More sharing options...
Alicia Posted November 24, 2009 Author Share Posted November 24, 2009 Is there anyway we can set if the same digits are not side by side, don't display the input.. how can I edit from the existing script. So sorry for asking too many questions. Still noob in this. For instance for AABC, it will show only something like 1123, 3122 and not 2129, 1091. Remove those with 2 counts but not side by side. Thank you very much. Quote Link to comment https://forums.phpfreaks.com/topic/181984-for-loop-without-same-i-digits/#findComment-964444 Share on other sites More sharing options...
Alicia Posted November 24, 2009 Author Share Posted November 24, 2009 any guru can assists me please..... Quote Link to comment https://forums.phpfreaks.com/topic/181984-for-loop-without-same-i-digits/#findComment-964665 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.