Question

In: Computer Science

PleaSe write a GENERATOR function in Python: how to write it with yield ? XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX Thanks...

PleaSe write a GENERATOR function in Python: how to write it with yield ?

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Thanks in advance

Write /the/ generator /function/ genAccum(seq, fn), which takes in an iterable and a function fn and yields each accumulated value from applying fn to the running total and the next element. (Note: to convert an iterable into an iterator you use iter(iterable))

'''
>>> add = lambda x, y: x + y
>>> mul = lambda x, y: x * y
>>> list(genAccum([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], add))
[1, 3, 6, 10, 15, 21, 28, 36, 45, 55]
>>> list(genAccum([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], mul))
[1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800]
'''

Solutions

Expert Solution

Program Code to Copy:

# genAccum function with yield
def genAccum(seq, fn):
    # Initializing current value to first value
    curr = seq[0]
    # Yielding the current value
    yield curr
    # Running a loop from second value to last value
    for i in range(1, len(seq)):
        # Updating current value by applying fn to current value and element at current index
        curr = fn(curr, seq[i])
        # Yielding the current value
        yield curr


add = lambda x, y: x + y
mul = lambda x, y: x * y
print(list(genAccum([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], add)))
print(list(genAccum([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], mul)))

Program Code Screenshot with Output:


Related Solutions

how to write a unit test for a write function in python? is there a better...
how to write a unit test for a write function in python? is there a better way than running main once and checking whether the new file path exists?
The Python lookNsayNth(), given below, is a generator function that generates the digits (as characters) of...
The Python lookNsayNth(), given below, is a generator function that generates the digits (as characters) of the “n-th” “look-and-say number”. E.g., 1113… generates the characters ‘1’, ‘1’, ‘1’, ‘3’, etc. Use this generator to print out the 1,000,000-th to 1,000,019-th digits of the 200th “look-and-say” number. Check: your number will start with 211121… (Keep in mind that the 1st digit has index 0 if the digits were put in an array). Note that the 200th “look-and-say” number has somewhere around...
Python please Write a function that takes a string as an argument checks whether it is...
Python please Write a function that takes a string as an argument checks whether it is a palindrome. A palindrome is a word that is the same spelt forwards or backwards. Use similar naming style e.g. name_pal. E.g. If we call the function as abc_pal(‘jason’) we should get FALSE and if we call it a abc_pal(‘pop’) we should get TRUE. Hint: define your function as abc_pal(str). This indicates that string will be passed. Next create two empty lists L1=[] and...
In python please write the following code the problem. Write a function called play_round that simulates...
In python please write the following code the problem. Write a function called play_round that simulates two people drawing cards and comparing their values. High card wins. In the case of a tie, draw more cards. Repeat until someone wins the round. The function has two parameters: the name of player 1 and the name of player 2. It returns a string with format '<winning player name> wins!'. For instance, if the winning player is named Rocket, return 'Rocket wins!'.
I just wrote Python code to solve this problem: Write a generator that will return a...
I just wrote Python code to solve this problem: Write a generator that will return a sequence of month names. Thus gen = next_month('October') creates a generator that generates the strings 'November', 'December', 'January' and so on. If the caller supplies an illegal month name, your function should raise a ValueError exception with text explaining the problem. Here is my code: month_names = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'] def next_month(name: str) -> str:...
Please use Python 3 3). Write a function that writes a series of random numbers to...
Please use Python 3 3). Write a function that writes a series of random numbers to a text file named ‘random_number.txt’. Each random number should be in the range of 1 through 500. The function should let the user specify how many random numbers the file will hold. Then write another function that reads the random numbers from the ‘random_number.txt’ file, displays the numbers, and then displays the total of the numbers and the number of random numbers read from...
(Please solve the question using C Language. Thanks). Write a function called is_perfect which takes an...
(Please solve the question using C Language. Thanks). Write a function called is_perfect which takes an integer n and returns 1 if n is a perfect number, otherwise it will return 0. If the sum of a number’s proper divisors are equal to the number, than the number is called a perfect number. For example, 6 is a perfect number: 6=1+2+3.
Python: How would I write a function that takes a directory and a size in bytes,...
Python: How would I write a function that takes a directory and a size in bytes, and returns a list of files in the directory or below that are larger than the size. For example, I can use this function to look for files larger than 1 Meg below my Home directory.
Python: How would I write a function that takes a URL of a webpage and finds...
Python: How would I write a function that takes a URL of a webpage and finds the first link on the page? The hint given is the function should return a tuple holding two strings: the URL and the link text.
solve with python 3.8 please 1,Write a function called make_squares_coordinate_pair that has one parameter - an...
solve with python 3.8 please 1,Write a function called make_squares_coordinate_pair that has one parameter - an int. The function should return a tuple containing that int and its square. Suppose you were to call your function like this: print(make_squares_coordinate_pair(5)) This should output: (5, 25) Your function MUST be called make_squares_coordinate_pair. You can write code in the main part of your program to test your function, but when you submit, your code must ONLY have the function definition! 2, Write a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT