Question: Trying to create a Python function reverse_iter that returns a generator that yields items from iterable in reverse order. Below is what I came up

Trying to create a Python function reverse_iter that returns a generator that yields items from iterable in reverse order. Below is what I came up with: def reverse_iter(iterable): """Return a generator that yields items from iterable in reverse order""" r = [i for i in iterable[:1-1:-1]]

yield from r

# Here is how it should work #: --> from RI import reverse_iter --> nums = [8, 3, 6] --> it = reverse_iter(nums) --> 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 = reverse_iter(items) --> iter(it) is it True --> next(it) 'c' --> next(it) 'b' --> next(it) 'a' --> next(it) Traceback (most recent call last): [...] StopIteration

# Everthing is fine until i get to next(it) after the 3, instead of yielding 8 I get the StopIteration traceback error?

Here is the challenges explanation: Create a function that returns a generator that yields the items given in the input sequence in reverse order. Note that after using the function, the original sequence should not be changed. Here are some catch 22's: - Don't create any new lists - Don't use built in functions or methods (e.g. reverse(), reversed(), iter() ) - Should also work when the input is a tuple

Any help would be appreciated! Thank you.

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!