# Generate binary sequences of length n?

**URL:** <https://www.ruby-forum.com/t/generate-binary-sequences-of-length-n/173460>\
**Category:** Ruby\
**Created:** [August 9, 2009, 8:05pm UTC](https://www.ruby-forum.com/t/generate-binary-sequences-of-length-n/173460 "2009-08-09T20:05:05Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomtoday](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/35a633/32.png) [@tomtoday](https://www.ruby-forum.com/u/tomtoday)\
**Post date:** [August 9, 2009, 8:05pm UTC](https://www.ruby-forum.com/t/generate-binary-sequences-of-length-n/173460/1 "2009-08-09T20:05:05Z")

</div>

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/](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[i] 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!

---

<div class="post-metadata">

**Author:** ![tomtoday](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/35a633/32.png) [@tomtoday](https://www.ruby-forum.com/u/tomtoday)\
**Post date:** [August 9, 2009, 8:33pm UTC](https://www.ruby-forum.com/t/generate-binary-sequences-of-length-n/173460/2 "2009-08-09T20:33:23Z")

</div>

Hi –

On Mon, 10 Aug 2009, Tom Best wrote:

> 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

---

<div class="post-metadata">

**Author:** ![tomtoday](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/35a633/32.png) [@tomtoday](https://www.ruby-forum.com/u/tomtoday)\
**Post date:** [August 9, 2009, 9:09pm UTC](https://www.ruby-forum.com/t/generate-binary-sequences-of-length-n/173460/3 "2009-08-09T21:09:40Z")

</div>

On Sun, Aug 9, 2009 at 8:32 PM, David A. Black[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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

---

<div class="post-metadata">

**Author:** ![tomtoday](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/35a633/32.png) [@tomtoday](https://www.ruby-forum.com/u/tomtoday)\
**Post date:** [August 9, 2009, 9:16pm UTC](https://www.ruby-forum.com/t/generate-binary-sequences-of-length-n/173460/4 "2009-08-09T21:16:42Z")

</div>

> (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  
}

---

<div class="post-metadata">

**Author:** ![tomtoday](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/35a633/32.png) [@tomtoday](https://www.ruby-forum.com/u/tomtoday)\
**Post date:** [August 9, 2009, 9:14pm UTC](https://www.ruby-forum.com/t/generate-binary-sequences-of-length-n/173460/5 "2009-08-09T21:14:53Z")

</div>

Robert D. wrote:

> On Sun, Aug 9, 2009 at 8:32 PM, David A. Black[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> 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

---

<div class="post-metadata">

**Author:** ![tomtoday](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/35a633/32.png) [@tomtoday](https://www.ruby-forum.com/u/tomtoday)\
**Post date:** [August 10, 2009, 10:43am UTC](https://www.ruby-forum.com/t/generate-binary-sequences-of-length-n/173460/6 "2009-08-10T10:43:40Z")

</div>

Robert D. 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”

---

<div class="post-metadata">

**Author:** ![tomtoday](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/35a633/32.png) [@tomtoday](https://www.ruby-forum.com/u/tomtoday)\
**Post date:** [August 10, 2009, 2:04pm UTC](https://www.ruby-forum.com/t/generate-binary-sequences-of-length-n/173460/7 "2009-08-10T14:04:37Z")

</div>

On Mon, Aug 10, 2009 at 10:43 AM, Brian C.[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Robert D. 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”  
> > > 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

---

<div class="post-metadata">

**Author:** ![tomtoday](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/35a633/32.png) [@tomtoday](https://www.ruby-forum.com/u/tomtoday)\
**Post date:** [August 10, 2009, 6:33pm UTC](https://www.ruby-forum.com/t/generate-binary-sequences-of-length-n/173460/8 "2009-08-10T18:33:55Z")

</div>

Hi –

On Mon, 10 Aug 2009, Robert D. wrote:

> > > [“0000”,“0001”,“0010”,“0011”,“0100”,“0101”,“0110”,“0111”,“1000”,“1001”,“1010”,“1011”,“1100”,“1101”,“1110”,“1111”]  
> > > Well 1.9 has to offer some elegance here
> 
> (1\<\<n).times.map{ | d | “%0{n}b” % d }

Good idea – I was definitely doing an extra round trip to binary 🙂

David
