示例#1
0
def test_add_and_get():
    size = 24
    table = Hashtable(size)
    table.add("key1", "value1")
    actual = table.get("key1")
    expected = "value1"
    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
示例#3
0
def test_key_not_found():
    size = 24
    table = Hashtable(size)
    table.add("key1", "value1")
    actual = table.get("key2")
    expected = None
    assert actual == expected
示例#4
0
def test_in_range_hash():
    hashtable = Hashtable()
    actual = hashtable._hash('spam')

    # assert actual >= 0
    # assert actual < hashtable._size

    assert 0 <= actual < hashtable._size
示例#5
0
def test_6():
    hashtable = Hashtable()
    actual = hashtable.hash('ABAB')
    # A -> 65
    # B -> 66
    # sum = 65+66+65+66 = 262
    # sum * 23 = 6026
    # sum % size(1024) --> 906
    expected = 906
    assert actual == expected
示例#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
示例#7
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
示例#8
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
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
示例#11
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
示例#12
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]]
示例#13
0
def test_hash_table_generic():
    hashtable = Hashtable(1024)

    # testing for single input
    hashtable.add('ahmad', 25)
    hashtable.add('silent', True)

    assert hashtable.contains('ahmad') is True
    assert hashtable.contains('ahmadd') is False

    # testing for multiple inputs for a single bucket

    hashtable.add('hamad', 29)
    hashtable.add('listen', 'Hey man')

    assert hashtable.contains('ahmad') is True
    assert hashtable.contains('hamad') is True
    assert hashtable.contains('listen') is True
    assert hashtable.contains('silent') is True

    # check for duplicate input
    assert hashtable.add('ahmad', 35) == 'key already exists'

    # testing the values

    assert hashtable.get('ahmad') == 25
    assert hashtable.get('hamad') == 29
    assert hashtable.get('dada') == None
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
示例#15
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
示例#16
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
示例#17
0
def test_get_silent_and_listen():
    hashtable = Hashtable()
    hashtable.set('listen', 'to me')
    hashtable.set('silent', 'so quiet')

    assert hashtable.get('listen') == 'to me'
    assert hashtable.get('silent') == 'so quiet'
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
示例#19
0
    def binary_tree_to_unique_hashtable(binarytree):
        bt_hashtable = Hashtable()

        def walk(root):
            if root.left_node:
                walk(root.left_node)

            if root.value and not bt_hashtable.contains(str(root.value)):
                bt_hashtable.add(str(root.value), 1)

            if root.right_node:
                walk(root.right_node)

        walk(binarytree.root)

        return bt_hashtable
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'
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
def test_hash_function_returns_valid_index():
    hashtable = Hashtable()
    key = "apple"
    index = hashtable.hash(key)
    actual = hashtable.hash(key)
    assert actual == index
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
def test_contains_returns_false_if_key_is_not_present():
    hashtable = Hashtable()
    hashtable.add("xyz", 10)
    assert hashtable.contains("abc") == False
def test_contains_returns_true_if_key_is_present():
    hashtable = Hashtable()
    hashtable.add("xyz", 10)
    assert hashtable.contains("xyz")
def test_different_words_will_produce_different_values():
    hashtable = Hashtable()
    key_a = "xyz"
    key_b = "abc"
    assert hashtable.hash(key_a) != hashtable.hash(key_b)
def test_can_instantiate_hashtable():
    new_hashtable = Hashtable()
    assert new_hashtable
def test_similar_words_will_result_same_hash():
    hashtable = Hashtable()
    key_a = "xyz"
    key_b = "zyx"
    assert hashtable.hash(key_a) == hashtable.hash(key_b)
def test_hash_function_will_return_a_value_within_range():
    hashtable = Hashtable()
    actual = hashtable.hash("test")
    assert actual <= 1024
    assert actual >= 0