Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Consider the following code fragment: square_numbers = Squares(5, 50) for num in square_numbers: print(num) This prints all of the square numbers between 5 and 50

Consider the following code fragment:

square_numbers = Squares(5, 50) for num in square_numbers: print(num)

This prints all of the square numbers between 5 and 50 inclusive (note: a square number is one which has a whole square root, such as 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, etc.). It works because the Squares class is iterable, i.e., it has an __iter__() method which creates an Iterator object (which has an implementation of the __next__() method). Define the Squares class and SquaresIterator class so that the square numbers in the range specified can be printed using a for loop as in the example above. HINT: you can assume that the math module has been imported - you may find math.sqrt() and math.ceil() helpful functions.

For example:

Test Result
for i in Squares(5, 50): print(i)
9 16 25 36 49
for i in Squares(4, 16): print(i)
4 9 16

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

Recommended Textbook for

Microsoft SQL Server 2012 Unleashed

Authors: Ray Rankins, Paul Bertucci

1st Edition

0133408507, 9780133408508

More Books

Students also viewed these Databases questions

Question

2. Do small companies need to develop a pay plan? Why or why not?

Answered: 1 week ago

Question

3. Are our bosses always right? If not, what should we do?

Answered: 1 week ago