A dollar word is a word for which the sum of the values of the letters adds up to 100 ($1.00).
"a" has a value of 1 and "z" has a value of 26. Special characters such as apostrophes are ignored.
First try looked like:
import string
valMap = {}
for index,item in enumerate(string.lowercase):
valMap[item] = index +1
def isDollarWord(word):
lowercase = word.lower().strip()
total = 0
for letter in lowercase:
if letter in valMap:
total += valMap[letter]
return total == 100
words = open("C:\Users\astroboy\Downloads\UKACD17.TXT")
for line in words:
if isDollarWord(line):
print(line)
I started feeling kind of bad that words like "Hälleflinta" and "divorcée" might be denied their rightful place in the dollar word list. No known rules for how to handle characters with accents, so I made up my own as below (à would be counted as a). That means replacing all diactritics with the plain letter.
import string
import unicodedata
valMap = {}
for index,item in enumerate(string.lowercase):
valMap[item] = index +1
def remove_marks(word):
unicode_word = word.decode('cp1252')
return unicodedata.normalize('NFKD',unicode_word).encode('ascii','ignore')
def isDollarWord(word):
lowercase = word.lower().strip()
normalized = remove_marks(lowercase)
total = 0
for n in normalized:
if n in valMap:
total += valMap[n]
return total == 100
words = open("C:\Users\astroboy\Downloads\UKACD17.TXT")
for line in words:
if isDollarWord(line):
print(remove_marks(line))
I'm posting for any feedback about this code. See any room for improvement?