Example #1
0
    # l = (1, 2)
    # l[0] = 2
    # l[1] = 3
    # print(l)
    # q = Queue()
    # q.enqueue(10)
    # q.enqueue(2)
    # q.enqueue(30)
    # q.enqueue(15)
    # print(q)
    # q.dequeue()
    # q.dequeue()
    # q.dequeue()
    # q.enqueue(20)
    # print(q.peek())
    # q.enqueue(25)
    # q.enqueue(40)
    # q.enqueue(10)
    # print(q)

    # hash table test
    ht = HashTable(size=5)
    ht.put(1, 'pavan')
    ht.put(406, 'kumar')
    ht.put(506, 'lav')
    ht.put(506, 'kus')
    ht.put('pavan', 5)
    ht.put('pavan', 'kumar')
    ht.remove('pavan')
    print(ht.get(506))
Example #2
0
 def test_initial_keys(self):
     """
     A HashTable initially has no keys.
     """
     h = HashTable()
     self.assertEqual([], h.keys())
Example #3
0
 def test_size(self):
     """
     A default HashTable has a size attribute that is 10.
     """
     h = HashTable()
     self.assertEqual(10, h.size)
Example #4
0
def hash_table_prime():
    """Prime hash table dict."""
    hash_table = HashTable(58237, optimus_prime_hash)
    for word in DICTIONARY:
        hash_table.set(word, word)
    return hash_table
Example #5
0
def test_set_stores_value_given_key():
    """Test set method stores given val using given key."""
    hash_table = HashTable(17)
    hash_table.set('testkey2', 'testval2')
    assert hash_table.get("testkey2") == "testval2"
Example #6
0
def test_hash_table_is_hash_table_instance():
    """Test that hash table is HashTable type."""
    from hash_table import HashTable
    ht = HashTable()
    assert isinstance(ht, HashTable)
Example #7
0
def empty_hash_table():
    """Empty Hash Table fixture."""
    from hash_table import HashTable
    return HashTable()
 def test_simple_insertion(self):
     h = HashTable()
     try:
         h['foo'] = 'bar'
     except TypeError:
         self.fail("HashTable has no __setitem__ implementation")
 def test_simple_retrieval(self):
     h = HashTable()
     try:
         _ = h['foo']
     except TypeError:
         self.fail("HashTable has no __getitem__ implementation")
def test_get():
    """Test that get() retrieves value."""
    h_table = HashTable(10)
    h_table.set('thinking', 'tiring')
    assert h_table.get('thinking') == 'tiring'
def test_simple_additive_hash():
    """Test that additive hash on small word."""
    h_table = HashTable(10)
    assert h_table._additive_hash('a') == 7
def test_set_add_word():
    """Test that set() adds key-value pair."""
    h_table = HashTable(10)
    h_table.set('thinking', 'tiring')
    assert h_table.get('thinking') == 'tiring'
def test_set_add_a_nonstring():
    """Test that set() won't take in a number type."""
    h_table = HashTable(10)
    with pytest.raises(TypeError):
        h_table.set(3, 'AI')
def test_simple_xor_hash():
    """Test that additive hash on small word."""
    h_table = HashTable(10, 'xor')
    assert h_table._xor_hash('at') == 21
Example #15
0
def empty_bern_hash():
    """Empty Hash Table fixture with Bern hash."""
    from hash_table import HashTable
    return HashTable(hash_func='bern')
 def test_data(self):
     """
     A HashTable has an internal array for storing k-v pairs.
     """
     h = HashTable()
     self.assertEqual(list, type(h.data))
Example #17
0
def test_raise_value_error_with_bad_hash():
    """Test ValueError raised with bad hash."""
    from hash_table import HashTable
    with pytest.raises(ValueError):
        HashTable(hash_func='rat_hash')
def test_init():
    ht = HashTable()
    assert len(ht.table) == 1024
    ht2 = HashTable(10000)
    assert len(ht2.table) == 10000
Example #19
0
def test_hash_table_with_size_arg_length_is_arg():
    """Test hash table with size arg has size of arg."""
    from hash_table import HashTable
    ht = HashTable(40)
    assert ht.size == 40
def test_hash():
    ht = HashTable()
    ht.set('coffee', 'coffee')
    assert ht.get('coffee') == 'coffee'
Example #21
0
def test_expand_shrink():
    ht = HashTable()
    assert (ht._len == 0)
    assert (ht._size == 8)
    ht.put("James Bond", 982268945)
    ht.put("Jon Snow", 981275678)
    ht.put("Princess Zelda", 987651123)
    ht.put("Carmen Sandiego", 981112365)
    ht.put("Link the Hero", 981112345)
    ht.put("Tingle the green", 987652234)
    ht.put("Kin the golden", 36424852)
    ht.put('Clarinha, the little snow', 11111111)
    assert (ht._len == 8)
    assert (ht._size == 8)
    assert (ht._slots == [[('Carmen Sandiego', 981112365)], [], [],
                          [('James Bond', 982268945)],
                          [('Kin the golden', 36424852),
                           ('Clarinha, the little snow', 11111111)],
                          [('Link the Hero', 981112345),
                           ('Tingle the green', 987652234)],
                          [('Jon Snow', 981275678)],
                          [('Princess Zelda', 987651123)]])
    ht.put('Pepeta', 22222222)
    assert (ht._len == 9)
    assert (ht._size == 16)
    assert (ht._slots == [[('Carmen Sandiego', 981112365)], [], [],
                          [('James Bond', 982268945)],
                          [('Clarinha, the little snow', 11111111)],
                          [('Tingle the green', 987652234)], [],
                          [('Princess Zelda', 987651123)], [], [], [], [],
                          [('Kin the golden', 36424852)],
                          [('Link the Hero', 981112345)],
                          [('Jon Snow', 981275678)], [('Pepeta', 22222222)]])
    ht.remove('Pepeta')
    ht.remove('Clarinha, the little snow')
    ht.remove("Kin the golden")
    ht.remove("Tingle the green")
    assert (ht._slots == [[('Carmen Sandiego', 981112365)], [], [],
                          [('James Bond', 982268945)], [], [], [],
                          [('Princess Zelda', 987651123)], [], [], [], [], [],
                          [('Link the Hero', 981112345)],
                          [('Jon Snow', 981275678)], []])
    assert (ht._len == 5)
    assert (ht._size == 16)
    ht.remove('Pepeta')
    ht.remove('Clarinha, the little snow')
    ht.remove("Kin the golden")
    ht.remove("Tingle the green")
    ht.remove("Link the Hero")
    assert (ht._slots == [[('Carmen Sandiego', 981112365)], [], [],
                          [('James Bond', 982268945)], [], [],
                          [('Jon Snow', 981275678)],
                          [('Princess Zelda', 987651123)]])
    assert (ht._len == 4)
    assert (ht._size == 8)
    ht.remove("Carmen Sandiego")
    assert (ht._slots == [[], [], [], [('James Bond', 982268945)], [], [],
                          [('Jon Snow', 981275678)],
                          [('Princess Zelda', 987651123)]])
    ht.put("Some random 1", 00000000)
    ht.put("Some random 2", 00000000)
    ht.put("Some random 3", 00000000)
    ht.put("Some random 4", 00000000)
    ht.put("Some random 5", 00000000)
    assert (ht._slots == [[('Some random 3', 0)], [('Some random 4', 0)],
                          [('Some random 5', 0)], [('James Bond', 982268945)],
                          [], [],
                          [('Jon Snow', 981275678), ('Some random 1', 0)],
                          [('Princess Zelda', 987651123),
                           ('Some random 2', 0)]])
    ht.put("Some random 6", 00000000)
    assert (ht._slots == [[], [], [], [('James Bond', 982268945)], [], [],
                          [('Some random 1', 0)],
                          [('Princess Zelda', 987651123),
                           ('Some random 2', 0)], [('Some random 3', 0)],
                          [('Some random 4', 0)], [('Some random 5', 0)],
                          [('Some random 6', 0)], [], [],
                          [('Jon Snow', 981275678)], []])
def test_duplicate_hash_val():
    ht = HashTable()
    ht.set('bob', 'bob')
    ht.set('obb', 'obb')
    assert ht.get('bob') == 'bob'
    assert ht.get('obb') == 'obb'
Example #23
0
def test_get_returns_stored_value():
    """Test get method and see if it returns stored val."""
    hash_table = HashTable(17)
    hash_table.set('testkey', 'testval')
    assert hash_table.get("testkey") == "testval"
def test_non_item():
    ht = HashTable()
    ht.set('coffee', 'coffee')
    with pytest.raises(KeyError):
        ht.get('milk')
Example #25
0
 def test_retrieve_none(self):
     """
     Retreiving a value for non-existent key returns None.
     """
     h = HashTable(3)
     self.assertEqual(None, h['foo'])
def test_non_bucket():
    ht = HashTable()
    with pytest.raises(IndexError):
        ht.table[1025]
Example #27
0
 def test_initial_values(self):
     """
     A HashTable initially has no values.
     """
     h = HashTable(3)
     self.assertEqual([], h.values())
def test_non_string():
    ht = HashTable()
    with pytest.raises(TypeError):
        ht.set(13, 13)
Example #29
0
 def test_instantiation_with_size(self):
     """
     A HashTable can be instantiated with an optional size.
     """
     h = HashTable(33)
     self.assertEqual(33, h.size)
Example #30
0
 def test_init(self):
     ht = HashTable(4)
     assert len(ht.buckets) == 4
     assert ht.length() == 0
     assert ht.size == 0