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
Beispiel #2
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
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 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_hash_function_returns_valid_index():
    hashtable = Hashtable()
    key = "apple"
    index = hashtable.hash(key)
    actual = hashtable.hash(key)
    assert actual == index
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_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
Beispiel #9
0
def test_hash_one():
    test = Hashtable()
    actual = test.hash('tim')
    assert actual == 192