Skip to content

Instantly share code, notes, and snippets.

@wmakley
Last active Dec 24, 2021
Embed
What would you like to do?
Count the number of times each gift is given
#!/usr/bin/env ruby
verses = {
1 => "Patridges in a pear tree",
2 => "Turtle doves",
3 => "French hens",
4 => "Calling birds",
5 => "Golden rings",
6 => "Geese a layin'",
7 => "Swans a swimmin'",
8 => "Maids a milkin'",
9 => "Ladies dancing",
10 => "Lords a leapin'",
11 => "Pipers pipin'",
12 => "Twelve drummers drummin'"
}
counts = {}
1.upto verses.size do |verse|
1.upto verse do |line|
counts[line] ||= 0
counts[line] += line
end
end
counts.each do |verse, count|
puts "#{count} #{verses[verse]}"
end
total = counts.values.sum
puts "Total gifts: #{total}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment