コード例 #1
0
def main():
    """Runs the main program"""
    hash_map = HashMap()

    def count(key):
        """Function to return the value attached to the key given"""
        number = hash_map.get(key, 0)
        return number

    with open("AliceInWonderland.txt", 'r') as alice:
        for line in alice:
            words = clean_line(line)
            for i in words:
                hash_map.set(i, count(i) + 1)

    ranking = []
    position = 0
    while hash_map.lyst[position] is not None:
        ranking.append(hash_map.lyst[position])
        position += 1
    ranking.sort(key=lambda x: x[1], reverse=True)

    top_15 = []
    for i in range(15):
        top_15.append(ranking[i])

    print("The most common words are:")
    for i in top_15:
        if len(i[0]) >= 3:
            print(i[0], '\t', '\t', '\t', '\t', i[1])
        else:
            print(i[0], '\t', '\t', '\t', '\t', '\t', i[1])
コード例 #2
0
ファイル: tests.py プロジェクト: josefdaly/hashmap
 def test_can_set_and_get_basic_case(self):
     hashmap = HashMap()
     self.assertEqual(hashmap._number_of_elements, 0)
     hashmap.set('key', 'val')
     self.assertEqual(hashmap._number_of_elements, 1)
     self.assertEqual(hashmap.get('key'), 'val')
     self.assertIsNone(hashmap.get('otherval'))
コード例 #3
0
ファイル: test_hashmap.py プロジェクト: mikehaus/CS2420
 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)
コード例 #4
0
def test_rehashing():
    keys = [(r, r) for r in (range(10))]
    values = list(range(1, 11))
    hm = HashMap()
    for k, v in zip(keys, values):
        hm.set(k, v)
    assert hm.size() == 10
    assert hm.capacity() == 13
コード例 #5
0
ファイル: test_hashmap.py プロジェクト: mikehaus/CS2420
 def test_key_present(self):
     hm = HashMap()
     self.assertEqual(hm.get("asdf"), None)
     self.assertEqual(hm.get("asdf", 0), 0)
     hm.set("qwerty", 12345)
     self.assertEqual(hm.get("qwerty"), 12345)
     self.assertEqual(hm.size(), 1)
     self.assertEqual(hm.capacity(), 8)
コード例 #6
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
コード例 #7
0
def test_clear():
    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)
    hm.clear()
    assert hm.capacity() == 7
    assert hm.size() == 0
コード例 #8
0
ファイル: main.py プロジェクト: tannershimanek/hw-cs2420
def main():
    '''Driver function for hashmap.py.'''
    hm = HashMap()
    with open('AliceInWonderland.txt', 'r') as file:
        for line in file:
            for word in clean_line(line):
                hm.set(word, 1)
    print('The most common words are:')
    get_most_used_words(hm)
コード例 #9
0
ファイル: tests.py プロジェクト: josefdaly/hashmap
 def test_can_set_and_get_many_values(self):
     hashmap = HashMap()
     for number in range(0, 20):
         key, val = str(number), number
         hashmap.set(key, val)
     self.assertEqual(hashmap._number_of_elements, 20)
     for number in range(0, 20):
         key, expected_val = str(number), number
         self.assertEqual(hashmap.get(key), expected_val)
コード例 #10
0
ファイル: tests.py プロジェクト: josefdaly/hashmap
 def test_can_set_and_get_multiple_values(self):
     hashmap = HashMap()
     self.assertEqual(hashmap._number_of_elements, 0)
     hashmap.set('key', 'val')
     self.assertEqual(hashmap._number_of_elements, 1)
     self.assertEqual(hashmap.get('key'), 'val')
     hashmap.set('otherkey', 'otherval')
     self.assertEqual(hashmap._number_of_elements, 2)
     self.assertEqual(hashmap.get('otherkey'), 'otherval')
     self.assertEqual(hashmap.get('key'), 'val')
     self.assertIsNone(hashmap.get('blah'))
コード例 #11
0
def test_remove():
    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)
    hm.remove((3, 3))
    print(hm)
    with pytest.raises(KeyError):
        hm.get((3, 3))
    assert hm.get((5, 5)) == 6
コード例 #12
0
 def test_ten_keys(self):
     hm = HashMap()
     keys = ["first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth"]
     values = list(range(1, 11))
     for i in range(len(keys)):
         hm.set(keys[i], values[i])
     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)
コード例 #13
0
def test_get_set():
    hm = HashMap()
    with pytest.raises(KeyError):
        hm.get((0, 0))

    keys = [(r, r) for r in (range(10))]
    values = list(range(1, 11))
    for k, v in zip(keys, values):
        hm.set(k, v)
    assert hm.get((5, 5)) == 6
    assert hm.get((9, 9)) == 10
    hm.set((2, 2), 409)
    assert hm.get((2, 2)) == 409
コード例 #14
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()
コード例 #15
0
def main():
    """ Main driver to run program. """
    hashmap = HashMap()

    text_file = open("AliceInWonderland.txt", "r", encoding="utf8")

    for line in text_file:
        temp = clean_line(line)

        for word in temp:
            if hashmap.contains(word) is True:
                tempvalue = hashmap.get(word)
                hashmap.set(word, (tempvalue + 1))
            else:
                hashmap.set(word, 1)

    hashmap.print_top()
    text_file.close()
コード例 #16
0
ファイル: main.py プロジェクト: mikehaus/CS2420
def main():
    """
    Main function
    """
    hashmap = HashMap()
    file = open('AliceInWonderland.txt', 'r')
    for line in file:
        text = clean_line(line)
        for word in text:
            hashmap.set(word)
    file.close()
    mylist = []
    for i in hashmap.dictionary:
        if i is not None:
            mylist.append(i)
    mylist.sort(key=lambda x: x[1], reverse=True)
    print('The most common words are:')
    for i in range(0, 15):
        index = mylist[i]
        print(index[0] + '       ' + str(index[1]))
コード例 #17
0
class test_hashmap(unittest.TestCase):
    """
    Test class for class Hashmap

    In all the quadratic probing tests unexpected errors can occur, for example
    if size of hashmap is small even one element can fill up a significant amount
    of size of the hashmap. Therefore I didn't use loop testing in quadratic probing
    it was causing random errors! 
    """
    def setUp(self):
        size = math.floor(
            random.random() *
            30) + 30  # minimum size 15 to avoid coflicts with line 26
        self._dict = HashMap(size, probing='quadratic', verbose=False)

    def test_hash_index(self):
        idx = self._dict.hash_index('divyansh')
        verify = 'divyansh'.__hash__() & (
            self._dict.size - 1
        )  # __hash__ returns different hashes for different python runs
        self.assertEqual(idx, verify)

    def test_sanity_check_key(self):
        self.assertRaises(ValueError, self._dict.sanity_check_key,
                          23)  # passed any number to raise exception
        self.assertRaises(ValueError, self._dict.sanity_check_key,
                          [2, 3, 4])  # passed a list to raise exception
        self.assertRaises(ValueError, self._dict.sanity_check_key,
                          ('asd', 'asd'))  # passed a tuple to raise exception

    def test_probe_linear(self):
        self._dict.probing = 'linear'
        num = self._dict.probe(5)
        self.assertEqual(num, 6)
        self._dict.ctr = 1
        num = self._dict.probe(self._dict.size + 3)
        self.assertEqual(num, 4)

    def test_probe_quadratic(self):
        self._dict.probing = 'quadratic'
        self.assertEqual(self._dict.probe(5), 6)
        self.assertEqual(self._dict.probe(6), 10)
        self.assertEqual(self._dict.probe(1), 10)
        self._dict.reset()
        self.assertEqual(self._dict.probe(self._dict.size - 1), 0)
        self.assertEqual(self._dict.probe(self._dict.size - 1), 3)

    def test_set_quadratic(self):
        self.assertEqual(self._dict.set('animal', 'monkey'), True)
        self.assertEqual(self._dict.set('animal', 'lion'), True)
        self.assertRaises(ValueError, self._dict.set, 23, 'twenty_three')

    def test_set_linear(self):
        self._dict.probing = 'linear'
        self.assertEqual(self._dict.set('animal', 'monkey'), True)
        self.assertEqual(self._dict.set('animal', 'lion'), True)
        self.assertRaises(ValueError, self._dict.set, 23, 'twenty_three')
        self._dict.reset()
        for i in range(self._dict.size):
            self.assertEqual(
                self._dict.set('animal' + str(i), 'monkey' + str(i)), True)
        self.assertEqual(self._dict.load(), 1)
        self.assertEqual(
            self._dict.set('one_more_key_to_cause_overflow', 'monkey_n'),
            False)

    def test_get_quadratic(self):
        self.probing = 'quadratic'
        self.assertEqual(self._dict.set('animal' + str(3), 'monkey' + str(3)),
                         True)
        self.assertEqual(self._dict.get('animal' + str(3)), 'monkey' + str(3))
        self.assertRaises(KeyError, self._dict.get, 'bird')

    def test_get_linear(self):
        self._dict.probing = 'linear'
        for i in range(self._dict.size):
            self.assertEqual(
                self._dict.set('animal' + str(i), 'monkey' + str(i)), True)
        for i in reversed(range(self._dict.size)):
            self.assertEqual(self._dict.get('animal' + str(i)),
                             'monkey' + str(i))
        self.assertRaises(KeyError, self._dict.get, 'bird')

    def test_delete_quadratic(self):
        self._dict.probing = 'quadratic'
        self._dict.set('animal', 'monkey')
        self._dict.delete('animal')
        self.assertRaises(KeyError, self._dict.get, 'animal')

    def test_delete_linear(self):
        self._dict.probing = 'linear'
        self._dict.set('animal', 'monkey')
        self._dict.delete('animal')
        for i in range(self._dict.size):
            self.assertEqual(
                self._dict.set('animal' + str(i), 'monkey' + str(i)), True)
        for i in reversed(range(self._dict.size)):
            self.assertEqual(self._dict.delete('animal' + str(i)), None)
            self.assertRaises(KeyError, self._dict.delete, 'animal' + str(i))
        self.assertRaises(KeyError, self._dict.get, 'animal')

    def test_load_quadratic(self):
        self.probing = 'quadratic'
        itr = math.floor(random.random() * self._dict.size / 2 + 5)
        for i in range(itr):
            random_key = ''.join(
                random.choices(string.ascii_uppercase + string.digits, k=15))
            self._dict.set(random_key, 'monkey' + str(i))
        load_value = self._dict.load()
        self.assertLessEqual(load_value, 0.6)

    def test_load_linear(self):
        self.probing = 'linear'
        itr = math.floor(random.random() * self._dict.size + 5)
        for i in range(itr):
            random_key = ''.join(
                random.choices(string.ascii_uppercase + string.digits, k=15))
            self._dict.set(random_key, 'monkey' + str(i))
        load_value = self._dict.load()
        self.assertLessEqual(load_value, 1)
コード例 #18
0
ファイル: usage.py プロジェクト: divyanshj16/hashmap
from hashmap import HashMap

my_dict1 = HashMap(100, probing='quadratic')
my_dict1.set('animal', 'lion')
print("obj=mydict1, key = animal, value = %s" % my_dict1.get('animal'))

my_dict2 = HashMap(10, 'linear')
my_dict2.set('animal', 'monkey')
print("obj=mydict2, key = animal, value = %s" % my_dict2.get('animal'))
コード例 #19
0
ファイル: tests.py プロジェクト: josefdaly/hashmap
 def test_len_method(self):
     hashmap = HashMap()
     self.assertEqual(len(hashmap), 0)
     for number in range(0, 20):
         hashmap.set(str(number), number)
     self.assertEqual(len(hashmap), 20)