Пример #1
0
 def test_rehashing(self):
     keys = [
         "first", "second", "third", "fourth", "fifth", "sixth", "seventh",
         "eighth", "ninth", "tenth"
     ]
     values = list(range(1, 11))
     hm = HashMap()
     self.assertEqual(hm.size(), 0)
     self.assertEqual(hm.capacity(), 8)
     for i in range(0, 5):
         hm.set(keys[i], values[i])
     self.assertEqual(hm.size(), 5)
     self.assertEqual(hm.capacity(), 8)
     output = hm.keys()
     self.assertEqual(len(output), 5)
     for key in output:
         self.assertIn(key, keys)
     for i in range(5, 10):
         hm.set(keys[i], values[i])
     self.assertEqual(hm.size(), 10)
     self.assertEqual(hm.capacity(), 16)
     output = hm.keys()
     for key in output:
         self.assertIn(key, keys)
     for key in keys:
         self.assertIn(key, output)
Пример #2
0
def test_keys():
    hm = HashMap()
    keys = [(r, r) for r in (range(10))]
    values = list(range(1, 11))
    for k, v in zip(keys, values):
        hm.set(k, v)
    keys2 = hm.keys()
    keys2.sort()
    assert keys == keys2
class TestHashMapFunctions(unittest.TestCase):

    def setUp(self):
        self._ref = {}
        self._map = HashMap()

    def add(self, key, value):
        self._ref[key] = value
        self._map[key] = value

    def delete(self, key, value):
        del self._ref[key]
        del self._map[key]

    def test_add(self):
        for i in xrange(100):
            self.add("key_"+str(i), "value_"+str(i))
        self.assert_ref_equals_map()

    def test_delete(self):
        for i in xrange(100):
            self.add("key_"+str(i), "value_"+str(i))
        for i in xrange(0, 100, 2):
            self.delete("key_"+str(i), "value_"+str(i))
        self.assert_ref_equals_map()

    def test_get(self):
        for i in xrange(100):
            self.add("key_"+str(i), "value_"+str(i))
        for i in xrange(100):
            self.assertEqual(self._ref["key_"+str(i)], self._map["key_"+str(i)])

    def test_random(self):
        import random
        for i in xrange(10000):
            r = random.randint(0, 2)
            key = "key_"+str(random.randint(0, 9))
            value = "value_"+str(random.randint(0, 9))
            if r == 0:
                self.add(key, value)
            elif r == 1 and self._ref.get(key, None) is not None:
                self.delete(key, value)
            else:
                self.assertEqual(self._ref.get(key, None), self._map.get(key, None))



    def assert_ref_equals_map(self):
        self.assertItemsEqual(self._ref.keys(), self._map.keys())
        self.assertItemsEqual(self._ref.values(), self._map.values())
Пример #4
0
def main():
    ''' driver for project '''
    hash_map = HashMap()
    with open("AliceInWonderland.txt", encoding='utf8') as input_file:
        for raw_line in input_file:
            words = clean_line(raw_line)
            for word in words:
                hash_map.set(word, hash_map.get(word, 0) + 1)
    keys = hash_map.keys()
    key_values = [(key, hash_map.get(key)) for key in keys]
    key_values.sort(reverse=True, key=lambda x: x[1])

    print("The most common words are:")
    for element in key_values[:15]:
        print(f'{element[0]}\t\t{element[1]}')
    print()
Пример #5
0
 def test_ten_keys(self):
     hm = HashMap()
     keys = [
         "first", "second", "third", "fourth", "fifth", "sixth", "seventh",
         "eighth", "ninth", "tenth"
     ]
     values = list(range(1, 11))
     print(values)
     for i in range(len(keys)):
         hm.set(keys[i], values[i])
     print(hm.keys())
     # print(hm.get('sixth'))
     self.assertEqual(hm.get("sixth"), 6)
     self.assertNotEqual(hm.get("sixth"), 7)
     self.assertNotEqual(hm.get("sixth"), 5)
     hm.set("third", 409)
     self.assertEqual(hm.get("third"), 409)
     self.assertEqual(hm.size(), 10)
     self.assertEqual(hm.capacity(), 16)
Пример #6
0
class Distance:
    def __init__(self):
        self.address = HashMap()
        self.distance = []
        self.address_manager = []
        self.combo = {}

    def add_address(self, address, zip_code):
        # Big O: O(1)
        # inserts address into address list
        self.address.insert(address, zip_code)

    def print_addresses(self):
        # Big O: O(1)
        # keys to address list
        self.address.keys()

    def add_distance(self, distance):
        # Big O: O(1)
        # appends distance to list
        self.distance.append(distance)

    def combine(self):
        # Big O: O(N^2)
        # combines the two separate lists (addresses and distances)
        # into a usable dictionary
        keys = self.address.keys()
        distances = self.distance
        for i in keys:
            for j in distances:
                if i in j:
                    del j[0]
                    self.combo[i] = j
        return self.combo

    def distances_from_address(self, address):
        # Big O: O(1)
        # return all distances from address
        return self.combo[address]

    def distance_to_specific(self, address_a, index_b):
        # Big O: O(1)
        # Retrieve the distance between two addresses
        return float(self.combo[address_a][index_b - 1])

    def manage_address(self, address):
        # Big O: O(1)
        # appends address to an address list
        self.address_manager.append(address)
        return self.address_manager

    def index_to_address(self, index):
        # Big O: O(1)
        # finds the address associated with an index
        return self.address_manager[index]

    def address_to_index(self, address):
        # Big O: O(N)
        index_number = 0
        for i in self.address_manager:
            index_number += 1
            if i == address:
                return index_number

    def closest_neighbor(self, address):
        # Big O: O(N)
        # return index # of closest distance
        testing = []
        for i in self.combo[address]:
            testing.append(float(i))
        m = min(i for i in testing if i > 0)
        min_index = testing.index(m)
        closest = self.index_to_address(min_index)
        print('Between {} and {} is {}'.format(address, closest, m))

    def distance_traveled(self, time, list):
        # Big O: O(N)
        # calculates the distances between each address in the list
        # returns the distances in list form
        a = list
        distance_between = []
        for i, nexti in zip(a, a[1::]):
            next_index = self.address_to_index(nexti)
            distance_between.append(self.distance_to_specific(i, next_index))
        return distance_between

    def load_distances(self, node_list, combo, distances):
        # Big O: O(N)
        # read files, sets up addresses and distances between each address
        with open(node_list) as set_address:
            for line in set_address:
                line = line.strip().split('\t')
                distances.add_address(line[0], line)
                distances.manage_address(line[0])
        with open(combo) as set_distance:
            for line in set_distance:
                split = line.strip().split('\t')
                distances.add_distance(split)
        distances.combine()
Пример #7
0
from hashmap import HashMap

# Utilizing hashmap class
hash_map = HashMap()

print("Initializing empty hashmap " + str(hash_map))

hash_map["FirstKey"] = 23
hash_map["SecondKey"] = 53
print("Adding two values to hashmap " + str(hash_map))

hash_map["FirstKey"] = 13
print("Updating first value in hashmap " + str(hash_map))

hash_map.delete("FirstKey")
print("Deleting value in hashmap " + str(hash_map))

hash_map_two = HashMap()
hash_map_two["FirstKey"] = 203
print("Initializing second hashmap " + str(hash_map_two))

hash_map.update(hash_map_two)
print("Updating first hashmap with second hashmap " + str(hash_map))

print("All keys in hashamp " + str(hash_map.keys()))
print("All values in hashmap " + str(hash_map.values()))

print("The amount of buckets in hashmap " + str(hash_map.num_sections()))