def test_empty_buckets(self):
        """Checks the empty_buckets method"""
        test_values = [("test_5", 5), ("test_-5", -5), ("test_5_", 5),
                       ("diff_word", 15), ("another_word", 20), ("set", 10),
                       ("anotha_one", -7), ("completely_different", 5),
                       ("getting_there", -1)]

        empty_buckets = initial_capacity = 10
        student_map = HashMap(initial_capacity, hash_function_1)
        self.assertEqual(empty_buckets, student_map.empty_buckets())

        empty_buckets = initial_capacity = 20
        student_map = HashMap(initial_capacity, hash_function_1)
        self.assertEqual(empty_buckets, student_map.empty_buckets())

        student_map.put("first_value", 5)
        self.assertEqual(empty_buckets - 1, student_map.empty_buckets())

        student_map = HashMap(10, hash_function_1)
        student_map_hf2 = HashMap(10, hash_function_2)
        for key, val in test_values:
            student_map.put(key, val)
            student_map_hf2.put(key, val)
        # should have 5 empty buckets with hash_function_1
        self.assertEqual(5, student_map.empty_buckets())
        # 4 with hash_function_2
        self.assertEqual(4, student_map_hf2.empty_buckets())
Ejemplo n.º 2
0
 def test_empty_buckets_1(self):
     """
     Test empty_buckets() with Example #1 from the guidelines.
     :passed: yes
     """
     print("--- EXAMPLE 1 ---")
     m = HashMap(100, hash_function_1)
     print(m.empty_buckets(), m.size, m.capacity)
     m.put('key1', 10)
     print(m.empty_buckets(), m.size, m.capacity)
     m.put('key2', 20)
     print(m.empty_buckets(), m.size, m.capacity)
     m.put('key1', 30)
     print(m.empty_buckets(), m.size, m.capacity)
     m.put('key4', 40)
     print(m.empty_buckets(), m.size, m.capacity)
Ejemplo n.º 3
0
 def test_empty_buckets_2(self):
     """
     Test empty_buckets() with Example #2 from the guidelines.
     :passed: yes
     """
     print("--- EXAMPLE 2 ---")
     m = HashMap(50, hash_function_1)
     for i in range(150):
         m.put('key' + str(i), i * 100)
         if i % 30 == 0:
             print(m.empty_buckets(), m.size, m.capacity)
Ejemplo n.º 4
0
 def test_put_5(self):
     """
     Test put() with Example #2 from the guidelines.
     :passed: yes
     """
     print("--- EXAMPLE 2 ---")
     m = HashMap(40, hash_function_2)
     for i in range(50):
         m.put('str' + str(i // 3), i * 100)
         if i % 10 == 9:
             print(m.empty_buckets(), m.table_load(), m.size, m.capacity)
Ejemplo n.º 5
0
 def test_put_4(self):
     """
     Test put() with Example #1 from the guidelines.
     :passed: yes
     """
     print("--- EXAMPLE 1 ---")
     m = HashMap(50, hash_function_1)
     for i in range(150):
         m.put('str' + str(i), i * 100)
         if i % 25 == 24:
             print(m.empty_buckets(), m.table_load(), m.size, m.capacity)
def top_words(source, number):
    """
    Takes a plain text file and counts the number of occurrences of case insensitive words.
    Returns the top `number` of words in a list of tuples of the form (word, count).

    Args:
        source: the file name containing the text
        number: the number of top results to return (e.g. 5 would return the 5 most common words)
    Returns:
        A list of tuples of the form (word, count), sorted by most common word. (e.g. [("a", 23), ("the", 20), ("it", 10)])
    """

    result = []

    ht = HashMap(2500, hash_function_2)

    # This block of code will read a file one word at a time
    # and add it to the hash map
    with open(source) as f:
        for line in f:
            words = rgx.findall(line)
            for w in words:
                lw = w.lower()

                # If the word is not in the hash map, add it with a value of 1
                if not ht.contains_key(lw):
                    ht.put(lw, 1)
                else:
                    # Otherwise, update the value by increasing it by 1
                    ht.put(lw, ht.get(lw) + 1)

    for bucket in ht._buckets:
        cur = bucket.head
        while cur is not None:
            result.append((cur.key, cur.value))
            cur = cur.next

    print(ht.table_load())
    print(ht.empty_buckets())

    sort_words(result)
    return result[:number]
Ejemplo n.º 7
0
# Author: Leon Samuel
# Date: May 28, 2020
# Description: Testing for hash map program

from hash_map import SLNode, LinkedList, HashMap, hash_function_1, hash_function_2
""" EMPTY BUCKETS """
print("\n\n********   EMPTY_BUCKETS()   ********")
print("--- EXAMPLE 1 ---")
m = HashMap(100, hash_function_1)
print(m.empty_buckets(), m.size, m.capacity)
m.put('key1', 10)
print(m.empty_buckets(), m.size, m.capacity)
m.put('key2', 20)
print(m.empty_buckets(), m.size, m.capacity)
m.put('key1', 30)
print(m.empty_buckets(), m.size, m.capacity)
m.put('key4', 40)
print(m.empty_buckets(), m.size, m.capacity)

print("--- EXAMPLE 2 ---")
m = HashMap(50, hash_function_1)
for i in range(150):
    m.put('key' + str(i), i * 100)
    if i % 30 == 0:
        print(m.empty_buckets(), m.size, m.capacity)
""" TABLE LOAD """
print("\n\n********   TABLE_LOAD()   ********")
print("--- EXAMPLE 1 ---")
m = HashMap(100, hash_function_1)
print(m.table_load())
m.put('key1', 10)