Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Odometer pattern iterator (in C).

by Anonymous Monk
on May 29, 2015 at 09:00 UTC ( #1128242=note: print w/replies, xml ) Need Help??


in reply to Odometer pattern iterator (in C). (Updated.)

#!/usr/bin/perl # http://perlmonks.org/?node_id=1128230 use strict; $_ = '00111'; 1 while print("$_\n"), s/.*\K01(.*)/10 . reverse $1/e;

gives

00111 01011 01101 01110 10011 10101 10110 11001 11010 11100

Replies are listed 'Best First'.
Re^2: Odometer pattern iterator (in C).
by hdb (Monsignor) on May 29, 2015 at 09:12 UTC

    Wow! This is impressively simple. I would have never thought that this was possible.

      yes, we have a very good AnonymousMonk's contributions nowadays!..



      There are no rules, there are no thumbs..
      Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re^2: Odometer pattern iterator (in C).
by salva (Abbot) on May 29, 2015 at 09:48 UTC
    Quite clever!
      Indeed, quite clever.
Re^2: Odometer pattern iterator (in C).
by BrowserUk (Pope) on May 29, 2015 at 11:58 UTC

    As cute as that is (and it is damn cute!), calling back into Perl and then invoking the regex engine won't make for a performant solution.

    Also, as I've highlighted in my update above, I want the positions of the set bits, not the bit patterns themselves.


    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

      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.

        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

      A chance to brush off my C :)

      // inc_c - http://perlmonks.org/?node_id=1128230 #include <stdlib.h> #include <stdio.h> #define M 5 // place #define N 3 // number of elements wanted static int place[N]; int step(void) { for(int i = 0; i < N; i++) { if(i < N - 1 ? place[i] < place[i + 1] - 1 : place[i] < M - 1 ) { place[i]++; for(int j = i - 1; j >= 0; j--) place[j] -= place[0]; return 1; } } return 0; } int main(int argc, char **argv) { int i; int more = 1; for(i = 0; i < N; i++) place[i] = i; while(more) { for(i = 0; i < N; i++) printf(" %d", place[i]); putchar('\n'); more = step(); } exit(0); }

        That works. But it's about 40% slower than the A::C version I nicked. 11 seconds instead of 8 for the 601 million 16 from 32:

        [17:27:38.79] C:\test\humanGenome>junk 32 16 ^C [17:41:10.28] C:\test\humanGenome>junk 32 16 Iters:601080390 [17:41:17.49] C:\test\humanGenome>junk2 32 16 ^C [17:41:40.82] C:\test\humanGenome>junk2 32 16 Iters:601080390 [17:41:52.30] C:\test\humanGenome>

        junk2 is your code adapted to count rather than print.


        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
Node Status?
node history
Node Type: note [id://1128242]
help
Chatterbox?
[LanX]: the real problem: Britain ignores social mobility at its peril
[LanX]: oops sorry, should be msg

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2017-12-13 14:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (368 votes). Check out past polls.

    Notices?