The following needs to be coded in Python 3.x. Please use correct the correct ve
ID: 3823299 • Letter: T
Question
The following needs to be coded in Python 3.x. Please use correct the correct version of Python, formatting, and indentation. If need be, post the code into a site such as pastebin.com to keep the formatting. Be sure to complete at least part a and b, c is extra credit and would be appreciated. Thanks.
Problem 3. Functional Code with Random Number Sequences Write a generator gen rndtup(n) that creates an infinite sequence of tuples (a, b) where a and b are random integers, with 0 K a,b K n. If n 7, then a and b could be the numbers on a pair of dice. Use a the random module. a) Use lambda expressions, the itertools islice function Dython.org/3/M OO htinl#itertoolsislice), and the filter function to display the UNDS first 10 generated tuples (a, b) from gen rndtup(7 that have a b n 2 Example: with n 7 the output could be: (4,1), (2,6), (6,6), (3,5), b) Write code that does the same thing using generator expressions and one for loop. Place all the code in file p3.py and paste that in h6.doc. Extra credit part, for 5 points:Explanation / Answer
Answer:-
Solution
a.
import itertools
from random import randint
def gen_rndtup(n):
a = randint(1,n-1)
b = randint(1, n-1)
while True:
yield (a, b)
if __name__=='__main__':
n = 7
dice_roll = itertools.islice(gen_rndtup(n), 10)
print(list(Number on pair of dice ))
b.
def gen_rndtup(n):
"""
a = randint(1,n-1)
b = randint(1, n-1)
while True:
yield (a, b)
if __name__ == "__main__":
for dice_roll gen_randtup(7):
print(dice_roll).
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.