Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a function called score that meets the specifications below. def score ( word , f ) : word, a string of

Write a function called score that meets the specifications below.
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
"""
#YOUR CODE HERE
Paste your entire function, including the definition, in the box below. Do not leave any print statements.
here is the code:
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
"""
lst =[]
word = word.lower()
for i in range(len(word)):
x = word[i]
lst.append((ord(x)-ord('a')+1)*i)
maxValue = lst[0]
secondMax = None
for x in lst:
if (maxValue < x):
if (secondMax == None or secondMax < maxValue):
secondMax = maxValue
maxValue = x
elif (secondMax == None or secondMax < x):
secondMax = x
return f(maxValue,secondMax)
# Testing
def add(a,b):
return a+b
print(score("adD",add))
and here is the error: Make sure you removed all debugging print statements. I am running on a college website.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions