def pivot(ll, k): lower_head = LL.Node('dummy') equal_head = LL.Node('dummy') higher_head = LL.Node('dummy') lower = lower_head equal = equal_head higher = higher_head cur = ll.head while (cur): if cur.data < k: lower.nextNode = cur lower = lower.nextNode elif cur.data == k: equal.nextNode = cur equal = equal.nextNode else: higher.nextNode = cur higher = higher.nextNode cur = cur.nextNode higher.nextNode = None lower.nextNode = equal_head.nextNode equal.nextNode = higher_head.nextNode ll.head = lower_head.nextNode return repr(ll)
def test_linked_list_insert(self): temp_list = [12, 23, 34, 45, 56, 67, 78, 89] test_llist = ll.LinkedList() for i in temp_list: test_llist.add_in_tail(ll.Node(i)) # insert into begin: item_insert = 10 test_llist.insert(None, ll.Node(item_insert)) temp_list.insert(0, item_insert) self.compare_list_llist(temp_list, test_llist) self.assertEqual(temp_list[0], test_llist.head.value) # insert into middle: item_insert = 39 test_llist.insert(test_llist.find(34), ll.Node(item_insert)) temp_list.insert(4, item_insert) self.compare_list_llist(temp_list, test_llist) # insert into end: item_insert = 99 test_llist.insert(test_llist.find(89), ll.Node(item_insert)) temp_list.append(item_insert) self.compare_list_llist(temp_list, test_llist) self.assertEqual(temp_list[-1], test_llist.tail.value) # insert into empty linked list: test_llist.clean() test_llist.insert(test_llist.find(89), ll.Node(item_insert)) self.assertEqual(item_insert, test_llist.head.value) self.assertEqual(item_insert, test_llist.tail.value)
def test_linked_list2_insert(self): temp_list = [12, 23, 34, 45, 56, 67, 78, 89] test_llist2 = ll.LinkedList2() for i in temp_list: test_llist2.add_in_tail(ll.Node(i)) # test_llist2.print_all_nodes_both_row() # insert into end: item_insert = 10 test_llist2.insert(None, ll.Node(item_insert)) temp_list.append(item_insert) # test_llist2.print_all_nodes_both_row() self.common_tests(temp_list, test_llist2) self.assertEqual(temp_list[-1], test_llist2.tail.value) # insert into middle: item_insert = 39 test_llist2.insert(test_llist2.find(34), ll.Node(item_insert)) temp_list.insert(3, item_insert) # test_llist2.print_all_nodes_both_row() self.common_tests(temp_list, test_llist2) # insert into end: item_insert = 99 test_llist2.insert(test_llist2.find(10), ll.Node(item_insert)) temp_list.append(item_insert) self.common_tests(temp_list, test_llist2) self.assertEqual(temp_list[-1], test_llist2.tail.value) # insert into empty linked list: test_llist2.clean() test_llist2.insert(test_llist2.find(89), ll.Node(item_insert)) self.assertEqual(item_insert, test_llist2.head.value) self.assertEqual(item_insert, test_llist2.tail.value)
def test_linked_list2_del_all(self): test_llist = ll.LinkedList2() self.assertEqual(None, test_llist.delete(1, all=True)) temp_list = [ 11, 11, 11, 11, 12, 23, 11, 11, 34, 45, 56, 67, 11, 78, 11, 89, 11, 11, 11 ] for i in temp_list: test_llist.add_in_tail(ll.Node(i)) del_item = 11 test_llist.delete(del_item, all=True) # test_llist.print_all_nodes_both_row() temp_list = [i for i in temp_list if i != del_item] self.common_tests(temp_list, test_llist) # delete all same node: test_llist.clean() for i in range(10): test_llist.add_in_tail(ll.Node(del_item)) test_llist.delete(del_item, all=True) self.assertEqual(None, test_llist.head) self.assertEqual(None, test_llist.tail) self.assertEqual(0, test_llist.len()) self.assertEqual(0, test_llist.len_reverse())
def sum_list(ll1: LinkedList, ll2: LinkedList) -> LinkedList: """ >>> sum_list(LinkedList(7, 1, 6), LinkedList(5, 9, 2)) [2, 1, 9] >>> sum_list(LinkedList(7, 1, 6), LinkedList(5, 9, 2, 1)) [2, 1, 9, 1] >>> sum_list(LinkedList(5, 9, 2, 1), LinkedList(7, 1, 6)) [2, 1, 9, 1] >>> sum_list(LinkedList(5), LinkedList(1)) [6] """ carry = 0 curr1, curr2 = ll1.head, ll2.head while (curr1.next if curr1 else None) or \ (curr2.next if curr2 else None): s = (curr1.val if curr1 else 0) + (curr2.val if curr2 else 0) + carry carry = int(s / 10) curr1 = append_to_node(s % 10, curr1) curr2 = append_to_node(s % 10, curr2) s = (curr1.val if curr1 else 0) + (curr2.val if curr2 else 0) + carry carry = int(s / 10) if curr1: curr1 = append_to_node(LinkedList.Node(carry), curr1, val=s % 10) return ll1 if curr2: curr2 = append_to_node(LinkedList.Node(carry), curr2, val=s % 10) return ll2
def setup_for_read_numbers(): l1 = LL.Node(3) l1.add(1) l1.add(5) l2 = LL.Node(5) l2.add(9) l2.add(2) return l1, l2
def test_linked_list2_add_in_head(self): temp_list = [12, 23, 34, 45, 56, 67, 78, 89] test_llist2 = ll.LinkedList2() for i in temp_list: test_llist2.add_in_tail(ll.Node(i)) item_head = 10 temp_list.insert(0, item_head) test_llist2.add_in_head(ll.Node(item_head)) # test_llist2.print_all_nodes_both_row() self.common_tests(temp_list, test_llist2)
def test_find_circle(): head = LL.Node(5) n = head for i in range(10): n.next = LL.Node(i) n = n.next circle = head.next.next.next n.next = circle assert LL.find_circle(head) is circle circle.next = LL.Node(1) circle.next.next = circle assert LL.find_circle(head) is circle
def updateSnake(self, cell): #case for hitting wall when gameOver already set to True if cell == 0: return 0 #case for crashing into self, sets gameOver if cell.CellType == "Snake": node = self.snake.head #get end of snake while (node.nextNode != None and node.nextNode.cell.CellType != "Empty"): node = node.nextNode #if next cell is tail if node.cell is cell: node = self.snake.head while (node.nextNode.cell != cell): node = node.nextNode #make tail the new head node.nextNode = None temp = self.snake.head newNode = lk.Node(cell) if (temp.cell.CellType != "Empty"): newNode.nextNode = temp self.snake.head = newNode else: #set gameOver is hit snake part other than tail self.gameOver = True return 0 #case for when cell is bait, then grow snake elif cell.CellType == "Bait": self.grow(cell) self.generateFood() return 0 else: #when cell is a empty, can move to it. Update snake node = self.snake.head #get end of snake and set to empty while (node.nextNode != None and node.nextNode.cell.CellType != "Empty"): node = node.nextNode node.cell.setCell(0) #update on board self.board[node.cell.row][node.cell.col] = node.cell temp = self.snake.head #set new head of snake as input cell cell.setCell(1) newNode = lk.Node(cell) if (temp.cell.CellType != "Empty"): newNode.nextNode = temp self.snake.head = newNode return 0
def test_linked_list_del_single(self): test_llist = ll.LinkedList() test_llist.add_in_tail(ll.Node(99)) item_del = 99 test_llist.delete(item_del, all=False) self.assertEqual(None, test_llist.head) self.assertEqual(None, test_llist.tail)
def grow(self, bait): #make bait cell as new snake head as 'growth' node = self.snake.head newNode = lk.Node(bait) newNode.cell.setCell(1) newNode.nextNode = node self.snake.head = newNode
def isPalindrome(root): curr = root revcurr = LinkedList.Node(curr.value) while curr: curr = curr.next if curr: revnew = LinkedList.Node(curr.value) revnew.next = revcurr revcurr = revnew prll(revcurr) curr = root curr2 = revcurr while curr: if curr.value != revcurr.value: return False curr = curr.next revcurr = revcurr.next return True
def partition(ll: LinkedList, x: int) -> LinkedList: """ Time: O(n) Space: O(n) >>> partition(LinkedList(1, 2, 3, 5, 24, 2 ,567, 3, 45, 0), 20) [1, 2, 3, 5, 2, 3, 0, 24, 567, 45] >>> partition(LinkedList(1, 2, 10, 5, 1, 0, 8, 12, 4), 5) [1, 2, 1, 0, 4, 10, 5, 8, 12] """ curr = ll.head ll_low = None ll_high = None last_low = None last_high = None while curr: if curr.val >= x: if not ll_high: ll_high = LinkedList.Node(curr.val) last_high = ll_high else: last_high.next = LinkedList.Node(curr.val) last_high = last_high.next else: if not ll_low: ll_low = LinkedList.Node(curr.val) last_low = ll_low else: last_low.next = LinkedList.Node(curr.val) last_low = last_low.next curr = curr.next partitioned = None if last_low: last_low.next = ll_high partitioned = ll_low else: partitioned = ll_high curr = partitioned ans = [] while curr: ans.append(curr.val) curr = curr.next return LinkedList(ans)
def test_linked_list_clean(self): temp_list = [ 12, 23, 11, 11, 34, 45, 56, 67, 11, 78, 11, 89, 11, 11, 99 ] test_llist = ll.LinkedList() for i in temp_list: test_llist.add_in_tail(ll.Node(i)) test_llist.clean() self.assertEqual(None, test_llist.head) self.assertEqual(None, test_llist.tail)
def test_linked_list_del_last(self): temp_list = [12, 23, 11, 11, 34, 45, 56, 67, 11, 78, 11, 89, 11, 99] test_llist = ll.LinkedList() for i in temp_list: test_llist.add_in_tail(ll.Node(i)) del_item = 99 test_llist.delete(del_item, all=False) temp_list.remove(del_item) self.compare_list_llist(temp_list, test_llist) self.assertEqual(temp_list[-1], test_llist.tail.value)
def test_linked_list_find_len(self): temp_list = [ 12, 23, 11, 11, 34, 45, 56, 67, 11, 78, 11, 89, 11, 11, 99 ] test_llist = ll.LinkedList() for i in temp_list: test_llist.add_in_tail(ll.Node(i)) self.assertEqual(len(temp_list), test_llist.len()) test_llist.clean() self.assertEqual(0, test_llist.len())
def test_linked_list_del_first(self): temp_list = [ 99, 11, 11, 12, 23, 11, 11, 34, 45, 56, 67, 11, 78, 11, 89, 11, 11, 11 ] test_llist = ll.LinkedList() for i in temp_list: test_llist.add_in_tail(ll.Node(i)) del_item = 99 test_llist.delete(del_item, all=False) temp_list.remove(del_item) self.compare_list_llist(temp_list, test_llist)
def test_linked_list_find_all(self): temp_list = [ 12, 23, 11, 11, 34, 45, 56, 67, 11, 78, 11, 89, 11, 11, 99 ] test_llist = ll.LinkedList() for i in temp_list: test_llist.add_in_tail(ll.Node(i)) item_find = 11 self.assertEqual(temp_list.count(item_find), len(test_llist.find_all(item_find))) test_llist.clean() self.assertEqual(0, len(test_llist.find_all(item_find)))
def test_linked_list_addition(self): temp_list_1 = [1, 2, 3, 4, 5, 6, 7, 8] temp_list_2 = [10, 20, 30, 40, 50, 60, 70, 80] temp_sum_lists = [a + b for a, b in zip(temp_list_1, temp_list_2)] test_llist_1 = ll.LinkedList() for i in temp_list_1: test_llist_1.add_in_tail(ll.Node(i)) test_llist_2 = ll.LinkedList() for i in temp_list_2: test_llist_2.add_in_tail(ll.Node(i)) test_sum_llist = test_llist_1 + test_llist_2 self.compare_list_llist(temp_sum_lists, test_sum_llist) # for linked list if different length: test_llist_1.add_in_tail(ll.Node(9)) test_sum_llist_2 = test_llist_1 + test_llist_2 self.assertEqual(None, test_sum_llist_2)
def get_k_last(ll, k): dummy = LL.Node('dummy') dummy.nextNode = ll.head kthNode = dummy endNode = dummy for i in range(k+1): endNode = endNode.nextNode while (endNode): endNode = endNode.nextNode kthNode = kthNode.nextNode ll.head = dummy.nextNode return kthNode, kthNode.nextNode
def test_linked_list_del_one(self): test_llist = ll.LinkedList() self.assertEqual(None, test_llist.delete(1, all=False)) temp_list = [ 11, 11, 11, 11, 12, 23, 11, 11, 34, 45, 56, 67, 11, 78, 11, 89, 11, 11, 11 ] for i in temp_list: test_llist.add_in_tail(ll.Node(i)) del_item = 11 test_llist.delete(del_item, all=False) temp_list.remove(del_item) self.compare_list_llist(temp_list, test_llist)
def test_linked_list2_del_one(self): test_llist = ll.LinkedList2() self.assertEqual(None, test_llist.delete(1, all=False)) temp_list = [ 11, 11, 11, 11, 12, 23, 11, 11, 34, 45, 56, 67, 11, 78, 11, 89, 11, 11, 11 ] for i in temp_list: test_llist.add_in_tail(ll.Node(i)) del_item = 11 test_llist.delete(del_item, all=False) temp_list.remove(del_item) # test_llist.print_all_nodes_both_row() self.common_tests(temp_list, test_llist)
def partition(root): curr = root marker = LinkedList.Node('*') left = marker right = marker while curr: if curr.value < k: nex = curr.next curr.next = left left = curr curr = nex elif curr.value >= k: nex = curr.next curr.next = None right.next = curr right = curr curr = nex marker.value = marker.next.value marker.next = marker.next.next return left
def addll(root1,root2): curr1 = root1 curr2 = root2 total = LinkedList.Node(None) carry = 0 while curr1 or curr2: if not curr1: total.append(curr2.value+carry) carry = 0 curr2 = curr2.next elif not curr2: total.append(curr1.value+carry) carry = 0 curr1 = curr1.next else: total.append((curr1.value+curr2.value+carry)%10) if curr1.value+curr2.value+carry >= 10: carry = 1 else: carry = 0 curr1 = curr1.next curr2 = curr2.next return total.next
def even_odd(ll): dummyhead = LL.Node('dummy') dummyhead.nextNode = ll.head even = dummyhead odd = ll.head while (True): if odd.nextNode: even.nextNode = odd.nextNode even = even.nextNode else: even.nextNode = ll.head ll.head = dummyhead.nextNode return repr(ll) if even.nextNode: odd.nextNode = even.nextNode odd = odd.nextNode else: odd.nextNode = None even.nextNode = ll.head ll.head = dummyhead.nextNode return repr(ll)
def startup(): #the function that makes the game global v1 global v2 global rowinp global colinp global newGame global screen root.destroy() rowinp = v1.get() colinp = v2.get() snake = lk.LinkedList() snake.head = lk.Node(c.Cell(row = 9, col = 4)) snake.head.cell.setCell(1) newGame = SnakeGame(snake, row =rowinp, col =colinp) screen = pygame.display.set_mode((vel*colinp, vel*rowinp)) while newGame.gameOver != True: #loop to run snake game on GUI newGame.printBoard() userinput = readInput(newGame.direction) if userinput == "d": if (newGame.direction != 3): newGame.setDirection(1) elif userinput == "s": if (newGame.direction != 4): newGame.setDirection(2) elif userinput == "a": if (newGame.direction != 1): newGame.setDirection(3) elif userinput == "w": if (newGame.direction != 2): newGame.setDirection(4) cell = newGame.nextCell() newGame.updateSnake(cell) print("Game Over!") #print gameover in a tkinter gui instead if want to optimize return 0
import sys import LinkedList root = LinkedList.Node(sys.argv[1]) for i in range(2,len(sys.argv)): root.append(sys.argv[i]) def prll(root): curr = root ll = '' while curr: ll = ll + str(curr.value) +', ' curr = curr.next print ll prll(root) def isPalindrome(root): curr = root revcurr = LinkedList.Node(curr.value) while curr: curr = curr.next if curr: revnew = LinkedList.Node(curr.value) revnew.next = revcurr revcurr = revnew prll(revcurr) curr = root curr2 = revcurr while curr: if curr.value != revcurr.value:
def readIn(): #initializes list of CSV files datfilespeed = list() datfiledist = list() #adds the CSV files to the list datfilespeed.append('Speed2.csv') datfilespeed.append('Speed3.csv') datfilespeed.append('Speed4.csv') datfilespeed.append('Speed5.csv') datfiledist.append('Distance1.csv') datfiledist.append('Distance2.csv') datfiledist.append('Distance3.csv') datfiledist.append('Distance4.csv') #strings for regex speed1 = 'Counter for vehicle speed within (105-120 kph)' speed2 = 'Counter for vehicle speed within (120-135 kph)' speed3 = 'Counter for vehicle speed within (135-150 kph)' distance1 = 'Counter for FCA gap setting at (Near)' distance2 = 'Counter for FCA gap setting at (Medium)' distance3 = 'Counter for FCA gap setting at (Far)' distance4 = 'Counter for FCA gap setting at (Off)' #initialize errors and found errors = 0 found = False #creates linked list valueList = LinkedList.LinkedList() #loops through all the files for fh in datfilespeed: #opens the file and reads in data f = open(fh, 'r') #goes through each line for line in f: #sets values equal to an array of each variable in the line values = line.split(',') if (speed1 in values[0]): #iterate through to see if vin exists in linked list, and if so add speed for Node in valueList: if (Node.vin in values[4]): if (int(values[2]) > 0): Node.speed1 = 1 found = True if (not found): #if it isn't, then add vin to linked list and set speed to 1 valueList.add_first(LinkedList.Node(values[4][0:6])) if (int(values[2]) > 0): valueList.head.speed1 = 1 found = False elif (speed2 in values[0]): #iterate through to see if vin exists in linked list, and if so add speed for Node in valueList: if (Node.vin in values[4]): if (int(values[2]) > 0): Node.speed2 = 1 found = True if (not found): #if it isn't, then add vin to linked list and set speed to 1 valueList.add_first(LinkedList.Node(values[4][0:6])) if (int(values[2]) > 0): valueList.head.speed2 = 1 found = False elif (speed3 in values[0]): #iterate through to see if vin exists in linked list, and if so add speed for Node in valueList: if (Node.vin in values[4]): if (int(values[2]) > 0): Node.speed3 = 1 found = True if (not found): #if it isn't, then add vin to linked list and set speed to 1 valueList.add_first(LinkedList.Node(values[4][0:6])) if (int(values[2]) > 0): valueList.head.speed3 = 1 found = False f.close() for fh in datfiledist: #opens the file and reads in data f = open(fh, 'r') #goes through each line for line in f: values = line.split(',') if (distance1 in values[0]): #iterate through to see if vin exists in linked list, and if so add speed for Node in valueList: if (Node.vin in values[5]): if (int(values[2]) > 0): Node.near = int(values[2]) + Node.near found = True #if it isn't, then increment the errors, as we don't have that vin in the list from speed if (not found): errors = errors + 1 found = False elif (distance2 in values[0]): #iterate through to see if vin exists in linked list, and if so add speed for Node in valueList: if (Node.vin in values[5]): if (int(values[2]) > 0): Node.medium = int(values[2]) + Node.medium found = True #if it isn't, then increment the errors, as we don't have that vin in the list from speed if (not found): errors = errors + 1 found = False elif (distance3 in values[0]): #iterate through to see if vin exists in linked list, and if so add speed for Node in valueList: if (Node.vin in values[5]): if (int(values[2]) > 0): Node.far = int(values[2]) + Node.far found = True #if it isn't, then increment the errors, as we don't have that vin in the list from speed if (not found): errors = errors + 1 found = False elif (distance4 in values[0]): #iterate through to see if vin exists in linked list, and if so add speed for Node in valueList: if (Node.vin in values[5]): if (int(values[2]) > 0): Node.off = int(values[2]) + Node.off found = True #if it isn't, then increment the errors, as we don't have that vin in the list from speed if (not found): errors = errors + 1 found = False f.close() #set the setting to the one (near far medium or off) with the highest counter for Node in valueList: if (Node.far > Node.near and Node.far > Node.medium and Node.far > Node.off): Node.setting = 'Far' elif (Node.near > Node.far and Node.near > Node.medium and Node.near > Node.off): Node.setting = 'Near' elif (Node.medium > Node.far and Node.medium > Node.near and Node.medium > Node.off): Node.setting = 'Medium' elif (Node.off > Node.far and Node.off > Node.near and Node.off > Node.medium): Node.setting = 'Off' else: errors = errors + 1 valueList.remove_node(Node.vin) return valueList #prints out the linked list to make sure it is correct print(valueList) #errors are cars that have no setting numbers--all are set to 0! print(errors)
def delete_from_middle(ls, value): current = ls.head previous = None if current.value == value: ls.head = current.next ls.size -= 1 while current.next: if current.value == value: previous.next = current.next ls.size -= 1 break else: previous = current current = previous.next if __name__ == '__main__': linked_list = LinkedList.LinkedList(LinkedList.Node(1)) linked_list.insert(2) linked_list.insert(7) linked_list.insert(5) linked_list.insert(7) linked_list.insert_at_beginning(0) delete_from_middle(linked_list, 5) linked_list.print()
shared_before_cycle = shared_node(ll_a, ll_b) if shared_before_cycle: return shared_before_cycle else: a_cycle.nextNode = old_a b_cycle.nextNode = old_b for i in range(b_length): if b_cycle == a_cycle: return a_cycle b_cycle = b_cycle.nextNode return False if __name__ == '__main__': #test cases: shared/not shared for non cycles, 1 cycle 1 not, and 2 cycles a = LL.Node('a') b = LL.Node('b') c = LL.Node('c') d = LL.Node('d') e = LL.Node('d') f = LL.Node('f') g = LL.Node('g') h = LL.Node('h') ll_a = LL.LinkedList() ll_b = LL.LinkedList() #cycle no shared node a.nextNode = b b.nextNode = c c.nextNode = d