Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
linked_lists_2
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) curr.next = newNode print(newNode) if curr.next.next != None: newNode.next = curr.next.next break curr = curr.next return curr print(alternating_split([1, 2]).second) print(Node([1]).data) n = build_one_two_three() print(n) n2 = build_one_two_three_four_five_six() n3 = None print('------------------------') print(length(n)) print(get_nth(n, 0)) print('------------------------') print(length(n2)) print(count(n2, 2)) print(get_nth(n2, 3)) print('------------------------') print(length(n3)) print(count(n3, 2)) print(get_nth(n3, 0)) print('#####') print(insert_nth(n2, 1, 23)) nn = build_one_two_three_four_five_six() print(length(nn)) print([nn.data, nn.next.data, nn.next.next.data])
run
|
edit
|
history
|
help
0
Юра и заселение
PyList
newasd
Random number
calucation
Python. Even
ip2cidr
Add missing names
quizcorrect
Submission.py