def tree_intersection(tree_1, tree_2=0): arr = [] hashtable = Hashtable() top = tree_1.preOrder().top while top: if not hashtable.contains(top.value): hashtable.add(0, top.value) top = top.next top = tree_2.preOrder().top while top: if hashtable.contains(top.value): arr.append(int(top.value)) top = top.next return arr
def test_hashtable_contains_true(): hashtable = Hashtable() hashtable.set('listen',1) actual = hashtable.contains('listen') expected = True assert actual == expected
def twoSumWithHash(self, nums, target): hashtable = Hashtable() for i in range(len(nums)): # Check if complement exists. if hashtable.contains(target - nums[i]): # Return it if it does. return [hashtable.get(target - nums[i]), i] else: hashtable.put(nums[i], i)
def repeated_word(sentence): words = sentence.split() for w in range(len(words)): pattern = r'[^\w\s]' words[w] = re.sub(pattern,'', words[w]) hash = Hashtable() for word in words : if hash.contains(word.upper()): return word hash.add(word,word.upper())
def repeated_word_check(text_to_check): case_format = text_to_check.lower() punctuation_clean = case_format.translate( str.maketrans('', '', string.punctuation)) iterable_words = punctuation_clean.split() ht = Hashtable() for word in iterable_words: if ht.contains(word): return word else: ht.set(word, word) return 'No Words Repeated'
def test_does_not_contain(): new_hashtable = Hashtable() assert new_hashtable.contains('Couch') == False
def test_contains(): new_hashtable = Hashtable() new_hashtable.add('Soda', 'MtnDew') assert new_hashtable.contains('Soda') == True
def test_contains_false(): hashtable = Hashtable() hashtable.add('roger', 45) actual = hashtable.contains('roger') expected = False assert actual == expected
def test_contains_false(): ht = Hashtable() ht.add('potato', 'object') assert ht.contains('test') is False
def test_contains_true(): ht = Hashtable() ht.add('test', 'object') assert ht.contains('test') is True