Ruby dojo #2 reverse string, array permutation, and filtering binary numbers

less than 1 minute read

It was the second ruby dojo yesterday (Thu. 9/2/2012). A few more people turned up this time. No pizza, sadly, but understandable that they can't buy pizza and drinks every time.

This time, it was more organised. If you are agile, you get better at each iteration.

3 problems were given.

  • Reverse the string: "Welcome! Today's challenges: Reverse the string"
  • Find all permutations of the letters: "welcome"
  • Find all 10-digit binary numbers with no consecutive 1s

The first and the third were relatively straightforward, but permutations of "welcome" was a real challenge. We ended up using ruby's "permutation" method, which was a cheat.

Reverse the string

[sourcecode language="ruby"]
string = "Hello Welcome to Ruby Dojo"
reverse = []
string.each_char do |char|
reverse.unshift char
puts reverse.to_s


[sourcecode language="ruby"]
filtered = []
max = "1100000000".to_i(2)
(0..max).each do |number|
n = number.to_s(2)
filtered << n unless n.match /11/

puts filtered


[sourcecode language="ruby"]
string = "welcome".split("")
string.permutation do |str|
puts str.inspect