tangled
alpha
login
or
join now
thecoded.prof
/
CMU
0
fork
atom
CMU Coding Bootcamp
0
fork
atom
overview
issues
pulls
pipelines
feat: oct 8 level 1
thecoded.prof
4 months ago
74d84bf6
abbce9ce
verified
This commit was signed with the committer's
known signature
.
thecoded.prof
SSH Key Fingerprint:
SHA256:ePn0u8NlJyz3J4Zl9MHOYW3f4XKoi5K1I4j53bwpG0U=
+134
3 changed files
expand all
collapse all
unified
split
python
oct8
level1
insertRowAndCol.py
isRectangular.py
largestColumnSum.py
+80
python/oct8/level1/insertRowAndCol.py
···
1
1
+
from typing import List
2
2
+
3
3
+
4
4
+
def mutatingInsertRowAndCol(L: List[List[int]], row: int, col: int, val: int):
5
5
+
"""Inserts a row and column into a matrix."""
6
6
+
L.insert(row, [val] * len(L[0]))
7
7
+
for i in range(len(L)):
8
8
+
L[i].insert(col, val)
9
9
+
10
10
+
11
11
+
def nonmutatingInsertRowAndCol(
12
12
+
L: List[List[int]], row: int, col: int, val: int
13
13
+
) -> List[List[int]]:
14
14
+
"""Inserts a row and column into a matrix without mutating the original matrix."""
15
15
+
new_list = [[v for v in l] for l in L]
16
16
+
17
17
+
new_list.insert(row, [val] * len(new_list[0]))
18
18
+
for i in range(len(new_list)):
19
19
+
new_list[i].insert(col, val)
20
20
+
21
21
+
return new_list
22
22
+
23
23
+
24
24
+
def testMutatingInsertRowAndCol():
25
25
+
print("Testing mutatingInsertRowAndCol()...", end="")
26
26
+
L = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
27
27
+
mutatingInsertRowAndCol(L, 1, 2, 42)
28
28
+
assert L == [
29
29
+
[1, 2, 42, 3, 4],
30
30
+
[42, 42, 42, 42, 42],
31
31
+
[5, 6, 42, 7, 8],
32
32
+
[9, 10, 42, 11, 12],
33
33
+
]
34
34
+
35
35
+
L = [[1, 2, 3], [4, 5, 6]]
36
36
+
mutatingInsertRowAndCol(L, 0, 0, 5)
37
37
+
assert L == [[5, 5, 5, 5], [5, 1, 2, 3], [5, 4, 5, 6]]
38
38
+
39
39
+
L = [[1, 2, 3], [4, 5, 6]]
40
40
+
mutatingInsertRowAndCol(L, 1, 3, 5)
41
41
+
assert L == [[1, 2, 3, 5], [5, 5, 5, 5], [4, 5, 6, 5]]
42
42
+
43
43
+
44
44
+
def testNonmutatingInsertRowAndCol():
45
45
+
print("Testing nonmutatingInsertRowAndCol()...", end="")
46
46
+
L = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
47
47
+
assert nonmutatingInsertRowAndCol(L, 1, 2, 42) == [
48
48
+
[1, 2, 42, 3, 4],
49
49
+
[42, 42, 42, 42, 42],
50
50
+
[5, 6, 42, 7, 8],
51
51
+
[9, 10, 42, 11, 12],
52
52
+
]
53
53
+
54
54
+
L = [[1, 2, 3], [4, 5, 6]]
55
55
+
assert nonmutatingInsertRowAndCol(L, 0, 0, 5) == [
56
56
+
[5, 5, 5, 5],
57
57
+
[5, 1, 2, 3],
58
58
+
[5, 4, 5, 6],
59
59
+
]
60
60
+
61
61
+
L = [[1, 2, 3], [4, 5, 6]]
62
62
+
assert nonmutatingInsertRowAndCol(L, 1, 3, 5) == [
63
63
+
[1, 2, 3, 5],
64
64
+
[5, 5, 5, 5],
65
65
+
[4, 5, 6, 5],
66
66
+
]
67
67
+
68
68
+
# Verify that the function is non-mutating
69
69
+
L = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
70
70
+
nonmutatingInsertRowAndCol(L, 1, 2, 42)
71
71
+
assert L == [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
72
72
+
print("Passed!")
73
73
+
74
74
+
75
75
+
def main():
76
76
+
testMutatingInsertRowAndCol()
77
77
+
testNonmutatingInsertRowAndCol()
78
78
+
79
79
+
80
80
+
main()
+24
python/oct8/level1/isRectangular.py
···
1
1
+
from typing import List
2
2
+
3
3
+
4
4
+
def isRectangular(L: List[List[int]]) -> bool:
5
5
+
"""Check if the length of each row is equal to the length of the first row."""
6
6
+
return all(len(row) == len(L[0]) for row in L)
7
7
+
8
8
+
9
9
+
def testIsRectangular():
10
10
+
print("Testing isRectangular()...", end="")
11
11
+
L = [[1, 2, 3], [4, 5, 6]]
12
12
+
assert isRectangular(L) == True
13
13
+
L = [[1, 2], [4, 5, 6]]
14
14
+
assert isRectangular(L) == False
15
15
+
L = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
16
16
+
assert isRectangular(L) == True
17
17
+
print("Passed!")
18
18
+
19
19
+
20
20
+
def main():
21
21
+
testIsRectangular()
22
22
+
23
23
+
24
24
+
main()
+30
python/oct8/level1/largestColumnSum.py
···
1
1
+
from typing import List
2
2
+
3
3
+
4
4
+
def largestColumnSum(L: List[List[int]]) -> int:
5
5
+
"""Return the largest sum of any column in the matrix."""
6
6
+
cols = [sum(col) for col in zip(*L)]
7
7
+
return max(cols)
8
8
+
9
9
+
10
10
+
def testLargestColumnSum():
11
11
+
print("Testing largestColumnSum()...", end="")
12
12
+
L = [[1, 2, 3], [4, 5, 6]]
13
13
+
assert largestColumnSum(L) == 9 # sum([3, 6])
14
14
+
15
15
+
L = [[3, 2, 1], [-5, -3, -6]]
16
16
+
assert largestColumnSum(L) == -1 # sum([2, -3])
17
17
+
18
18
+
L = [[1, 2, 3], [4, 5, 6], [7, -1, -2]]
19
19
+
assert largestColumnSum(L) == 12 # sum([1, 4, 7])
20
20
+
21
21
+
L = [[7, -1, -2]]
22
22
+
assert largestColumnSum(L) == 7 # sum([7])
23
23
+
print("Passed!")
24
24
+
25
25
+
26
26
+
def main():
27
27
+
testLargestColumnSum()
28
28
+
29
29
+
30
30
+
main()