-
Notifications
You must be signed in to change notification settings - Fork 0
/
code_snippets.py
421 lines (361 loc) · 9.27 KB
/
code_snippets.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
# Code snippets to be used in the survey. Snippets should include either
# similar or different identifiers, validated using objective criteria
# from an IS paper. The relevant lines should be flagged with a comment
# orthographic, phonological, semantic
# funcx_a -> function without identifier similarity
# funcx_b -> function with identifier similarity
# -------------------------------------------------------------------------- #
# SNIPPET 1: SENTENCE TO ALL CAPS
# ORTHOGRAPHIC
def func1_a(t):
a = ""
for u in t:
if u.isalpha():
# get ascii value of b
x = ord(u)
x -= 32
# convert ascii number to char
z = chr(x)
a += z
else:
a += u
return a
def func1_b(p):
q = ""
for b in p:
if b.isalpha():
# get ascii value of b
g = ord(b)
g -= 32
# convert ascii number to char
d = chr(g)
q += d
else:
q += b
return q
print("func1")
print(
func1_b("in 2 years, i will get three new pets!")
)
print("\n\n")
# -------------------------------------------------------------------------- #
# SNIPPET 2: SORT EVENS ASCENDING, ODDS DESCENDING
# ORTHOGRAPHIC
def func2_a(a):
# sort the list in ascending order
a = sorted(a, reverse=False)
l = len(a)
u = []
t = []
for i in range(l):
if a[i] % 2 == 0:
u.append(a[i])
else:
t.insert(0, a[i])
return u, t
def func2_b(lst1):
# sort the list in ascending order
lst1 = sorted(lst1, reverse=False)
l = len(lst1)
lst2 = []
lst3 = []
for i in range(l):
if lst1[i] % 2 == 0:
lst2.append(lst1[i])
else:
lst3.insert(0, lst1[i])
return lst2, lst3
print("func2")
print(
func2_b(
[6, 8, 35, 17, 0, 9, 45]
)
)
print("\n\n")
# -------------------------------------------------------------------------- #
# SNIPPET 3: SEPARATE VOWEL WORDS
# ORTHOGRAPHIC
def func3_a(t):
x = ""
u = ""
for z in t.split():
if z[0] in ["a", "e", "i", "o", "u", "y"]:
x = x + z + " "
else:
u = u + z + " "
return x, u
def func3_b(string):
words1 = ""
words2 = ""
for word in string.split():
if word[0] in ["a", "e", "i", "o", "u", "y"]:
words1 = words1 + word + " "
else:
words2 = words2 + word + " "
return words1, words2
print("func3")
print(
str(
func3_b("the inclination angle of some galaxy will affect the component of the")
)
)
print("\n\n")
# -------------------------------------------------------------------------- #
# SNIPPET 4: MOST HOLES IN ONE
# ORTHOGRAPHIC
def func4_a(a, b):
l = len(a)
u = 0
m = 1e9
c = ""
x = ""
for i in range(l):
k = len(b[i])
r = 0
for j in range(k):
if b[i][j] == 1:
r += 1
if r > u:
c = a[i]
u = r
if r < m:
x = a[i]
m = r
return c, x
def func4_b(player_list, scorecard_list):
l = len(player_list)
most_holes = 0
least_holes = 1e9
most_holes_player = ""
least_holes_player = ""
for i in range(l):
k = len(scorecard_list[i])
num_hole_in_ones = 0
for j in range(k):
if scorecard_list[i][j] == 1:
num_hole_in_ones += 1
if num_hole_in_ones > most_holes:
most_holes_player = player_list[i]
most_holes = num_hole_in_ones
if num_hole_in_ones < least_holes:
least_holes_player = player_list[i]
least_holes = num_hole_in_ones
return most_holes_player, least_holes_player
print("func4")
print(
str(
func4_a(
["Milo", "Ben", "Tamsin"],
[
[1, 2, 4, 3, 1, 1, 3],
[4, 2, 1, 1, 1, 3, 1],
[6, 2, 1, 3, 4, 3, 2]
]
) # should print (Ben, Tamsin)
)
)
print("\n\n")
# -------------------------------------------------------------------------- #
# SNIPPET 5: LIST VS ARRAY
# SEMANTIC
# list gets only int
# array gets only strs
# if it's a float, add zero to other list
def func5_a(a, b):
i = 0
while i < len(a):
print(i)
if type(a[i]) == str:
b.append(a[i])
a.remove(a[i])
elif type(a[i]) == float:
b.append("0")
a.remove(a[i])
else:
i += 1
j = 0
while j < len(b):
if type(b[j]) == int:
a.append(b[j])
b.remove(b[j])
elif type(b[j]) == float:
a.append(0)
b.remove(b[j])
else:
j += 1
return a, b
def func5_b(input_list, input_array):
i = 0
while i < len(input_list):
print(i)
if type(input_list[i]) == str:
input_array.append(input_list[i])
input_list.remove(input_list[i])
elif type(input_list[i]) == float:
input_array.append("0")
input_list.remove(input_list[i])
else:
i += 1
j = 0
while j < len(input_array):
if type(input_array[j]) == int:
input_list.append(input_array[j])
input_array.remove(input_array[j])
elif type(input_array[j]) == float:
input_list.append(0)
input_array.remove(input_array[j])
else:
j += 1
return input_list, input_array
print("func5")
print(
func5_a(
[2, "three", "four", 5, 6.7, "seven", "eight", 9, "ten"],
["zero", 1, "two", 4.5, 8, 16, "thirty-two", 64.0]
)
)
print("\n\n")
# -------------------------------------------------------------------------- #
# SNIPPET 6: SMALL STRING, SHORT STRING
# SEMANTIC
def func6_a(a):
b = False
c = False
for x in a:
if len(x) < 5:
b = True
else:
if len(x) < 10:
c = True
if b:
print(x[0])
elif c:
print(b)
else:
print(x[1])
b = False
c = False
def func6_b(str_list):
short_string = False
small_string = False
for little_string in str_list:
if len(little_string) < 5:
short_string = True
else:
if len(little_string) < 10:
small_string = True
if short_string:
print(little_string[0])
elif small_string:
print(short_string)
else:
print(little_string[1])
short_string = False
small_string = False
print("func6")
func6_a(
["treble", "bass", "microphone", "amplifier", "guitar", "glasses", "anyone"]
)
print("\n\n")
# -------------------------------------------------------------------------- #
# SNIPPET 7: CHAR VS LETTER
# SEMANTIC
# if a character is in a string, but the full string isn't in the string, print the char
def func7_a(a, b):
c = []
for x in a:
for y in x:
t = False
if y in b:
t = True
if x in b:
t = False
if t:
c.append(y)
return c
def func7_b(input_list, string):
output_list = []
for char in input_list:
for letter in char:
add = False
if letter in string:
add = True
if char in string:
add = False
if add:
output_list.append(letter)
return output_list
print("func7")
print(
func7_a(
["string", "forget", "about", "i got strings", "whoz dat"],
"strings are what i got strings are what i need"
)
)
print("\n\n")
# -------------------------------------------------------------------------- #
# SNIPPET 8: START AND BEGIN
# SEMANTIC
# if you begin while stopped, then start
# if you begin while started, print
# if you stop while started, then end
# if you stop while stopped, print
def func8_a(a):
l = len(a)
i = 0
b = False
c = False
x = True
y = False
while i < l:
if a[i] != 0 and x == True:
c = True
elif a[i] == 0 and x == True:
print(a[i])
elif a[i] != 0 and b == True:
print(a[i])
elif a[i] == 0 and b == True:
y = True
if y:
x = True
b = False
c = False
y = False
if c:
b = True
x = False
c = False
y = False
i += 1
def func8_b(input_list):
l = len(input_list)
i = 0
start = False
begin = False
stop = True
end = False
while i < l:
if input_list[i] != 0 and stop == True:
begin = True
elif input_list[i] == 0 and stop == True:
print(input_list[i])
elif input_list[i] != 0 and start == True:
print(input_list[i])
elif input_list[i] == 0 and start == True:
end = True
if end:
stop = True
start = False
begin = False
end = False
if begin:
start = True
stop = False
begin = False
end = False
i += 1
print("func8")
func8_a(
[0, 4, 5, 1, 0, 1, 0, 0, 9]
)
print("\n\n")