Esempio n. 1
0
def test_all():
    hash_map = HashMap()
    el_count = EL_COUNT
    for x in range(el_count):
        hash_map.put(str(x), str(x))
    for x in range(el_count):
        assert hash_map.get(str(x)) == str(x)
Esempio n. 2
0
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)])
    """

    keys = set()

    ht = HashMap(2500,hash_function_2)

    # This block of code will read a file one word as a time and
    # put the word in `w`. It should be left as starter code.
    with open(source) as f:
        for line in f:
            words = rgx.findall(line)
            for w in words:
                if ht.contains_key(w.lower()):
                    ht.put(w.lower(), ht.get(w.lower()) + 1)
                else:
                    ht.put(w.lower(), 1)

        tup = ht.sorted_tup()

        return tup[:number]
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)])
    """

    keys = set()  # Variable representing empty set.
    ht = HashMap(
        2500, hash_function_2
    )  # Variable to represent hash map construct utilizing above function.

    # This block of code will read a file one word as a time and
    # put the word in `w`. It should be left as starter code.
    with open(source
              ) as f:  # Opens file to be used declaring it as variable 'f'.
        for line in f:  # Loops through each line within file (f).
            words = rgx.findall(
                line)  # Variable utilized to represent words within each line.
            for w in words:  # Loops through each word within each line.
                lw = w.lower(
                )  # Turns words lowercase to remove case sensitivity.
                keys.add(
                    lw
                )  # Adds lowercase word to set represented by variable 'key'.
                if ht.contains_key(
                        lw):  # Checks if word is already present in hash map.
                    new_value = (
                        ht.get(lw) + 1
                    )  # Variable represents word count increased by one.
                    ht.put(
                        lw, new_value
                    )  # Inserts word into hash map to have word count be updated.
                else:
                    ht.put(
                        lw, 1
                    )  # Inserts word into hash map with initial count of one.
    keys_list = []  # Variable represents an empty list.
    for values in keys:  # Loops through words present in set represented by variable 'keys'.
        ind = ht._hash_function(values) % ht.capacity
        # Variable to represent number established by chosen function and available capacity.
        temp = ht._buckets[
            ind]  # Variable to represent position within hash map containing linked list.
        node = temp.contains(
            values
        )  # Variable to represent node containing key if already present.
        keys_list.append(
            (node.key,
             node.value))  # Adds tuple to list containing word, word count.
    keys_list.sort(
        key=lambda tup: tup[1],
        reverse=True)  # Sorts list in descending order based on word count.
    return keys_list[
        0:
        number]  # Returns list of top words within given range provided by user.
    def test_get(self):
        """Tests the HashMap get 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)]

        collision_values = [("completely_different", 5), ("anotha_one", -7),
                            ("set", 10), ("another_word", 20)]
        head_node = collision_values[0]
        tail_node = collision_values[3]
        student_map = HashMap(10, hash_function_1)

        # add all key value pairs to the table
        for key, val in test_values:
            student_map.put(key, val)

        # test get at linked_list head
        self.assertEqual(student_map.get(head_node[0]), head_node[1])

        # test get at linked_list tail
        self.assertEqual(student_map.get(tail_node[0]), tail_node[1])

        # test get at > 2 collision bucket
        for node in collision_values:
            self.assertEqual(student_map.get(node[0]), node[1])

        # test get with no collision
        self.assertEqual(student_map.get("getting_there"), -1)

        # test that all values are in the list
        for node in test_values:
            self.assertEqual(student_map.get(node[0]), node[1])
    def test_resize_table(self):
        """Tests the resize_table method: checks that links are not changed and properties are updated"""
        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)]
        student_map = HashMap(10, hash_function_1)
        for key, value in test_values:
            student_map.put(key, value)
        self.assertEqual(10, student_map.capacity)

        # get before resize state
        keys_before_resize = get_keys_from_map(student_map)
        size_before_resize = student_map.size

        # Test 1
        # resize the table smaller -> bigger
        student_map.resize_table(50)
        self.assertEqual(50, student_map.capacity)

        # get after resize state
        keys_after_resize = get_keys_from_map(student_map)
        size_after_resize = student_map.size

        # test that no nodes were lost in the resize
        self.assertEqual(len(keys_before_resize), len(keys_after_resize))
        self.assertEqual(size_before_resize, size_after_resize)
        for key in keys_before_resize:
            self.assertTrue(key in keys_after_resize)
        for key in keys_after_resize:
            self.assertTrue(key in keys_before_resize)

        # Test 2
        student_map = HashMap(10, hash_function_1)
        for key, value in test_values:
            student_map.put(key, value)
        self.assertEqual(10, student_map.capacity)

        # get before resize state
        keys_before_resize = get_keys_from_map(student_map)
        size_before_resize = student_map.size

        # resize the table bigger -> smaller
        student_map.resize_table(5)
        self.assertEqual(5, student_map.capacity)

        # get after resize state
        keys_after_resize = get_keys_from_map(student_map)
        size_after_resize = student_map.size

        # test that no nodes were lost in the resize
        self.assertEqual(len(keys_before_resize), len(keys_after_resize))
        self.assertEqual(size_before_resize, size_after_resize)
        for key in keys_before_resize:
            self.assertTrue(key in keys_after_resize)
        for key in keys_after_resize:
            self.assertTrue(key in keys_before_resize)
Esempio n. 6
0
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)])
    """

    keys = set()

    ht = HashMap(2500, hash_function_2)

    # Reads a file one word as a time and
    with open(source) as f:
        for line in f:
            words = rgx.findall(line)
            for w in words:
                w = w.lower(
                )  # covert word to lowercase for case-insensitive comparisons
                if ht.contains_key(
                        w
                ):  # if word already exists as key in ht, add 1 to value to track count
                    value = ht.get(w)
                    ht.put(w, value + 1)

                else:
                    ht.put(
                        w, 1
                    )  # if word does not exist in ht as key, add word as key and initialize value as 1
                    keys.add(w)  # add word to set of keys

    count_dict = {}  # initialize empty dictionary
    count_array = []  # initialize empty array

    for key in keys:  # for each key, get it's value from ht and then add key/value pair to count_dict
        value = ht.get(key)
        count_dict[key] = value

    for key in keys:  # for each key, add value/key pair to array for sorting
        count_array.append((count_dict[key], key))

    count_array = sorted(
        count_array, reverse=True
    )  # reverse sort count_array from largest to smallest value

    for i in range(
            len(count_array)
    ):  # reswap key/value pairs to get (word, count) for each tuple in count_array
        count_array[i] = (count_array[i][1], count_array[i][0])

    return count_array[:
                       number]  # return only the requested number of top words
 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 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)
 def test_table_load_2(self):
     """
     Test table_load() with Example #2 from the guidelines.
     :passed: yes
     """
     print("--- EXAMPLE 2 ---")
     m = HashMap(50, hash_function_1)
     for i in range(50):
         m.put('key' + str(i), i * 100)
         if i % 10 == 0:
             print(m.table_load(), m.size, m.capacity)
Esempio n. 10
0
 def test_resize_table_1(self):
     """
     Test resize_table() with Example #1 from the guidelines.
     :passed: yes
     """
     print("--- EXAMPLE 1 ---")
     m = HashMap(20, hash_function_1)
     m.put('key1', 10)
     print(m.size, m.capacity, m.get('key1'), m.contains_key('key1'))
     m.resize_table(30)
     print(m.size, m.capacity, m.get('key1'), m.contains_key('key1'))
Esempio n. 11
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)
Esempio n. 12
0
    def test_put_1(self):
        """
        Test put() on a hash map of capacity 0.
        :passed: yes
        """
        hash_m = HashMap(0, hash_function_1)
        print("map before put():", hash_m)

        hash_m.put("key1", 10)
        print("put('key1', 10):", hash_m)

        print(hash_m.put("key1", 10))
Esempio n. 13
0
 def test_remove_1(self):
     """
     Test remove() with Example #1 from the guidelines.
     :passed: yes
     """
     print("--- EXAMPLE 1 ---")
     m = HashMap(50, hash_function_1)
     print(m.get('key1'))
     m.put('key1', 10)
     print(m.get('key1'))
     m.remove('key1')
     print(m.get('key1'))
     m.remove('key4')
Esempio n. 14
0
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)])
    """

    keys = set()

    ht = HashMap(2500, hash_function_2)

    # This block of code will read a file one word as a time and  |
    # put the word in `w`. It should be left as starter code.     |
    with open(source) as f:  # |
        for line in f:  # |
            words = rgx.findall(line)  # |
            for w in words:  # |
                # lower case all incoming words to make case insensitive
                w = w.lower()
                # Check if hashtable already contains word
                if ht.contains_key(w):
                    # If so retrieve the count for the given word
                    count = ht.get(w)
                    # Update existing key with incremented count value
                    ht.put(w, count + 1)
                else:
                    # Add new word to keys set collection
                    keys.add(w)
                    # put new word in hash table with a count of 1
                    ht.put(w, 1)
                # Check if table load is over load limit before next word
                if ht.table_load() > 8:
                    # if so, resize hash table to twice the capacity
                    ht.resize_table(2 * ht.capacity)
    # initialize tuple word/count list
    topWords = []
    # for each key in the set of keys
    for key in keys:
        # append the key and value as a tuple in the topWords list
        topWords.append((key, ht.get(key)))
    # once all tuples are added to list, sort list by the count of each key in descending order
    topWords.sort(key=lambda keyCountTup: keyCountTup[1], reverse=True)
    # After sort, set top word list to only contain the given number of tuples requested
    topWords = topWords[:number]
    # return topWords list of tuples of length equal to number
    return topWords
Esempio n. 15
0
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)])
    """

    keys = set()

    ht = HashMap(2500, hash_function_2)

    # This block of code will read a file one word at a time and
    # put the word in `w`. It should be left as starter code.

    with open(source) as f:
        for line in f:
            words = rgx.findall(line)
            for w in words:
                # Convert all words to lowercase prior to insertion
                w = w.lower()
                # If the word is already in the hash map, pass the value with a new updated count
                if ht.contains_key(w):
                    count = ht.get(w) + 1
                    ht.put(w, count)
                else:
                    # Otherwise, create a new entry in the hashmap
                    ht.put(w, 1)

    # Add all of the words to the keys set
    for bucket in ht.get_buckets():
        # Iterate through each bucket/linked list
        curr = bucket.head
        while curr is not None:
            # Add the keys as a tuple
            keys.add((curr.key, curr.value))
            curr = curr.next

    # Cast the set as a list
    all_words = list(keys)
    # Sort the words according to their value in the tuple
    all_words.sort(key=lambda word: word[1])
    slice_val = (number * -1 - 1)
    top_wds = all_words[:slice_val:-1]

    return top_wds
Esempio n. 16
0
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)])
    """

    keys = set()

    ht = HashMap(2500, hash_function_2)

    # This block of code will read a file one word as a time and
    # put the word in `w`. It should be left as starter code.
    with open(source) as f:
        for line in f:
            words = rgx.findall(line)
            for w in words:
                #append the individual words to the list and convert letters
                #to lowercase for case sensitivity
                lower_case = w.lower()
                keys.add(lower_case)
                #check if word is alread in hashmap
                if ht.contains_key(lower_case):
                    #increase word count and insert into hasmap and update count
                    val = (ht.get(lower_case) + 1)
                    ht.put(lower_case, val)
                else:
                    #insert into hasmap with initial count being one 1 if not in hashmap already
                    ht.put(lower_case, 1)
    #create a new list if words
    word_list = []
    #loop thru the list
    for k in keys:
        index = ht._hash_function(k) % ht.capacity
        temp = ht._buckets[index]
        #add tuples to list containing word and count
        linked_node = temp.contains(k)
        word_list.append((linked_node.key, linked_node.value))
    #sort list in descending order
    word_list.sort(key=lambda tup: tup[1], reverse=True)
    #return list of top words
    return word_list[0:number]


# print(top_words("alice.txt",10))  # COMMENT THIS OUT WHEN SUBMITTING TO GRADESCOPE
    def test_table_load(self):
        """Tests HashMap table_load method"""
        test_values = [("test_5", 5), ("test_-5", -5), ("test_5_", 5),
                       ("diff_word", 15), ("another_word", 20)]
        init_capacity = 10
        student_map = HashMap(init_capacity, hash_function_1)
        student_map_d = HashMap(init_capacity * 2, hash_function_1)
        # 0 / 10 = 0, 0 / 20 = 0
        self.assertEqual(student_map.table_load(), student_map_d.table_load())

        for key, val in test_values:
            student_map.put(key, val)
            student_map_d.put(key, val)

        # test known load ( 5 / 10 = 0.5 ),  ( 5 / 20 = 0.25 )
        self.assertEqual((len(test_values) / init_capacity),
                         student_map.table_load())
        self.assertEqual((len(test_values) / (init_capacity * 2)),
                         student_map_d.table_load())

        student_map = HashMap(init_capacity, hash_function_1)
        student_map_d = HashMap(init_capacity * 2, hash_function_1)

        # test high table load
        random_cases = 1000
        # add random key, value pairs to the table
        for i in range(random_cases):
            key, value = create_random_tuple()
            student_map.put(key, value)
            student_map_d.put(key, value)

        self.assertAlmostEqual(student_map.table_load(),
                               student_map_d.table_load() * 2)
Esempio n. 18
0
 def test_clear_1(self):
     """
     Test clear() with Example #1 from the guidelines.
     :passed: yes
     """
     print("--- EXAMPLE 1 ---")
     m = HashMap(100, hash_function_1)
     print(m.size, m.capacity)
     m.put('key1', 10)
     m.put('key2', 20)
     m.put('key1', 30)
     print(m.size, m.capacity)
     m.clear()
     print(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)])
    """

    keys = set()

    ht = HashMap(2500, hash_function_2)

    # This block of code will read a file one word as a time and
    # put the word in `w`. It should be left as starter code.
    with open(source) as f:
        for line in f:
            words = rgx.findall(line)
            for w in words:  # Iterate through all the words in the line.
                # Place lowercased version of words.
                if ht.contains_key(
                        w.lower()):  # If the word is in the hashmap.
                    ht.put(w.lower(),
                           ht.get(w.lower()) +
                           1)  # Update the word count by 1.
                else:  # If the word does not exist in the hashmap.
                    ht.put(w.lower(),
                           1)  # Place the key in the map with the value of 1.
                    keys.add(w.lower())  # Add the new keys into the keys set.

    list_of_occurences = [
    ]  # Create an empty list to hold the tuples of keys and values.
    for key in keys:  # Iterate through all the keys.
        list_of_occurences.append(
            (key, ht.get(key)))  # Add the key and value tuple into the list.

    # Source to help me find a way to implement this:
    # stackoverflow.com/questions/10695139/sort-a-list-of-tuples-by-2nd-item-integer-value

    # We use lambda here to sort the list of tuples by its second value.
    # The sorting is also reversed to make it in descending order.
    sorted_list = sorted(list_of_occurences, key=lambda x: x[1], reverse=True)
    return sorted_list[:
                       number]  # Using list slice, return the top numbers of the list depending on what the user inputs


# print(top_words("alice.txt",10))  # COMMENT THIS OUT WHEN SUBMITTING TO GRADESCOPE
Esempio n. 20
0
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)])
    """

    keys = set()

    ht = HashMap(2500, hash_function_2)

    # This block of code will read a file one word as a time and
    # put the word in `w`. It should be left as starter code.
    with open(source) as f:
        for line in f:
            words = rgx.findall(line)
            for w in words:
                if (ht.get(w) is None):
                    ht.put(w, 1)
                else:
                    #Buffer into hash map
                    ht.put(w, ht.get(w) + 1)

    #Handle sorting
    tuples = []

    for i in range(ht.capacity):
        cur = ht._buckets[i]
        if cur.head is not None:
            cur = cur.head
            while (cur != None):
                #Buffer key pair into tuples
                tuples.append((cur.key, cur.value))

                #Go to next
                cur = cur.next

    tuples = sorted(tuples, key=lambda x: x[1], reverse=True)
    newTuples = []

    for j in range(number):
        newTuples.append(tuples[j])
    return newTuples
Esempio n. 21
0
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)])
    """

    keys = set()

    ht = HashMap(2500,hash_function_2)

    # This block of code will read a file one word as a time and
    # put the word in `w`. It should be left as starter code.
    with open(source) as f:
        for line in f:
            words = rgx.findall(line)
            for w in words:
                w = w.lower() # make everything lower case

                # if the word is already in keys, get and increase the count.
                if w in keys:
                    count = ht.get(w)
                    count += 1
                    ht.put(w, count) # update the count

                # otherwise add the new word to the hashmap with count of 1
                else:
                    ht.put(w, 1)
                    keys.add(w)

    count_list = [] # list of tuples of keys and counts

    # for each key, get the value, and add these pairs as tuples to count_list
    for k in keys:
        val = ht.get(k)
        pair = (k, val)
        count_list.append(pair)

    # sort count_list with a lambda function using sorted(). Reverse the list for descending order
    count_list = sorted(count_list, key=lambda x: x[1], reverse=True) # where x is a tuple and x[1] is the word count

    # return the appropriate number of top words
    return count_list[:number]
Esempio n. 22
0
 def test_clear_2(self):
     """
     Test clear() with Example #2 from the guidelines.
     :passed: yes
     """
     print("--- EXAMPLE 2 ---")
     m = HashMap(50, hash_function_1)
     print(m.size, m.capacity)
     m.put('key1', 10)
     print(m.size, m.capacity)
     m.put('key2', 20)
     print(m.size, m.capacity)
     m.resize_table(100)
     print(m.size, m.capacity)
     m.clear()
     print(m.size, m.capacity)
Esempio n. 23
0
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)])
    """

    keys = set()

    ht = HashMap(2500, hash_function_2)

    # This block of code will read a file one word as a time and
    # put the word in `w`. It should be left as starter code.
    with open(source) as f:
        for line in f:
            words = rgx.findall(line)
            for w in words:

                # Place word in hash map or update value by one
                w = w.lower()
                if not ht.contains_key(w):
                    keys.add(w)
                    ht.put(w, 1)
                else:
                    ht.put(w, ht.get(w) + 1)

    # Sort the words by mentions
    sorted_words = []
    for word in keys:
        next_word = (word, ht.get(word))
        if sorted_words == []:
            sorted_words.append(next_word)
        else:
            for index, value in enumerate(sorted_words):
                if next_word[1] >= value[1]:
                    sorted_words.insert(index, next_word)
                    break

    return sorted_words[:number]


# print(top_words("alice.txt",10))  # COMMENT THIS OUT WHEN SUBMITTING TO GRADESCOPE
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)])
    """

    keys = set()

    ht = HashMap(2500, hash_function_2)

    # This block of code will read a file one word as a time and
    # put the word in `w`. It should be left as starter code.
    # build hash table
    with open(source) as f:
        for line in f:
            words = rgx.findall(line)
            for w in words:
                # convert to lowercase so words are counted properly
                lowercase_w = w.lower()
                keys.add(lowercase_w)
                word_count = ht.get(lowercase_w)
                # have value of node track number of times word has appeared
                if word_count is None:
                    ht.put(lowercase_w, 1)
                else:
                    ht.put(lowercase_w, word_count + 1)
    # for the amount of top words requested, find the word with maximum count
    max_list = []
    for count in range(number):
        max_w = ""
        max_value = 0
        # iterate over all words to find max key, value
        for w in keys:
            value = ht.get(w)
            if value > max_value:
                max_key = w
                max_value = value
        max_list.append((max_key, max_value))
        # remove max word from set for next iteration to get next top word
        keys.remove(max_key)
    return max_list
    def test_contains_key(self):
        """Tests the HashMap contains_key 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)]

        student_map = HashMap(10, hash_function_1)

        # simple check to test that all values are in the list
        for key, val in test_values:
            student_map.put(key, val)
            found = False
            for bucket in student_map._buckets:
                if bucket.contains(key):
                    found = True
            self.assertEqual(found, student_map.contains_key(key))
Esempio n. 26
0
 def test_contains_key_5(self):
     """
     Test contains_key with Example #1 from the guidelines.
     :passed: yes
     """
     print("--- EXAMPLE 1 ---")
     m = HashMap(50, hash_function_1)
     print(m.contains_key('key1'))
     m.put('key1', 10)
     m.put('key2', 20)
     m.put('key3', 30)
     print(m.contains_key('key1'))
     print(m.contains_key('key4'))
     print(m.contains_key('key2'))
     print(m.contains_key('key3'))
     m.remove('key3')
     print(m.contains_key('key3'))
Esempio n. 27
0
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)])
    """

    keys = set()

    ht = HashMap(2500, hash_function_2)

    # This block of code will read a file one word as a time and
    # put the word in `w`. It should be left as starter code.
    with open(source) as f:
        for line in f:
            words = rgx.findall(line)
            for w in words:
                w = w.lower()
                count = ht.get(w)
                if count is None:
                    ht.put(w, 1)  # Word is not in the hash map; add it
                else:
                    ht.put(w, count +
                           1)  # Word is in the hash map; increment the count

    heap = Heap()  # Create a heap to do the sorting

    for i in range(ht.capacity):
        node = ht._buckets[i].head
        while node is not None:
            t = (node.key, node.value)
            heap.insert(t)  # Add each tuple to the heap
            node = node.next

    heap.sort()  # Sort by word count, descending

    if number <= len(heap.heap):
        return heap.heap[0:number]
    else:
        return heap.heap  # Handles the case where the user requests too many words
Esempio n. 28
0
 def test_contains_key_6(self):
     """
     Test contains_key with Example #2 from the guidelines.
     :passed: yes
     """
     print("--- EXAMPLE 2 ---")
     m = HashMap(75, hash_function_2)
     keys = [i for i in range(1, 1000, 20)]
     for key in keys:
         m.put(str(key), key * 42)
     print(m.size, m.capacity)
     result = True
     for key in keys:
         # all inserted keys must be present
         result = result and m.contains_key(str(key))
         # all NOT inserted keys must be absent
         result = result and not m.contains_key(str(key + 1))
     print(result)
Esempio n. 29
0
    def test_hash_map(self):
        test_map = HashMap()
        for i in range(0,1000):
            if i%2 is 0:
                test_map.put((i,), i)
            else: # i%2 is 1
                test_map.put(str(i), [i])

        # initialize the map with 1001 kv pairs
        self.assertEquals(test_map.size(), 1000)
        for i in range(0,1000):
            if i%2 is 0:
                self.assertEquals(test_map.get((i,)), i)
            else: # i%2 is 1
                self.assertEquals(test_map.get(str(i)), [i])

        with self.assertRaises(KeyNotFound):
            test_map.get("8")

        with self.assertRaises(KeyNotFound):
            test_map.get((991,))

        with self.assertRaises(KeyNotFound):
            test_map.get("test_key")

        with self.assertRaises(KeyNotFound):
            test_map.get((1002,))

        # remove 700 elements
        for i in range(100,800):
            if i%2 is 0:
                test_map.remove((i,))
            else: # i%2 is 1
                test_map.remove(str(i))

        self.assertEquals(test_map.size(), 300)

        self.assertTrue(test_map.contains((80,)))
        self.assertFalse(test_map.contains((120,)))

        # insert 9000 more values
        for i in range(1000,10000):
            if i%2 is 1:
                test_map.put((i,), i)
            else: # i%2 is 0
                test_map.put(str(i), [i])

        self.assertEquals(test_map.size(), 9300)

        for i in range(1000,10000):
            if i%2 is 1:
                self.assertEquals(test_map.get((i,)), i)
            else: # i%2 is 0
                self.assertEquals(test_map.get(str(i)), [i])
Esempio n. 30
0
def top_words(source, number):
    """
    Take a plain text file and count the number of occurrences of case insensitive words.
    Return the top `number` of words in a list of tuples of the form (word, count).

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

    ht = HashMap(2500, hash_function_2)

    # Read the file one word at a time and put the word in `w`
    with open(source) as f:
        for line in f:
            words = rgx.findall(line)
            for w in words:
                # Convert the word to lowercase to enforce case insensitivity
                word_lower = w.lower()

                # If the word already exists in the table, get and update its
                # current count
                if ht.contains_key(word_lower):
                    cur_count = ht.get(word_lower)  # Get current count
                    ht.put(word_lower, cur_count + 1)  # Update current count

                # If the word does not exist in the table, add it and set its
                # count to 1
                else:
                    ht.put(word_lower, 1)

    # Get a list of tuples consisting of all the key-value pairs in the table
    tuple_list = ht.get_tuples()

    # Sort the list of tuples in descending order by word count
    tuple_list.sort(key=get_count, reverse=True)
    # print("sorted tuple_list:", tuple_list)

    # Slice the list of tuples to contain `number` amount of tuples
    sliced_list = tuple_list[0:number]

    # Return the sliced list of tuples
    return sliced_list
Esempio n. 31
0
    def test_hash_map(self):
        test_map = HashMap()
        for i in range(0, 1000):
            if i % 2 is 0:
                test_map.put((i, ), i)
            else:  # i%2 is 1
                test_map.put(str(i), [i])

        # initialize the map with 1001 kv pairs
        self.assertEquals(test_map.size(), 1000)
        for i in range(0, 1000):
            if i % 2 is 0:
                self.assertEquals(test_map.get((i, )), i)
            else:  # i%2 is 1
                self.assertEquals(test_map.get(str(i)), [i])

        with self.assertRaises(KeyNotFound):
            test_map.get("8")

        with self.assertRaises(KeyNotFound):
            test_map.get((991, ))

        with self.assertRaises(KeyNotFound):
            test_map.get("test_key")

        with self.assertRaises(KeyNotFound):
            test_map.get((1002, ))

        # remove 700 elements
        for i in range(100, 800):
            if i % 2 is 0:
                test_map.remove((i, ))
            else:  # i%2 is 1
                test_map.remove(str(i))

        self.assertEquals(test_map.size(), 300)

        self.assertTrue(test_map.contains((80, )))
        self.assertFalse(test_map.contains((120, )))

        # insert 9000 more values
        for i in range(1000, 10000):
            if i % 2 is 1:
                test_map.put((i, ), i)
            else:  # i%2 is 0
                test_map.put(str(i), [i])

        self.assertEquals(test_map.size(), 9300)

        for i in range(1000, 10000):
            if i % 2 is 1:
                self.assertEquals(test_map.get((i, )), i)
            else:  # i%2 is 0
                self.assertEquals(test_map.get(str(i)), [i])