-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfib.py
72 lines (63 loc) · 1.58 KB
/
fib.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import random
class Node:
def __init__(self, value=None, next=None):
self.value = value
self.next = next
class List:
def __init__(self, slist=None, next=None):
self.slist = slist
self.next = next
def merge_single_node(dst, data):
node = data[0]
data[0] = node.next
node.next = None
dst[0] = node
dst[0] = node.next
def merge_pair(dst, sub1, sub2):
while sub1 or sub2:
if not sub2 or (sub1 and sub1.value < sub2.value):
merge_single_node(dst, [sub1])
else:
merge_single_node(dst, [sub2])
def seq_sort_core(data):
dst = None
while random.randint(0, 1):
next = data.next
if not next:
data.next = dst
dst = data
break
merge_pair([data.slist], data.slist, next.slist)
data.next = dst
dst = data
data = next.next
return dst
def inspect_before(shape):
while shape.next:
shape = shape.next
assert shape.next == None
def inspect_after(shape):
assert shape.next == None
pos = shape.slist
while pos.next:
pos = pos.next
assert pos.next == None
def main():
data = None
while random.randint(0, 1):
node = Node()
node.next = node
node.value = random.randint(0, 1)
item = List()
item.slist = node
item.next = data
data = item
if not data:
return
inspect_before(data)
while random.randint(0, 1):
data = seq_sort_core(data)
inspect_after(data)
node = data.slist
assert 0 == 1
main()