Example #1
0
class Histogram:
	# Creates a histogram containing the given categories
	def __init__(self, catSeq):
		self._freqCounts = HashMap()
		for cat in catSeq:
			self._freqCounts.add(cat, 0)

	# Returns the frequency count for the given category
	def getCount(self, category):
		assert category in self._freqCounts, "Invalid histogram category"

		return self._freqCounts.valueOf(category)

	# Increments the counter for the given category
	def incCount(self, category):
		assert category in self._freqCounts, "Invalid histogram category"

		value = self._freqCounts.valueOf(category)
		self._freqCounts.add(category, value + 1)

	# Returns the sum of the frequency counts
	def totalCount(self):
		total = 0
		for cat in self._freqCounts:
			total += self._freqCounts.valueOf(cat)

		return total

	# Returns an iterator for traversing the categories
	def __iter__(self):
		return iter(self._freqCounts)
Example #2
0
class Histogram:
    """ Implementation of the Histogram ADT using a Hash Map """

    def __init__(self, catSeq):
        """ Creates a histogram containing the given categories """

        self._freqCounts = HashMap()
        for cat in catSeq:
            self._freqCounts.add(cat, 0)

    def getCount(self, category):
        """ Returns the frequency count for the given category. """

        assert category in self._freqCounts, "Invalid histogram category."
        return self._freqCounts.valueOf(category)

    def incCount(self, category):
        """ Increments the counter for the given category """

        assert category in self._freqCounts, "Invalid histogram category."
        value = self._freqCounts.valueOf(category)
        self._freqCounts.add(category, value + 1)

    def totalCount(self):
        """ Returns the sum of the frequency counts. """

        total = 0
        for cat in self._freqCounts:
            total += self._freqCounts.valueOf(cat)
        return total

    def __iter__(self):
        """ Returns an iterator for traversing the categories """

        return iter(self._freqCounts)
Example #3
0
class Histogram(object):
	# Create a histogram containing the given categories.
	def __init__(self, catSeq):
		self._freqCounts = HashMap()
		for cat in catSeq:
			self._freqCounts.add(cat, 0)
			
	# Return the frequency count for the given category.
	def getCount(self, category):
		assert category in self._freqCounts, 'Invalid histogram category.'
		return self._freqCounts.valueOf(category)
		
	# Increment the counter for the given category.
	def incCount(self, category):
		assert category in self._freqCounts, 'Invalid histogram category.'
		value = self._freqCounts.valueOf(category)
		self._freqCounts.add(category, value + 1)
		
	# Return the sum of the frequency counts.
	def totalCount(self):
		total = 0
		for cat in self._freqCounts:
			total += self._freqCounts.valueOf(cat)
		return total
		
	# Return an iterator for traversing the categories.
	def __iter__(self):
		return iter(self._freqCounts)
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 )
Example #5
0
keys = [ 6, 13, 100, 29, 12, 5, 15, 18, 101, 56 ]
values = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]

# Test insertion
for i in range( len( keys ) ):
    print( 'inserting ', keys[ i] , '/', values[ i]  )
    myMap.add( keys[ i ], values[ i ] )    # (key, value) pair

# Test the iterator
print( 'original map : ' )
for v in myMap:
    print( '[', v.key, '/', v.value, ']', end = ' ' )
print()

# Test search
print( 'value of key 6 should be 0, it is ', myMap.valueOf( 6 ) )
print( 'value of key 56 should be 9, it is ', myMap.valueOf( 56 ) )
print( 'value of key 12 should be 4, it is ', myMap.valueOf( 12 ) )

# Test removal
myMap.remove( 100 )
myMap.remove( 56 )

print( 'map after removing keys "100", "56" : ' )
for v in myMap:
    print( '[', v.key, '/', v.value, ']', end = ' ' )
print()

print( 'value of key 6 should be 0, it is ', myMap.valueOf( 6 ) )
print( 'value of key 12 should be 4, it is ', myMap.valueOf( 12 ) )
print( 'value of key 56 should be "not found", it is ', myMap.valueOf( 56 ) )