def test_hash_table_insertion_and_retrieval(self): ht = HashTable(8) ht.put("key-0", "val-0") ht.put("key-1", "val-1") ht.put("key-2", "val-2") ht.put("key-3", "val-3") ht.put("key-4", "val-4") ht.put("key-5", "val-5") ht.put("key-6", "val-6") ht.put("key-7", "val-7") ht.put("key-8", "val-8") ht.put("key-9", "val-9") return_value = ht.get("key-0") self.assertTrue(return_value == "val-0") return_value = ht.get("key-1") self.assertTrue(return_value == "val-1") return_value = ht.get("key-2") self.assertTrue(return_value == "val-2") return_value = ht.get("key-3") self.assertTrue(return_value == "val-3") return_value = ht.get("key-4") self.assertTrue(return_value == "val-4") return_value = ht.get("key-5") self.assertTrue(return_value == "val-5") return_value = ht.get("key-6") self.assertTrue(return_value == "val-6") return_value = ht.get("key-7") self.assertTrue(return_value == "val-7") return_value = ht.get("key-8") self.assertTrue(return_value == "val-8") return_value = ht.get("key-9") self.assertTrue(return_value == "val-9")
def test_get1(self): table = HashTable(8, lambda x: x) for item in (range(10, 71, 10)): table.insert(item) self.assertEqual(table.get(10), 2) self.assertEqual(table.get(20), 4) self.assertEqual(table.get(30), 6)
def test_get(): foo = HashTable(size=1024) foo.hashtable[91].append(('foo', 'foo')) foo.hashtable[91].append(('spoofs', 'spoofs')) foo.hashtable[885].append(('utopia', 'utopia')) assert foo.get('foo') == 'foo' assert foo.get('spoofs') == 'spoofs' assert foo.get('utopia') == 'utopia'
def test_set_and_get(self): ht = HashTable() ht.set('I', 1) ht.set('V', 5) ht.set('X', 10) assert ht.get('I') == 1 assert ht.get('V') == 5 assert ht.get('X') == 10 assert ht.length() == 3
def test_set_after_delete(self): ht = HashTable() ht.set('G', 900) assert ht.get('G') == 900 ht.delete('G') with self.assertRaises(KeyError): ht.get('G') ht.set('G', 900) assert ht.get('G') == 900
def test_my_set(self): ht = HashTable() ht.set('G', 4) ht.set('D', 4) assert ht.get('G') == 4 assert ht.length() == 2 ht.set('G', 5) assert ht.get('G') == 5 assert ht.length() == 2
class Set(object): """Set Class.""" def __init__(self, elements=None): self.setItems = HashTable() self.size = 0 if elements: for element in elements: self.add(element) def contains(self, element): if isinstance(element, list): for item in element: if not self.setItems.get(item): return False return True else: if self.setItems.get(element): return True else: return False def add(self, element): self.setItems.set(element, True) self.size += 1 def remove(self, element): self.setItems.delete(element) self.size -= 1 def union(self, other_set): newSet = Set(self.setItems.keys()) for element in other_set.setItems.keys(): if not newSet.contains(element): newSet.add(element) return newSet def intersection(self, other_set): newSet = Set() for element in self.setItems.keys(): if other_set.contains(element): newSet.add(element) return newSet def difference(self, other_set): newSet = Set() for element in other_set.setItems.keys(): if not self.contains(element): newSet.add(element) return newSet def is_subset(self, other_set): for element in other_set.setItems.keys(): if not self.contains(element): return False return True
def test_collision(): ht = HashTable() ht.add('coffee', 'brew') ht.add('offeec', 'nonsense') ht.add('tea', 'steep') ht.add('eat', 'bite') assert ht.get('coffee') == 'brew' assert ht.get('offeec') == 'nonsense' assert ht.get('tea') == 'steep' assert ht.get('eat') == 'bite'
def test_my_get(self): ht = HashTable() ht.set('G', 4) ht.set('D', 4) ht.set('A', 4) assert ht.get('G') == 4 assert ht.get('D') == 4 assert ht.get('A') == 4 with self.assertRaises(KeyError): ht.get('AA')
def test_set_and_get(self): ht = HashTable() ht.set('I', 1) ht.set('V', 5) ht.set('X', 10) assert ht.get('I') == 1 assert ht.get('V') == 5 assert ht.get('X') == 10 assert ht.length() == 3 with self.assertRaises(KeyError): ht.get('A')
def test_set_get_collisions(self): # without knowing hash function details, this is the only way # to ensure keys will collide by setting the size of the table h1 = HashTable(1) h1.set("test", 1776) h1.set("table", 1783) v1 = h1.get("test") v2 = h1.get("table") self.assertEqual(v2, 1783) self.assertEqual(v1, 1776)
def test_hash_table_insertion_and_retrieval(self): ht = HashTable(0x10000) ht.put("key-0", "val-0") ht.put("key-1", "val-1") ht.put("key-2", "val-2") return_value = ht.get("key-0") self.assertTrue(return_value == "val-0") return_value = ht.get("key-1") self.assertTrue(return_value == "val-1") return_value = ht.get("key-2") self.assertTrue(return_value == "val-2")
def test_set_get_collisions(self): # without knowing hash function details, this is the only way # to ensure keys will collide by setting the size of the table h1 = HashTable(1) h1.set("test", 1776) h1.set("table", 1783) v1 = h1.get("test") v2 = h1.get("table") self.assertEqual(v2, 1783) self.assertEqual(v1, 1776)
def test_set_twice_and_get(self): ht = HashTable() ht.set('I', 1) ht.set('V', 4) ht.set('X', 9) assert ht.length() == 3 ht.set('V', 5) # Update value ht.set('X', 10) # Update value assert ht.get('I') == 1 assert ht.get('V') == 5 assert ht.get('X') == 10 assert ht.length() == 3 # Check length is not overcounting
def test_my_delete(self): ht = HashTable() ht.set('G', 4) ht.set('D', 4) assert ht.length() == 2 ht.delete('G') with self.assertRaises(KeyError): ht.get('G') assert ht.length() == 1 ht.set('G', 4) assert ht.length() == 2
def test_hash_table_resize(self): ht = HashTable(8) ht.put("key-0", "val-0") ht.put("key-1", "val-1") ht.put("key-2", "val-2") ht.put("key-3", "val-3") ht.put("key-4", "val-4") ht.put("key-5", "val-5") ht.put("key-6", "val-6") ht.put("key-7", "val-7") ht.put("key-8", "val-8") ht.put("key-9", "val-9") # ht.resize() self.assertTrue(len(ht.storage) == 16) return_value = ht.get("key-0") self.assertTrue(return_value == "val-0") return_value = ht.get("key-1") self.assertTrue(return_value == "val-1") return_value = ht.get("key-2") self.assertTrue(return_value == "val-2") return_value = ht.get("key-3") self.assertTrue(return_value == "val-3") return_value = ht.get("key-4") self.assertTrue(return_value == "val-4") return_value = ht.get("key-5") self.assertTrue(return_value == "val-5") return_value = ht.get("key-6") self.assertTrue(return_value == "val-6") return_value = ht.get("key-7") self.assertTrue(return_value == "val-7") return_value = ht.get("key-8") self.assertTrue(return_value == "val-8") return_value = ht.get("key-9") self.assertTrue(return_value == "val-9") # ht.delete("key-9") # ht.delete("key-8") # ht.delete("key-7") ht.delete("key-6") ht.delete("key-5") ht.delete("key-4") ht.delete("key-3") ht.delete("key-2") ht.delete("key-1") ht.delete("key-0") self.assertTrue(len(ht.storage) == 8)
class TestHashTable(unittest.TestCase): def setUp(self): self.hash_table = HashTable() @given(st.randoms(), st.randoms()) def test_insertion(self, key, value): self.hash_table[key] = value self.assertEqual(self.hash_table.get(key), value) def test_insert_function(self): self.hash_table.insert("age", 55) self.assertEqual(self.hash_table.get("age"), 55) def test_deletion(self): self.hash_table["age"] = 55 self.hash_table["name"] = "John" self.hash_table.delete("age") self.assertEqual(self.hash_table.get("age"), None) def test_update_if_key_exists(self): self.hash_table["age"] = 55 self.hash_table["age"] = 66 self.assertEqual(self.hash_table.get("age"), 66) def test_size(self): self.hash_table["age"] = 55 self.hash_table["name"] = "John" self.assertEqual(len(self.hash_table), 2) def test_initial_size(self): self.assertEqual(len(self.hash_table._slot), 8) def test_increment_resize(self): for i in range(9): self.hash_table[f"{i}"] = "John" self.assertEqual(self.hash_table.get_slot_size(), 8 * 2) def test_decrement_resize(self): for i in range(14): self.hash_table[f"{i}"] = "John" for i in range(10): self.hash_table.delete(f"{i}") self.assertEqual(self.hash_table.get_slot_size(), 8) def test_size_after_update(self): self.hash_table["age"] = 55 self.hash_table["name"] = "John" self.hash_table["name"] = "John" self.hash_table["name"] = "John" self.assertEqual(len(self.hash_table), 2)
def test_hash_table_pution_overwrites_correctly(self): ht = HashTable(8) ht.put("key-0", "val-0") ht.put("key-1", "val-1") ht.put("key-2", "val-2") ht.put("key-3", "val-3") ht.put("key-4", "val-4") ht.put("key-5", "val-5") ht.put("key-6", "val-6") ht.put("key-7", "val-7") ht.put("key-8", "val-8") ht.put("key-9", "val-9") ht.put("key-0", "new-val-0") ht.put("key-1", "new-val-1") ht.put("key-2", "new-val-2") ht.put("key-3", "new-val-3") ht.put("key-4", "new-val-4") ht.put("key-5", "new-val-5") ht.put("key-6", "new-val-6") ht.put("key-7", "new-val-7") ht.put("key-8", "new-val-8") ht.put("key-9", "new-val-9") return_value = ht.get("key-0") print('return_value: ' + return_value) self.assertTrue(return_value == "new-val-0") return_value = ht.get("key-1") print('return_value: ' + return_value) self.assertTrue(return_value == "new-val-1") return_value = ht.get("key-2") self.assertTrue(return_value == "new-val-2") return_value = ht.get("key-3") self.assertTrue(return_value == "new-val-3") return_value = ht.get("key-4") self.assertTrue(return_value == "new-val-4") return_value = ht.get("key-5") self.assertTrue(return_value == "new-val-5") return_value = ht.get("key-6") self.assertTrue(return_value == "new-val-6") return_value = ht.get("key-7") self.assertTrue(return_value == "new-val-7") return_value = ht.get("key-8") self.assertTrue(return_value == "new-val-8") return_value = ht.get("key-9") self.assertTrue(return_value == "new-val-9")
def test_get(): ht = HashTable() # test with integer keys ht.put(9, 100) ht.put(20, 110) # test with string keys ht.put('cat', 125) ht.put('meow', 'blah!') assert(ht.get(9) == 100) assert(ht.get(20) == 110) assert(ht.get('cat') == 125) assert(ht.get('meow') == 'blah!')
def test_collision_retrieved(): hash = HashTable() hash.add('cat', 'meow') hash.add('atc', 'value') actual = 'value' expected = hash.get('atc') assert actual == expected
class WordJumble: def __init__(self): self.word_table = HashTable() #initialize a hastable self.sort_all_words() #call the sort all words function def sort_all_words(self): ''' ''' with open('/usr/share/dict/words') as f: words = f.read() words_list = words.split() for word in words_list: sorted_characters = sorted(word) sorted_word = ''.join(sorted_characters) self.word_table.set(sorted_word, word) def solve(self, jumble): if jumble is not None: assert isinstance(jumble, str) else: raise KeyError('jumble is not a string') sorted_word = ''.join(sorted(jumble)) found_word = self.word_table.get(sorted_word) return found_word
def test_collision_retrieved(): hash = HashTable() hash.add('cat', 'wendy') hash.add('act', 'cat') actual = 'cat' expected = hash.get('act') assert actual == expected
def test_collision_get(): hash_table = HashTable(0) hash_table.add('cat', 'Jingles') hash_table.add('tac', 'ouchy') actual = 'ouchy' expected = hash_table.get('tac') assert actual == expected
def test_set_get_latest(self): # the last entry for a key for a value should be the value h1 = HashTable(4) h1.set("A", 0) h1.set("A", 1) v = h1.get("A") self.assertEqual(v, 1)
def test_set_get_simple(self): # the value set for a key should have that value when retrieved for hs in MyFuncTestCase.hash_size_instances: h1 = HashTable(hs) h1.set("XYTZ", 1776) v = h1.get("XYTZ") self.assertEqual(v, 1776) for k in MyFuncTestCase.key_instances: h1.set(k, len(k)) v = h1.get(k) self.assertEqual(v, len(k)) for k in MyFuncTestCase.key_instances: v = h1.get(k) self.assertEqual(v, len(k))
def test_set_get_latest(self): # the last entry for a key for a value should be the value h1 = HashTable(4) h1.set("A", 0) h1.set("A", 1) v = h1.get("A") self.assertEqual(v, 1)
def test_get(self): hash = HashTable(8000) hash.put("hello", "there") hash.put("test", "two") hash.put("another", "test") self.assertEqual(hash.get("hello"), "there")
def test_set_get_simple(self): # the value set for a key should have that value when retrieved for hs in MyFuncTestCase.hash_size_instances: h1 = HashTable(hs) h1.set("XYTZ", 1776) v = h1.get("XYTZ") self.assertEqual(v, 1776) for k in MyFuncTestCase.key_instances: h1.set(k, len(k)) v = h1.get(k) self.assertEqual(v, len(k)) for k in MyFuncTestCase.key_instances: v = h1.get(k) self.assertEqual(v, len(k))
class Jumble(object): @time_it def __init__(self, words): """Initialize a new Hash Table and then clean up each line to get individual words. Add the dictionary words that equal the length of the scrambled words. """ self.words = words self.hash = HashTable() lengths = [] #Determining the lengths of the scrambled words, increases speed greatly. for scrambled_word in words: if len(scrambled_word) not in lengths: lengths.append(len(scrambled_word)) with open("/usr/share/dict/words", 'r') as f: for word in f: #Remove endline characters word = word.strip().lower() #Adds the word back into a hash table from the dictionary #Also only sets the word if it equals the length of the scrambled words if len(word) in lengths: self.hash.set(word, word) def get_permutations(self, scrambled_word): """Get every single permutation of the scrambled word.""" permutations = [] if len(scrambled_word) == 1: return scrambled_word else: for char in scrambled_word: #Replacing parts of the scrambled word until we get down to 1 letter, #then continue to re-add strings to the character in each call for string in self.get_permutations( scrambled_word.replace(char, "", 1)): #Checking to not repeat permutations if (char + string) not in permutations: permutations.append(char + string) return permutations def find_word(self, permutations): """Take each of the permutation of the scrambled word and look for it in the hash table.""" #Look up the word in the hash table for perm in permutations: if self.hash.contains(perm): return self.hash.get(perm) return "Word not Found" @time_it def unscramble(self): """Unscramble each word in our list of scrambled words.""" unscrambled = [] for word in self.words: unscrambled.append(self.find_word(self.get_permutations(word))) return unscrambled
def test_get_form_collision(): '''Successfully retrieve a value from a bucket within the hashtable that has a collision''' hash = HashTable() hash.add('dog', 'bark') hash.add('god', 'karb') actual = hash.get('dog') expected = 'bark' assert expected == actual
def test_hash_table_pution_overwrites_correctly(self): ht = HashTable(0x10000) ht.put("key-0", "val-0") ht.put("key-1", "val-1") ht.put("key-2", "val-2") ht.put("key-0", "new-val-0") ht.put("key-1", "new-val-1") ht.put("key-2", "new-val-2") return_value = ht.get("key-0") self.assertTrue(return_value == "new-val-0") return_value = ht.get("key-1") self.assertTrue(return_value == "new-val-1") return_value = ht.get("key-2") self.assertTrue(return_value == "new-val-2")
def test_set_and_get_word_list(word_list): foo = HashTable() words = word_list for word in words: foo.set(word, word) for word in words: value = foo.get(word) assert word == value
def test_get(): '''Retrieving based on a key returns the value stored''' hash = HashTable() hash.add('dog', 'bark') hash.add('Freddie', 'Nightmare') actual = hash.get('Freddie') expected = 'Nightmare' assert expected == actual
def testPutAndGet(self): ht = HashTable(16) ht.put('one', 1) ht.put('two', 2) ht.put('three', 3) ht.put('four', 4) v = ht.get('three') self.assertEqual(v, 3)
def test_add_multiple_hash_pass(): hashtable = HashTable() hashtable.add('Chuck', 45) hashtable.add('kcuhC', 33) hashtable.add('Ckcuh', 11) actual = hashtable.get('Ckcuh') expected = 11 assert actual == expected
def test_hash_table_resize(self): ht = HashTable(0x10000) ht.put("key-0", "val-0") ht.put("key-1", "val-1") ht.put("key-2", "val-2") ht.resize() self.assertTrue(len(ht.storage) == 128) return_value = ht.get("key-0") self.assertTrue(return_value == "val-0") return_value = ht.get("key-1") self.assertTrue(return_value == "val-1") return_value = ht.get("key-2") self.assertTrue(return_value == "val-2")
def test_get_complete(): hash_table = HashTable(1024) word_list = [] with open('words') as f: for line in f: word = line.rstrip() word_list.append(word) hash_table.set(word, word) for word in word_list: assert hash_table.get(word) == word
def test_set_get(self): for hs in MyFuncTestCase.hash_size_instances: h1 = HashTable(hs) # check different key types with self.assertRaises(TypeError): for inst in MyFuncTestCase.non_string_instances: h1.set(inst, inst) # do the set/get of some various typed values and check if the # value is set immediately after entry for inst in MyFuncTestCase.non_string_instances: h1.set(str(inst), inst) v = h1.get(str(inst)) self.assertEqual(v, inst) # then, after all the values have been set, try to get them again for inst in MyFuncTestCase.non_string_instances: v = h1.get(str(inst)) self.assertEqual(v, inst)
def test_set_get_manywords(self): h1 = HashTable(10000) f = open('/usr/share/dict/words') if f is not None: for line in f: h1.set(line, 0) # now test get on all the words sum = 0 for line in f: sum += h1.get(line) self.assertEqual(sum, 0)
def test_set_get(): """set(key, val) should store the given val using the given key, get(key) should return the value stored with the given key""" # There are 235886 words in my dictionary. # 1.6 * the expected size is 377418, ideal for performance ideal_size = 377418 table = HashTable(ideal_size) word = "init" table.set("hound", "puppy") assert table.get("hound") == "puppy" with pytest.raises(TypeError) as err: table.set(12345, "oogabooga") assert err.value == "Key must be a string" with pytest.raises(TypeError): table.set("oogabooga") # key & value both required with pytest.raises(TypeError): table.get() # key required with pytest.raises(TypeError) as err: table.set(12345) assert err.value == "Key must be a string" with io.open('/usr/share/dict/words') as words: while word != "": word = words.readline().strip() table.set(word, word) # fill the table, key == value # Now, we should get the same stuff back. with io.open('/usr/share/dict/words') as words: while word != "": word = words.readline().strip() assert table.get(word) == word with pytest.raises(KeyError): table.get("alkejralekjreqr") # Should also be able to reset stuff assert table.get("bacon") == "bacon" table.set("bacon", "delicious") assert table.get("bacon") == "delicious"
def test_bucket_collision(self): h = HashTable(13) h.put('a', 1) # both will use bucket 6 h.put('n', 2) self.assertEqual(h.get('a'), 1) self.assertEqual(h.get('n'), 2)
def test_get_missing_key(): foo = HashTable() with pytest.raises(KeyError): foo.get('bar')
def test_distinct_buckets(self): h = HashTable(13) h.put('a', 1) h.put('b', 2) h.put('c', 3) self.assertEqual(h.get('a'), 1)
def test_duplicate_keys(): hash_table = HashTable(1024) hash_table.set('foo', 1) hash_table.set('foo', 2) assert hash_table.get('foo') == 2
def test_get_simple(): hash_table = HashTable(1024) hash_table.set('pig', 'pig') hash_table.set('sasquatch', 'sasquatch') assert hash_table.get('pig') == 'pig' assert hash_table.get('sasquatch') == 'sasquatch'
def insertaElementoTest(self): h = HashTable() h.put(self.clave,self.valor) self.assertEquals(self.valor,h.get(self.clave))
def formatDataForPlot(dateInterval, sortedData, span): #start by creating empty arrays for the data and and names. this is later returned data = [None]*sortedData[1] names = [None]*sortedData[1] stats = [None]*sortedData[1] #go through the loop for each name for j in range(0,sortedData[1]): #hash table for data entry HT = HashTable(int(span)*365/12) #container for the final data xdata=[] #the start date--(current date) tempdate = dateInterval[0] #loop that goes through each day and fills it in with 0 while tempdate < dateInterval[1]: #id for the hash table ID = hash(str(tempdate)[:4]+str(tempdate)[5:7]+str(tempdate)[8:10]) #fill inn the day with 0 HT.put(ID,0.0) #increment current date tempdate = tempdate + datetime.timedelta(days = 1) #for loop that goes through all the dates in the result set and sets the data for those dates for i in range(0,len(sortedData[0][j])): value = float(sortedData[0][j][i][1]) date = sortedData[0][j][i][3] ID = hash(date[:4]+date[5:7]+date[8:10]) HT.put(ID,value) tempdate = dateInterval[0] #values to find out the min, max, and avr tempMax = -1000000000000 tempMin = 1000000000000 sum = 0 count = 0 tempAVR = 0 #a loop that gets all the values for all the days in the span and puts it into an array while tempdate < dateInterval[1]: count += 1 ID = hash(str(tempdate)[:4]+str(tempdate)[5:7]+str(tempdate)[8:10]) data32 = HT.get(ID) if data32 > tempMax: tempMax = data32 if data32 < tempMin: tempMin = data32 sum += data32 xdata.append(data32) tempdate = tempdate + datetime.timedelta(days = 1) tempAVR = float(sum/float(count)) AVR = "%.2f" % tempAVR #load the data into a new arrays data[j] = xdata names[j] = sortedData[0][j][0][0] stats[j] = [tempMax,tempMin,AVR] #return the formatted data return [data, names, stats]
def test_get_missing_key(): hash_table = HashTable(1024) hash_table.set('pig', 'pig') hash_table.set('sasquatch', 'sasquatch') with pytest.raises(KeyError): hash_table.get('dog')