Exemplo n.º 1
0
def test_key_retrieve_value_collision():
    hashtable = Hashtable()
    hashtable.add('listen', 'test_value')
    hashtable.add('silent', 'other_value')
    actual = hashtable.get('silent')
    expected = 'other_value'
    assert actual == expected
Exemplo n.º 2
0
def test_key_not_found():
    size = 24
    table = Hashtable(size)
    table.add("key1", "value1")
    actual = table.get("key2")
    expected = None
    assert actual == expected
def test_add_works_correctly():
    hashtable = Hashtable()
    index = hashtable.hash("spam")
    assert hashtable.buckets[index] is None
    hashtable.add("spam", "eggs")
    bucket = hashtable.buckets[index]
    assert bucket
Exemplo n.º 4
0
def test_add_and_get():
    size = 24
    table = Hashtable(size)
    table.add("key1", "value1")
    actual = table.get("key1")
    expected = "value1"
    assert actual == expected
Exemplo n.º 5
0
def test_5():
    hashtable = Hashtable()
    hashtable.add('python', 'az')  # hash key = 941
    hashtable.add('Java', 'za')  # hash key = 941
    actual = hashtable.get('az')
    expected = 'python'
    assert actual == expected
    actual = hashtable.get('za')
    expected = 'Java'
    assert actual == expected
Exemplo n.º 6
0
def test_collision_add_and_get():
    size = 24
    table = Hashtable(size)
    table.add("key1", "value1")
    table.add("yek1", "value2")
    actual1 = table.get("key1")
    actual2 = table.get("yek1")
    expected1 = "value1"
    expected2 = "value2"
    assert actual1 == expected1
    assert actual2 == expected2
def test_add_method_will_add_to_ll_and_handle_collisions():
    hashtable = Hashtable()
    hashtable.add('xyz', 10)
    hashtable.add('zxy', 20)
    index = hashtable.hash("xyz")
    count = 0
    current = hashtable.buckets[index].head

    while current:
        count += 1
        current = current.next
    assert count == 2
def find_common_vals(tree_1, tree_2):
    """ Accepts two binary tree objects, returns a list of values present
    in both trees. Uses a hashtable class. """
    ht = Hashtable()
    rtn = []
    val_list_1 = tree_1.inorder(tree_1.root)
    val_list_2 = tree_2.inorder(tree_2.root)
    for val in val_list_1:
        ht.add(val, str(val))
    for val in val_list_2:
        if ht.contains(val):
            rtn.append(val)
    return rtn
def repeat_word(long_string):
    start = 0
    word = ''
    words_ht = Hashtable()
    long_string = long_string.lower()
    words = long_string.split(' ')
    for word in words:
        if words_ht.contains(word):
            return word
        else:
            word_hash = words_ht.hash(word)
            words_ht.add(word, word_hash)
    return 'No matching Words'
Exemplo n.º 10
0
def hash_intersection(tree1, tree2):
    placeholder = []
    tree1 = tree1.pre_order()
    tree2 = tree2.pre_order()
    hashtable = Hashtable()

    for value in tree1:
        hashtable.add(key=str(value), value=value)

    for value in tree2:
        if hashtable.contains(str(value)):
            placeholder.append(value)

    return placeholder
def test_only_one_first_ht():
    ht1 = Hashtable()
    ht1.add("fond", "enamored")
    ht2 = Hashtable()
    ht2.add("fond", "averse")
    ht2.add("wrath", "delight")
    ht2.add("diligent", "idle")
    ht2.add("guide", "follow")
    ht2.add("flow", "jam")
    actual = left_join(ht1, ht2)
    # changed order from example. order is dependent on hash function. intent met.
    expected = [
        ["fond", "enamored", "averse"],
    ]
    assert actual == expected
def first_repeated_word(string):

    if len(string) == 0:
        return None

    hash_map = Hashtable()
    lowered = string.lower()
    words_array = re.findall(r"\w+", lowered)

    for word in words_array:
        if hash_map.contains(word):
            return word
        else:
            hash_map.add(word, word)

    return None
def test_empty_left_table():
    ht_left = Hashtable()

    ht_right = Hashtable()
    ht_right.add('fond', 'averse')
    ht_right.add('wrath', 'delight')
    ht_right.add('flow', 'jam')
    ht_right.add('guide', 'follow')

    joined_vals = left_join_hashmaps(ht_left, ht_right)
    assert len(joined_vals) == 0
def test_one_overlap():
    table1 = Hashtable()
    table1.add("fond", "enamored")
    table1.add("wrath", "anger")
    table1.add("diligent", "employed")
    table1.add("outfit", "garb")
    table1.add("guide", "usher")

    table2 = Hashtable()
    table2.add("fond", "averse")

    actual = left_join(table1, table2)
    assert actual == [
        ["fond", "enamored", "averse"],
        ["guide", "usher", None],
        ["outfit", "garb", None],
        ["diligent", "employed", None],
        ["wrath", "anger", None],
    ]
Exemplo n.º 15
0
def test_get_three():
    test = Hashtable()
    test.add('chris', 'ball')
    test.add('tim', 'schoen')
    test.add('tony', 'tiger')
    test.add('uncle', 'joe')
    actual = test.get('foo')
    assert actual == None
Exemplo n.º 16
0
def test_contains_three():
    test = Hashtable()
    test.add('chris', 'ball')
    test.add('tim', 'schoen')
    test.add('tony', 'tiger')
    test.add('uncle', 'joe')
    actual = test.contains('foo')
    assert actual == False
def test_empty_second_ht():
    ht1 = Hashtable()
    ht1.add("fond", "enamored")
    ht1.add("wrath", "anger")
    ht1.add("diligent", "employed")
    ht1.add("outfit", "garb")
    ht1.add("guide", "usher")
    ht2 = Hashtable()
    actual = left_join(ht1, ht2)
    # changed order from example. order is dependent on hash function. intent met.
    expected = [
        ["fond", "enamored", None],
        ["diligent", "employed", None],
        ["guide", "usher", None],
        ["outfit", "garb", None],
        ["wrath", "anger", None],
    ]
    assert actual == expected
Exemplo n.º 18
0
def test_three():
    ht1 = Hashtable()
    ht1.add('chris', 'ball')
    ht1.add('tim', 'schoen')
    ht1.add('tony', 'tiger')
    ht1.add('uncle', 'joe')

    ht2 = Hashtable()

    actual = left_join(ht1, ht2)
    assert actual == [['tim', 'schoen', None], ['tony', 'tiger', None],
                      ['uncle', 'joe', None], ['chris', 'ball', None]]
def test_empty_right_table():
    ht_left = Hashtable()
    ht_left.add('fond', 'enamoured')
    ht_left.add('wrath', 'anger')
    ht_left.add('outfit', 'garb')
    ht_left.add('guide', 'usher')

    ht_right = Hashtable()

    joined_vals = left_join_hashmaps(ht_left, ht_right)
    assert len(joined_vals) == 4
    assert ['fond', 'enamoured', None] in joined_vals
    assert ['wrath', 'anger', None] in joined_vals
    assert ['outfit', 'garb', None] in joined_vals
    assert ['guide', 'usher', None] in joined_vals
def test_contains_returns_true_if_key_is_present():
    hashtable = Hashtable()
    hashtable.add("xyz", 10)
    assert hashtable.contains("xyz")
def test_right_join():
    ht1 = Hashtable()
    ht1.add("fond", "enamored")
    ht1.add("wrath", "anger")
    ht1.add("diligent", "employed")
    ht1.add("outfit", "garb")
    ht1.add("guide", "usher")
    ht2 = Hashtable()
    ht2.add("fond", "averse")
    ht2.add("wrath", "delight")
    ht2.add("diligent", "idle")
    ht2.add("guide", "follow")
    ht2.add("flow", "jam")
    actual = right_join(ht2, ht1)
    # changed order from example. order is dependent on hash function. intent met.
    expected = [
        ["flow", "jam", None],
        ["fond", "averse", "enamored"],
        ["diligent", "idle", "employed"],
        ["guide", "follow", "usher"],
        ["wrath", "delight", "anger"],
    ]
    assert actual == expected
Exemplo n.º 22
0
def test_hashtable_aio():
    ht = Hashtable(1024)
    ht.add("spoons","dangerously low")
    assert ht.contains("spoons") == True
    assert ht.get("spoons") == "dangerously low"
    
Exemplo n.º 23
0
def test_one():
    ht1 = Hashtable()
    ht1.add('chris', 'ball')
    ht1.add('tim', 'schoen')
    ht1.add('tony', 'tiger')
    ht1.add('uncle', 'joe')

    ht2 = Hashtable()
    ht2.add('chris', 'jones')
    ht2.add('jack', 'danials')
    ht2.add('tony', 'see')
    ht2.add('uncle', 'nickols')
    actual = left_join(ht1, ht2)
    assert actual == [['tim', 'schoen', None], ['tony', 'tiger', 'see'],
                      ['uncle', 'joe', 'nickols'], ['chris', 'ball', 'jones']]
Exemplo n.º 24
0
def test_4():
    hashtable = Hashtable()
    hashtable.add('python', 'az')  # hash key = 941
    actual = hashtable.add('Java', 'za')  # hash key = 941
    expected = True
    assert actual == expected
def test_contains_returns_false_if_key_is_not_present():
    hashtable = Hashtable()
    hashtable.add("xyz", 10)
    assert hashtable.contains("abc") == False
def test_get_method_returns_value_if_key_is_present():
    hashtable = Hashtable()
    hashtable.add("xyz", 10)
    assert hashtable.get("xyz") == 10
def test_get_method_returns_None_for_value_not_present():
    hashtable = Hashtable()
    hashtable.add("xyz", 10)
    assert hashtable.get("abc") is None
Exemplo n.º 28
0
def test_1():
    hashtable = Hashtable()
    actual = hashtable.add('python', 'hkutr')
    expected = True
    assert actual == expected
def test_get_method_will_account_for_ll_with_multiple_values():
    hashtable = Hashtable()
    hashtable.add("abcd", 10)
    hashtable.add("dcba", 20)
    hashtable.add("cbad", 30)
    assert hashtable.get("dcba") == 20
Exemplo n.º 30
0
def test_key_retrieve_value():
    hashtable = Hashtable()
    hashtable.add('alpha', 'test_value')
    actual = hashtable.get('alpha')
    expected = 'test_value'
    assert actual == expected