Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
2 simple solutions solution in Clear category for Worth of Words by BootzenKatzen
VALUES = {'e': 1, 'a': 1, 'i': 1, 'o': 1, 'n': 1, 'r': 1,
't': 1, 'l': 1, 's': 1, 'u': 1, 'd': 2, 'g': 2,
'b': 3, 'c': 3, 'm': 3, 'p': 3, 'f': 4, 'h': 4,
'v': 4, 'w': 4, 'y': 4, 'k': 5, 'j': 8, 'x': 8,
'q': 10, 'z': 10}
# my first solution:
def worth_of_words(words):
price = {} # new dictionary for words and their value
for word in words: # for each of the words
worth = 0 # establish their value
for letter in word: # for each letter
worth += VALUES.get(letter) # add the letter value to the word value
price[word] = worth # add the word and it's value to the new dictionary
return max(price, key= lambda x: price[x]) # return the max word with the key being the value of the word
# my second solution, which ironically is a little simpler
def worth_of_words2(words):
max_word = None # setting up our max variables
max_value = None
for word in words: # iterate through each word
value = 0 # starting value of 0
for letter in word: # for each letter in the word
value += VALUES.get(letter) # add it's value
if not max_value or value > max_value: # if there's not a max value already, or our value is bigger
max_value = value # the max value is now the current value
max_word = word # and the max word is the current word
return max_word
April 25, 2024
Comments: