Last active
December 11, 2015 23:58
-
-
Save localshred/4680045 to your computer and use it in GitHub Desktop.
Because correcting 2nd grade homework can be tedious.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This one is smaller with more ruby magic. | |
# I love you case statement. | |
words = %w( too two bargain treasure American America nearly advice advise newspaper | |
Christmas Christ length long strength strong popular interest destroy comparison | |
compare disaster beneath cocoa guard island cousin cabbage eager ) | |
longest = words.inject(0) { |length, word| word.size > length ? word.size : length } | |
char_cost = lambda do |char| | |
case char | |
when /[AEIOUWYaeiouwy]/ then 0.05 | |
when /[DHLNRSTBdhlnrstb]/ then 0.10 | |
when /[CFGMPVcfgmpv]/ then 0.15 | |
when /[JKQXZjkqxz]/ then 0.25 | |
end | |
end | |
words.each do |word| | |
price = word.each_char.inject(0) { |total, char| total + char_cost.call(char) } | |
puts "%-#{longest}s %0.2f" % [word, price] | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
words = %w( too two bargain treasure American America nearly advice advise newspaper | |
Christmas Christ length long strength strong popular interest destroy comparison | |
compare disaster beneath cocoa guard island cousin cabbage eager ) | |
cost = { | |
/[AEIOUWYaeiouwy]/ => 0.05, | |
/[DHLNRSTBdhlnrstb]/ => 0.10, | |
/[CFGMPVcfgmpv]/ => 0.15, | |
/[JKQXZjkqxz]/ => 0.25 | |
} | |
longest = words.inject(0) { |length, word| length = word.size if word.size > length; length } | |
words.each do |word| | |
price = word.each_char.inject(0) do |total, char| | |
cost_key = cost.keys.detect { |key| char =~ key } | |
total += cost[cost_key] | |
end | |
puts "%-#{longest}s %0.2f" % [word, price] | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
too 0.20 | |
two 0.20 | |
bargain 0.60 | |
treasure 0.60 | |
American 0.70 | |
America 0.60 | |
nearly 0.45 | |
advice 0.55 | |
advise 0.50 | |
newspaper 0.80 | |
Christmas 0.90 | |
Christ 0.60 | |
length 0.60 | |
long 0.40 | |
strength 0.80 | |
strong 0.60 | |
popular 0.65 | |
interest 0.65 | |
destroy 0.55 | |
comparison 0.95 | |
compare 0.70 | |
disaster 0.65 | |
beneath 0.55 | |
cocoa 0.45 | |
guard 0.45 | |
island 0.50 | |
cousin 0.50 | |
cabbage 0.65 | |
eager 0.40 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment