Question: 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)

OOPOOP 2. (10 points) Generators. (a) (1 point) A generator is essentially

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 stop

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!