Computers and Technology
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.

answer
Answers: 2

Other questions on the subject: Computers and Technology

image
Computers and Technology, 21.06.2019 16:00, oranzajimenez
Create a logic array qualifyingindex with true for any location where the runner is male with a running time less than 8.2. row array runnergenders indicate if a runner if male (m) or female ( f. row array runnertimes indicates the corresponding runner's time.
Answers: 1
image
Computers and Technology, 22.06.2019 13:50, juan3937
The instruction ishl (shift left integer) exists in jvm but not in ijvm. it uses the top two values on the stack, replacing the two with a single value, the result. the sec- ond-from-top word of the stack is the operand to be shifted. its content is shifted left by a value between 0 and 31, inclusive, depending on the value of the 5 least signifi- cant bits of the top word on the stack (the other 27 bits of the top word are ignored). zeros are shifted in from the right for as many bits as the shift count. the opcode for ishl is 120 (0x78).a. what is the arithmetic operation equivalent to shifting left with a count of 2? b. extend the microcode to include this instruction as a part of ijv.
Answers: 1
image
Computers and Technology, 23.06.2019 04:00, ittmanny6138
Laire writes a letter to her grandmother, in which she describes an amusement park she visited last week. she adds pictures of that place in her letter. which feature of a word processing program will claire to remove unwanted parts of the pictures?
Answers: 3
image
Computers and Technology, 23.06.2019 14:30, qveenvslayin
The basic work area of the computer is it screen that you when you first fire up your computer
Answers: 1
Do you know the correct answer?
The Backwardable class decorates iterables (and is itself iterable), allowing calls to both the next...

Questions in other subjects:

Konu
English, 16.10.2020 15:01
Konu
Mathematics, 16.10.2020 15:01