Answered step by step
Verified Expert Solution
Question
1 Approved Answer
OOP 2. (10 points) Generators. (a) (1 point) A generator is essentially an iterator. Is this statement true? Answer Yes or No. (b) (4 points)
OOP
2. (10 points) Generators. (a) (1 point) A generator is essentially an iterator. Is this statement true? Answer Yes or No. (b) (4 points) Define a generator class First PrimeNumbers in the class form. FirstNPrimeNumbers generates the first n prime numbers, where n is passed a sole argument for its initialization method. A prime number is a natural number larger than 1 and only divisible by 1 and itself. Assume that you can call a helper function is-prime(n) which returns True iff n is a prime number and False otherwise. class First NPrime Numbers: pass # Get the first 4 primes prime = First N PrimeNumbers (4) print next (prime)) # print 2 print next prime)) # print 3 print next prime)) # print 5 print next prime)) # print 7 print next(prime)) # raise erception (c) (3 points) Define the same generator class using the more concise "function" form. def First N PrimeNumbers (n): pass primes = First N Prime Numbers (4) for p in primes: print (P) (d) (2 points) What is the output in (c) if FirstNPrimeNumbers (n) is correctly defined. How does the for loop know when to stopStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started