Jump to content

for loop without same $i digits?


Alicia

Recommended Posts

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.

 

 

 

 

 

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

 

 

 

Link to comment
Share on other sites

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>';
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

<?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>';
?> 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.