-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshandom-ruffle.py
169 lines (119 loc) · 4.97 KB
/
shandom-ruffle.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
class Node:
def __init__(self,root,left,right,_min,_max,_value):
self.range_min = _min
self.range_max = _max
self.value = _value
self.root = root
self.left = left
self.right = right
class RangeTree:
def __init__(self,n):
self.root = Node(None,None,None,1,n,None)
self.n = n
self._build_tree(self.root)
def traverse(self,root):
if root == None:
return
self.traverse(root.left)
#print "left child min {0} max {1}".format(root.range_min , root.range_max)
print "root min {0} max {1} value {2}".format(root.range_min , root.range_max, root.value)
self.traverse(root.right)
#print "right child min {0} max {1}".format(root.range_min , root.range_max)
def _build_tree(self,root):
# print "build tree min {0} max {1}".format(root.range_min,root.range_max)
if root.range_max == root.range_min:
return
if root.range_max - root.range_min == 1:
print "build tree min {0} max {1}".format(root.range_min,root.range_max)
root.right = Node (root,None,None,root.range_max,root.range_max,None)
root.left = Node (root,None,None,root.range_min,root.range_min,None)
return
# self._build_tree(root.right)
# self._build_tree(root.left)
if root.range_max < root.range_min:
root.range_max = root.range_min
return
mid_point = root.range_min + ( root.range_max - root.range_min )/2
#print root.range_min,mid_point,root.range_max
root.right = Node (root,None,None,mid_point+1,root.range_max,None)
root.left = Node (root,None,None,root.range_min,mid_point,None)
self._build_tree(root.right)
self._build_tree(root.left)
def change_range_value(self, root,range_min, range_max, value):
mid_point = root.range_min + (root.range_max - root.range_min)/2
#assert 1<=range_min<=range_max<=self.n
if range_min == root.range_min and range_max == root.range_max:
if root.root != None:
if root.root.left == root :
root.root.right.value = root.root.value
root.root.value = None
else:
root.root.left.value = root.root.value
root.root.value = None
if root.value ==None:
root.value = value
else:
root.value +=value
print "value assigned"
return
# go right
if range_min>mid_point and range_max <= root.range_max:
print "Go right"
print "rootmin {0} rootmax {1} midpoint {2}".format(root.range_min,root.range_max,mid_point)
print "min {0} max {1}".format(range_min,range_max)
self.change_range_value(root.right, range_min, range_max,value)
#go left
elif range_max<= mid_point and range_min>= root.range_min:
print "Go left"
print "rootmin {0} rootmax {1} midpoint {2}".format(root.range_min,root.range_max,mid_point)
print "min {0} max {1}".format(range_min,range_max)
self.change_range_value(root.left, range_min, range_max,value)
#divide
elif range_min >= root.range_min and range_min <= mid_point and range_max >= mid_point and range_max <= root.range_max:
print "Go left & right"
print "left min {0} max {1} right min {2} max {3}".format(range_min,mid_point,mid_point+1,range_max)
self.change_range_value(root.left, range_min,mid_point,value)
self.change_range_value(root.right, mid_point+1,range_max,value)
else:
print "else"
print "min {0} max {1}".format(range_min,range_max)
def shandom_ruffle_naive(a,b,A):
a-=1
b-=1
bStart = b
while a<bStart and b<=len(A):
temp = A[a]
A[a] = A[b]
A[b] = temp
a+=1
b+=1
return A
def random_shuffle():
#handle input
#n = map(int,raw_input())
n =4
ABs=[(3,1),(1,3),(3,2),(2,3)]
tree = RangeTree(n)
for _ in xrange(n):
#a,b = map(int,raw_input().split())
a,b = ABs[_]
if (a<b):
if (b-a< n -b ):
tree.change_range_value(tree.root,a,b-1,b-a)
tree.change_range_value(tree.root,b,2*b-a,a-b)
else:
tree.change_range_value(tree.root,a,b,b-a)
tree.change_range_value(tree.root,b,n,a-b)
else:
continue
return tree
# ABs.append(map(int,raw_input().split()))
def main():
#print "hello"
tree =random_shuffle()
#tree.change_range_value(tree.root,1,3,-1)
#print
#tree.change_range_value(tree.root,2,3,-1)
tree.traverse(tree.root)
if __name__ == "__main__":
main()