subject
Computers and Technology, 10.02.2020 22:19 riah133

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; I’ve defined a similar prev function in q4solution. py (to call the __prev__ method defined in Backwardable’s B_iter class). 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. For example, given i = iter(Backwardable(’abc’)) then we could call both next(i) and prev(i) to iterate over the string. The sequence of calls on the left would produce the values on the right (the far-right is print(i)). See a longer example (also using clear) in the q4solution. py file. Executes Prints What print(i) would print (see below) after the call Before execution _all=[], _index=-1 next(i) 'a’ _all=['a'], _index=0 next(i) 'b’ _all=['a', 'b'], _index=1 prev(i) 'a’ _all=['a', 'b'], _index=0 #prev(i) would raise AssertionError exception next(i) 'b’ _all=['a', 'b'], _index=1 next(i) 'c’ _all=['a', 'b', 'c'], _index=2 prev(i) 'b’ _all=['a', 'b', 'c'], _index=1 next(i) 'c’ _all=['a', 'b', 'c'], _index=2 next(i) raises StopIteration exception Backwardable takes any iterable as an argument. As with other classes decorating iterators, it defines only the __init__ and __iter__ methods, with __iter__ defining its own special B_iter class for actually doing the iteration. I’ve written __init__ and __str__ for this class; do not change these. You write only the __next__, __prev__, and __clear__ methods. Here is a brief description of the attributes defined in __init__. • The _all attribute stores a list remembering all the values returned via __next__, so we can go backwards and forwards through the values already produced by Backwardable’s iterable argument. • The _iterator attribute can be passed to next to produce a new value or raise StopIteration. • The _index stores the index in _all of the value most recently returned from a call of the __next__ or __prev__ methods. It typically is incremented/decremented in calls to __next__ or __prev__. You must write the code in __next__, __prev__ , and __clear__ that coordinates these attributes to produce the behavior illustrated in the example above. How does __next__ work? 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. How does __prev__ work? 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. How does __clear__ work? 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 them.

ansver
Answers: 2

Another question on Computers and Technology

question
Computers and Technology, 22.06.2019 08:10
Alook-up table used to convert pixel values to output values on a monitor. essentially, all pixels with a value of 190 or above are shown as white (i.e. 255), and all values with a value of 63 or less are shown as black (i.e. 0). in between the pixels are scaled so that a pixel with a value p is converted to a pixel of value 2/127 −+3969). if a pixel has a value of 170 originally, what value will be used to display the pixel on the monitor? if a value of 110 is used to display the pixel on the monitor, what was the original value of the pixel?
Answers: 1
question
Computers and Technology, 22.06.2019 15:20
This os integrated the processing power of windows nt with the easy-to-use gui of windows 98. windows 2000 windows 3.11 windows for workgroups windowa millennium edition
Answers: 1
question
Computers and Technology, 22.06.2019 22:30
You are new to microsoft certification and want to start out by getting a certification geared around windows 8. what microsoft certification should you pursue?
Answers: 1
question
Computers and Technology, 23.06.2019 07:30
Which option allows you to view slides on the full computer screen?
Answers: 1
You know the right answer?
The Backwardable class decorates iterables (and is itself iterable), allowing calls to both the next...
Questions
question
Mathematics, 01.04.2021 23:00
question
English, 01.04.2021 23:00
question
English, 01.04.2021 23:00
question
Physics, 01.04.2021 23:00
question
Mathematics, 01.04.2021 23:00
Questions on the website: 13722367