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') # Key does not exist
def Parser(file_name): """ Parses the xmlbif file and builds the list of variables. Also builds the list condiotional probability tables. Also couple hash tables are built to quickly access things. """ tree = ET.parse(file_name) root = tree.getroot() n_prime = 67 Hash_Variables = HashTable(n_prime) Hash_CPT = HashTable(n_prime) Hash_Nodes = HashTable(n_prime) variables = [] cp_tables = [] if root.tag != "BIF": print "file must be xmlbif" else: root = root[0] if root.tag != "NETWORK": print "file must have a NETWORK tag" else: for i in xrange(1, len(root)): if root[i].tag == "VARIABLE": domain = getDomain(root[i]) rv = RandomVariable(root[i][0].text, domain) variables.append(rv) Hash_Variables.put(rv.name, rv) elif root[i].tag == "DEFINITION": cpt = CPT() for j in xrange(0, len(root[i])): if root[i][j].tag == "FOR": cpt.add_for_variable( Hash_Variables.get(root[i][j].text)) elif root[i][j].tag == "GIVEN": cpt.add_given_variable( Hash_Variables.get(root[i][j].text)) elif root[i][j].tag == "TABLE": cpt.build_table() split_text = root[i][j].text.split(" ") for t in xrange(0, len(split_text)): try: p = float(split_text[t]) cpt.add_prob(p) except ValueError: ad = 3 cp_tables.append(cpt) Hash_CPT.put(cpt.name, cpt) return variables, cp_tables, Hash_Variables, Hash_CPT, Hash_Nodes
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 find_itinerary(tickets): ht = HashTable() ht.store([(x[1], x[0]) for x in tickets]) for t in tickets: if not ht.get(t[0]): start = t[0] break ht.store(tickets) result = [] while ht.get(start): result.append((start, ht.get(start))) start = ht.get(start) return result
def Parser(file_name): """ Parses the xmlbif file and builds the list of variables. Also builds the list conditional probability tables. Also couple hash tables are built to quickly access things. """ tree = ET.parse(file_name) root = tree.getroot() n_prime = 67 Hash_Variables = HashTable(n_prime) Hash_CPT = HashTable(n_prime) Hash_Nodes = HashTable(n_prime) variables = [] cp_tables = [] if root.tag != "BIF": print "file must be xmlbif" else: root = root[0] if root.tag != "NETWORK": print "file must have a NETWORK tag" else: for i in xrange(1, len(root)): if root[i].tag == "VARIABLE": domain = getDomain(root[i]) rv = RandomVariable(root[i][0].text, domain) variables.append(rv) Hash_Variables.put(rv.name,rv) elif root[i].tag == "DEFINITION": cpt = CPT() for j in xrange(0, len(root[i])): if root[i][j].tag == "FOR": cpt.add_for_variable(Hash_Variables.get(root[i][j].text)) elif root[i][j].tag == "GIVEN": cpt.add_given_variable(Hash_Variables.get(root[i][j].text)) elif root[i][j].tag == "TABLE": cpt.build_table() split_text = root[i][j].text.split(" ") for t in xrange(0, len(split_text)): try: p = float(split_text[t]) cpt.add_prob(p) except ValueError: ad = 3 cp_tables.append(cpt) Hash_CPT.put(cpt.name,cpt) return variables, cp_tables, Hash_Variables, Hash_CPT, Hash_Nodes
def testHashTable(self): hash = HashTable() hash.add("march 6", 130) hash.add("march 7", 783) hash.add("march 8", 130) hash.add("march 17", 63457) self.assertEqual(hash.exists("march 8"), True) self.assertEqual(hash.get("march 6"), 130) self.assertEqual(hash.get("march 17"), 63457) hash.add("march 17", 23421) self.assertEqual(hash.remove("march 17"), ('march 17', 23421)) self.assertEqual(hash.exists("march 17"), False)
def test_resize2(self): m = 11 h = HashTable(m=m) for i in range(m - 2): h.insert(i, i + 1) assert h.get(5) == 6
def union(l1, l2): ht = HashTable() ht.store(l1) result = set(l1) for l in l2: if not ht.get(l[0]): result.add(l) return list(result)
def find_pair(array, x): ht = HashTable() ht.store(array) result = [] for i in array: if ht.get(x - i[1]): result.append((i[1], x - i[1])) return result
def test_remove1(self): m = 11 h = HashTable(m=m) for i in range(m): h.insert(i, i + 1) h.remove(5) assert h.get(5) == None
def test_hash_table_perfomance(self): ht = HashTable() tl = 0 for y in range(1, 2): keys = list(set([randrange(500000) for x in range(1, 500000)])) values = [randrange(500000) for x in range(1, y)][:len(keys)] tl += len(keys) array = [(key, value) for key, value in zip(keys, values)] ht.store(array) for el in array: self.assertEqual(ht.get(el[0]), el[1]) print(tl)
class PriorityQueue: def __init__(self): # self.current_size = 0 # self.queue = LinkedList() self.queue = [] self.dictionary = HashTable() heapq.heapify(self.queue) self.current_size = 0 def isEmpty(self): if self.current_size == 0: return True return False def enqueue(self, data): # self.queue.add(data) # heapq.heapify(self.queue) score, item = data score *= -1 # invert to make a max heap heapq.heappush(self.queue, score) self.dictionary.put(score, item) self.current_size += 1 def dequeue(self): if self.current_size == 0: raise IndexError("Queue is empty") # front = self.queue[0] # self.queue.delete(0) top = heapq.heappop(self.queue) item = self.dictionary.get(top) self.current_size -= 1 return item def peek(self): front = self.queue[0] return front def size(self): return self.current_size
from HashTable import HashTable H = HashTable() H.insert('hello', 'goodbye') H.insert('f', 'g') hello = H.get('hello') g = H.get('f') print(hello) print(g) H.remove('hello') hello = H.get('hello') print(hello)
def intersect(l1, l2): ht = HashTable() ht.store(l1) return [l for l in l2 if ht.get(l[0])]
def testGet(): states = HashTable(100) states.add("Texas", "Austin") assert states.get("Texas") == "Austin"
class KnowledgeBase(object): """ This represents the Knowledge Base object. It contains a python list of sentences. A sentence is a particular object created for this project. It contains a Hash Table that relates a symbol's alphabetical name to a randomly chosen id. It also contains a python list of models, where each element is a particular model and includes all 2^n possible models. A model is an object created for this project that represents one possible set of values for the symbols. """ def __init__(self): self.sentences = [] # will be a list of sentences or clauses self.SymbolTable = HashTable(13) # will be a HashTable that relates strings to Symbols def delete_last_sentence(self): self.sentences.pop() def build_models(self): """ This will build every possible model where there are 2^n number of models """ scount = self.SymbolTable.key_count symbol_int_list = self.SymbolTable.list_of_ints self.ModelTable = ModelTable(scount, symbol_int_list) self.model_list = [None]*(2**scount) for i in xrange(0, 2**scount): self.model_list[i] = Model(self.ModelTable.Table[i]) def intern(self,name): """ This places a specific symbol into a HashTable that makes it easier to verify equality. """ k = self.SymbolTable.get(name) if k == -1: self.SymbolTable.put(name) return Symbol(name) def add(self, sentence): """ This will add a sentence to the knowledge base. A more complicated sentence like a implication is added also using this. """ self.sentences.append(sentence) def find_KB_models(self): """ This will return a list of the indexes of every model that satisfies the knowledge base """ list_of_verified_models = [] for i in xrange(0, np.size(self.model_list)): check = True for j in xrange(0, len(self.sentences)): if check: check = check and self.sentences[j].isSatisfiedBy(self.model_list[i].model, self.SymbolTable) if check: list_of_verified_models.append(i) return list_of_verified_models def verify_alpha(self, sentence_verify): """ This will return a list of the indexes of every model that satsifies the alpha sentence. """ list_of_verified_models = [] for i in xrange(0, np.size(self.model_list)): if sentence_verify.isSatisfiedBy(self.model_list[i].model, self.SymbolTable): list_of_verified_models.append(i) return list_of_verified_models def walk_SAT(self, p, max_flips): """ This is uses the WalkSAT method to find a model that satisfies all the conditions. If no models satsify all the conditions, then it returns false. This first randomly chooses a model. Then checks if it satisfies all the clauses. If it does not, then it selecs a random clause that is not satsfied. Then with a probability of p it selects a random symbol in that clause and flips it. If the probability p is not randomly satisfied, then it uses flips whichever symbol maximizes the number of satisfied clauses. It loops through 10,000 times, and if no model satisfies everything it assumes that, entailment is true. If it does find a model, then entailment is false. """ k = np.random.randint(0, len(self.model_list)) # randomly chosen model to start model = self.model_list[k].model for i in xrange(0, max_flips): check = True list_of_unsatisfied_clauses = [] for j in xrange(0, len(self.sentences)): clause_value = self.sentences[j].isSatisfiedBy(model, self.SymbolTable) check = check and clause_value if not(clause_value): list_of_unsatisfied_clauses.append(j) if check: # a model satisfies the sentence print "a model satisfies the sentences" print model return True else: k = np.random.randint(0, len(list_of_unsatisfied_clauses)) index_of_sentence = list_of_unsatisfied_clauses[k] list_of_symbols = self.get_all_symbols_from_sentence(self.sentences[index_of_sentence]) list_of_symbol_ids = self.get_symbol_ids(list_of_symbols) if np.random.rand() < p: # randomly choose a symbol in the selected clause k = np.random.randint(0, len(list_of_symbols)) id = list_of_symbol_ids[k] # flip sign of selected symbol where = np.where(model == id)[0][0] model[where][1] = (model[where][1] + 1) % 2 else: id_of_symbol_chosen = self.get_maximize_satisfied_clauses(list_of_symbol_ids, model) print "No model was found" return False def test(self): print self.walk_SAT(.5, 1000) def get_maximize_satisfied_clauses(self, list_of_symbol_ids, model): number_of_satisfied_clauses = np.zeros(len(list_of_symbol_ids), dtype=np.int16) for i in xrange(0, len(list_of_symbol_ids)): temp_model = np.copy(model) self.flip_sign(temp_model,list_of_symbol_ids[i]) for j in xrange(0, len(self.sentences)): if self.sentences[j].isSatisfiedBy(temp_model, self.SymbolTable): number_of_satisfied_clauses[i] += 1 index_of_max = np.argmax(number_of_satisfied_clauses) return list_of_symbol_ids[index_of_max] def flip_sign(self, temp_model, id): where = np.where(temp_model == id)[0][0] temp_model[where][1] = (temp_model[where][1] + 1) % 2 def get_symbol_ids(self, list_of_symbols): list_of_symbol_ids = [] for i in xrange(0, len(list_of_symbols)): list_of_symbol_ids.append(self.SymbolTable.get(list_of_symbols[i])) return list_of_symbol_ids def get_all_symbols_from_sentence(self, sen): if sen.grammar_type < 2: return [sen.sentence.atom.name] return self.get_all_symbols_from_sentence(sen.sentenceLHS) + self.get_all_symbols_from_sentence(sen.sentenceRHS)
#Creer l'alphabet alphabet = list(alphabet) with io.open('input.txt','r',encoding='utf8') as f: inputTxt = f.read() #retire les ponctuations phrase = ''.join( char for char in inputTxt if char not in '.,:;!?\'\"').split(" ") correctedInput = '' #regarde chaque mot du text input for mot in phrase: #Si mot correctement orthographier (i.e est dans le dictionnaire), passer au prochain mot if hashTable.get(mot.lower()): correctedInput += ' ' +mot continue correctedInput += ' [' + mot+ '](' #si mal orthagraphier, creer des suggestion d'orthographes possibles suggestion = Suggestion(mot, alphabet) suggestion.createSuggestion() bonneSuggestion = [] #verifier si suggestion est bonne for motPotentiel in suggestion.tab: if hashTable.get(motPotentiel): bonneSuggestion.append(motPotentiel)
def test_insert1(self): h = HashTable() h.insert(5, 10) assert h.get(5) == 10
def test_get_empty1(self): h = HashTable() assert h.get(5) == None
from HashTable import HashTable from Permutations import Permutations ht = HashTable() ht.add(1) ht.add(2, {1:"Kapil", 2:"KKA"}) print ht.get(1) print ht.get(2) print ht.get(3) print ht.get(2) a = [1, 2, 3, 4] p = Permutations(a) p.dummy() p.permuter()
rows.append(location) keyCol = 0 for key, value in item.items(): # we can quit if we have reached the column which represents the same address as our row if key == location: break if keyCol >= 2 and key == columns[keyCol]: # add to our distances each value which is associated with a specific key # our columns are ahead by 2 because of the distance and hubs columns within the item dictionary # subtracting 2 from keyCol value will allow a match of address values by row and col. # SPACE COMPLEXITY: O(m*n) # assignment of distance hash table values is multi-linear O(M * N) for each column(n) in the dictionary # of the current loop a value will be assigned to the hashtable row(m) distances.get(rows[keyCol - 2]).append( [location, value]) # pull data vertically and add to hash table distances.get(location).append( [rows[keyCol - 2], value]) # pull data horizontally and add to hashtable # prepare to pull next address data for hashtable # advance col to next address keyCol += 1 # index is keeping track of which column/row combination we should be on index += 1 packages = HashTable(40) # TOTAL TIME COMPLEXITY FOR CODE BLOCK: O(m*n) multi-linear # TOTAL SPACE COMPLEXITY FOR CODE BLOCK: O(m*n) multi-linear
from HashTable import HashTable h = HashTable() h['a'] = 'teste a' h[44] = 'teste 44' h['b'] = 'teste b' h[31] = 'teste 31' h['c'] = 'teste c' h[14] = 'teste 14' h['e'] = 'teste e' h[84] = 'teste 84' h['f'] = 'teste f' h[71] = 'teste 71' print(h) print(h.get('a')) del h[71] print(h)
def test_simple_hash_table(self): ht = HashTable() array = [(x, 10 - x) for x in range(10)] ht.store(array) for x in range(10): self.assertEqual(ht.get(x), 10 - x)
from HashTable import HashTable newHashTable = HashTable(10) newHashTable.insert(78, 'Dovah') newHashTable.insert(8, 'Geralt') newHashTable.insert(9, 'Snake') newHashTable.insert(91, 'Quiet') newHashTable.insert(8, 'Viper') print(newHashTable.get(8))
class MinHeap: """ MinHeap class """ def __init__(self, max_size): """ Constructor """ # Size variables self.max_size = max_size self.size = 0 # Heap self.heap = [[]] * (self.max_size + 1) self.heap[0] = [-1 * sys.maxsize] # HashTable self.hash_table = HashTable() def get_parent(self, index): """ Returns the parent index """ return index // 2 def get_left_child(self, index): """ Returns the left child index """ return 2 * index def get_right_child(self, index): """ Returns the right child index """ return 1 + (2 * index) def is_leaf(self, index): """ Determines whether index is a leaf or not """ return self.size // 2 <= index < self.size def swap(self, index_1, index_2): """ Swaps two indexes """ self.heap[index_1], self.heap[index_2] = self.heap[index_2], self.heap[index_1] # Swap heap self.hash_table.add_range([[self.heap[index_1][1], index_1], [self.heap[index_2][1], index_2]]) # Update HashTable def print(self): """ Prints the heap """ print(self.heap) def get_heap(self): """ Returns the heap """ return self.heap def heapify(self, index): """ Heapifies the heap """ # Get left & right children left_child, right_child = self.get_left_child(index), self.get_right_child(index) if (left_child < self.size and right_child < self.size and not self.is_leaf(index)): swapper = left_child if (self.heap[left_child][0] < self.heap[right_child][0]) else right_child self.swap(swapper, index) # Swap self.heapify(swapper) # Recursive here! def add(self, value, node): """ Adds a value to the heap """ if self.size >= self.max_size: print("Max Size Reached!") return self.size += 1 # Increment size self.heap[self.size] = [value, node] # Set value self.hash_table.add(node, self.size) # Add to HashTable current = self.size while self.heap[current][0] < self.heap[self.get_parent(current)][0]: self.swap(current, self.get_parent(current)) current = self.get_parent(current) def add_range(self, items): """ Adds a list of node/values into the heap """ for node in items.keys(): self.add(items[node], node) def pop(self): """ Pops the minimum value in the heap """ popped = self.heap[1][0] self.heap[1] = self.heap[self.size] self.size -= 1 self.heapify(1) return popped def get_min(self): """ Returns the minimum value in the heap """ return self.heap[1] def clear(self): """ Clears the heap """ # Reset HashTable self.hash_table.clear() # Reset heap self.size = 0 self.heap = [0] * (self.max_size + 1) self.heap[0] = [-1 * sys.maxsize] def update(self, node, new_value): """ Updates a value in the heap """ index = self.hash_table.get(node) # Get index self.remove(index) # Remove the spesific index self.add(new_value, node) # Add the new value def print_hash_table(self): """ Prints the hash table """ print(self.hash_table.table) def get_node_min(self): """ Returns the node id for the minimum value in the heap """ min = self.get_min() return min[1] def remove(self, index): """ Removes a spesific index """ index_to_delete = self.heap[index] self.heap[index] = self.heap[self.size] self.size -= 1 self.heapify(index) # Heapify return index_to_delete[1]