Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^3: Odometer pattern iterator (in C).

by hdb (Monsignor)
on May 29, 2015 at 12:06 UTC ( [id://1128261]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Odometer pattern iterator (in C).
in thread Odometer pattern iterator (in C). (Updated.)

Translating that regex into C is trivial: (1) Find rightmost occurence of substring '01' (we are done if there is none), (2) change '01' to '10', (3) reverse the string to the right of that.

Finding the indices of the '1's is also simple.

  • Comment on Re^3: Odometer pattern iterator (in C).

Replies are listed 'Best First'.
Re^4: Odometer pattern iterator (in C).
by BrowserUk (Patriarch) on May 29, 2015 at 12:36 UTC

    Simple yes, but not quick.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
    In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked

      Ok, here is the index-based algorithm:

      use strict; use warnings; sub first_idx { my( $N, $M ) = @_; return ( $N-$M .. $N-1 ); } sub next_idx { my( $N, $M, @idx ) = @_; return () if $idx[ -1 ] == $M-1; my $i = $M-1; $i-- while $i>0 and $idx[$i]-$idx[$i-1]==1; return ( @idx[0..$i-1], $idx[ $i ]-1, map { $idx[ $i ] + $N - $idx[ $M - $_+ $i ] } $i+1 .. $M-1); } my $N = 5; my $M = 3; my @indices = first_idx( $N, $M ); 1 while print( "@indices\n"), @indices = next_idx( $N, $M, @indices );

        Cool! It even runs in the same direction as my original example.

        Though I suspect that once the slice and map operations have been unwound in C, it'll look a lot more complicated.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
        In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked

Log In?
Username:
Password:

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

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

    No recent polls found