-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfun_post_for_blog.py
221 lines (161 loc) · 6.81 KB
/
fun_post_for_blog.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
""" just because you can doesn't mean you should:
a python function written in normal and idiomatic python
then contrast it with cryptic python, compare the best practices
to the frankenstein method
I will do it on a function that takes two sorted lists and returns a single sorted
list with no duplicates
"""
#simplest starting answer
def merge_sorted(list_a, list_b):
""" take two lists of integers sorted smallest to largest,
merge them to a single output list with
no duplicate values """
output_list = []
while (len(list_a) > 0) and (len(list_b) > 0):
if list_a[0] == list_b[0]:
output_list.append(list_a.pop(0))
list_b.pop(0)
elif list_a[0] < list_b[0]:
output_list.append(list_a.pop(0))
elif list_a[0] > list_b[0]:
output_list.append(list_b.pop(0))
if len(list_a) > 0:
output_list.extend(list_a)
elif len(list_b) > 0:
output_list.extend(list_b)
return output_list
#weird stuff to stay in the constraints
globals().__setitem__('x', 10)
#instead of
x = 10
len(list_a) is not 0
#instead of
len(list_a) > 0
list_a[0] is list_b[0]
#instead of
list_a[0] == list_b[0]
# that isn't that much worse, but generally is should not be used as a direct
# equivalent to == it works here but that is by no means true in all context.
# just remember:
a = [1,2,3]
b = [1,2,3]
a == b
a is b
abs(list_b[0] - list_a[0]) is (list_b[0] - list_a[0])
#instead of
list_a[0] < list_b[0]
#this one is messier than the is substitute for ==
# we don't just need to know if they differ, but we also need to know which
# of the two is the smaller number (so that we can append that to the output)
# I use the abs() to make this work. The logic isn't the most linear thing,
# but here is why this works.
# take the first member of list_a, subtract the first member of list_b
# this will give us a positive (list_a's number is bigger) or
# negative (list_b's number is bigger). The case where they are equal is already
# dealt with by the preceeding if statement.
# abs() will turn a negative integer to a positive one (absloute value)
# while a positive number will remain a positive integer
# so if abs() changes the sign of the int,
abs(list_a[0] - list_b[0]) is (list_a[0] - list_b[0])
# will evaluate to false, and we know that the list_a value is smaller than the list_b value
#building this in to the function we can use both an is and is not
#elif function to control the logic and evaluate to true in opposite instances
#when this is true the list_a value is smaller
abs(list_a[0] - list_b[0]) is not (list_a[0] - list_b[0])
#when this is tru the list_b value is smaller
abs(list_a[0] - list_b[0]) is (list_a[0] - list_b[0])
#clear as mud right? do you miss the > and < signs yet?
def constrained_merge_sorted(list_a, list_b):
globals().__setitem__('output_list' , list_a * 0)
while (len(list_a) is not 0) and (len(list_b) is not 0):
if (list_a[0]) is (list_b[0]):
globals().__setitem__('output_list', output_list + [list_a.pop(0)])
list_b.pop(0)
elif abs(list_a[0] - list_b[0]) is not (list_a[0] - list_b[0]):
globals().__setitem__('output_list', output_list + [list_a.pop(0)])
elif abs(list_a[0] - list_b[0]) is (list_a[0] - list_b[0]):
globals().__setitem__('output_list', output_list + [list_b.pop(0)])
if len(list_a) is not 0:
return output_list + list_a
elif len(list_b) is not 0:
return output_list + list_b
else:
return output_list
def constrained_merge_sorted(list_a, list_b):
# multiplying another list by zero is a valid way to get an empty a list
# this wasn't a constraint, but I do it because its silly
globals().__setitem__('output_list' , list_a * 0)
#while statement here
while (len(list_a) is not 0) and (len(list_b) is not 0):
#equivalent to: list_a[0] == list_b[0]
if (list_a[0]) is (list_b[0]):
# output_list.append(list_a.pop(0))
globals().__setitem__('output_list', output_list + [list_a.pop(0)])
#hey this is the same :)
list_b.pop(0)
#equivalent to: list_a[0] < list_b[0]
elif abs(list_a[0] - list_b[0]) is not (list_a[0] - list_b[0]):
# output_list.append(list_a.pop(0))
globals().__setitem__('output_list', output_list + [list_a.pop(0)])
#equivalent to: list_a[0] > list_b[0]
elif abs(list_a[0] - list_b[0]) is (list_a[0] - list_b[0]):
# output_list.append(list_b.pop(0))
globals().__setitem__('output_list', output_list + [list_b.pop(0)])
# to avoid more hideous globals().__setitem__() calls, I here return
# straight out of the if and elif as oppsed to appending to output_list
if len(list_a) is not 0:
return output_list + list_a
elif len(list_b) is not 0:
return output_list + list_b
else:
return output_list
list_a = [1,3,4,5]
list_b = [2,5,6,7,8]
constrained_merge_sorted(list_a, list_b)
x = [1,2,3,7]
y = [2,3,6,8,9]
merge_sorted(x, y)
def merge_sorted(list_a, list_b):
""" take two lists of integers sorted smallest to largest,
merge them to a single output list with no duplicate values """
output_list = []
while (len(list_a) > 0) and (len(list_b) > 0):
if list_a[0] == list_b[0]:
output_list.append(list_a.pop(0))
list_b.pop(0)
elif list_a[0] < list_b[0]:
output_list.append(list_a.pop(0))
elif list_a[0] > list_b[0]:
output_list.append(list_b.pop(0))
if len(list_a) > 0:
output_list.extend(list_a)
elif len(list_b) > 0:
output_list.extend(list_b)
return output_list
def constrained_merge_sorted(list_a, list_b):
# multiplying another list by zero is a valid way to get an empty a list
# this wasn't a constraint, but I do it because its silly
globals().__setitem__('output_list' , list_a * 0)
while (len(list_a) is not 0) and (len(list_b) is not 0):
#equivalent to: list_a[0] == list_b[0]
if (list_a[0]) is (list_b[0]):
# output_list.append(list_a.pop(0))
globals().__setitem__('output_list', output_list + [list_a.pop(0)])
#hey this is the same :)
list_b.pop(0)
#equivalent to: list_a[0] < list_b[0]
elif abs(list_a[0] - list_b[0]) is not (list_a[0] - list_b[0]):
# output_list.append(list_a.pop(0))
globals().__setitem__('output_list', output_list + [list_a.pop(0)])
#equivalent to: list_a[0] > list_b[0]
elif abs(list_a[0] - list_b[0]) is (list_a[0] - list_b[0]):
# output_list.append(list_b.pop(0))
globals().__setitem__('output_list', output_list + [list_b.pop(0)])
# to avoid more hideous globals().__setitem__() calls, I here return
# straight out of the if and elif as oppsed to appending to output_list
if len(list_a) is not 0:
return output_list + list_a
elif len(list_b) is not 0:
return output_list + list_b
else:
return output_list