def test_get_loop_one_elem(self): """ list: 1 -> None get_loop should return the head of the list. :return: void """ n = Node(1, None) n.next = n self.assertTrue(get_loop(n) is n)
def test_get_loop_no_loop(self): """ list: 1 -> 2 -> 3 -> 4 get_loop should return None as there is not a loop :return: void """ n = Node(4, None) n = Node(3, n) n = Node(2, n) n = Node(1, n) self.assertTrue(get_loop(n) is None)
def reverse(head, k, root=None): a, result = None, None while head is not None: for i in range(0, k): if head is None: break root = Node(head.value, root) head = head.next if a is None: result = Node(root.value, root.next) a = result else: while a.next is not None: a = a.next a.next = Node(root.value, root.next) root = None return result
def is_balanced(word): s = Stack() for c in word: if c == '(': s.push(Node(c)) elif c == ')': if s.head is not None: s.pop() else: return False return s.head is None
def testNodeDeque(): print("====test NodeDeque====") print("Init NodeDeque") try: d = NodeDeque() # init print("\tNodeDeque initialized") except Exception: print("\tInit failed") # append print("append") d.append(Node(1)) d.append(Node(2)) d.append(Node(3)) # validate print(d.head) print(d.tail) for i in range(10): d.append(Node(randrange(1, 255))) # __repr__ print("__repr__:") print(d) # remove print("remove:") d.remove(0) d.remove(-1) d.remove(1) print(d) # pop print("pop:") print(d.pop()) print(d) print("len:") print(len(d)) # peek print("%s:" % d.peek.__name__) print(d.peek()) print("==========END==========\n\n")
def setUp(self) -> None: self.n1 = Node("n1") a = Node("a") self.b = Node("b") c = Node("c") self.n2 = Node("n2") self.d = Node("d") self.n1.next = a a.next = self.b self.b.next = c self.n2.next = self.d self.d.next = self.b
def josephus_iterVer(n): # 1. init crowdDeq = NodeDeque() # number the crowd, 1~n for i in range(1, n + 1): crowdDeq.append(Node(i)) # 2. looping untill the crowd has 1 person left # start from crowdDeq[0], if is -1, means last killing loop, n is odd startIdx = 0 while len(crowdDeq) > 1: # Method 1(goal: reduce codes quntity) if startIdx + n % 2 == 0: # (startIdx == -1 and n%2 == 1) or (startIdx == 0 and n%2 == 0) upperBound = n - 1 else: upperBound = n - 2 # update startIdx, startIdx must be updated before n changed if n % 2 == 1 and startIdx == 0: startIdx = -1 elif n % 2 == 1 and startIdx == -1: startIdx = 0 for i in range(upperBound, -1, -2): crowdDeq.remove(i) # must be removed in reverse order n -= 1 # # Method 2(goal: easier to understand) # if startIdx == 0: # if n%2 == 0: # for i in range(n-1, -1, -2): # crowdDeq.remove(i) # must be removed in reverse order # n -= 1 # else: # n%2 == 1 # for i in range(n-2, -1, -2): # crowdDeq.remove(i) # n -= 1 # startIdx = -1 # else: # startIdx = -1 # if n%2 == 0: # for i in range(n-2, -1, -2): # crowdDeq.remove(i) # n -= 1 # else: # for i in range(n-1, -1, -2): # crowdDeq.remove(i) # n -= 1 # startIdx = 0 return crowdDeq.peek().value()
def test_get_loop(self): """ list: 1 -> 2 -> 3 -> 4 -> 2 (2 is the same Node instance as the first 2) get_loop should return the Node instance with value 2 :return: void """ n4 = Node(4, None) n3 = Node(3, n4) n2 = Node(2, n3) n1 = Node(1, n2) n4.next = n2 #import pdb #pdb.set_trace() self.assertTrue(get_loop(n1) is n2)
def add(self, item): current = self.head previous = None stop = False while not stop and current != None: if current.get_data() > item: stop = True else: previous = current current = current.get_next() new_item = Node(item) if previous == None: new_item.set_next(self.head) self.head = new_item else: new_item.set_next(current) previous.set_next(new_item)
self.mins = Stack() def push(self, elem: Node): self.items.push(elem) if self.mins.head is None or elem.value < self.mins.head.value: self.mins.push(Node(elem.value)) else: self.mins.push(Node(self.mins.head.value)) def pop(self): self.mins.pop() return self.items.pop() def min(self): return self.mins.head.value ms = MinStack() ms.push(Node(2)) print(ms.min()) ms.push(Node(3)) print(ms.min()) ms.push(Node(1)) print(ms.min()) ms.pop() print(ms.min()) ms.pop() print(ms.min())
def push(self, elem: Node): if self.head is None: self.head = elem else: elem.set_next(self.head) self.head = elem
def add(self, item): new_item = Node(item) new_item.set_next(self.head) self.head = new_item
from lists.node import Node def reverse(head, k, root=None): a, result = None, None while head is not None: for i in range(0, k): if head is None: break root = Node(head.value, root) head = head.next if a is None: result = Node(root.value, root.next) a = result else: while a.next is not None: a = a.next a.next = Node(root.value, root.next) root = None return result n1 = Node(1, Node(2, Node(3, Node(4, Node(5, Node(6, Node(7, Node(8)))))))) r = reverse(n1, 5) while r is not None: print(r.value, end=' ') r = r.next # 2 1 4 3 6 5
from lists.node import Node five = Node(5) four = Node(4, five) three = Node(3, four) two = Node(2, three) head = Node(1, two) # five.set_next(head) def kth_last_node(head, k): first = head second = head for i in range(k): second = second.next while second is not None: second = second.next first = first.next return first.value def has_cycle(head: Node): first = head second = head.next while first is not None and second is not None: if first == second: return True else: first, second = first.next, second.next.next
def push(self, elem: Node): self.items.push(elem) if self.mins.head is None or elem.value < self.mins.head.value: self.mins.push(Node(elem.value)) else: self.mins.push(Node(self.mins.head.value))
from lists.node import Node class Queue: def __init__(self): self.head, self.tail = None, None def add(self, elem: Node): if self.head is None: self.head, self.tail = elem, elem else: self.tail.next, self.tail = elem, elem def remove(self): if self.head is not None: e, self.head = self.head, self.head.next return e.value q = Queue() q.add(Node(1)) q.add(Node(2)) q.add(Node(3)) q.add(Node(4)) print(q.remove()) print(q.remove()) print(q.remove()) print(q.remove())
class Stack: def __init__(self, head=None): self.head = head def push(self, elem: Node): if self.head is None: self.head = elem else: elem.set_next(self.head) self.head = elem def pop(self): elem = self.head if self.head is not None: self.head = elem.next return elem.value if __name__ == '__main__': s = Stack() s.push(Node(1)) s.push(Node(2)) s.push(Node(3)) print(s.pop()) print(s.pop()) print(s.pop()) print(s.pop())