Generate binary sequences of length n?

Discussion in 'Ruby' started by Tom Best, Aug 9, 2009.

  1. Tom Best

    Tom Best Guest

    I'm rather new to Ruby. I feel this should be very simple, but I'm
    having trouble:

    I'd like to write a script to work through all possible binary sequences
    of length n.



    The script would work as follows:

    mylength = 4
    resultz = binary_seq_generator(mylength)
    puts "#{resultz}"

    #resultz, not necessarily in this order:
    ["0000","0001","0010","0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1101","1110","1111"]



    Important to my use: This binary_seq_generator method must be written
    in such a way so that each subsequent member of the resultz array would
    be completely generated before the next member starts. This is where
    I'm stuck. I have trying manipulating the graycode algorithm here:
    http://yagni.com/graycode/

    This graycode algorithm seems to produce an array where each member is
    only finalized on the final recurse. I may be wrong...but by this
    method, I can't use resultz for something before the algorithm starts
    to build resultz[i+1], and I need to use each member of the resultz
    array before moving on to the next binary string in the sequence.

    Thanks much for helping a newbie!
    --
    Posted via http://www.ruby-forum.com/.
     
    Tom Best, Aug 9, 2009
    #1
    1. Advertising

  2. Hi --

    On Mon, 10 Aug 2009, Tom Best wrote:

    > I'm rather new to Ruby. I feel this should be very simple, but I'm
    > having trouble:
    >
    > I'd like to write a script to work through all possible binary sequences
    > of length n.
    >
    >
    >
    > The script would work as follows:
    >
    > mylength = 4
    > resultz = binary_seq_generator(mylength)
    > puts "#{resultz}"
    >
    > #resultz, not necessarily in this order:
    > ["0000","0001","0010","0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1101","1110","1111"]


    Try this:

    def binary_seq_generator(n)
    (0...(1 << n)).map {|e| "%0#{n}d" % e.to_s(2) }
    end

    Doesn't necessarily roll off the fingers as readily as some Ruby
    idioms do :) But I think all or most of what you need is there, and
    there are some interesting bits to it.


    David

    --
    David A. Black / Ruby Power and Light, LLC / http://www.rubypal.com
    Q: What's the best way to get a really solid knowledge of Ruby?
    A: Come to our Ruby training in Edison, New Jersey, September 14-17!
    Instructors: David A. Black and Erik Kastner
    More info and registration: http://rubyurl.com/vmzN
     
    David A. Black, Aug 9, 2009
    #2
    1. Advertising

  3. Tom Best

    Robert Dober Guest

    On Sun, Aug 9, 2009 at 8:32 PM, David A. Black<> wrote:
    > Hi --
    >
    > On Mon, 10 Aug 2009, Tom Best wrote:
    >
    >> I'm rather new to Ruby. =C2=A0I feel this should be very simple, but I'm
    >> having trouble:
    >>
    >> I'd like to write a script to work through all possible binary sequences
    >> of length n.
    >>
    >>
    >>
    >> The script would work as follows:
    >>
    >> mylength =3D 4
    >> resultz =3D binary_seq_generator(mylength)
    >> puts "#{resultz}"
    >>
    >> #resultz, not necessarily in this order:
    >>
    >> ["0000","0001","0010","0011","0100","0101","0110","0111","1000","1001","=

    1010","1011","1100","1101","1110","1111"]
    >
    > Try this:
    >
    > def binary_seq_generator(n)
    > =C2=A0(0...(1 << n)).map {|e| "%0#{n}d" % e.to_s(2) }
    > end
    >
    > Doesn't necessarily roll off the fingers as readily as some Ruby
    > idioms do :) But I think all or most of what you need is there, and
    > there are some interesting bits to it.

    Well 1.9 has to offer some elegance here

    (1<<n).times.map{ | d | "%0{n}b" % d }

    HTH
    Robert

    --=20
    module Kernel
    alias_method :=CE=BB, :lambda
    end
     
    Robert Dober, Aug 9, 2009
    #3
  4. Tom Best

    Tom B. Guest

    Robert Dober wrote:
    > On Sun, Aug 9, 2009 at 8:32 PM, David A. Black<>
    > wrote:
    >>>

    >>
    >> Try this:
    >>
    >> def binary_seq_generator(n)
    >>  (0...(1 << n)).map {|e| "%0#{n}d" % e.to_s(2) }
    >> end
    >>
    >> Doesn't necessarily roll off the fingers as readily as some Ruby
    >> idioms do :) But I think all or most of what you need is there, and
    >> there are some interesting bits to it.

    > Well 1.9 has to offer some elegance here
    >
    > (1<<n).times.map{ | d | "%0{n}b" % d }
    >
    > HTH
    > Robert



    They worked! Thank you both very much - having a comprehensive
    understanding of these operators will move my coding to the next level!
    Very much appreciated, Tom
    --
    Posted via http://www.ruby-forum.com/.
     
    Tom B., Aug 9, 2009
    #4
  5. Tom Best

    Robert Dober Guest

    >
    > (1<<n).times.map{ | d | "%0{n}b" % d }

    Oh I just forgot, maybe you need the "combinatoric" method ;)

    n.times.inject( [ "" ] ){ |s,| s.map{ |e| [ e + "0", e + "1" ] }.flatten }
     
    Robert Dober, Aug 9, 2009
    #5
  6. Robert Dober wrote:
    > (1<<n).times.map{ | d | "%0{n}b" % d }


    Perhaps safer to avoid the interpolation in the format string, using '*'
    to give the number of digits.

    >> "%0*b" % [8,123]

    => "01111011"
    --
    Posted via http://www.ruby-forum.com/.
     
    Brian Candler, Aug 10, 2009
    #6
  7. Tom Best

    Robert Dober Guest

    On Mon, Aug 10, 2009 at 10:43 AM, Brian Candler<> wrote:
    > Robert Dober wrote:
    >> (1<<n).times.map{ | d | "%0{n}b" % d }

    >
    > Perhaps safer to avoid the interpolation in the format string, using '*'
    > to give the number of digits.
    >
    >>> "%0*b" % [8,123]

    > =3D> "01111011"

    Well safer, you mean regarding to my typo, well spotted ;).
    This is a fascinating idiom I was not aware of. I too prefer it, thx
    for sharing.

    Cheers
    Robert
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >




    --=20
    module Kernel
    alias_method :=EB, :lambda
    end
     
    Robert Dober, Aug 10, 2009
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mitchua
    Replies:
    5
    Views:
    2,834
    Eric J. Roode
    Jul 17, 2003
  2. Nickolay Kolev

    Zip with sequences of diffrent length

    Nickolay Kolev, Apr 26, 2004, in forum: Python
    Replies:
    2
    Views:
    312
    Peter Hansen
    Apr 26, 2004
  3. Girish Sahani
    Replies:
    17
    Views:
    587
    Boris Borcic
    Jun 9, 2006
  4. Anthra Norell

    unpaking sequences of unknown length

    Anthra Norell, Aug 27, 2006, in forum: Python
    Replies:
    4
    Views:
    334
    Anthra Norell
    Aug 28, 2006
  5. Rune Allnor
    Replies:
    66
    Views:
    1,921
    James Kanze
    Nov 9, 2009
Loading...

Share This Page