Question: Trying to write a Python class ReverseIter but there are some catch 22's: - The class should not print anything - Can't use built-in functions

Trying to write a Python class ReverseIter but there are some catch 22's:

- The class should not print anything - Can't use built-in functions or methods (e.g. reverse(),iter()) you can use the len() function - Should also work when the input is tuple - When the instance is exhausted (finished iterating over the sequence) you need to raise your own StopIterationError when there are no more items. - After using ReverseIter, the original sequence should be unchanged.

Explanation: Create an iterator class that takes an input sequence and creates an instance that is an iterator that iterates over the input sequence in reverse order.

Run Time example:

from APP import ReverseIter

--> nums = [8, 3, 6] --> it = ReverseIter(nums) --> iter(it) is it True --> next(it) == 6 True --> next(it) 3 --> next(it) 8 --> next(it) Traceback (most recent call last): [...] StopIteration --> nums [8, 3, 6] --> items = ['a', 'b', 'c'] --> it = ReverseIter(items) --> next(it) == 'c' True --> next(it) 'b' --> next(it) 'a' --> next(it) Traceback (most recent call last): [...] StopIteration --> items ['a', 'b', 'c']

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!