Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
def word_to_hash(word)
primes = {
'a' => 2, 'b' => 3, 'c' => 5, 'd' => 7, 'e' => 1, 'f' => 13, 'g' => 17,
'h' => 19, 'i' => 23, 'j' => 29, 'k' => 31, 'l' => 37, 'm' => 41,
'n' => 43, 'o' => 47, 'p' => 51, 'q' => 53, 'r' => 59, 's' => 61,
't' => 67, 'u' => 71, 'v' => 73, 'w' => 79, 'x' => 83, 'y' => 97,
'z' => 101
}
sum = 1
word.split('').each{|ch|
sum *= primes[ch]
}
sum
end
dicFile = File.new("dictionary.txt",'r')
contents = dicFile.readlines
wordlist = Array.new
#Wish I could find a better way to do the total count... it's not right.
total = 0
contents.each do |line|
hash = word_to_hash(line.strip)
total += 1 if wordlist.select{|x| x == hash}.size > 0
wordlist.push(hash)
end
puts total
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.