Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
linked_lists
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 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(length(n3)) print(count(n3, 2)) print(get_nth(n3, 0))
run
|
edit
|
history
|
help
0
contarsucesoresmayores
PyMetaParam
E
Atur cara mengira luas dan isipadu sebuah trapezium
Game4
26.09.2018
mbti
Guru
Skillenza- Subjects
Pure Python Square root without using Math.sqrt()