def test_retrieve_value_in_collision_bucket():
    hashtable = Hashtable()
    initial = hashtable.add('listen', 'One')
    secondary = hashtable.add('silent', 'Two')
    actual = hashtable.get('silent')
    expected = 'Two'
    assert actual == expected
def comb(string):
    words = string.replace(",", "").replace(".", "").split(" ")
    ht = Hashtable()
    for word in words:
        word = word.lower()
        if ht.contains(word):
            return word
        else:
            ht.add(key=word, value=word)
Example #3
0
def tree_intersection(tree1, tree2) -> list:
    common_vals = []
    if tree1.root and tree2.root:
        tree1_list, tree2_list = tree1.pre_order(), tree2.pre_order()
        hashy = Hashtable()
        for val in tree1_list: hashy.add(str(val), "number")
        for val in tree2_list: 
            if hashy.contains(str(val)): common_vals.append(int(val))
    return common_vals
Example #4
0
def word_repeat(string):
    words = string.replace(',', '').replace('.', '').split(" ")
    ht = Hashtable()

    for word in words:
        word = word.lower()
        if ht.contains(word):
            return word
        else:
            ht.add(word, word)
def repeat_word(text: str) -> str:
    valid_chars = "abcdefghijklmnopqrstuvwxyz "
    words = "".join([x for x in text.lower() if x in valid_chars])
    hashy = Hashtable()
    for word in words.split():
        if hashy.contains(word):
            return word
        hashy.add(word, "word")

    return "no duplicate words found"
def tree_intersection(bt1, bt2):
    new_arr = []
    tree1 = bt1.pre_order()
    tree2 = bt2.pre_order()
    ht = Hashtable()
    for value in tree1:
        ht.add(key=str(value), value=value)
    for value in tree2:
        if ht.contains(str(value)):
            new_arr.append(value)
    return new_arr
Example #7
0
def test_left_join_one_empty():
    ht1 = Hashtable()
    ht2 = Hashtable()
    ht1.add('fond', 'enamored')
    ht1.add('wrath', 'anger')
    actual = hashmap_left_join(ht1, ht2)
    expected = [
        ['fond', 'enamored', None],
        ['wrath', 'anger', None],
    ]
    for entry in expected:
        assert entry in actual
def test_hash_two():
    hashy = Hashtable()
    hashy.add('fond', 'averse')
    hashy.add('wrath', 'delight')
    hashy.add('diligent', 'idle')
    hashy.add('guide', 'follow')
    hashy.add('flow', 'jam')
    return hashy
def test_hash_one():
    hashy = Hashtable()
    hashy.add('fond', 'enamored')
    hashy.add('wrath', 'anger')
    hashy.add('diligent', 'employed')
    hashy.add('outfit', 'garb')
    hashy.add('guide', 'usher')
    return hashy
def test_unknown_key():
    ht = Hashtable()
    ht.add("banana", 9)
    actual = ht.get("cucumber")
    expected = None
    assert actual == expected
def test_get_banana():
    ht = Hashtable()
    ht.add("banana", 9)
    actual = ht.get("banana")
    expected = 9
    assert actual == expected
def test_key_that_does_not_exist():
    hashtable = Hashtable()
    hashtable.add('Sunday', 'One')
    actual = hashtable.contains('Monday')
    expected = False
    assert actual == expected
def test_hashtable_collision():
    hashtable = Hashtable()
    initial = hashtable.add('listen', 'One')
    secondary = hashtable.add('silent', 'Two')
    assert hashtable._hash('listen') == hashtable._hash('silent')
def test_adding_keyval_pair():
    hashtable = Hashtable()
    hashtable.add('Sunday', 'One')
    actual = hashtable.contains('Sunday')
    expected = True
    assert actual == expected
def test_retrieving_value_from_key():
    hashtable = Hashtable()
    hashtable.add('Sunday', 'One')
    actual = hashtable.get('Sunday')
    expected = 'One'
    assert actual == expected
def test_add():
    ht = Hashtable()
    actual = ht.add("apple", 42)
    expected = None
    assert actual == expected
Example #17
0
def test_left_join():
    ht1 = Hashtable()
    ht2 = Hashtable()
    ht1.add('fond', 'enamored')
    ht1.add('wrath', 'anger')
    ht1.add('diligent', 'employed')
    ht2.add('fond', 'averse')
    ht2.add('wrath', 'delight')
    ht2.add('happy', 'follow')
    actual = hashmap_left_join(ht1, ht2)
    expected = [
        ['fond', 'enamored', 'averse'],
        ['wrath', 'anger', 'delight'],
        ['diligent', 'employed', None]
    ]
    for entry in expected:
        assert entry in actual
Example #18
0
def test_get_value():
    ht = Hashtable()
    ht.add('rum ham', 6)
    actual = ht.get('rum ham')
    expected = 6
    assert actual == expected
Example #19
0
def test_contains():
    ht = Hashtable()
    ht.add('rum ham', 6)
    actual = ht.contains('toe knife')
    expected = False
    assert actual == expected
def test_left():
    ht1 = Hashtable()
    ht2 = Hashtable()
    ht1.add("fond", "enamored")
    ht1.add("wrath", "anger")
    ht1.add("diligent", "employed")
    ht2.add("fond", "averse")
    ht2.add("wrath", "delight")
    ht2.add("diligent", "follow")
    actual = left_join(ht1, ht2)
    expected = [
        ["wrath", "anger", "delight"],
        ["diligent", "employed", "follow"],
        ["fond", "enamored", "averse"],
    ]
    for entry in expected:
        assert entry in actual
def test_left1():
    ht1 = Hashtable()
    ht2 = Hashtable()
    ht1.add("1", "enamored")
    ht1.add("2", "anger")
    ht1.add("3", "employed")
    ht2.add("1", "averse")
    ht2.add("2", "delight")
    ht2.add("3", "follow")
    actual = left_join(ht1, ht2)
    expected = [
        ["1", "enamored", "averse"],
        ["2", "anger", "delight"],
        ["3", "employed", "follow"],
    ]
    assert actual == expected
Example #22
0
def test_add():
    ht = Hashtable()
    actual = ht.add('rum ham', 11)
    expected = None
    assert actual == expected