Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

Online Market Research Cost Effective Searching Of The Internet And Online Databases

Authors: John F. Lescher

1st Edition

0201489295, 978-0201489293

More Books

Students also viewed these Databases questions

Question

Please help me evaluate this integral. 8 2 2 v - v

Answered: 1 week ago