Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
linked_lists_2+decisions
class Node(object): def __init__(self, data=None): self.data = data self.next = None class Context(object): def __init__(self, first, second): self.first = first self.second = second def alternating_split(head): #if type(head) is Node: if head == None or len(head) == 1: raise Exception first = [v for i, v in enumerate(head) if i%2 == 0] second = [v for i, v in enumerate(head) if i%2 != 0] return Context(first, second) def push(head, data): newNode = Node(data) newNode.next = head return newNode def build_one_two_three(): head = None head = push(head, 3) head = push(head, 2) head = push(head, 1) return head def build_one_two(): head = None head = push(head, 2) head = push(head, 1) return head def build_one_two_three_four_five_six(): head = None head = push(head, 6) head = push(head, 5) head = push(head, 4) head = push(head, 3) head = push(head, 2) head = push(head, 1) return head def build_list(data): data.reverse() head = None for num in data: head = push(head, num) return head def assert_linked_list_equals(listA, listB, message): while listA is not None and listB is not None: test.assert_equals(listA.data, listB.data, message) listA = listA.next listB = listB.next def length(node): len = 0 if node != None: current = node while current.next != None: current = current.next len += 1 return len + 1 return len def count(node, data): count = 0 if node != None: current = node if node.data == data: count +=1 while current.next != None: current = current.next if current.data == data: count +=1 return count def get_nth(node, index): if node != None: ind = 0 current = node if index == 0: return current.data while current.next != None: current = current.next ind += 1 if ind == index: return current.data return None #подсмотрено def get_nth1(head, index): if not head: raise ValueError('Head node cannot be None') if index < 0: raise ValueError('Index cannot be negative') if index > length(head) - 1: raise ValueError('Index cannot exceed number of nodes in list') counter = 0 current_node = head while counter != index: current_node = current_node.next counter += 1 return current_node print('______Insert Nth Node_____') def insert_nth(head, index, data): if index > length(head)-1: raise Exception('Index') if index == 0: newNode = Node(data) newNode.next = head return newNode count = 0 curr = head while curr != None: count += 1 if count == index: newNode = Node(data) if curr.next.next != None: newNode.next = curr.next.next curr.next = newNode break curr = curr.next return curr def iterNode(head): curr = head arr = [] #for i in range(length(head)): while curr: arr.append(curr.data) curr = curr.next return arr nn = build_one_two_three_four_five_six() nn = push(nn, 23) nn = insert_nth(nn, 0, 7) nn = insert_nth(nn, 1, 8) print(iterNode(nn)) nn = insert_nth(nn, 3, 42) print(iterNode(nn)) #//Решения #1 class Node(object): def __init__(self, data, next=None): self.data = data self.next = next def insert_nth(head, n, data): if n==0: head=Node(data,head) else: head.next=insert_nth(head.next,n-1,data) if not head: raise "Something's fucky!" return head #2 class Node(object): def __init__(self, data, next=None): self.data = data self.next = next def __iter__(self): current = self while current is not None: yield current current = current.next def insert_nth(head, index, data): if not head: if index == 0: return Node(data) else: raise IndexError else: prev = None for i, node in enumerate(head): if index == i: new_node = Node(data, node) if prev: prev.next = new_node else: head = new_node return head prev = node if i + 1 == index: node.next = Node(data) return head else: raise IndexError
run
|
edit
|
history
|
help
0
Ghost 👻👻👻👻👻 Game 👻👻👻👻👻👻
Rasty profile
75 95
Decimal, Octal, Hexadecimal and Binary Format
Algorytm szybkiego potęgowania
Prime_list
RANDOM NUMBER
Ej2_python_204676887
Hi.py
Filtering texts within brackets