How to use getnext method in tox

Best Python code snippet using tox_python

main.py

Source:main.py Github

copy

Full Screen

1import random2from time import thread_time3class LinkedList:4 # The __Node class is used internally by the LinkedList class. It is5 # invisible from outside this class due to the two underscores6 # that precede the class name. Python mangles names so that they7 # are not recognizable outside the class when two underscores8 # precede a name but aren't followed by two underscores at the9 # end of the name (i.e. an operator name).10 class __Node:11 def __init__(self, item, next=None):12 self.item = item13 self.next = next14 def getItem(self):15 return self.item16 def getNext(self):17 return self.next18 def setItem(self, item):19 self.item = item20 def setNext(self, next):21 self.next = next22 def __init__(self, contents=[]):23 # Here we keep a reference to the first node in the linked list24 # and the last item in the linked list. The both point to a25 # dummy node to begin with. This dummy node will always be in26 # the first position in the list and will never contain an item.27 # Its purpose is to eliminate special cases in the code below.28 self.first = LinkedList.__Node(None, None)29 self.last = self.first30 self.numItems = 031 for e in contents:32 self.append(e)33 def __getitem__(self, index):34 if index >= 0 and index < self.numItems:35 cursor = self.first.getNext()36 for i in range(index):37 cursor = cursor.getNext()38 return cursor.getItem()39 raise IndexError("LinkedList index out of range")40 def __setitem__(self, index, val):41 if index >= 0 and index < self.numItems:42 cursor = self.first.getNext()43 for i in range(index):44 cursor = cursor.getNext()45 cursor.setItem(val)46 return47 raise IndexError("LinkedList assignment index out of range")48 def insert(self, index, item):49 cursor = self.first50 if index < self.numItems:51 for i in range(index):52 cursor = cursor.getNext()53 node = LinkedList.__Node(item, cursor.getNext())54 cursor.setNext(node)55 self.numItems += 156 else:57 self.append(item)58 def __add__(self, other):59 if type(self) != type(other):60 raise TypeError("Concatenate undefined for " + \61 str(type(self)) + " + " + str(type(other)))62 result = LinkedList()63 cursor = self.first.getNext()64 while cursor != None:65 result.append(cursor.getItem())66 cursor = cursor.getNext()67 cursor = other.first.getNext()68 while cursor != None:69 result.append(cursor.getItem())70 cursor = cursor.getNext()71 return result72 def __contains__(self, item):73 for e in self:74 if e == item:75 return True76 return False77 def __delitem__(self, index):78 cursor = self.first.getNext()79 for i in range(index - 1):80 cursor = cursor.getNext()81 cursor.setNext(cursor.getNext().getNext())82 self.numItems -= 183 def __eq__(self, other):84 if type(other) != type(self):85 return False86 if self.numItems != other.numItems:87 return False88 cursor1 = self.first.getNext()89 cursor2 = other.first.getNext()90 while cursor1 != None:91 if cursor1.getItem() != cursor2.getItem():92 return False93 cursor1 = cursor1.getNext()94 cursor2 = cursor2.getNext()95 return True96 def __iter__(self):97 cursor = self.first.getNext() # idiom: start at the beginning98 while cursor != None:99 yield cursor.getItem()100 cursor = cursor.getNext()101 def __len__(self):102 return self.numItems103 def append(self, item):104 node = LinkedList.__Node(item)105 self.last.setNext(node)106 self.last = node107 self.numItems += 1108 def __str__(self):109 cursor = self.first.getNext()110 out = "["111 while cursor != None:112 out += str(cursor.getItem())113 cursor = cursor.getNext()114 if cursor != None:115 out += ","116 else:117 break118 return out + "]"119 def __repr__(self):120 # This is left as an exercise for the reader.121 pass122 def split(self, index):123 cursor = self.first.getNext()124 newlst = LinkedList()125 for i in range(index - 1):126 cursor = cursor.getNext()127 newlst.first.setNext(cursor.getNext())128 cursor.setNext(None)129 newlst.numItems = self.numItems - index130 self.numItems = index131 return newlst132 def sorted(self):133 cursor = self.first.getNext()134 while cursor.getNext() != None:135 prev = cursor.getItem()136 cursor = cursor.getNext()137 if prev > cursor.getItem():138 return False139 return True140 def merge(self, other):141 if type(self) != type(other):142 raise TypeError("Merge undefined for " + \143 str(type(self)) + " + " + str(type(other)))144 cursor_self = self.first.getNext()145 cursor_other = other.first.getNext()146 merged_list = LinkedList()147 while (cursor_self is not None) and (cursor_other is not None):148 if cursor_self.getItem() < cursor_other.getItem():149 merged_list.append(cursor_self.getItem())150 cursor_self = cursor_self.getNext()151 else:152 merged_list.append(cursor_other.getItem())153 cursor_other = cursor_other.getNext()154 while cursor_self is not None:155 merged_list.append(cursor_self.getItem())156 cursor_self = cursor_self.getNext()157 while cursor_other is not None:158 merged_list.append(cursor_other.getItem())159 cursor_other = cursor_other.getNext()160 self.first = merged_list.first161 self.last = merged_list.last162 self.numItems = merged_list.numItems163 def merge_sort(self):164 if self.numItems == 1:165 return166 pivot = self.numItems // 2167 newlst = self.split(pivot)168 newlst.merge_sort()169 self.merge_sort()170 self.merge(newlst)171def main():172 lst = LinkedList()173 for i in range(100):174 lst.append(i)175 lst2 = LinkedList(lst)176 print(lst)177 print(lst2)178 if lst == lst2:179 print("Test 1 Passed")180 else:181 print("Test 1 Failed")182 lst3 = lst + lst2183 if len(lst3) == len(lst) + len(lst2):184 print("Test 2 Passed")185 else:186 print("Test 2 Failed")187 if 1 in lst3:188 print("Test 3 Passed")189 else:190 print("Test 3 Failed")191 if 2 in lst3:192 print("Test 4 Passed")193 else:194 print("Test 4 Failed")195 del lst[1]196 if 1 in lst:197 print("Test 5 Failed")198 else:199 print("Test 5 Passed")200 if len(lst) == 99:201 print("Test 6 Passed")202 else:203 print("Test 6 Failed")204 if lst == lst2:205 print("Test 7 Failed")206 else:207 print("Test 7 Passed")208 del lst2[2]209 if lst == lst2:210 print("Test 8 Failed")211 else:212 print("Test 8 Passed")213 lst4 = LinkedList(lst)214 lst.insert(0, 100)215 lst4 = LinkedList([100]) + lst4216 if lst == lst4:217 print("Test 9 Passed")218 else:219 print("Test 9 Failed")220 lst.insert(1000, 333)221 lst4.append(333)222 if lst == lst4:223 print("Test 10 Passed")224 else:225 print("Test 10 Failed")226 print(lst)227 print(lst4)228 lst5 = LinkedList([1, 2, 3, 4, 5, 6, 7, 8])229 lst6 = lst5.split(4)230 print(lst5)231 print(lst6)232 lst7 = LinkedList([1, 2, 3, 4, 5, 6, 7, 8])233 lst5.merge(lst6)234 print(lst5)235 if lst5 == lst7:236 print("Test 11 passed")237 else:238 print("Test 11 failed")239 lst8 = LinkedList([5, 3, 6, 2, 1, 7, 4, 8])240 if lst7.sorted() and (not lst8.sorted()):241 print("Test 12 passed")242 else:243 print("Test 12 failed")244 longlst = list(range(1000))245 random.shuffle(longlst)246 lst8 = LinkedList(longlst)247 runtime = thread_time()248 lst8.merge_sort()249 runtime = thread_time() - runtime250 if lst8.sorted():251 print("Test 13 passed")252 else:253 print("Test 13 failed")254 print(runtime)255 with open("merge_sort.csv", "w") as f:256 for i in range(2, 1000000):257 lst = list(range(i))258 random.shuffle(lst)259 toSort = LinkedList(lst)260 runtime = thread_time()261 toSort.merge_sort()262 runtime = thread_time() - runtime263 f.write(str(runtime) + ", " + str(i) + "\n")264if __name__ == "__main__":...

Full Screen

Full Screen

LinkedList.py

Source:LinkedList.py Github

copy

Full Screen

1#!/usr/bin/env python32# -*- coding: utf-8 -*-3"""4Created on Tue Dec 19 16:07:06 20175@author: supperxxxs6"""7class Node:8 def __init__(self, initdata):9 self.__data = initdata10 self.__next = None11 def getData(self):12 return self.__data13 def getNext(self):14 return self.__next15 def setData(self, newdata):16 self.__data = newdata17 def setNext(self, newnext):18 self.__next = newnext19class SinLinkedlist:20 def __init__(self):21 self.head = None22 """ #bymyself23 def add(self, item):24 temp = Node(item)25 prev = self.head26 while prev.getNext() != None:27 cur = prev.getNext()28 if cur.getNext() == None:29 cur.setNext(temp)30 prev = prev.getNext()31 """ 32 def add(self, item):33 if self.empty():34 self.head = Node(item)35 else:36 temp = Node(item)37 cur = self.head38 while cur.getNext() != None:39 cur=cur.getNext()40 cur.setNext(temp)41 42 def addNode(self, node):43 if self.empty():44 self.head = node45 else:46 cur = self.head47 while cur.getNext() != None:48 cur=cur.getNext()49 cur.setNext(node)50 51 def remove(self,item):52 prev =self.head53 while prev.getNext() != None:54 cur = prev.getNext()55 if cur.getData() == item:56 prev.setNext(cur.getNext())57 prev = prev.getNext()58 def search(self, item):59 cur = self.head.getNext()60 while cur != None:61 if cur.getData() == item:62 return True63 cur = cur.getNext()64 return False65 def empty(self):66 return self.head == None67 68 def size(self):69 count = 070 cur = self.head.getNext()71 while cur != None:72 count += 173 cur = cur.getNext()74 return count75 def output(self):76 cur=self.head77 while cur!= None:78 print(cur.getData())79 cur=cur.getNext()80x = [2,4,5,2,5,6]81if __name__ == '__main__':82 s= SinLinkedlist()83 for i in x:84 s.add(i)85 #s.output()86 """87 #2.1.hashtable88 hashtable=set([])89 prev = s.head90 hashtable.add(prev.getData())91 cur = prev.getNext()92 while cur != None:93 if cur.getData() in hashtable:94 prev.setNext(cur.getNext())95 cur = cur.getNext()96 else:97 hashtable.add(cur.getData())98 cur=cur.getNext()99 prev = prev.getNext()100 s.output() 101 """ 102 """103 #2.1.without temporary buffer104 prev = s.head105 cur=prev.getNext()106 while cur != None:107 test =s.head108 while test != cur:109 if test.getData() == cur.getData():110 prev.setNext(cur.getNext())111 cur =cur.getNext()112 break113 test = test.getNext()114 if test == cur:115 cur= cur.getNext()116 prev= prev.getNext()117 # else:118 # cur =cur.getNext()119 # prev =prev120 s.output()121 """122 """123 #2.2. nth to the last element124 n = 3125 prev =s.head126 cur = s.head127 for i in range(n):128 prev=prev.getNext()129 while prev != None:130 prev=prev.getNext()131 cur= cur.getNext()132 print(cur.getData())133 """134 #2.3.carryadder135 a =999136 b =999137 x=SinLinkedlist()138 y=SinLinkedlist()139 result=SinLinkedlist()140 for i in range(len(str(a))):141 x.add(a%10)142 a=int(a/10)143 for i in range(len(str(b))):144 y.add(b%10)145 b=int(b/10)146 x_cur= x.head147 y_cur= y.head 148 carry = 0149 while x_cur != None or y_cur != None: 150 if x_cur == None:151 result.add((y_cur.getData()+carry)%10)152 if y_cur.getData()+carry >9:153 carry=1154 else:155 carry=0156 y_cur=y_cur.getNext()157 elif y_cur == None:158 result.add((x_cur.getData()+carry)%10)159 if x_cur.getData()+carry >9:160 carry=1161 else:162 carry=0163 x_cur=x_cur.getNext()164 else: 165 result.add((x_cur.getData()+y_cur.getData()+carry)%10)166 if x_cur.getData()+y_cur.getData()+carry >9:167 carry =1168 else:169 carry =0170 x_cur =x_cur.getNext()171 y_cur =y_cur.getNext()172 if carry ==1:173 result.add(carry)174 result.output()175 #2.5.create circular linked list 176 t=SinLinkedlist()177 A=Node('A')178 t.addNode(A)179 B=Node('B')180 t.addNode(B)181 C=Node('C')182 t.addNode(C)183 D=Node('D')184 t.addNode(D)185 E=Node('E')186 t.addNode(E)187 E.setNext(C)188# function1189 ht =set([])190 cur = t.head191 while cur.getData() not in ht:192 ht.add(cur.getData())193 cur = cur.getNext()194 if cur.getData() in ht:195 print(cur.getData())196#function2 197 turtle = t.head.getNext()198 rabbit = t.head.getNext().getNext()199 while turtle.getData() != rabbit.getData():200 turtle = turtle.getNext()201 rabbit = rabbit.getNext().getNext()202 pig = t.head203 while pig.getData() != turtle.getData():204 pig = pig.getNext()205 turtle= turtle.getNext()206 print(pig.getData())207 ...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run tox automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful