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 06:00
Which of the following is a way that advancements in technology improve humans' ability to measure air quality and protect human health? a. air quality data gathered by new instruments can be used to reduce exposure to pollutants. b. new technologies enable natural and human-made pollution sources to be located and monitored. c. air quality data gathered by new instruments can be used to predict trends in pollution over time. d. new technologies allow governments to make informed decisions about the regulation of pollution. (choose more than one)
Answers: 2
question
Computers and Technology, 23.06.2019 01:30
Jason works as an accountant in a department store. he needs to keep a daily record of all the invoices issued by the store. which file naming convention would him the most? a)give the file a unique name b)name the file in yymmdd format c)use descriptive name while naming the files d)use capital letters while naming the file
Answers: 3
question
Computers and Technology, 23.06.2019 10:50
Your friend kayla is starting her own business and asks you whether she should set it up as a p2p network or as a client-server network. list three questions you might ask to kayla decide which network to use and how her answers to those questions would affect your recommendation.
Answers: 2
question
Computers and Technology, 23.06.2019 15:00
Jake really works well with numbers and is skilled with computers but doesn't work well with others. which of the jobs discussed in this unit might be best for jake? why?
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, 03.06.2021 14:00
question
Chemistry, 03.06.2021 14:00
question
Mathematics, 03.06.2021 14:00
question
Biology, 03.06.2021 14:00
question
Biology, 03.06.2021 14:00
Questions on the website: 13722367