-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEXAM.py
More file actions
36 lines (30 loc) · 1.49 KB
/
EXAM.py
File metadata and controls
36 lines (30 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
def score(word, f):
"""
word, a string of length > 1 of alphabetical
characters (upper and lowercase)
f, a function that takes in two int arguments and returns an int
Returns the score of word as defined by the method:
1) Score for each letter is its location in the alphabet (a=1 ... z=26)
times its distance from start of word.
Ex. the scores for the letters in 'adD' are 1*0, 4*1, and 4*2.
2) The score for a word is the result of applying f to the
scores of the word's two highest scoring letters.
The first parameter to f is the highest letter score,
and the second parameter is the second highest letter score.
Ex. If f returns the sum of its arguments, then the
score for 'adD' is 12
"""
letter_locator = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6, 'g': 7, 'h': 8, 'i': 9, 'j': 10, 'k': 11, 'l': 12, 'm': 13, 'n': 14, 'o': 15, 'p': 16, 'q': 17, 'r': 18, 's': 19, 't': 20, 'u': 21, 'v': 22, 'w': 23, 'x': 24, 'y': 25, 'z': 26}
score_list = []
for i in range(len(word)):
letter = word[i].lower()
value_letter = letter_locator[letter]
score = value_letter * i
score_list.append(score)
highest_score = max(score_list)
score_list.remove(highest_score)
second_highest_score = max(score_list)
return f(highest_score, second_highest_score)
def f(highest_score, second_highest_score):
return highest_score + second_highest_score
print(score('adD', f))