Skip to content

Instantly share code, notes, and snippets.

@simplesessions
Created September 13, 2010 18:58
Show Gist options
  • Save simplesessions/577804 to your computer and use it in GitHub Desktop.
Save simplesessions/577804 to your computer and use it in GitHub Desktop.
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Given the following Greek, report the count of each group of words that share the same letter count. For example...
-----------------------------
| Number of Letters | Found |
-----------------------------
| 3 | 7 |
| 4 | 13 |
| 5 | 8 |
...
Paragraph:
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
# Ruby Version
# configure
contents, res = [], {}
col_a, col_b = 'Number of Letters', 'Found'
row_border, col_border = '-', '|'
spacer = ' '
File.open("input.txt") do |file|
while line = file.gets
words = line.gsub(/[\n\.,]/, '').split(' ')
words.each { |w| res[w.length] = res[w.length].nil? ? 1 : res[w.length] + 1 }
end
end
# draw the results
out = (0...col_a.length + col_b.length + 3).map { row_border }.join + "\n"
out << col_border + col_a + col_border + col_b + col_border + "\n"
out << (0...col_a.length + col_b.length + 3).map { row_border }.join + "\n"
res.sort.each do |key, val|
out << col_border + key.to_s
out << (0...col_a.length - key.to_s.length).map { spacer }.join + col_border + val.to_s
out << (0...col_b.length - val.to_s.length).map { spacer }.join + col_border + "\n"
end
out << (0...col_a.length + col_b.length + 3).map { row_border }.join + "\n"
puts out
# Results
# -------------------------
# |Number of Letters|Found|
# -------------------------
# |3 |5 |
# |4 |11 |
# |5 |10 |
# |6 |9 |
# |7 |8 |
# |8 |4 |
# |9 |4 |
# |10 |1 |
# |11 |2 |
# |12 |1 |
# |13 |1 |
# -------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment