Пример #1
0
def test_add_key():
    """
    1.Adding a key/value to your hashtable results in the value being in the data structure
    """
    ht = HashTable(1024)
    ht.add('one', 1)
    assert ht.contains('one') == True
def test_no_random_hash():
    hashtable = HashTable()
    one = hashtable._hash('test')
    two = hashtable._hash('test')
    three = hashtable._hash('test')
    four = hashtable._hash('test')
    five = hashtable._hash('test')
    assert one == two == three == four == five
Пример #3
0
def test_in_range_hash():
    hashtable = HashTable()
    actual = hashtable._hash('spam')

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

    assert 0 <= actual < hashtable.initial_size
def test_add_collision():
    hashtable = HashTable()
    bucket_number_1 = hashtable.add('test_key', 'test_value')
    bucket_number_2 = hashtable.add('tset_key', 'tset_value')
    bucket_number_3 = hashtable.add('tste_key', 'tste_value')

    assert bucket_number_1 == bucket_number_2 == bucket_number_3
    assert hashtable._buckets[bucket_number_1].head.val[1] == 'test_value'
    assert hashtable._buckets[bucket_number_1].head.next.val[1] == 'tset_value'
    assert hashtable._buckets[bucket_number_1].head.next.next.val[
        1] == 'tste_value'
Пример #5
0
def test_add_multiple_hash_pass():
    hashtable = HashTable()
    hashtable.add('hadi', 45)
    hashtable.add('ahmad', 33)
    hashtable.add('moe', 11)
    actual = hashtable.get('moe')
    expected = 11
    assert actual == expected
Пример #6
0
def test_contains_pass_with_collsion():
    hashtable = HashTable()
    hashtable.add('hadi', 40)
    hashtable.add('ahmad', 33)
    hashtable.add('moe', 11)
    expected = hashtable.contains('ahmad')
    actual = True
    assert actual == expected
def tree_intersection(Bt1, Bt2):
    ht = HashTable()
    match_array = []

    def _tree_put(curr):

        if curr:
            ht.add(curr.data, curr.data)

            if curr.left:
                _tree_put(curr.left)
            if curr.right:
                _tree_put(curr.right)

    def _tree_check(curr):
        if curr:
            if ht.contains(curr.data):
                match_array.append(curr.data)
            if curr.left:
                _tree_check(curr.left)
            if curr.right:
                _tree_check(curr.right)

    _tree_put(Bt1.root)
    _tree_check(Bt2.root)
    return match_array
Пример #8
0
def test_add_multiple_collision():
    """  
    4.Successfully handle a collision within the hashtable
    5.Successfully retrieve a value from a bucket within the hashtable that has a collision
    6.Successfully retrieve a value from a bucket within the hashtable that has a collision
    """
    ht = HashTable(1024)
    ht.add('one', 1)
    ht.add('two', 2)
    ht.add('two', 3)
    assert ht.contains('one') == True
    assert ht.contains('two') == True
    assert ht.get('two') == 2
    def find_repeated_words(self, string_input):
        """[summary]

        Args:
            string_input ([type]): [description]

        Returns:
            [string]: [either the repeated string, or the entire unique string]
        """

        hashtable = HashTable(size=1024)
        string_input = string_input.lower().split()

        for word in string_input:
            # tried to use regex here... it didn't work so well, maybe come back to this!
            word = word.strip(str(string.punctuation))
            if hashtable.contains(word):
                return word
            else:
                hashtable.add(word, word)
                continue

        return f'String is Unique {string_input}'
Пример #10
0
def test_hash():
    """
    6.Successfully hash a key to an in-range value
    """

    ht = HashTable(1024)
    ht.add('four', 4)
    assert ht.contains('four') == True
    assert ht.contains('whale') == False
Пример #11
0
def test_get():
    """
    2.Retrieving based on a key returns the value stored
    3.Successfully returns null for a key that does not exist in the hashtable
    """
    ht = HashTable(1024)
    ht.add('five', 5)
    assert ht.get('five') == 5
    assert ht.get('tiger') == None
def test_hashtable_size_58_pass():
    hashtable = HashTable(58)
    expected = 58
    actual = hashtable.size
    assert actual == expected
Пример #13
0
def test_hashtable():
    hashtable = HashTable()

    hashtable.set("darkblue", 1)
    hashtable.set("salmon", 2)
    hashtable.set("hohoj", 3)

    assert hashtable.get("darkblue") == 1
    assert hashtable.get("salmon") == 2
    assert hashtable.get("empty") == None

    assert hashtable.keys() == ["darkblue", "salmon", "hohoj"]
    assert hashtable.values() == [1, 2, 3]
def test_contains_fail():
    hashtable = HashTable()
    hashtable.add('test_key', 'test_value')
    assert hashtable.contains('not_test_key') == False
def test_contains_pass():
    hashtable = HashTable()
    hashtable.add('test_key', 'test_value')
    assert hashtable.contains('test_key') == True
Пример #16
0
def test_contains_pass_false():
    hashtable = HashTable()
    hashtable.add('hadi', 40)
    expected = hashtable.contains('Daniel')
    actual = False
    assert actual == expected
Пример #17
0
def test_contains_fail():
    hashtable = HashTable()
    hashtable.add('hadi', 40)
    expected = hashtable.contains('Daniel')
    actual = True
    assert actual != expected
Пример #18
0
 def __init__(self, hash_table=HashTable()):
     self.hash_table = hash_table
Пример #19
0
def test_get():
    hashtable = HashTable()
    hashtable.add('glisten', 'glitter')
    expected = 'glitter'
    actual = hashtable.get('glisten')
    assert actual == expected
Пример #20
0
def test_different_hash():
    hashtable = HashTable()
    initial = hashtable._hash('glisten')
    secondary = hashtable._hash('silent')
    assert initial != secondary
Пример #21
0
def test_same_hash():
    hashtable = HashTable()
    initial = hashtable._hash('listen')
    secondary = hashtable._hash('silent')
    assert initial == secondary
Пример #22
0
def test_hashtable():
    """
    1. Adding a key/value to your hashtable results in the value being in the data structure
    2. Retrieving based on a key returns the value stored
    3. Successfully returns null for a key that does not exist in the hashtable
    4. Successfully handle a collision within the hashtable
    5. Successfully retrieve a value from a bucket within the hashtable that has a collision
    6. Successfully hash a key to an in-range value
    """

    hashtable = HashTable()

    hashtable.add('reema', 5)

    assert hashtable.contains('reema')

    assert hashtable.get('reema') == 5

    assert hashtable.get('nokey') == None

    hashtable.add('collision', 4)

    assert hashtable.contains('collision')

    assert hashtable.get('collision') == 4

    assert hashtable.hash('key')
Пример #23
0
def test_create():
    hashtable = HashTable()
    assert hashtable
def test_hashtable_size_58_fail():
    hashtable = HashTable(58)
    expected = 57
    actual = hashtable.size
    assert actual != expected
Пример #25
0
def test_contains_and_add():
    hashtable = HashTable()
    hashtable.add('glisten', 'glitter')
    expected = True
    actual = hashtable.contains('glisten')
    assert actual == expected
def test_single_hash_pass():
    hashtable = HashTable()
    hashtable.add('roger', 45)
    actual = hashtable.get('roger')
    expected = 45
    assert actual == expected
Пример #27
0
def test_predictable_hash():
    hashtable = HashTable()
    initial = hashtable._hash('spam')
    secondary = hashtable._hash('spam')
    assert initial == secondary
def test_hashtable_default_size():
    hashtable = HashTable()
    expected = 1024
    actual = hashtable.size
    assert actual == expected
Пример #29
0
def test_func():
    with open('Lorem_ipsum.txt', 'r') as fp:
        words = fp.read().split(' ')

    start = time.time()
    ht = HashTable(100)
    word_count = 0
    for word in words:
        ht.add(str(word_count) + word, word_count)
        word_count += 1
    end = time.time()
    print("HashTable(100) add time:%f\n" % (end - start))
    print(ht.get_distribution())
    print('\n')

    start = time.time()
    ht1 = HashTable(1000)
    word_count = 0
    for word in words:
        ht1.add(str(word_count) + word, word_count)
        word_count += 1
    end = time.time()
    print("HashTable(1000) add time:%f\n" % (end - start))
    print(ht1.get_distribution())
    print('\n')

    start = time.time()
    ht2 = HashTable(150000)
    word_count = 0
    for word in words:
        ht2.add(str(word_count) + word, word_count)
        word_count += 1
    end = time.time()
    print("HashTable(150000) add time:%f\n" % (end - start))
    print(ht2.get_distribution())
    print('\n')

    start = time.time()
    for item in ht2:
        ht2.remove(item.key)
    end = time.time()
    print("remove entry cost time:%f\n" % (end - start))
def test_single_hash_fail():
    hashtable = HashTable()
    hashtable.add('roger', 45)
    actual = hashtable.get('roger')
    expected = 44
    assert actual != expected