Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Sorry for taking so long to do this. I saw how to do it fairly quickly but didn't get time to write it up.

Fun problem. Figuring out how my solution works is also fun. ;-)

Incidentally the fact that you didn't want the empty set made life easier - I could just use the empty set as my "end of data" metadata marker. If I didn't have that, then I'd have had to use a less convenient API. (I'd have probably just returned array references instead.)

Note that I suspect a naive approach such as the ones above is actually faster.

#! /usr/bin/perl -w use strict; my $iterator = Set::IterateAndSkip->new( @ARGV ? @ARGV : 'A'..'D' ); while (my @subset = $iterator->next) { print @subset, ":\tskip? "; if (<STDIN> =~ /y/i) { $iterator->skip_subsets; } } print "Done\n"; package Set::IterateAndSkip; sub new { my $class = shift; my $set = [map {[1, $_]} @_]; $set->[0][0] = 0; return bless { set => $set, }, $class; } sub end_of_first_run { my $self = shift; my $set = $self->{set}; my $n = -1; for (@$set) { if ($_->[0]) { $n++; } else { last; } } return $n; } sub skip_subsets { my $self = shift; $self->{set}[0][0] = 0; } sub next { my $self = shift; my $set = $self->{set}; my $end = $self->end_of_first_run; if (-1 < $end) { $set->[$end][0] = 0; } else { $set->[0][0] = 1; $end = $self->end_of_first_run; if ($end == $#$set) { if ($self->{started}) { # Reinitialize in case we want to cycle through again. delete $self->{started}; $set->[0][0] = -1; return (); } else { $self->{started} = 1; } } else { $set->[$end][0] = 0; $set->[$end + 1][0] = 1; } } my @result = map {$_->[0] ? $_->[1] : ()} @$set; # Need to skip the empty set. return @result ? @result : $self->next(); }

In reply to Re: Powerset short-circuit optimization by tilly
in thread Powerset short-circuit optimization by Limbic~Region

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others examining the Monastery: (4)
    As of 2020-10-25 14:49 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      My favourite web site is:












      Results (249 votes). Check out past polls.

      Notices?