Created
September 26, 2013 00:04
-
-
Save harlanbarnes/6708000 to your computer and use it in GitHub Desktop.
Homework
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
#!/usr/bin/ruby | |
# our dictionary file | |
WORDS = 'english.txt' | |
# build the letter-to-point map | |
letters = Hash.new | |
count = 0 | |
('a'..'z').to_a.each do |x| | |
count += 1 | |
letters[x] = count | |
end | |
# open the file, read each line | |
File.open(WORDS,'r').read.each_line do |line| | |
# get rid of the newline | |
line.chomp! | |
# make everything lowercase | |
line.downcase! | |
# make sure the word is ASCII a-z (no words with graphemes, numbers, etc.) | |
if /^[a-z]+$/.match(line).nil? | |
next | |
end | |
# split the line into each letter, lookup the amount, add it to the total | |
total = 0 | |
line.split('').each do |letter| | |
total += letters[letter] | |
end | |
# if the total is a dollar word, print it out | |
if total == 100 | |
puts "#{line}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment