コード例 #1
0
def test_collision_retrieved():
    hash = HashTable()
    hash.add('cat', 'meow')
    hash.add('atc', 'value')
    actual = 'value'
    expected = hash.get('atc')
    assert actual == expected
コード例 #2
0
def test_collision():
    hash_table = HashTable(0)
    hash_table.add('cat', 'Jingles')
    hash_table.add('tac', 'ouchy')
    actual = 2
    expected = hash_table.size
    assert actual == expected
コード例 #3
0
def test_collision_handled():
    hash = HashTable()
    hash.add('cat', 'meow')
    hash.add('atc', 'value')
    actual = 2
    expected = hash.size
    assert actual == expected
コード例 #4
0
def test_collision_get():
    hash_table = HashTable(0)
    hash_table.add('cat', 'Jingles')
    hash_table.add('tac', 'ouchy')
    actual = 'ouchy'
    expected = hash_table.get('tac')
    assert actual == expected
コード例 #5
0
def left_join_tables(hashtable1, hashtable2):
    """[summary]
    This function takes in 2 hashtables and returns an array after
    combining both data structures using left join method.
    
    """
    hashtable3 = HashTable(11)
    ht1_keys = []
    ht3_data = []

    for item in hashtable1._buckets:
        if item:
            ht1_keys.append(item.get_keys())
    
    for bucket in ht1_keys:
        for key in bucket:
            value1 = hashtable1.get(key)
            value2 = None
            if hashtable2.contains(key):
                value2 = hashtable2.get(key)
                hashtable3.add(key, value1, value2)
            else:
                hashtable3.add(key, value1)
            
            ht3_data.append((key, value1, value2))
    
    for item in hashtable3._buckets:
        if item:
            item.display()
    
    return ht3_data
コード例 #6
0
def test_hash_delete():
    ht = HashTable()
    ht.add('spam', 'eggs')
    ht.delete('spam')

    with pytest.raises(ValueError):
        ht.get('spam')
コード例 #7
0
def test_collision_retrieved():
    hash = HashTable()
    hash.add('cat', 'wendy')
    hash.add('act', 'cat')
    actual = 'cat'
    expected = hash.get('act')
    assert actual == expected
コード例 #8
0
def test_collision():
    hash = HashTable()
    hash.add('cat', 'marvin')
    hash.add('act', 'cat')
    actual = 2
    expected = hash.size
    assert actual == expected
def test_get_form_collision():
    '''Successfully retrieve a value from a bucket within the hashtable that has a collision'''
    hash = HashTable()
    hash.add('dog', 'bark')
    hash.add('god', 'karb')
    actual = hash.get('dog')
    expected = 'bark'
    assert expected == actual
def test_get():
    '''Retrieving based on a key returns the value stored'''
    hash = HashTable()
    hash.add('dog', 'bark')
    hash.add('Freddie', 'Nightmare')
    actual = hash.get('Freddie')
    expected = 'Nightmare'
    assert expected == actual
コード例 #11
0
def test_contains_pass_with_collsion():
    hashtable = HashTable()
    hashtable.add('Chuck', 40)
    hashtable.add('kcuhC', 33)
    hashtable.add('Ckcuh', 11)
    expected = hashtable.contains('kcuhC')
    actual = True
    assert actual == expected
コード例 #12
0
def test_add_multiple_hash_pass():
    hashtable = HashTable()
    hashtable.add('Chuck', 45)
    hashtable.add('kcuhC', 33)
    hashtable.add('Ckcuh', 11)
    actual = hashtable.get('Ckcuh')
    expected = 11
    assert actual == expected
def test_get_collision():
    '''Successfully handle a collision within the hashtable'''
    hash = HashTable()
    hash.add('dog', 'bark')
    hash.add('god', 'karb')
    actual = hash.contains('dog')
    actual = hash.contains('god')
    expected = True
    assert expected == actual
def left_table_synonyms():
    ht = HashTable()
    ht.add('fond', 'enamored')
    ht.add('wrath', 'anger')
    ht.add('diligent', 'employed')
    ht.add('outfit', 'clothes')
    ht.add('guide', 'usher')

    return ht
コード例 #15
0
def test_no_repeats():
    first = HashTable()
    first.add('cat', 'meow')
    first.add('dog', 'woof')
    second = HashTable()
    second.add('snake', 'hiss')
    actual = left_join(first, second)
    expected = [('cat', 'meow', None), ('dog', 'woof', None)]
    assert actual == expected
コード例 #16
0
def repeated_word(phrase):
    start = 0
    word_table = HashTable()
    words = phrase.split(' ')
    for word in words:
        if not word_table.contains(word):
            word_table.add(word, word_table.hash(word))
        else:
            return word
    return 'No matching words.'
コード例 #17
0
 def test_adder(self):
     st = set()
     hasht = HashTable()
     for i in range(1000):
         num = randint(1, 1000000000)
         hasht.add(num)
         st.add(num)
     s = set()
     for j in hasht:
         s.add(j)
     assert s == st
コード例 #18
0
def test_export_keys():
    ht = HashTable()
    for x in range(10):
        ht.add(str(x), 1)
    actual = ht.export_keys()
    expected = []
    for x in range(10):
        dict = {'name': str(x), 'value': 1}
        expected.append(dict)
    actual = sorted(actual, key=lambda i: i['name'])
    expected = sorted(expected, key=lambda i: i['name'])
    assert expected == actual
コード例 #19
0
def test_get_many():
    ht = HashTable()
    ht.add('apple', 43)
    ht.add('banana', 32)
    ht.add('mango', 21)
    ht.add('pear', 54)
    ht.add('strawbery', 65)

    assert ht.get('apple') == 43
    assert ht.get('banana') == 32
    assert ht.get('mango') == 21
    assert ht.get('pear') == 54
    assert ht.get('strawbery') == 65
    assert ht.get('tomato') == None
コード例 #20
0
def hash_1():
    hash_1 = HashTable()
    hash_1.add('sad', 'depressed')
    hash_1.add('happy', 'glad')
    hash_1.add('tired', 'sleepy')
    hash_1.add('mad', 'angry')
    return hash_1
コード例 #21
0
def repeated_word(text_string):
    ht = HashTable()
    x = text_string.split()
    count = 0

    for word in x:
        if word[-1] in string.ascii_letters:
            if ht.contains(word.lower()):
                return word
            ht.add(word.lower(), count)
        else:
            if ht.contains(word[:-1].lower()):
                return word[-1]
            ht.add(word[:-1], count)
        count += 1
コード例 #22
0
def main():
    string = 'string bem grande pra gente ver os caracteres unicos'
    ht = HashTable()
    for char in string:
        count = ht.get(char)
        if count == None:
            count = 1
        else:
            count += 1
        ht.add(char, count)
    print('Unique chars are :: ')
    [
        print(item[0]) if item[1] == 1 else None for sublist in ht.hash_table
        for item in sublist
    ]
def right_table_antonyms():
    ht = HashTable()
    ht.add('fond', 'averse')
    ht.add('wrath', 'delight')
    ht.add('diligent', 'idle')
    ht.add('guide', 'follow')
    ht.add('flow', 'jam')

    return ht
コード例 #24
0
    def test_checker(self):
        st = set()
        hasht = HashTable()
        for i in range(10000):
            num = randint(1, 1000000)
            hasht.add(num)
            st.add(num)
        s = set()
        ss = set()
        for i in range(10):
            num = randint(1, 1000000)
            if (num in hasht):
                s.add(num)
            if (num in st):
                ss.add(num)

        assert s == ss
コード例 #25
0
    def test_len(self):
        st = set()
        hasht = HashTable()
        for i in range(100000):
            num = randint(1, 1000)
            hasht.add(num)
            st.add(num)
        s = set()
        ss = set()
        for i in range(10):
            num = randint(1, 1000)
            hasht.remove(num)
            st.remove(num)
            s.add(len(hasht))
            ss.add(len(st))

        assert s == ss
def test_add_to_table():
    '''Adding a key/value to your hashtable results in the value being in the data structure'''
    hash = HashTable()
    hash.add('dog', 'bark')
    hash.add('cat', 'bark')
    hash.add('bird', 'bark')
    hash.add('mouse', 'bark')
    hash.add('person', 'bark')
    expected = 5
    actual = hash.size
    assert expected == actual
コード例 #27
0
def test_collision():
    ht = HashTable()
    ht.add('coffee', 'brew')
    ht.add('offeec', 'nonsense')
    ht.add('tea', 'steep')
    ht.add('eat', 'bite')

    assert ht.get('coffee') == 'brew'
    assert ht.get('offeec') == 'nonsense'
    assert ht.get('tea') == 'steep'
    assert ht.get('eat') == 'bite'
コード例 #28
0
def test_bucket_add_duplicates_includes():
    ht = HashTable()
    ht.add('apple', 43)
    ht.add('apple', 6)
    ht.add('apple', 643)
    apple_index = ht._hash('apple')
    ht.add('mango', 'steen')
    mango_index = ht._hash('mango')

    assert ht._hash_table[mango_index].includes('mango') == True
    assert ht._hash_table[apple_index].includes('mango') == False
    assert ht._hash_table[apple_index].includes('apple') == True
コード例 #29
0
def synonyms():
    ht = HashTable()
    ht.add('fond', 'enamored')
    ht.add('diligent', 'employed')
    ht.add('outfit', 'garb')

    return ht
コード例 #30
0
def antonyms():
    ht = HashTable()
    ht.add('fond', 'averse')
    ht.add('diligent', 'idle')
    ht.add('guide', 'follow')

    return ht