Question
ALL IN PYTHON 3 LANGUAGE (PLEASE ANSWER ALL PARTS SINCE THEYRE RELATED TO EACH OTHER) The Backwardable class decorates iterables (and is itself iterable), allowing
ALL IN PYTHON 3 LANGUAGE (PLEASE ANSWER ALL PARTS SINCE THEYRE RELATED TO EACH OTHER)
The Backwardable class decorates iterables (and is itself iterable), allowing calls to both the next and prev functions: next is standard and defined in builtins. So, we can move both forward and backward in the iterable that Backwardable class decorates by having it remember a list of previous values. In addition the clear function can be called to forget permanently earlier values that are unneeded (saving space). Use only classes, not generators.
Please help me solve all parts since they are related to each other
-
__next__ Depending on value of _index and the length of _all, it might just return a value from _all; but if _index is at the end of the list, __next__ will need to call next on _iterator to get a new one to return (while also appending this new value at the end of _all). Ultimately _index must be updated as appropriate.
-
__prev__ It just returns a value from the _all list; but it raises the AssertionError exception if an attempt is made to get a value previous to the first value produced by the iterable.
-
__clear__ It resets the attributes, forgetting any previous values produced (but remembering the current one, if there is one); with this method, if we are done looking at the earlier part of an iterator with many values, we can forget those values and reclaim the list space storing the list space storing them
class Backwardable: def init (self,iterable): self, iterable = iterable def iter (self): :class B iter: def init_ (self,iterable): selt._al self.iterator = iter(iterable) self,-index = - -1 # index of value just returned from-next-or-prev def _str_(self): return -allr(, -index-{}.format (self._ali,self.-index) def-next-(self): pass def_prev_(self): pass def clear (self): pass return B_iter(self._iterable) def prev (x): return x.__prev_() def clear(x): x. clear () Executes Prints What print (i) would print (see below) after the call Before execution next(1) next(1) prev(i) #prev (i) would raise AssertionError exception next(1) next(1) prev(i) next(1) next (i)raises StopIteration exception all-[l, index--1 all-['a'], index-0 all-['a', 'b'], index-1 all-['a', 'b'], index-0 all-['a', 'b'], index-1 all-['a', 'b', 'c'], index-2 all-['a', 'b', 'c'], _index-1 all-['a', 'b', 'c'], index-2 class Backwardable: def init (self,iterable): self, iterable = iterable def iter (self): :class B iter: def init_ (self,iterable): selt._al self.iterator = iter(iterable) self,-index = - -1 # index of value just returned from-next-or-prev def _str_(self): return -allr(, -index-{}.format (self._ali,self.-index) def-next-(self): pass def_prev_(self): pass def clear (self): pass return B_iter(self._iterable) def prev (x): return x.__prev_() def clear(x): x. clear () Executes Prints What print (i) would print (see below) after the call Before execution next(1) next(1) prev(i) #prev (i) would raise AssertionError exception next(1) next(1) prev(i) next(1) next (i)raises StopIteration exception all-[l, index--1 all-['a'], index-0 all-['a', 'b'], index-1 all-['a', 'b'], index-0 all-['a', 'b'], index-1 all-['a', 'b', 'c'], index-2 all-['a', 'b', 'c'], _index-1 all-['a', 'b', 'c'], index-2
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