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

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

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

ten_digit_binary.rb

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

puts filtered

permutations_of_welcome

string = "welcome".split("")
string.permutation do  |str|
  puts str.inspect
end
Ruby dojo #2 reverse string, array permutation, and filtering binary numbers

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s