Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^2: Generate sequential array element combos

by exussum0 (Vicar)
on Jul 28, 2004 at 03:08 UTC ( [id://377911]=note: print w/replies, xml ) Need Help??


in reply to Re: Generate sequential array element combos
in thread Generate sequential array element combos

It LOOkS like, he wants to do all permutations ('cept backwards from my example) using 4 characters to 6. The counting scheme seems such that
0000 0001 0011 0101 1001 0002 0012 0102 1002 0009 0019 0109 1009 0029 0209 2009

Bart: God, Schmod. I want my monkey-man.

Replies are listed 'Best First'.
Re^3: Generate sequential array element combos
by hbo (Monk) on Jul 28, 2004 at 03:24 UTC
    I'm with you up to '1009', but after that it's guess work. The only clue we have is that the final number is all nines. That suggests something like what you present, but it could just as well jump from '9001' (using his digit order) to 00000, the first five digit entry.

    Insufficent data.

    "Even if you are on the right track, you'll get run over if you just sit there." - Will Rogers
Re^3: Generate sequential array element combos
by Anonymous Monk on Jul 28, 2004 at 04:57 UTC
    Yeah I guess that is kinda weird list I gave.

    You are right though. I want to step through every possible combination given the integers 0 through 9 from 4 to 6 digits long. I guess a better list would be:

    0000
    0001
    0002
    0003
    0004
    0005
    0006
    0007
    0008
    0009
    0010
    0011
    0012
    ....
    ....
    9999 # Once the script maxes each place digit at 9
    00000 # it adds another place digit and starts over.
    00001
    .....
    .....
    99999 # Same here when all 5 digits are maxed at 9 it
    000000 # adds a 6th digit and starts over.
    000001
    ......
    ......
    999999
    Hope that makes more since.

      For that case you can get as simple as

      for( map '0'x$_ .. '9'x$_, 4..6 ) {

      but that chews up quite a chunk of memory. You can use tons less memory with something like

      for my $len ( 4 .. 6 ) { my $pat = '0' x $len; while( $len == length $pat ) { # ... $pat++; } }

      But, of course, that won't help much if you hope to change your list of characters to something other than 0..9.

      Algorithm::Loops would probably be quite helpful for this if you do hope to use characters other than digits. But I think I'll not code an example for that at this point, though it can be done fairly simply (or more complexly if you want a single iterator, for example).

      - tye        

      Mostly springing from tye's solution, but it avoids the memory growth.

      $\ = "\n"; for $n ( 4 .. 6 ){ print for '0' x $n .. '9' x $n; }

      And if you needed different 'digits' then

      $\ = "\n"; my @digits = 'a' .. 'j'; for my $n ( 4 .. 6 ) { print @digits[ split '', $_ ] for '0' x $n .. '9' x $n; }

      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "Think for yourself!" - Abigail
      "Memory, processor, disk in that order on the hardware side. Algorithm, algoritm, algorithm on the code side." - tachyon
      This is a class assignment, isn't it?

      "Even if you are on the right track, you'll get run over if you just sit there." - Will Rogers

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://377911]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (6)
As of 2024-04-23 22:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found