コード例 #1
0
ファイル: test_hashmap.py プロジェクト: bharatnc/hash_map
    def test_has(self):
        # New HashMap object
        HM = HashMap()

        # For Normal Inputs

        # Putting Value
        self.assertTrue(HM.put(1, "one"))
        self.assertTrue(HM.put(2, "two"))

        # Testing has()
        self.assertTrue(HM.has(1))
        self.assertTrue(HM.has(2))

        # For Inputs with colliding hash values
        self.assertTrue(HM.put(3, "three"))
        self.assertTrue(HM.put(25, "twenty-five"))
        self.assertTrue(HM.put(36, "thirty-six"))
        self.assertTrue(HM.put(47, "forty-seven"))
        self.assertTrue(HM.has(3))
        self.assertTrue(HM.has(25))
        self.assertTrue(HM.has(36))
        self.assertTrue(HM.has(47))
        self.assertTrue(HM.has(1))
        self.assertTrue(HM.has(2))
        self.assertFalse(HM.has(4))
コード例 #2
0
class DictionaryTable:
    __slots__ = 'hash_table'

    def __init__(self, hash_function, initial_size=10, max_load_factor=0.7):
        """
        This function is used to initialize the hashtable based on the hashfunction provided with a certain load factor
        which is used to increase the size of the hash table
        :param hash_function: a particular hash value calculating function
        :param initial_size: initial size of the table
        :param max_load_factor: loac factor for a table
        """
        self.hash_table = HashMap(initsz=initial_size, hash_func=hash_function, maxload=max_load_factor)

    def process_file(self, file_name):
        """
        This function is used to take input of words from the file with a provided filename
        :param file_name: name of the file along with the extension
        :return: None
        """
        with open(file_name) as file_pointer:
            for each_line in file_pointer:
                each_line = each_line.strip()
                list_of_words = re.split('\W+', each_line)
                for word in list_of_words:
                    if len(word) > 0:
                        self.count_frequency(word.lower())

    def count_frequency(self, word):
        """
        This function is used to keep the count of repetative words given in a particular file
        :param word: current word to check the count of
        :return: None
        """
        try:
            self.hash_table.put(word, self.hash_table.get(word) + 1)
        except KeyError:
            self.hash_table.put(word, 1)

    def print_hash_table(self):
        """
        This function is used to make a call to the print_map function print the entire hash table
        :return: None
        """
        self.hash_table.print_map()

    def get_max_occurred_word(self):
        """
        This method is used to return the word with maximum count
        :return: word which occured maximum time in a particular file
        """
        element = Entry(None, 0)
        for named_tuple in self.hash_table.table:
            if named_tuple is not None:
                if named_tuple.value > element.value:
                    element = named_tuple
        return element
コード例 #3
0
    def test_put_remove(self):
        hash_map = HashMap()
        size = 20
        for key in range(1, size + 1):
            value = key * 2
            hash_map.put(key, value)
        
        self.assertEqual(hash_map.size(), size)

        for key in range(1, size + 1):            
            hash_map.remove(key)

        self.assertEqual(hash_map.size(), 0)
コード例 #4
0
ファイル: test_hashmap.py プロジェクト: bharatnc/hash_map
    def test_sizeof(self):
        HM = HashMap()

        # Size for Normal Inputs
        self.assertTrue(HM.put(1, "one"))
        self.assertTrue(HM.put(2, "two"))
        self.assertEquals(HM.sizeof(), 2)

        # Size including colliding hashes
        self.assertTrue(HM.put(3, "three"))
        self.assertTrue(HM.put(25, "twenty-five"))
        self.assertTrue(HM.put(36, "thirty-six"))
        self.assertTrue(HM.put(47, "forty-seven"))
        self.assertEquals(HM.sizeof(), 6)
コード例 #5
0
def find_pairs(array):
    if len(array) < 2:
        return []
    else:
        output = []
        zero_count = 0
        for item in array:
            if item == 0:
                zero_count += 1
        if zero_count > 1:
            output.append((0, 0))
        mymap = HashMap()
        for item in array:
            mymap.put(-item, item)

        for item in array:
            if mymap.contains_key(item):
                if item != 0:
                    output.append((item, -item))
                    mymap.remove(item)
                    mymap.remove(-item)
        return output
コード例 #6
0
ファイル: test_hashmap.py プロジェクト: bharatnc/hash_map
    def test_put_and_get(self):
        # New HashMap object
        HM = HashMap()

        # For Normal Inputs

        # Putting Value
        self.assertTrue(HM.put(1, "one"))
        self.assertTrue(HM.put(2, "two"))
        # Getting value
        self.assertEquals(HM.get(1), "one")
        self.assertEquals(HM.get(2), "two")

        # For Inputs with colliding hash values

        # Putting values
        self.assertTrue(HM.put(3, "three"))
        self.assertTrue(HM.put(25, "twenty-five"))
        self.assertTrue(HM.put(36, "thirty-six"))
        self.assertTrue(HM.put(47, "forty-seven"))

        # Getting values
        self.assertEquals(HM.get(3), "three")
        self.assertEquals(HM.get(25), "twenty-five")
        self.assertEquals(HM.get(36), "thirty-six")
        self.assertEquals(HM.get(47), "forty-seven")

        # For inserting different values for same key

        # Putting value
        self.assertTrue(HM.put(4, "four"))
        # Getting value
        self.assertEquals(HM.get(4), "four")

        # Putting a different value

        self.assertTrue(HM.put(4, "four-again"))
        # Getting value
        self.assertTrue(HM.get(4), "four-again")

        # Getting a value not in hashmap

        self.assertFalse(HM.get(5), "five")

        # Getting value previously declared
        self.assertTrue(HM.get(4), "four-again")
コード例 #7
0
ファイル: test_hashmap.py プロジェクト: mosdragon/PythonDS
	def test_put(self):
		hmap = HashMap()
		keys = [(x + 1) for x in range(HashMap.CAPACITY_DEFAULT * 2)]
		vals = [key ** 3 for key in keys]

		threshold = HashMap.THRESHOLD
		size = 0

		# Stop just before the hashmap needs to be resized
		while float(size + 1) / float(hmap.capacity) < threshold:
			hmap.put(keys[size], vals[size])
			size += 1


		self.assertEquals(hmap.size, size)
		self.assertEquals(hmap.capacity, HashMap.CAPACITY_DEFAULT)
		self.assertEquals(len(hmap.mapping), HashMap.CAPACITY_DEFAULT)

		old_capacity = hmap.capacity
		# Will first resize itself
		# Then it will stop before the next resize
		while float(size + 1) / float(old_capacity * 2) < threshold:
			hmap.put(keys[size], vals[size])
			size += 1

		self.assertEquals(hmap.size, size)
		self.assertEquals(hmap.capacity, HashMap.CAPACITY_DEFAULT * 2)
		self.assertEquals(len(hmap.mapping), HashMap.CAPACITY_DEFAULT * 2)

		keys_size = len(keys)

		# Finish adding everything
		while size < keys_size:
			hmap.put(keys[size], vals[size])
			size += 1

		self.assertEquals(hmap.size, size)
		self.assertEquals(hmap.capacity, HashMap.CAPACITY_DEFAULT * 4)
		self.assertEquals(len(hmap.mapping), HashMap.CAPACITY_DEFAULT * 4)
コード例 #8
0
ファイル: test_hashmap.py プロジェクト: bharatnc/hash_map
    def test_remove(self):
        HM = HashMap()

        # Removing value from hashmap
        self.assertTrue(HM.put(6, "six"))
        self.assertTrue(HM.remove(6))

        # Removing values with colliding hash value
        self.assertTrue(HM.put(3, "three"))
        self.assertTrue(HM.put(25, "twenty-five"))
        self.assertTrue(HM.put(36, "thirty-six"))
        self.assertTrue(HM.put(47, "forty-seven"))
        self.assertTrue(HM.remove(3))
        self.assertTrue(HM.remove(25))
        self.assertTrue(HM.remove(36))
        self.assertFalse(HM.has(36))
        self.assertFalse(HM.has(25))
        self.assertTrue(HM.has(47))

        # Testing again
        self.assertTrue(HM.put(25, "twenty-five"))
        self.assertTrue(HM.has(25))

        self.assertTrue(HM.remove(47))
コード例 #9
0
ファイル: hashmap.test.py プロジェクト: kellyjovan/hashmap
class HashMapTest(unittest.TestCase):
    def setUp(self):
        self.hashmap = HashMap()

    def populate(self, values):
        for value in values:
            self.hashmap.put(*value)

    def remove(self, values):
        for value in values:
            self.hashmap.remove(value)

    def test_put(self):
        # Can add to hashmap
        self.hashmap.put('Apples', 5)
        self.assertEqual(self.hashmap.get('Apples'), 5)

        # Size should increase with added elements
        self.assertEqual(self.hashmap.size(), 1)

        # Hashmap should no longer be marked as empty
        self.assertEqual(self.hashmap.is_empty(), False)

        # Should still work with multiple items added
        fruits = [('Oranges', 1), ('Pineapples', 10), ('Mangos', 7),
                  ('Grapefruit', 9), ('Avocado', 53), ('Blueberry', 16),
                  ('Strawberries', 42), ('Cherries', 21), ('Durian', 18),
                  ('Guava', 99), ('Blackberries', 53), ('Cranberries', 42)]

        self.populate(fruits)

        # All inputted items should be retrievable
        for fruit in fruits:
            self.assertEqual(self.hashmap.get(fruit[0]), fruit[1])

        self.assertEqual(self.hashmap.size(), len(fruits) + 1)

        # Should be able to override pre-existing values
        self.hashmap.put('Apples', 20)
        self.assertEqual(self.hashmap.get('Apples'), 20)

        # Should not increase hashmap size
        self.assertEqual(self.hashmap.size(), len(fruits) + 1)

    def test_remove(self):
        # Should work if hashmap contains only one element in bucket
        self.hashmap.put('Apples', 15)
        self.hashmap.remove('Apples')
        self.assertEqual(self.hashmap.get('Apples'), None)

        fruits = [('Oranges', 1), ('Bananas', 2), ('Pineapples', 10),
                  ('Mangos', 7)]
        self.populate(fruits)

        # Should remove pre-existing element
        self.hashmap.remove('Bananas')
        self.assertEqual(self.hashmap.get('Bananas'), None)

        # Size should decrease
        self.assertEqual(self.hashmap.size(), len(fruits) - 1)

        for fruit in fruits:
            self.hashmap.remove(fruit[0])

        # Hash Map should be empty
        self.assertEqual(self.hashmap.size(), 0)
        self.assertEqual(self.hashmap.is_empty(), True)

    def test_resize(self):
        # Initialize HashMap with Initial Capacity of 4
        self.hashmap = HashMap(4)
        self.assertEqual(self.hashmap.capacity(), 4)

        fruits = [('Oranges', 1), ('Bananas', 2), ('Pineapples', 10),
                  ('Mangos', 7)]
        self.populate(fruits)

        # Hashmap should double in capacity
        self.assertEqual(self.hashmap.size(), 4)
        self.assertEqual(self.hashmap.capacity(), 8)

        # Items should still be accessible
        for fruit in fruits:
            self.assertEqual(self.hashmap.get(fruit[0]), fruit[1])

        for fruit in fruits:
            self.hashmap.remove(fruit[0])

        # Hashmap should halve in size
        self.assertEqual(self.hashmap.capacity(), 4)

    def test_mixed_calls(self):
        fruits = [('Oranges', 1), ('Pineapples', 10), ('Mangos', 7),
                  ('Grapefruit', 9), ('Avocado', 53), ('Blueberries', 16),
                  ('Strawberries', 42), ('Cherries', 21), ('Durian', 18),
                  ('Guava', 99), ('Blackberries', 53), ('Cranberries', 42)]

        self.populate(fruits)

        # Hashmap capacity should increase
        self.assertEqual(self.hashmap.capacity() > 8, True)

        fruits_to_remove = {'Grapefruit', 'Cherries', 'Guava', 'Oranges'}

        # Remove fruits from hashmap
        for fruit in fruits_to_remove:
            self.hashmap.remove(fruit)

        # Checks to make sure they are all deleted
        self.remove(fruits_to_remove)

        remaining = len(fruits) - len(fruits_to_remove)
        self.assertEqual(self.hashmap.size(), remaining)

        # Make sure all other fruits are still fine
        for fruit, val in fruits:
            if fruit in fruits_to_remove: continue
            self.assertEqual(self.hashmap.get(fruit), val)

        # Add some more fruits
        additional_fruits = [('Longan', 32), ('Pomegranate', 82),
                             ('Papaya', 92)]
        self.populate(additional_fruits)

        # Size should update accordingly
        new_size = remaining + len(additional_fruits)
        self.assertEqual(self.hashmap.size(), new_size)

        # Updating some existing fruit values
        updates = [('Papaya', 53), ('Cranberries', 32), ('Durian', 9)]
        self.populate(updates)

        # Size should not increase
        self.assertEqual(self.hashmap.size(), new_size)

        additional_fruits_to_remove = {
            'Longan', 'Pomegranate', 'Papaya', 'Strawberries', 'Blueberries',
            'Blackberries', 'Durian', 'Cranberries'
        }
        fruits_to_remove = fruits_to_remove.union(additional_fruits_to_remove)

        self.remove(fruits_to_remove)
        # Hashmap size should be a quarter of its capacity, capacity should halve
        self.assertEqual(self.hashmap.capacity(), 8)
コード例 #10
0
 def test_put_get(self):
     hash_map = HashMap()
     for key in range(1, 21):
         value = key * 2
         hash_map.put(key, value)
         self.assertEqual(hash_map.get(key), value)