Answered step by step
Verified Expert Solution
Link Copied!

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)

OOPimage text in transcribed

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

If they do, what effects do they have on human behaviour?

Answered: 1 week ago

Question

3. Explain the forces that influence how people handle conflict

Answered: 1 week ago