I&C SCI 33 Lecture 11: [Iterators II]

26 views3 pages

Document Summary

With this, you can now call the line i = prange(10) Classes that store interesting data and have iterators over the data: examine the definition of the following class that stores and processes histograms class percent_histogram: def __init__(self,init_percents=[]): self. histogram = 10*[0] # [0,0,0,,0,0] length 10, all 0s for p in init_percents: self. tally(p) # called when 0<=p<=100: 100//10 is 10 but belongs in index 9 def _tally(self,p): self. histogram[p//10 if p<100 else 9] += 1 def clear(self): for i in range(10): # vs self. histogram = 10*[0] self. histogram[i] = 0. # tally allows any number of arguments, collected in a tuple def tally(self,*args): if len(args) == 0 raise indexerror("percent_histogram. tally: no value(s) to tally") for p in args: if 0 <= p <= 100: self. _tally(p) else: raise indexerror("percent_histogram. tally: "+str(p)+" outside. # allow indexing for bins [0-9] but can mutate these values only through __init__, clear, and tally.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents