コード例 #1
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')
コード例 #2
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
コード例 #3
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
コード例 #4
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
コード例 #5
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
コード例 #6
0
    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}'
コード例 #7
0
def test_contains_and_add():
    hashtable = HashTable()
    hashtable.add('glisten', 'glitter')
    expected = True
    actual = hashtable.contains('glisten')
    assert actual == expected
コード例 #8
0
def test_contains_fail():
    hashtable = HashTable()
    hashtable.add('test_key', 'test_value')
    assert hashtable.contains('not_test_key') == False
コード例 #9
0
def test_contains_pass():
    hashtable = HashTable()
    hashtable.add('test_key', 'test_value')
    assert hashtable.contains('test_key') == True
コード例 #10
0
def test_contains_fail():
    hashtable = HashTable()
    hashtable.add('hadi', 40)
    expected = hashtable.contains('Daniel')
    actual = True
    assert actual != expected
コード例 #11
0
def test_contains_pass_false():
    hashtable = HashTable()
    hashtable.add('hadi', 40)
    expected = hashtable.contains('Daniel')
    actual = False
    assert actual == expected
コード例 #12
0
def test_contains_pass_true():
    hashtable = HashTable()
    hashtable.add('hadi', 40)
    expected = hashtable.contains('hadi')
    actual = True
    assert actual == expected
コード例 #13
0
def test_contains():
    ht = HashTable()
    actual = ht.contains('milk')
    expected = False
    assert actual == expected
コード例 #14
0
def test_add():
    ht = HashTable()
    ht.add('milk', 'cookies')
    actual = ht.contains('milk')
    expected = True
    assert actual == expected