def test_union_edge_cases_1(self): llist_1 = linkedlist.LinkedList() llist_2 = linkedlist.LinkedList() element_1 = [] element_2 = [] [llist_1.add(i) for i in element_1] [llist_2.add(i) for i in element_2] llist_u = linkedlist.union(llist_1, llist_2) s = set(element_1).union(set(element_2)) self.assertEqual({n for n in llist_u}, s)
def test_union(self): llist_1 = linkedlist.LinkedList() llist_2 = linkedlist.LinkedList() for n in llist_1: print(n) element_1 = [3, 2, 4, 35, 6, 65, 6, 4, 3, 21] element_2 = [6, 32, 4, 9, 6, 1, 11, 21, 1] [llist_1.add(i) for i in element_1] [llist_2.add(i) for i in element_2] llist_u = linkedlist.union(llist_1, llist_2) s = set(element_1).union(set(element_2)) self.assertEqual({n for n in llist_u}, s)
def test_intersection(self): llist_1 = linkedlist.LinkedList() llist_2 = linkedlist.LinkedList() element_1 = [3, 2, 4, 35, 6, 65, 6, 4, 3, 23] element_2 = [1, 7, 8, 9, 11, 21, 1] intersection = set(element_1).intersection(set(element_2)) [llist_1.add(i) for i in element_1] [llist_2.add(i) for i in element_2] llist_i = linkedlist.intersection(llist_1, llist_2) self.assertEqual({el for el in llist_i}, intersection)
def test(list): source = linkedlist.LinkedList() for item in list: source.append(item) list1, list2 = split(source.head) ll1 = linkedlist.LinkedList() ll2 = linkedlist.LinkedList() ll1.head = list1 ll2.head = list2 print('L1', end = '=') ll1.print() print('L2', end = '=') ll2.print() print()
def process(afile, deps, table, workq): """ search `afile' for lines of the form #include "xxx.h" for each one found, append the filename to `deps' if the filename not found in `table', insert it with empty dependency list and append it to workq """ f = open_file(afile) if f == None: print("Unable to open file:", afile) return for line in f: l = line.strip() # remove whitespace if l.startswith('#include'): # found #include line l = l[8:].lstrip() # remove whitespace to filename if l[0] == '"': # non-system include file li = [] for i in range(1, len(l)): if l[i] == '"': break li.append(l[i]) fn = ''.join(li) # create string from list deps.add_last(fn) # add to dependencies for afile if fn not in table: table[fn] = LL.LinkedList() # add to table workq.add_last(fn) # add to work queue f.close() return
def parser(self): """ Uses the string that was given to the constructor method to create a Linked List. Returns ------- LinkedList a list of all words from the input sentence including their type and polarity value. """ linkedList = linkedlist.LinkedList() #Creates an empty Linked List sentence_list = self.sentence.split() sentence_list.reverse() #Check the corresponding type for each word that was in the sentence by using the Lexicon class lexicon_obj = lexicon_parser.Lexicon() lexicon_obj.createLexicon() for sentence_word in sentence_list: for lexicon_word, lexicon_type in lexicon_obj.lexicon.items(): if sentence_word == lexicon_word: #Let 0 be an output polarity and 1 be an input polarity linkedList.add((sentence_word, lexicon_type, 1)) else: None node = linkedList.root linkedList.add((None, self.resultingType, 0)) #Adds the resulting type to the list return linkedList
def test_find(self): ll = linkedlist.LinkedList() original_len = 10 for i in range(original_len): ll.add(i) self.assertEqual(ll.find(2), 2)
def test_add_one(self): linkedlist = ll.LinkedList() linkedlist.add_value(1) first = linkedlist.get_first() self.assertEquals(1, first.value) self.assertEquals(1, linkedlist.get_size())
def __init__(self, arreglo): self.arreglo = arreglo self.maximo = len(self.arreglo) - 1 self.menor = math.factorial(len(arreglo)) self.heuristica = self.calculoH(self.arreglo) self.cantidad = 0 self.rate = 50 # Agregamos las reinas en el tablero self.ponerReinas(self.arreglo) self.pares = {} for i in range(0, len(arreglo)): self.pares[i] = linkedlist.LinkedList() self.printTablero() print("H principal = " , self.calculoH(self.arreglo)) i = 0 while (self.heuristica != 0): self.seleccionPorTorneos((self.maximo-(int(self.maximo/2)))**2) self.heuristica = self.calculoH(self.arreglo) print("H[", i, "] = ", self.heuristica, " ", self.arreglo) #print("movemos un slot") self.moverUno() i = i + 1 self.ponerReinas(self.arreglo) self.printTablero() print(self.arreglo, " H = 0")
def test_delete_middle(self): linkedlist = ll.LinkedList() initial = [1, 12, 3, 45] for x in initial: linkedlist.add_value(x) linkedlist.delete_value(3) self.assertEquals(3, linkedlist.get_size()) self.assertEquals([1, 12, 45], linkedlist.get_all())
def test_remove(self): ll = linkedlist.LinkedList() original_len = 10 for i in range(original_len): ll.add(i) ll.remove(3) self.assertEqual(len(ll), 9) self.assertFalse(2 in ll)
def test_insert(self): ll = linkedlist.LinkedList() original_len = 10 for i in range(original_len): ll.add(i) ll.insert(3, 33) self.assertEqual(len(ll), 11) self.assertTrue(33 in ll)
def test_delete_dups(self): linkedlist = ll.LinkedList() initial = [1, 12, 3, 45, 3, 78, 90, 12, 3, 45] for x in initial: linkedlist.add_value(x) linkedlist.remove_duplicates_no_buffer() expected = [1, 12, 3, 45, 78, 90] self.assertEquals(expected, linkedlist.get_all())
def mejorCamino(self): menorValor = 0 ultimaPosicion = -555 # En caso de que se necesite ir a la ultima posición, se la guarda. seEncontroComparacion = False seEncontroPosicion = False for i in range(0, len(self.agent.decisiones)): if (self.agent.decisiones[i] != -1): if (self.agent.ultimaPosicion != i): #print("Empezando por ",i) menorValor = i seEncontroComparacion = True break else: ultimaPosicion = i if (seEncontroComparacion == False): menorValor = ultimaPosicion for i in range(menorValor + 1, len(self.agent.decisiones)): if (self.agent.decisiones[i] < self.agent.decisiones[menorValor] and self.agent.decisiones[i] != -1): if (self.agent.ultimaPosicion != i): menorValor = i seEncontroPosicion = True else: #print("Guardando ultimaposicion: ", i) ultimaPosicion = i if (seEncontroPosicion == False): if (seEncontroComparacion == False): #print("Se necesita la ultima posicion: ", i, ", reemplazando: ", menorValor) menorValor = ultimaPosicion self.guardarUltimaUbicacion(menorValor) if (menorValor == 0): #print("arriba", " menorvalor: " , menorValor) self.agent.posicionX = self.agent.posicionX - 1 elif (menorValor == 1): # print("abajo", " menorvalor: " , menorValor) self.agent.posicionX = self.agent.posicionX + 1 elif (menorValor == 2): #print("izq", " menorvalor: " , menorValor) self.agent.posicionY = self.agent.posicionY - 1 else: # print("der" , " menorvalor: " , menorValor) self.agent.posicionY = self.agent.posicionY + 1 # Las siguientes LinkedList son utilizadas para guardar las posiciones donde debe mostrarse una flecha por donde pasa el Agente. L = linkedlist.LinkedList() newNode = linkedlist.Node() newNode.value = self.agent.posicionX L.head = newNode newNode2 = linkedlist.Node() newNode2.value = self.agent.posicionY L.head.nextNode = newNode2 newNode3 = linkedlist.Node() newNode3.value = menorValor L.head.nextNode.nextNode = newNode3 linkedlist.add(self.caminoHecho, L)
def test_delete_multiple_same_value(self): linkedlist = ll.LinkedList() initial = [1, 12, 3, 45, 3] for x in initial: linkedlist.add_value(x) linkedlist.delete_value(3) self.assertEquals(4, linkedlist.get_size()) self.assertEquals([1, 12, 45, 3], linkedlist.get_all())
def test_pop(self): ll = linkedlist.LinkedList() original_len = 10 for i in range(original_len): ll.add(i) for i in range(5): value = ll.pop() self.assertEqual(value, ((original_len - 1) - i)) self.assertEqual(len(ll), 5)
def test_add_multiple(self): linkedlist = ll.LinkedList() expected = [1, 12, 3, 45] for x in expected: linkedlist.add_value(x) result = linkedlist.get_all() self.assertEquals(4, linkedlist.get_size()) self.assertEquals(expected, result)
def main(): ll = linkedlist.LinkedList() for i in xrange(50): ll.append("test") ll.append("hai") ll.append("test") ll.populate("db.txt", "\n") print ll
def test_empty(self): a = "" ans = _( "When adding nothing get_reverse is supposed to return an empty string and you returned {}. \n You should watch you behaviour in case of empty lists." ) for i in range(len(a)): stu_list = linkedlist.LinkedList() stu_ans = stu_list.get_reverse() corr_list = corr.LinkedList() corr_ans = corr_list.get_reverse() self.assertEqual(corr_ans, stu_ans, ans.format(stu_ans))
def test_basic_operations(): #test making a list and populating it list1 = LS.LinkedList() list1.add("tristen") assert list1[0].value == "tristen" for x in range(100): list1.add(x) #create new list for comparison list2 = LS.LinkedList() list2.add("Me") assert list1.equals(list2) == False #create third list for more comparison of equal lists list3 = LS.LinkedList() list3.add("Me") assert list2.equals(list3)
def Partition1(l, x): left_part = linkedlist.LinkedList() right_part = linkedlist.LinkedList() last_node_left = None current = l.head while current: if current.value < x: left_part.add(current.value) if last_node_left: last_node_left = last_node_left.next else: last_node_left = left_part.head.next else: right_part.add(current.value) current = current.next if last_node_left: last_node_left.next = right_part.head return left_part else: return right_part
def __init__(self, grilla, agent): self.grilla = grilla self.agent = agent #print("A = [",self.agent.posicionX,"][",self.agent.posicionY,"]") self.grilla.grilla[agent.posicionX][agent.posicionY] = -2 self.grilla.grilla[agent.objetivoPosicionX][ agent.objetivoPosicionY] = -3 self.grilla.generarObstaculos() self.grilla.mostrarGrilla() self.caminoHecho = linkedlist.LinkedList() self.comenzarCamino()
def get_all_users_ascending(): users = User.query.all() all_users_ll = linkedlist.LinkedList() for user in users: all_users_ll.insert_at_end({ 'id': user.id, 'name': user.name, 'email': user.email, 'address': user.address, 'phone': user.phone }) return jsonify(all_users_ll.to_list()), 200
def DisplayAllStudentRecord(self): self.student = linkedlist.LinkedList() self.getAllStudentdno() self.student.head = self.student.mergeSort(self.student.head) #LINEAR SEARCH for i in range(self.student.getCount()): for line in open("students.txt", "r").readlines(): data = line.split(',') if (self.student.returnNthfromfirst(i) == data[0]): print("\n\t" + data[0] + " | " + data[1]) str(input("\n\nPress Any Key To Go Back To The main menu\n\n")) stack.pop()()
def get_one_user(user_id): users = User.query.all() all_users_ll = linkedlist.LinkedList() for user in users: all_users_ll.insert_beginning({ 'id': user.id, 'name': user.name, 'email': user.email, 'address': user.address, 'phone': user.phone }) user = all_users_ll.get_user_by_id(user_id) return jsonify(user), 200
class Bheap: """ Estructura Bheap, lo unico que tiene una referencia a un lista. """ bheaplist=linkedlist.LinkedList() def __str__(self): """ Permite hacer un print a una estructura Bheap """ str_list="" current=self.bheaplist.head.nextNode while current!=None: str_list= str_list+str(current.value)+" " current=current.nextNode return(str_list)
def LevelOrder(self, node): count = 0 nodelist = ll.LinkedList() nodelist.addlast(node) while nodelist.head is not None: count += 1 node = nodelist.head.data print(node.data) if node.left is not None: nodelist.addlast(node.left) if node.right is not None: nodelist.addlast(node.right) nodelist.removekey(node, 1)
def put_with_separate_chaining(hashtable, key, value): """ Adds the key value to the table and resolves collisions with separate chaining, meaning that array[hash] contains a linked list of all the key values which keys' hash are the same.""" key_hash = hashtable.hash_function(key) if hashtable.array[key_hash] is None: # Key, value, pointer to next node # This is a basic linked list implem hashtable.array[key_hash] = linkedlist.LinkedList() hashtable.array[key_hash].add_value((key, value)) else: ll = hashtable.array[key_hash] ll.add_value((key, value))
def test_reverse(self): randomList = [ ''.join( random.choice(string.ascii_letters + string.digits) for _ in range(random.randint(1, 10))) for _ in range(random.randint(1, 10)) ] ans = _("The answer should be {} and you returned {}.") stu_list = linkedlist.LinkedList() corr_list = corr.LinkedList() for i in range(len(randomList)): stu_list.add(i) corr_list.add(i) corr_ans = corr_list.get_reverse().replace(" ", "") stu_ans = stu_list.get_reverse().replace(" ", "") self.assertEqual(corr_ans, stu_ans, ans.format(corr_ans, stu_ans))
def __init__(self, arreglo): self.segundos = time.time() self.arreglo = arreglo self.maximo = len(self.arreglo) - 1 self.menor = math.factorial(len(arreglo)) self.heuristica = self.calculoH(self.arreglo) # Agregamos las reinas en el tablero self.ponerReinas(self.arreglo) self.pares = {} self.estados = 0 for i in range(0, len(arreglo)): self.pares[i] = linkedlist.LinkedList() self.printTablero() print("H principal = ", self.calculoH(self.arreglo)) self.calculoHs()