-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmaphist.py
27 lines (21 loc) · 850 Bytes
/
maphist.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# Implementation of the Histogram ADT using a Hash Map.
from hashmap import HashMap
class Histogram:
def __init__( self, catSeq ):
self._freqCounts = HashMap()
for cat in catSeq:
self._freqCounts.add( cat, 0 )
def getCount( self, category ):
assert category in self._freqCounts, "Invalid histogram category."
return self._freqCounts.valueOf( category )
def incCount( self, category ):
assert category in self._freqCounts, "Invalid histogram category."
value = self._freqCounts.valueOf( category )
self._freqCounts.add( category, value + 1 )
def totalCount( self ):
total = 0
for cat in self._freqCounts:
total += self._freqCounts.valueOf( cat )
return total
def __iter__( self ):
return iter( self._freqCounts )