Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

oooh, fun!

use strict; use warnings; my $zero = shift || 3; my $one = shift || 2; my @array = ( (0) x $zero, (1) x $one ); # print "@array\n"; print join ('', @array), "\n"; while (1) { my $cand = $#array; while ($cand) { if ($array[$cand-1] == 0 and $array[$cand] == 1) { ($array[$cand-1], $array[$cand]) = ($array[$cand], $array[ +$cand-1]); if ($cand < $#array) { @array[$cand+1..$#array] = sort @array[$cand+1..$#arra +y]; } last; } --$cand; } last unless $cand; # print "@array\n"; print join ('', @array), "\n"; }

Converting this to an iterator is left as an exercise to the reader (update:) remarkably trivial :)

sub iter { my $zero = shift || 3; my $one = shift || 2; my $init = 0; my @array = ( (0) x $zero, (1) x $one ); return sub { $init++ or return join('', @array); my $cand = $#array; while ($cand) { if ($array[$cand-1] == 0 and $array[$cand] == 1) { ($array[$cand-1], $array[$cand]) = ($array[$cand], $ar +ray[$cand-1]); if ($cand < $#array) { @array[$cand+1..$#array] = sort @array[$cand+1..$# +array]; } last; } --$cand; } return $cand ? join( '', @array) : undef; } } my $i = iter(@ARGV); while (my $str = $i->()) { print "$str\n"; }

update: tye was right (of course!), the sort may be advantageously replaced by a reverse. Furthermore, there is no point in reversing (or sorting) a one-element array...

$cand < $#array - 1 and @array[$cand+1..$#array] = reverse @array[$cand+1..$#array];

• another intruder with the mooring in the heart of the Perl


In reply to Re: One Zero variants_without_repetition by grinder
in thread One Zero variants_without_repetition by thenetfreaker

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-04-20 14:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found