Write a Python class that extends the Progression class so that each value in the progression is the absolute value of the difference between the
Write a Python class that extends the Progression class so that each value in the progression is the absolute value of the difference between the previous two values. You should include a constructor that accepts a pair of numbers as the first two values, using 2 and 200 as the defaults.
class Progression:
2 Iterator producing a generic progression.
3
4 Default iterator produces the whole numbers 0, 1, 2, ...
5
6
7 def init (self, start=0):
8 Initialize current to the first value of the progression.
9 self. current = start
10
11 def advance(self):
12 Update self. current to a new value.
13
14 This should be overridden by a subclass to customize progression.
15
16 By convention, if current is set to None, this designates the
17 end of a finite progression.
18
19 self. current += 1
20
21 def next (self):
22 Return the next element, or else raise StopIteration error.
23 if self. current is None: # our convention to end a progression
24 raise StopIteration( )
25 else:
26 answer = self. current # record current value to return
27 self. advance( ) # advance to prepare for next time
28 return answer # return the answer
29
30 def iter (self):
31 By convention, an iterator must return itself as an iterator.
32 return self
33
34 def print progression(self, n):
35 Print next n values of the progression.
36 print( .join(str(next(self)) for j in range(n)))
Step by Step Solution
There are 3 Steps involved in it
Step: 1
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