Consider function q1 below. Assume that inputString is not empty, that every character in inputString appears as a key in inputDict, that every value in inputDict is a non-negative integer, and that specialValue is a non-negative integer.
q1 returns a tuple of two items. For example, q1("abcd", {'a':3, 'b': 4, 'c':0, 'd':0}, 4) returns (1.75, 1)
In terms of inputString, inputDict, and specialValue
a) describe the first returned item
b) describe the second returned item
def q1(inputString, inputDict, specialValue): s = 0 s2 = 0 a = 0.0 for char in inputString: s = s + inputDict[char] if inputDict[char] == specialValue: s2 = s2 + 1 a = s / len(inputString) return (a, s2)
a) The first returned item is sum of all value in inputDict divied by length of input string. Not that in last we return a as first return item. we assign a=s/len(inputString) where s is sum of all value in inputDict which we are getting by s=s+inputDict[char] statement so the value stored in s=7 and len(inputString) gives length of the string "abcd" which is 4. so 7/4=1.75 which is returned by assigning it to a variable.
b)In the second return value, we return how many keys are there in inputDict whose value is same as specialValue. we can see that there is only 1 key which is 'c' whose value is 4 which is specialValue. so we store this result in s2 and return that as second returned value
Get Answers For Free
Most questions answered within 1 hours.