Question

Consider function q1 below. Assume that inputString is not empty, that every character in inputString appears...

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)

Homework Answers

Answer #1

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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Implement function q1w so that it is just like q1 but uses a while loop instead...
Implement function q1w so that it is just like q1 but uses a while loop instead of a for loop. 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)
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g,...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g, char wordlist[][MAX_WORD_LENGTH], int numwords)] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) What int setup_game needs to do setup_game() does exactly what the name suggests. It sets up a new game of hangman. This means that it picks a random word from the supplied wordlist array and...
Use Python to Complete the following on a single text file and submit your code and...
Use Python to Complete the following on a single text file and submit your code and your output as separate documents. For each problem create the necessary list objects and write code to perform the following examples: Sum all the items in a list. Multiply all the items in a list. Get the largest number from a list. Get the smallest number from a list. Remove duplicates from a list. Check a list is empty or not. Clone or copy...
The Relationship Between State Agencies and Nonprofit Organizations Introduction The relationship between government agencies and nonprofit...
The Relationship Between State Agencies and Nonprofit Organizations Introduction The relationship between government agencies and nonprofit organizations is the focus of increasing attention within the public administration community. Practitioners recognize that the organization of public services relies to a substantial degree upon what we have come to call third-party government (Salamon, 1981). Nongovernmental actors not only deliver government-funded services but also actively participate throughout the policy process. Often the third-party is a nonprofit organization. In the last decade or so,...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT