Question

In: Computer Science

Nested Loops Problem 3 Write a function called makesentence() that has three parameters: nouns, verbs, and...

Nested Loops

Problem 3

Write a function called makesentence() that has three parameters: nouns, verbs, and gerunds. Each parameter is a list of strings, where nouns list has noun strings (such as 'homework'), verbs list has veb strings (such as 'enjoy'), and gerunds list has gerund strings (those -ing words, such as 'studying'). The function will go through all these lists in a systematic fashion to create a list of all possible sentences that use all the noun, verb, and gerund strings. The function then returns the list of sentences.

Each sentence has the following format: 'I' followed by a verb, followed by a noun, followed by 'when ', followed by a gerund, exactly in this order.. For example: 'I enjoy homework when studying' uses the noun 'homework', verb 'enjoy', and gerund 'studying'.

Consider that we have the following lists:

some_nouns = ['homework', 'music', 'breaks']

some_verbs = ['enjoy', 'ignore']

some_gerunds = ['studying', 'sleeping', 'hiking']

After we write the function definition, we call the function, save its return value in the variable result, and print out the result:

result = makesentence(some_nouns, some_verbs, some_gerund)

print(result)

The output of obtained from the call above is:

['I enjoy homework when studying', 'I enjoy homework when sleeping', 'I enjoy homework when hiking', 'I enjoy music when studying', 'I enjoy music when sleeping', 'I enjoy music when hiking', 'I enjoy breaks when studying', 'I enjoy breaks when sleeping', 'I enjoy breaks when hiking', 'I ignore homework when studying', 'I ignore homework when sleeping', 'I ignore homework when hiking', 'I ignore music when studying', 'I ignore music when sleeping', 'I ignore music when hiking', 'I ignore breaks when studying', 'I ignore breaks when sleeping', 'I ignore breaks when hiking']

Part A Think about how to solve this problem and the computational steps of your solution BEFORE you write the Python code. Explain (1) what computations you’ll instruct Python and (2) why. Write your explanations in the order in which the computations should occur.

Part B

#1 Using the explanations of the computational steps of your solution, in an Active Code scratchpad, write the implementation. The implementation has the following structure: your function definition, followed by the three list assignments, followed by the function call and print() call, as follows:

[ your function definition goes in here ]

some_nouns = ['homework', 'music', 'breaks']

some_verbs = ['enjoy', 'ignore']

some_gerunds = ['studying', 'sleeping', 'hiking']

result = makesentence(some_nouns, some_verbs, some_gerund)

print(result)

Copy and paste the code of your function definition

Copy and paste the output you get when you run & save your code

Part C

#1 What was the most challenging while solving Problem 3?

#2 Why?

Solutions

Expert Solution

def makesentence(nouns,verbs,gerunds):

    sentence='I {} {} when {}'
    sentence_list=[]
    # nested 3 for loops
    for verb in verbs:
        for noun in nouns:
            for gerund in gerunds:
                sentence_list.append(sentence.format(verb,noun,gerund))
    return sentence_list


some_nouns = ['homework', 'music', 'breaks']
some_verbs = ['enjoy', 'ignore']
some_gerunds = ['studying', 'sleeping', 'hiking']
result = makesentence(some_nouns, some_verbs, some_gerunds)

print(result)

===============================================================


Related Solutions

Python Problem 3 Write a function named enterNewPassword. This function takes no parameters. It prompts the...
Python Problem 3 Write a function named enterNewPassword. This function takes no parameters. It prompts the user to enter a password until the entered password has 8-15 characters, including at least one digit. Tell the user whenever a password fails one or both of these tests.
Using Matlab: Using nested for loops (other methods are not allowed), write a function which takes...
Using Matlab: Using nested for loops (other methods are not allowed), write a function which takes as input a matrix, and as output returns true if at least one entry in the matrix is strictly greater than 8 and returns false otherwise.
#Write a function called solve_right_triangle. The function #solve_right_triangle should have three parameters: opposite, #adjacent, and use_degrees....
#Write a function called solve_right_triangle. The function #solve_right_triangle should have three parameters: opposite, #adjacent, and use_degrees. opposite and adjacent will be #floats, and use_degrees will be a boolean. use_degrees #should be a keyword parameter, and it should have a #default value of False. # #The function should return a tuple containing the #hypotenuse and angle of the right triangle (in that order). #If use_degrees is False, the angle should be in radians. #If use_degrees is True, the angle should be...
C++ Write the definition of a function minMax that has five parameters. The first three parameters...
C++ Write the definition of a function minMax that has five parameters. The first three parameters are integers. The last two are set by the function to the largest and smallest of the values of the first three parameters. The function does not return a value. The function can be used as follows: int a=31, b=5, c=19 big, small; minMax(a,b,c,&big,&small); /* big is now 31 */ /* small is now 5 */ **ONLY THE FUNCTION
In PYTHON Write an algorithm for a function called removeAll which takes 3 parameters: an array...
In PYTHON Write an algorithm for a function called removeAll which takes 3 parameters: an array of array type, a count of elements in the array, and a value. As with the remove method we discussed in class, elements passed the count of elements are stored as None. This function should remove all occurrences of value and then shift the remaining data down. The last populated element in the array should then be set to None. The function then returns...
Write a function called price_of_rocks. It has no parameters. In a while loop, get a rock...
Write a function called price_of_rocks. It has no parameters. In a while loop, get a rock type and a weight from the user. Keep a running total of the price for all requested rocks. Repeat until the user wants to quit. Quartz crystals cost $23 per pound. Garnets cost $160 per pound. Meteorite costs $15.50 per gram. Assume the user enters weights in the units as above. Return the total price of all of the material. Using Python For this...
Write a function called fillList that takes three parameters, an integer array, input file, and size....
Write a function called fillList that takes three parameters, an integer array, input file, and size. The function should fill the integer array with randomly generated values between two numbers lowLim and highLim read from the input file. in C++
Python 3: Write a function called Interest with floating point parameters Amount and InterestPercent that calculates...
Python 3: Write a function called Interest with floating point parameters Amount and InterestPercent that calculates the simple interest on Amount using InterestPercent as the interest rate.The function should return the interest amount.
in phyton programming: with numpy Create a function called biochild.  The function has as parameters...
in phyton programming: with numpy Create a function called biochild.  The function has as parameters the number m and the lists ??????h?? and ??????h??.  The lists ??????h?? and ??????h?? contain 0’s and 1’s.  For example: ??????h?? = [1,0,0,1,0,1] and ??????h?? = [1,1,1,0,0,1]  Both lists have the same length ?.  The 0's and 1's represent bits of information (remember that a bit is 0 or 1).  The function has to generate a new list (child)....
IN PYTHON Create a function called biochild.  The function has as parameters the number m...
IN PYTHON Create a function called biochild.  The function has as parameters the number m and the lists biomother and biofather.  The biomother and biofather lists contain 0’s and 1’s.  For example: biomother = [1,0,0,1,0,1] and biofather = [1,1,1,0,0,1]  Both lists have the same length n.  The 0's and 1's represent bits of information (remember that a bit is 0 or 1).  The function has to generate a new list (child).  The child...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT