Skip to content

Commit 9340ab7

Browse files
committed
Reformat all code
1 parent de9d66a commit 9340ab7

File tree

25 files changed

+601
-638
lines changed

25 files changed

+601
-638
lines changed

Absolute Value Sort/absSort.py

+27-27
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
def absSort(arr):
2-
abs_array = []
3-
4-
for i in range(len(arr)):
5-
if arr[i] < 0:
6-
abs_array.append([arr[i] * -1, -1])
7-
elif arr[i] > 0:
8-
abs_array.append([arr[i], 1])
9-
else:
10-
abs_array.append([0, 1])
11-
12-
min_index = 0
13-
for i in range(len(abs_array)):
14-
min_index = i
15-
for j in range(i+1, len(abs_array)):
16-
if abs_array[min_index][0] > abs_array[j][0]:
17-
min_index = j
18-
elif abs_array[min_index][0] == abs_array[j][0]:
19-
if abs_array[min_index][1] > abs_array[j][1]:
20-
min_index = j
21-
abs_array[min_index], abs_array[i] = abs_array[i], abs_array[min_index]
22-
23-
#print("Swap {} with {}". format(abs_array[min_index], abs_array[j]))
24-
25-
result = []
26-
for i in range(len(abs_array)):
27-
result.append(abs_array[i][0] * abs_array[i][1])
28-
return result
2+
abs_array = []
3+
4+
for i in range(len(arr)):
5+
if arr[i] < 0:
6+
abs_array.append([arr[i] * -1, -1])
7+
elif arr[i] > 0:
8+
abs_array.append([arr[i], 1])
9+
else:
10+
abs_array.append([0, 1])
11+
12+
min_index = 0
13+
for i in range(len(abs_array)):
14+
min_index = i
15+
for j in range(i + 1, len(abs_array)):
16+
if abs_array[min_index][0] > abs_array[j][0]:
17+
min_index = j
18+
elif abs_array[min_index][0] == abs_array[j][0]:
19+
if abs_array[min_index][1] > abs_array[j][1]:
20+
min_index = j
21+
abs_array[min_index], abs_array[i] = abs_array[i], abs_array[min_index]
22+
23+
# print("Swap {} with {}". format(abs_array[min_index], abs_array[j]))
24+
25+
result = []
26+
for i in range(len(abs_array)):
27+
result.append(abs_array[i][0] * abs_array[i][1])
28+
return result

Absolute Value Sort/test.py

+26-27
Original file line numberDiff line numberDiff line change
@@ -4,56 +4,55 @@
44

55
class test_absSort(unittest.TestCase):
66
def testcase_1(self):
7-
myInput = [2,-7,-2,-2,0]
8-
expected = [0,-2,-2,2,-7]
7+
myInput = [2, -7, -2, -2, 0]
8+
expected = [0, -2, -2, 2, -7]
99
self.assertEqual(absSort(myInput), expected)
1010

1111
def testcase_2(self):
12-
myInput = [-2,1]
13-
expected = [1,-2]
12+
myInput = [-2, 1]
13+
expected = [1, -2]
1414
self.assertEqual(absSort(myInput), expected)
15-
15+
1616
def testcase_3(self):
17-
myInput = [0,1,2]
18-
expected =[0,1,2]
17+
myInput = [0, 1, 2]
18+
expected = [0, 1, 2]
1919
self.assertEqual(absSort(myInput), expected)
20-
20+
2121
def testcase_4(self):
22-
myInput = [2,-1,-1,-1]
23-
expected =[-1,-1,-1,2]
22+
myInput = [2, -1, -1, -1]
23+
expected = [-1, -1, -1, 2]
2424
self.assertEqual(absSort(myInput), expected)
25-
25+
2626
def testcase_5(self):
27-
myInput = [-2,3,5,-1,4]
28-
expected = [-1,-2,3,4,5]
27+
myInput = [-2, 3, 5, -1, 4]
28+
expected = [-1, -2, 3, 4, 5]
2929
self.assertEqual(absSort(myInput), expected)
30-
30+
3131
def testcase_6(self):
32-
myInput = [4,-1,6,-4,2,-1]
33-
expected = [-1,-1,2,-4,4,6]
32+
myInput = [4, -1, 6, -4, 2, -1]
33+
expected = [-1, -1, 2, -4, 4, 6]
3434
self.assertEqual(absSort(myInput), expected)
35-
35+
3636
def testcase_7(self):
37-
myInput = [6,4,-5,0,-1,7,0]
38-
expected =[0,0,-1,4,-5,6,7]
37+
myInput = [6, 4, -5, 0, -1, 7, 0]
38+
expected = [0, 0, -1, 4, -5, 6, 7]
3939
self.assertEqual(absSort(myInput), expected)
40-
40+
4141
def testcase_8(self):
42-
myInput = [-7,-2,-2,6,5,-6,-2,-6]
43-
expected = [-2,-2,-2,5,-6,-6,6,-7]
42+
myInput = [-7, -2, -2, 6, 5, -6, -2, -6]
43+
expected = [-2, -2, -2, 5, -6, -6, 6, -7]
4444

4545
self.assertEqual(absSort(myInput), expected)
46-
46+
4747
def testcase_9(self):
48-
myInput =[-4,9,-1,1,-1,2,-8,-6,3]
49-
expected = [-1,-1,1,2,3,-4,-6,-8,9]
48+
myInput = [-4, 9, -1, 1, -1, 2, -8, -6, 3]
49+
expected = [-1, -1, 1, 2, 3, -4, -6, -8, 9]
5050
self.assertEqual(absSort(myInput), expected)
5151

5252

53-
54-
5553
def main():
5654
unittest.main()
5755

56+
5857
if __name__ == "__main__":
5958
main()
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
11
def index_equals_value_search(arr):
2-
size = len(arr)
3-
result = -1
4-
result = helper(0, size-1, arr)
5-
return result
6-
7-
2+
size = len(arr)
3+
result = -1
4+
result = helper(0, size - 1, arr)
5+
return result
6+
7+
88
def helper(lower, upper, arr):
9-
mid = (upper + lower)// 2
10-
val = arr[mid]
11-
12-
if(lower > upper):
13-
return -1
14-
if(val > mid):
15-
return helper(lower, mid-1, arr)
16-
17-
elif(val < mid):
18-
return helper(mid+1, upper, arr)
19-
20-
else:
21-
return val
22-
23-
9+
mid = (upper + lower) // 2
10+
val = arr[mid]
11+
12+
if lower > upper:
13+
return -1
14+
if val > mid:
15+
return helper(lower, mid - 1, arr)
16+
17+
elif val < mid:
18+
return helper(mid + 1, upper, arr)
19+
20+
else:
21+
return val

Array Index & Element Equality/test.py

+10-13
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,39 @@
44

55
class test_flatten_a_dicionary(unittest.TestCase):
66
def testcase_1(self):
7-
myInput = [0]
7+
myInput = [0]
88
expected = 0
99
self.assertEqual(index_equals_value_search(myInput), expected)
1010

11-
1211
def testcase_2(self):
13-
myInput = [0,3]
12+
myInput = [0, 3]
1413
expected = 0
1514
self.assertEqual(index_equals_value_search(myInput), expected)
16-
15+
1716
def testcase_3(self):
18-
myInput = [-8,0,1,3,5]
19-
expected = 3
17+
myInput = [-8, 0, 1, 3, 5]
18+
expected = 3
2019
self.assertEqual(index_equals_value_search(myInput), expected)
2120

22-
2321
def testcase_4(self):
24-
myInput = [-5,0,2,3,10,29]
25-
expected = 2
22+
myInput = [-5, 0, 2, 3, 10, 29]
23+
expected = 2
2624
self.assertEqual(index_equals_value_search(myInput), expected)
2725

2826
def testcase_5(self):
29-
myInput = [-5,0,3,4,10,18,27]
27+
myInput = [-5, 0, 3, 4, 10, 18, 27]
3028
expected = -1
3129
self.assertEqual(index_equals_value_search(myInput), expected)
3230

3331
def testcase_6(self):
34-
myInput = [-6,-5,-4,-1,1,3,5,7]
32+
myInput = [-6, -5, -4, -1, 1, 3, 5, 7]
3533
expected = 7
3634
self.assertEqual(index_equals_value_search(myInput), expected)
3735

3836

39-
40-
4137
def main():
4238
unittest.main()
4339

40+
4441
if __name__ == "__main__":
4542
main()
+23-28
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,27 @@
1-
21
import collections
2+
3+
34
def find_array_quadruplet(arr, s):
4-
arr.sort()
5-
myTable = {}
6-
for i in arr:
7-
if i in myTable:
8-
myTable[i] +=1
9-
else:
10-
myTable[i] = 1
11-
12-
for i in range(len(arr)-2):
13-
for j in range(i+1, len(arr)-1):
14-
for k in range(j+1, len(arr)):
15-
myTable[arr[i]] -= 1
16-
myTable[arr[j]] -= 1
17-
myTable[arr[k]] -= 1
18-
mySum = s - (arr[i] + arr[j] + arr[k])
19-
if mySum in myTable and myTable[mySum] > 0:
20-
return [arr[i], arr[j], arr[k], mySum]
21-
22-
myTable[arr[i]] += 1
23-
myTable[arr[j]] += 1
24-
myTable[arr[k]] += 1
25-
26-
return []
27-
28-
5+
arr.sort()
6+
myTable = {}
7+
for i in arr:
8+
if i in myTable:
9+
myTable[i] += 1
10+
else:
11+
myTable[i] = 1
12+
13+
for i in range(len(arr) - 2):
14+
for j in range(i + 1, len(arr) - 1):
15+
for k in range(j + 1, len(arr)):
16+
myTable[arr[i]] -= 1
17+
myTable[arr[j]] -= 1
18+
myTable[arr[k]] -= 1
19+
mySum = s - (arr[i] + arr[j] + arr[k])
20+
if mySum in myTable and myTable[mySum] > 0:
21+
return [arr[i], arr[j], arr[k], mySum]
2922

30-
31-
23+
myTable[arr[i]] += 1
24+
myTable[arr[j]] += 1
25+
myTable[arr[k]] += 1
3226

27+
return []

Array Quadruplet/test.py

+25-27
Original file line numberDiff line numberDiff line change
@@ -4,53 +4,51 @@
44

55
class test_absSort(unittest.TestCase):
66
def testcase_1(self):
7-
myInput = []
7+
myInput = []
88
s = 12
99
expected = []
10-
self.assertEqual(find_array_quadruplet(myInput,s), expected)
10+
self.assertEqual(find_array_quadruplet(myInput, s), expected)
1111

1212
def testcase_2(self):
13-
myInput = [4,4,4]
14-
s = 12
13+
myInput = [4, 4, 4]
14+
s = 12
1515
expected = []
16-
self.assertEqual(find_array_quadruplet(myInput,s), expected)
17-
16+
self.assertEqual(find_array_quadruplet(myInput, s), expected)
17+
1818
def testcase_3(self):
19-
myInput = [4,4,4,2]
19+
myInput = [4, 4, 4, 2]
2020
s = 16
21-
expected =[]
22-
self.assertEqual(find_array_quadruplet(myInput,s), expected)
23-
21+
expected = []
22+
self.assertEqual(find_array_quadruplet(myInput, s), expected)
23+
2424
def testcase_4(self):
25-
myInput = [4,4,4,4]
25+
myInput = [4, 4, 4, 4]
2626
s = 16
27-
expected =[4,4,4,4]
28-
self.assertEqual(find_array_quadruplet(myInput,s), expected)
29-
27+
expected = [4, 4, 4, 4]
28+
self.assertEqual(find_array_quadruplet(myInput, s), expected)
29+
3030
def testcase_5(self):
31-
myInput = [2,7,4,0,9,5,1,3]
31+
myInput = [2, 7, 4, 0, 9, 5, 1, 3]
3232
s = 20
33-
expected = [0,4,7,9]
34-
self.assertEqual(find_array_quadruplet(myInput,s), expected)
35-
33+
expected = [0, 4, 7, 9]
34+
self.assertEqual(find_array_quadruplet(myInput, s), expected)
35+
3636
def testcase_6(self):
37-
myInput = [2,7,4,0,9,5,1,3]
37+
myInput = [2, 7, 4, 0, 9, 5, 1, 3]
3838
s = 120
3939
expected = []
40-
self.assertEqual(find_array_quadruplet(myInput,s), expected)
41-
40+
self.assertEqual(find_array_quadruplet(myInput, s), expected)
41+
4242
def testcase_7(self):
43-
myInput = [1,2,3,4,5,9,19,12,12,19]
43+
myInput = [1, 2, 3, 4, 5, 9, 19, 12, 12, 19]
4444
s = 40
45-
expected =[4,5,12,19]
45+
expected = [4, 5, 12, 19]
4646
self.assertEqual(find_array_quadruplet(myInput, s), expected)
47-
48-
49-
50-
47+
5148

5249
def main():
5350
unittest.main()
5451

52+
5553
if __name__ == "__main__":
5654
main()

0 commit comments

Comments
 (0)