コード例 #1
0
def generate_HT():
    text = """
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
"""
    data = HT(32)
    for line in text.split('\n'):
        if line.strip():
            arr = line.split(' ')
            for i in arr:
                if data.get(i) == None:
                    print(i, line)
                    data.put(i, line)
                    break
    return data
コード例 #2
0
def test_set():
    """Set only accepts Strings or lists of chars.
    Raises an error for all other types."""
    ht = Hash_Table(100)
    for aType in [None, 1, [2, 3], {4: 5}, (6, 7), ht]:
        with pytest.raises(TypeError) as error_info:
            ht.set(aType, "regardless...")
コード例 #3
0
def test_hash():
    """Test hash by placing all entries in system dict to hash and 
    subsequently retrieving"""

    ht = Hash_Table(100000)
    with open('/usr/share/dict/words', 'r') as file:
        lines = file.readlines()
    lines = [line.rstrip('\n') for line in lines]

    for line in lines:
        ht.set(line, line)

    for line in lines:
        testval = ht.get(line)
        assert testval == line
コード例 #4
0
def populated_hash():
    h = Hash_Table()
    h.set('pear_key', 'pear_val')
    h.set('orange_key', 'orange_val')
    h.set('apple_key', 'apple_val')
    return h
コード例 #5
0
def empty_hash():
    h = Hash_Table()
    return h
コード例 #6
0
def test_hash_init_with_table_size():
    h = Hash_Table(1024)
    assert len(h.hash_table) == 1024
コード例 #7
0
def test_hash_init_empty():
    h = Hash_Table()
    assert h.hash_table == [None] * 10
コード例 #8
0
def populated_hash():
    h = Hash_Table()
    h.set('pear_key', 'pear_val')
    h.set('orange_key', 'orange_val')
    h.set('apple_key', 'apple_val')
    return h
コード例 #9
0
def big_hash():
    big_hash = Hash_Table(250)
    for word in WORDS:
        big_hash.set(word, word)
    return big_hash