def ht_right():
    ht = Hashtable()
    ht.add("fond", "averse")
    ht.add("wrath", "delight")
    ht.add("diligent", "idle")
    ht.add("guide", "follow")
    ht.add("flow", "jam")
    return ht
def ht_left():
    ht = Hashtable()
    ht.add("fond", "enamored")
    ht.add("wrath", "anger")
    ht.add("diligent", "employed")
    ht.add("outfit", "garb")
    ht.add("guide", "usher")
    return ht
예제 #3
0
def repeated_word(string):
    ht = Hashtable()
    valid = "abcdefghijklmnopqrstuvwxyz "
    words = "".join(ch for ch in string.lower() if ch in valid).split()

    for word in words:
        if ht.contains(word):
            return word
        ht.add(word, word)

    return ""
예제 #4
0
def test_contains():

    Ĥ = Hashtable()

    Ĥ.add("hash", "table")

    assert Ĥ.contains("hash") == True
예제 #5
0
def test_add_and_get():

    Ĥ = Hashtable()

    Ĥ.add("hash", "table")

    assert Ĥ.get("hash") == "table"
def test_left_join():
    h1 = Hashtable()
    h1.add("2", 2)
    h1.add("3", 3)

    h2 = Hashtable()
    h2.add("2", 2)
    h2.add("4", 4)

    assert left_join(h1, h2) == [['3', 3, None], ['2', 2, 2]]
def test_hashtable_collision():
    hashtable = Hashtable()
    hashtable.add("key", "value1")
    hashtable.add("yek", "value2")
    assert hashtable._hash("key") == hashtable._hash("yek")
    assert hashtable.get("key") == "value1"
    assert hashtable.get("yek") == "value2"
예제 #8
0
def test_contains_many_first():

    Ĥ = Hashtable()

    Ĥ.add("hash", "table")
    Ĥ.add("table", "hash")
    Ĥ.add("linked", "list")

    assert Ĥ.contains("linked") == True
def test_left_join_only_left(ht_left):
    actual = left_join(ht_left, Hashtable())
    expected = [
        ["outfit", "garb", None],
        ["guide", "usher", None],
        ["wrath", "anger", None],
        ["diligent", "employed", None],
        ["fond", "enamored", None],
    ]
    assert actual == expected
예제 #10
0
def repeated_word(strs): 

  strs = strs.lower()

  for ch in strs: 
    if ch not in ABC_dict: 
      strs.replace(ch, '')

  str_lst = strs.split(" ")

  ht = Hashtable()

  for string in str_lst: 
    if ht.contains(string): 
      return string
    else: 
      ht.add(string, string)  

  return None    
def test_hashtable_add_and_get():
    hashtable = Hashtable()
    hashtable.add("key", "value")
    assert hashtable.get("key") == "value"
def test_hashtable_get_none_key():
    hashtable = Hashtable()
    assert hashtable.get("key") == None
def test_hashtable_create():
    hashtable = Hashtable()
    assert hashtable
def test_hashtable_different_hash():
    hashtable = Hashtable()
    initial = hashtable._hash("glisten")
    secondary = hashtable._hash("silent")
    assert initial != secondary
def test_hashtable_same_hash():
    hashtable = Hashtable()
    initial = hashtable._hash("listen")
    secondary = hashtable._hash("silent")
    assert initial == secondary
def test_hashtable_in_range_hash():
    hashtable = Hashtable()
    actual = hashtable._hash("spam")
    assert 0 <= actual < hashtable.size
def tree_intersection(bn1, bn2):
    ht = Hashtable()
    for n in bn1.pre_order():
        ht.add(n, n)
    return [n for n in bn2.pre_order() if ht.contains(n)]
예제 #18
0
def test_hash_different():

    Ĥ = Hashtable()

    assert not Ĥ.hash("hash") == Ĥ.hash("table")
def test_hashtable_contains():
    hashtable = Hashtable()
    hashtable.add("key", "value")
    assert hashtable.contains("key") == True  
예제 #20
0
def test_hashtable():

    assert Hashtable()
def test_left_join_only_right(ht_right):
    actual = left_join(Hashtable(), ht_right)
    expected = []
    assert actual == expected
예제 #22
0
def test_get_none():

    Ĥ = Hashtable()

    assert Ĥ.get("hash") == None
def test_left_join_both_empty():
    actual = left_join(Hashtable(), Hashtable())
    expected = []
    assert actual == expected
def test_hashtable_predictable_hash():
    hashtable = Hashtable()
    initial = hashtable._hash("spam")
    secondary = hashtable._hash("spam")
    assert initial == secondary
예제 #25
0
def test_hash():

    Ĥ = Hashtable()

    assert Ĥ.hash("hash") == Ĥ.hash("hash")