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 23:30
Select all that apply. which of the following are proofreading options included in microsoft word? spell check find replace grammar check formatting check
Answers: 1
question
Computers and Technology, 23.06.2019 04:20
4. a1. vince owns a television repair shop that is insured undera commercial package policy. the policy includes thebuilding and personal property coverage form and thecauses-of-loss broad form. the declarations page indicatesthat coverage applies to both the building and the namedinsured's business property. explain whether or not thefollowing losses would be covered under his policy.a. a fire occurs on the premises, and the building isbadly damaged.b. a burglar steals some money and securities from anunlocked safe.c. a business computer is damaged by vandals whobreak into the shop after business hours.d. a tornado touches down near the store. several tel-evision sets of customers in the shop for repair aredamaged in the storm.til
Answers: 2
question
Computers and Technology, 23.06.2019 05:00
Which best explains why a digital leader would join a society specializing in technology
Answers: 1
question
Computers and Technology, 24.06.2019 17:40
Pseudocode pld #6, pg. 117 start// declarations// number numbertoguess// number myguess; numbertoguess = 92// while myguess ! = numbertoguess// output " guess an integer number between 1 and 100"// input myguess// if (myguess == numbertoguess)// output "you guessed the correct number"// else// output "the number you guessed was incorrect. try again! "// end if// end while// output " for playing the guessing game. have a great day! "// stop
Answers: 3
You know the right answer?
The Backwardable class decorates iterables (and is itself iterable), allowing calls to both the next...
Questions
question
Mathematics, 22.12.2020 19:30
question
Mathematics, 22.12.2020 19:30
question
Mathematics, 22.12.2020 19:30
question
English, 22.12.2020 19:30