def gammaDecode(data): ##Uncompressing a compressed posting list and returning a linked list curr = 0 full_list = [] posting_list = LinkedList() byte = "" number = "" total = 0 flag = False for bit in range(len(data)): ##Reading the stream of bits if data[bit] == '1' and flag == False: total += 1 elif data[bit] == '0' and flag == False: flag = True else: number += data[bit] total -= 1 if total == 0: full_list.append( binaryToDecimal(('1' + number), len(number), 0)) number = "" flag = False posting_list.add(full_list[0]) for i in range(1, len(full_list)): posting_list.add(full_list[i] + full_list[0]) return posting_list
def VBDecode(data): ##Uncompressing a compressed posting list and returning a linked list curr = 0 full_list = [] posting_list = LinkedList() lyst = [] byte = "" for bit in data: ##Reading the stream of bits curr += 1 byte += bit if curr == 8: ##If we have read a bit curr = 0 if byte[0] == '1': ##If the current bit is the last byte of saved integer byte = '0' + byte[1:] lyst.append(binaryToDecimal(byte, len(byte) - 1, 0)) byte = "" #print("Lyst:", lyst) #print("End number:", VBDecode_helper(lyst, len(lyst)-1, 0)) full_list.append(VBDecode_helper(lyst, len(lyst) - 1, 0)) lyst = [] else: lyst.append(binaryToDecimal(byte, len(byte) - 1, 0)) byte = "" posting_list.add(full_list[0]) for i in range(1, len(full_list)): posting_list.add(full_list[i] + full_list[0]) return posting_list
def createRandomPosting(): ##Creating a randomly generated posting list of given size ##between given integers SIZE = 3 START = 1 END = 10000 lyst = LinkedList() for i in range(SIZE): lyst.add(random.randint(START, END)) return lyst
def run_noDups(): from linkedList import LinkedList f = open(INPUT_FILE,'r') w = open(OUTPUT_FILE,'w') count = 0 lst = LinkedList() # writes field headers to file w.write('sourceStreetAddress' + "\t" + 'sourceLocality' + "\t" + 'sourceAdministrativeArea' + "\t" + 'sourceCountry' + "\t" + 'resultStatus' + "\t" + 'resultGeomType' + "\t" + 'resultLat' + "\t" + 'resultLon' + "\t" + 'resultFormatted' + "\t" + 'URL' + "\t" + 'note' + "\n") #reads lines from source file street_address = f.readline() print street_address while street_address : if lst.add(street_address): # sends address to function address = getCoord(street_address) # delay for API constraints while address == 'OVER_QUERY_LIMIT': sleep(10) address = getCoord(street_address) # checks for None in geocode result and converts - NOT WORKING if address is None: print str(street_address.strip()) + ": " + str(address) + "\n" w.write(str(address) + "\n".encode('utf-8')) # returns geocode result - NOT WORKING else: print str(street_address.strip()) + ": " + str(address) + "\n" w.write(str(address) + "\n".encode('utf-8')) count += 1 street_address = f.readline() return street_address f.close() w.close() # Count not working print count + "unique coordinates were queried"
def run_noDups(): from linkedList import LinkedList f = open(INPUT_FILE,'r') w = open(OUTPUT_FILE,'w') count = 0 lst = LinkedList() w.write('sourceStreetAddress' + "\t" + 'sourceLocality' + "\t" + 'sourceAdministrativeArea' + "\t" + 'sourceCountry' + "\t" + 'resultStatus' + "\t" + 'resultGeomType' + "\t" + 'resultLat' + "\t" + 'resultLon' + "\t" + 'resultFormatted' + "\t" + 'URL' + "\t" + 'note' + "\n") street_address = f.readline() print street_address while street_address : if lst.add(street_address): address = getCoord(street_address) while address == 'OVER_QUERY_LIMIT': sleep(10) address = getCoord(street_address) if address is None: print str(street_address.strip()) + ": " + str(getReducedCoord(street_address)) + "\n" w.write(str(getReducedCoord(street_address)) + "\n".encode('utf-8')) else: print str(street_address.strip()) + ": " + str(address) + "\n" w.write(str(address) + "\n".encode('utf-8')) count += 1 street_address = f.readline() return street_address f.close() w.close() # Count not working print count + "unique coordinates were queried"
def sum_lists(ll_a, ll_b): n1, n2 = ll_a.head, ll_b.head ll = LinkedList() carry = 0 while n1 or n2: result = carry if n1: result += n1.value n1 = n1.next if n2: result += n2.value n2 = n2.next ll.add(result % 10) carry = result // 10 if carry: ll.add(carry) return ll
def sumLists(A_llist, B_llist): carry_num = 0 node1 = A_llist.head node2 = B_llist.head linked_list = LinkedList() while node1 or node2: result = carry_num # take 1st list num if node1: result += node1.value node1 = node1.next # take 2nd list num and add if node2: result += node2.value node2 = node2.next # add the 1st list and 2nd list sum to a new list linked_list.add(int(result % 10)) carry_num = result / 10 return ll
def intersectWithSkips(lyst1, lyst2): ## Finding the intersection of two linked lists by making use of skip pointers answer = LinkedList() head1 = lyst1._head head2 = lyst2._head while head1 and head2: if head1.getData() == head2.getData(): answer.add(head1.getData()) head1 = head1.getNext() head2 = head2.getNext() elif head1.getData() < head2.getData(): if head1.hasSkip() and head1.getSkipPointer().getData() <= \ head2.getData(): while head1.hasSkip() and head1.getSkipPointer().getData() <= \ head2.getData(): head1 = head1.getSkipPointer() else: head1 = head1.getNext() else: if head2.hasSkip() and head2.getSkipPointer().getData() <= \ head1.getData(): while head2.hasSkip() and head2.getSkipPointer().getData() <= \ head1.getData(): head2 = head2.getSkipPointer()else: head2 = head2.getNext() print("Intersecting indices:", end=" ") head = answer._head while head: print(head.getData(), end=" ") head = head.getNext()
def run_noDups(): from linkedList import LinkedList f = open(INPUT_FILE, 'r') w = open(OUTPUT_FILE, 'w') count = 0 lst = LinkedList() # writes field headers to file w.write('sourceStreetAddress' + "\t" + 'sourceLocality' + "\t" + 'sourceAdministrativeArea' + "\t" + 'sourceCountry' + "\t" + 'resultStatus' + "\t" + 'resultGeomType' + "\t" + 'resultLat' + "\t" + 'resultLon' + "\t" + 'resultFormatted' + "\t" + 'URL' + "\t" + 'note' + "\n") #reads lines from source file street_address = f.readline() print street_address while street_address: if lst.add(street_address): # sends address to function address = getCoord(street_address) # delay for API constraints while address == 'OVER_QUERY_LIMIT': sleep(10) address = getCoord(street_address) # checks for None in geocode result and converts - NOT WORKING if address is None: print str(street_address.strip()) + ": " + str(address) + "\n" w.write(str(address) + "\n".encode('utf-8')) # returns geocode result - NOT WORKING else: print str(street_address.strip()) + ": " + str(address) + "\n" w.write(str(address) + "\n".encode('utf-8')) count += 1 street_address = f.readline() return street_address f.close() w.close() # Count not working print count + "unique coordinates were queried"
def run_noDups(): from linkedList import LinkedList f = open(INPUT_FILE, 'r') w = open(OUTPUT_FILE, 'w') count = 0 lst = LinkedList() nation = f.readline() while nation: if lst.add(nation): coord = getCoord(nation) while coord == 'OVER_QUERY_LIMIT': sleep(1) coord = getCoord(nation) print nation.strip() + ": " + coord + "\n" w.write(coord + "\n") count += 1 nation = f.readline() f.close() w.close() print count + "unique nations were queried"
def run_noDups(): from linkedList import LinkedList f = open(INPUT_FILE,'r') w = open(OUTPUT_FILE,'w') count = 0 lst = LinkedList() nation = f.readline() while nation : if lst.add(nation): coord = getCoord(nation) while coord == 'OVER_QUERY_LIMIT': sleep(1) coord = getCoord(nation) print nation.strip() + ": " + coord + "\n" w.write(coord + "\n") count += 1 nation = f.readline() f.close() w.close() print count + "unique nations were queried"
def run_noDups(): from linkedList import LinkedList f = open(INPUT_FILE, 'r') w = open(OUTPUT_FILE, 'w') count = 0 lst = LinkedList() w.write('sourceStreetAddress' + "\t" + 'sourceLocality' + "\t" + 'sourceAdministrativeArea' + "\t" + 'sourceCountry' + "\t" + 'resultStatus' + "\t" + 'resultGeomType' + "\t" + 'resultLat' + "\t" + 'resultLon' + "\t" + 'resultFormatted' + "\t" + 'URL' + "\t" + 'note' + "\n") street_address = f.readline() print street_address while street_address: if lst.add(street_address): sleep(1) address = getCoord(street_address) while address == 'OVER_QUERY_LIMIT': sleep(10) address = getCoord(street_address) if address is None: print str(street_address.strip()) + ": " + str( getReducedCoord(street_address)) + "\n" w.write( str(getReducedCoord(street_address)) + "\n".encode('utf-8')) else: print str(street_address.strip()) + ": " + str(address) + "\n" w.write(str(address) + "\n".encode('utf-8')) count += 1 street_address = f.readline() return street_address f.close() w.close() # Count not working print count + "unique coordinates were queried"
def createPartition(valLinkedList, partition): vFirstPartition = LinkedList() vNode = valLinkedList.head vSecondPartition = LinkedList() while (vNode != None): if (vNode.value < partition): #add the node to the vFirstPartition vFirstPartition.add(vNode) elif (vNode.value > partition): #print(vNode) vSecondPartition.add(vNode) elif (vNode.value == partition and vNode.next == None): vSecondPartition.add(Node) vNode = vNode.next #print(vFirstPartition) #print(vSecondPartition) return vFirstPartition.mergeLists(vSecondPartition)
p2 = linkedlist.head.next while p2 != None: """ beautiful solution!!!""" # two runner pointer p1 and p2, where p2 = p1.next, if p1.value > p2.value, assign p2 to head, so head always point to smaller part! if p2.value < x: p1.next = p2.next p2.next = linkedlist.head linkedlist.head = p2 p2 = p1.next else: p1 = p1.next p2 = p1.next #----------------test----------------- aList = LinkedList() aList.add(6) aList.add(6) aList.add(3) aList.add(7) aList.add(6) aList.add(1) aList.add(9) aList.add(8) aList.add(2) x = 5 print aList, " , x=5" partition(aList, x) print aList
''' Implement an algorithm to delete a node in the middle (i.e. any node but the first and last node, not necessarily the exact middle) of a singly linked list, given only access to that node. ''' from linkedList import LinkedList def delete_middle_node(node): node.value = node.next.value node.next = node.next.next ll = LinkedList() ll.addSeveral([1, 2, 3, 4]) middle_node = ll.add(5) ll.addSeveral([7, 8, 9]) print(ll) delete_middle_node(middle_node) print(ll)
while(tmp.next!=pivot): tmp = tmp.next tmp.next = None # need to break the link for the first half newHead = quicksortHelper(newHead,tmp) tmp = getTail(newHead) # the tail of the first half should be re-fetch tmp.next = pivot if newTail!=pivot: pivot.next = quicksortHelper(pivot.next,newTail) return newHead def quickSort(head): if head: tail = getTail(head) return quicksortHelper(head,tail) aList = LinkedList() aList.add(9) aList.add(4) aList.add(8) aList.add(3) aList.add(7) aList.add(6) print aList print aList.displayLinkedListFromNode(aList.head.next) print aList.displayLinkedListFromNode(quickSort(aList.head))
from linkedList import LinkedList l = LinkedList() l.add("ahoj") l.add("mami") print(l) print(l.pop(1)) print(l.peek()) print(l.peek())
def main(): #init myList = LinkedList() #test isEmpty() print "isEmpty: " + str(myList.isEmpty()) print "True if the initialized list is empty\n" #test size print "size: " + str(myList.size()) print "Size of empty list should be 0\n" #test add myList.add(5) print "isEmpty: " + str(myList.isEmpty()) print "Should be false since the list is no longer empty\n" print "size: " + str(myList.size()) print "Size of list should now be 1 after adding one element\n" myList.add(4) myList.add(3) myList.add(2) myList.add(1) myList.add(0) print "size: " + str(myList.size()) print "Size of list should now be 6 after adding five more elements\n" #test search print "search: " + str(myList.search(4)) print "Search should return true since 4 is an element of the list\n" print "search: " + str(myList.search(6)) print "Search should return false since 6 is not an element of the list\n" #test remove myList.remove(5) print "size: " + str(myList.size()) print "Size of list should now be 4 after removing an element\n" print "search: " + str(myList.search(5)) print "Search should return false since 5 is no longer an element of the list\n" #test printList print "printList:" myList.printList() print "Should print [0,1,2,3,4,5]" #test findAll print "findAll:" myList.findAll(6) print "Shoudn't find anything since 6 is not in the list\n" print "findAll:" myList.findAll(2) print "Should return [2] since there is only one element 2 in the index 2\n" myList.add(2) print "findAll:" myList.findAll(2) print "Should return [0,3] since another 2 was added to the head of the list\n" #test insert print "printList:" myList.printList() print "list before insertion\n" myList.insert(1, 1) myList.insert(7, 5) print "printList:" myList.printList() print "should return [2,1,0,1,2,3,4,5] after inserting 1 in position 1 and 5 in position 7(at the end)\n" #test popAtIndex print "popAtIndex: " + str(myList.popAtIndex(2)) print "should return 0 since the element at position 2 was popped\n" print "printList:" myList.printList() print "list should now be [2,1,1,2,3,4,5] after popping 0\n" #test appendLast myList.appendLast(6) print "printList:" myList.printList() print "list should now be [2,1,1,2,3,4,5,6] after appending 6 using appendLast\n"