[Ruby] A Quick Ruby Kata Solution


First solution of the kata described here:

http://railsrx.com/2010/09/27/a-quick-ruby-kata/




PAIRS = [ [1,1], [2,2], [3,3], [4,4], [5,5] ]
SINGLES = [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]

def single_from_pair(pair)
[pair[0]]
end

def sum(pair)
pair[0] + pair[1]
end

result = []

PAIRS.each do |pair|
(SINGLES - single_from_pair(pair)).each do |y|
(SINGLES - single_from_pair(pair)).each do |k|

z = 15 - sum(pair) - y - k
if z > 0 && z != single_from_pair(pair).first
candidate = [pair, y, k, z].flatten.sort
result << candidate unless result.include?(candidate)
end
end
end
end

Nessun commento: