On Sat, Feb 13, 2010 at 1:00 PM, Zach Bartels <no / spam.com> wrote:

> I think I can work out the program itself.   But I am looking for a
> quick and easy way to generate every possible 5 digit combination of
> the letters " C" and "N"  so I have the database I need to compare
> each 5 frame segment.
>
> Any helpful  code, links, or suggestions would be appreciated.
>
> -Zach
>
>
Here is one, also based off of Seebs' insight

digits = 5
format_string = "%0#{digits}d"
0.upto (1<<digits) - 1 do |i|
  permutation = ( format_string % i.to_s(2) ).gsub('1','N').gsub('0','C')
  puts permutation
end



First we find our format string "%0#{digits}d" The "%d" says to expect an
integer (though it seems to understand that an integer represented as a
string is also valid), the "#{digits}" in between the '%' and the 'd' tell
it there are however many digits in this integer. For example, if digits was
equal to 5, the format string would be "%5d", if it was equal to 10, it
would be "%10d". And lastly, the 0 in front of the "#{digits}" tells it that
if the number we pass in has fewer than the specified number of digits, then
it should fill in the missing digits with zero. ie we get "00001" instead of
"    1".
See http://ruby-doc.org/core/classes/Kernel.html#M005962 for more on format
strings

Second, it finds it's range, 1<<5 gives us 32, but 32 has six binary digits,
so we subtract 1 and get 31. So our range is 0 through 31. Note that you
could also write this as 0.upto 2**digits - 1 do |i|
See http://ruby-doc.org/core/classes/Fixnum.html#M001076
And http://en.wikipedia.org/wiki/Bitwise_operation#Bit_shifts

For each of those numbers, i, we convert it to a string, using a base 2
radix with i.to_s(2) this means that our number will be represented as
binary, and will be a string.
See http://ruby-doc.org/core/classes/Fixnum.html#M001050

Then we pass our base 2 (meaning binary) number into the format string. with
format_string % i.to_s(2). So if our format string is "%05d" and i is 5,
then i.to_s(2) is "101" and "%05d" % "101" is "00101"
See http://ruby-doc.org/core/classes/String.html#M000770

Then we substitute the '1's in our string with 'N's and the '0's in our
string with 'C's, using gsub, which gives us the current permutation.
See http://ruby-doc.org/core/classes/String.html#M000817