def test_retrieve_value_in_collision_bucket(): hashtable = Hashtable() initial = hashtable.add('listen', 'One') secondary = hashtable.add('silent', 'Two') actual = hashtable.get('silent') expected = 'Two' assert actual == expected
def test_not_contains(): hashtable = Hashtable() hashtable.set('one', '1') actual = hashtable.contains('two') expect = False assert actual == expect
def test_hash_two(): hashy = Hashtable() hashy.add('fond', 'averse') hashy.add('wrath', 'delight') hashy.add('diligent', 'idle') hashy.add('guide', 'follow') hashy.add('flow', 'jam') return hashy
def test_contains(): hashtable = Hashtable() hashtable.set('one', '1') actual = hashtable.contains('one') expect = True assert actual == expect
def test_hash_one(): hashy = Hashtable() hashy.add('fond', 'enamored') hashy.add('wrath', 'anger') hashy.add('diligent', 'employed') hashy.add('outfit', 'garb') hashy.add('guide', 'usher') return hashy
def test_get_hashes_length(): hashtable = Hashtable() hashtable.set('one', '1') hashtable.set('two', '2') actual = len(hashtable.get_hashes()) expect = 2 assert actual == expect
def tree_intersection(tree1, tree2) -> list: common_vals = [] if tree1.root and tree2.root: tree1_list, tree2_list = tree1.pre_order(), tree2.pre_order() hashy = Hashtable() for val in tree1_list: hashy.add(str(val), "number") for val in tree2_list: if hashy.contains(str(val)): common_vals.append(int(val)) return common_vals
def comb(string): words = string.replace(",", "").replace(".", "").split(" ") ht = Hashtable() for word in words: word = word.lower() if ht.contains(word): return word else: ht.add(key=word, value=word)
def test_in_range_hash(): hashtable = Hashtable() actual = hashtable._hash( 'JkhakjfhkjghjkwshgkjashgjkwhAGKJHGJKHWGEJKhahgfkjaghkjahgkjaghkjaghkjaghkjaghkjahgkjah' ) # assert actual >= 0 # assert actual < hashtable._size assert 0 <= actual < hashtable._size
def repeat_word(text: str) -> str: valid_chars = "abcdefghijklmnopqrstuvwxyz " words = "".join([x for x in text.lower() if x in valid_chars]) hashy = Hashtable() for word in words.split(): if hashy.contains(word): return word hashy.add(word, "word") return "no duplicate words found"
def word_repeat(string): words = string.replace(',', '').replace('.', '').split(" ") ht = Hashtable() for word in words: word = word.lower() if ht.contains(word): return word else: ht.add(word, word)
def tree_intersection(bt1, bt2): new_arr = [] tree1 = bt1.pre_order() tree2 = bt2.pre_order() ht = Hashtable() for value in tree1: ht.add(key=str(value), value=value) for value in tree2: if ht.contains(str(value)): new_arr.append(value) return new_arr
def test_left_join_with_antonym_right(): left = Hashtable() right = Hashtable() left.set('fond', 'enamored') right.set('fond', 'averse') actual = left.join(right) expect = [['fond', 'enamored', 'averse']] assert actual == expect
def test_right_join_collisions(): left = Hashtable() right = Hashtable() left.set('fond', 'enamored') left.set('donf', 'enamored') right.set('fond', 'averse') actual = left.join(right, right_join=True) expect = [['fond', 'averse', 'enamored']] assert actual == expect
def test_get_silent_and_listen(): hashtable = Hashtable() hashtable.set('listen', 'to me') hashtable.set('silent', 'so quiet') assert hashtable.get('listen') == 'to me' assert hashtable.get('silent') == 'so quiet'
def test_left_join_one_empty(): ht1 = Hashtable() ht2 = Hashtable() ht1.add('fond', 'enamored') ht1.add('wrath', 'anger') actual = hashmap_left_join(ht1, ht2) expected = [ ['fond', 'enamored', None], ['wrath', 'anger', None], ] for entry in expected: assert entry in actual
def test_join_left_with_empty_right(): left = Hashtable() right = Hashtable() left.set('fond', 'enamored') actual = left.join(right) expect = [['fond', 'enamored', None]] assert actual == expect
def test_right_join_with_empty_right(): left = Hashtable() right = Hashtable() left.set('fond', 'enamored') actual = left.join(right, right_join=True) expect = [] assert actual == expect
def test_predictable_hash(): hashtable = Hashtable() initial = hashtable._hash('spam') secondary = hashtable._hash('spam') assert initial == secondary
def test_in_range_hash(): hashtable = Hashtable() actual = hashtable._hash('spam') assert 0 <= actual < hashtable._size
def test_get_apple(): hashtable = Hashtable() hashtable.set("apple", "Used for apple sauce") actual = hashtable.get("apple") expected = "Used for apple sauce" assert actual == expected
def test_hashtable_collision(): hashtable = Hashtable() initial = hashtable.add('listen', 'One') secondary = hashtable.add('silent', 'Two') assert hashtable._hash('listen') == hashtable._hash('silent')
def test_key_that_does_not_exist(): hashtable = Hashtable() hashtable.add('Sunday', 'One') actual = hashtable.contains('Monday') expected = False assert actual == expected
def test_retrieving_value_from_key(): hashtable = Hashtable() hashtable.add('Sunday', 'One') actual = hashtable.get('Sunday') expected = 'One' assert actual == expected
def test_create(): hashtable = Hashtable() assert hashtable
def test_adding_keyval_pair(): hashtable = Hashtable() hashtable.add('Sunday', 'One') actual = hashtable.contains('Sunday') expected = True assert actual == expected
def test_different_hash(): hashtable = Hashtable() initial = hashtable._hash('glisten') secondary = hashtable._hash('silent') assert initial != secondary
def test_left_join_both_empty(): ht1 = Hashtable() ht2 = Hashtable() actual = hashmap_left_join(ht1, ht2) expected = [] assert actual == expected
def test_same_hash(): hashtable = Hashtable() initial = hashtable._hash('listen') secondary = hashtable._hash('silent') assert initial == secondary
def test_left_join(): ht1 = Hashtable() ht2 = Hashtable() ht1.add('fond', 'enamored') ht1.add('wrath', 'anger') ht1.add('diligent', 'employed') ht2.add('fond', 'averse') ht2.add('wrath', 'delight') ht2.add('happy', 'follow') actual = hashmap_left_join(ht1, ht2) expected = [ ['fond', 'enamored', 'averse'], ['wrath', 'anger', 'delight'], ['diligent', 'employed', None] ] for entry in expected: assert entry in actual