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
Step: 1
Get Instant Access to Expert-Tailored Solutions
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