tangled
alpha
login
or
join now
thecoded.prof
/
CMU
0
fork
atom
CMU Coding Bootcamp
0
fork
atom
overview
issues
pulls
pipelines
feat: oct 3 level 2
thecoded.prof
5 months ago
b98b09c3
929cf363
verified
This commit was signed with the committer's
known signature
.
thecoded.prof
SSH Key Fingerprint:
SHA256:ePn0u8NlJyz3J4Zl9MHOYW3f4XKoi5K1I4j53bwpG0U=
+174
4 changed files
expand all
collapse all
unified
split
python
oct3
level2
isNearlySorted.py
repeatingPattern.py
sortEvens.py
subCipher.py
+40
python/oct3/level2/isNearlySorted.py
···
1
1
+
from typing import Sequence, Literal, Set, Tuple
2
2
+
from copy import deepcopy
3
3
+
4
4
+
5
5
+
def isNearlySorted(L: Sequence[float]) -> Literal[False] | Sequence[float]:
6
6
+
"""Returns a tuple of indices that need to be swapped to sort the list, or False if it cannot be sorted with a single swap."""
7
7
+
if not len(L):
8
8
+
return False
9
9
+
sortPath: Set[Tuple[int, int]] = set()
10
10
+
sortedList = list(deepcopy(L))
11
11
+
for i in range(len(L) - 1):
12
12
+
for j in range(len(L) - 1, i, -1):
13
13
+
a = sortedList[i]
14
14
+
b = sortedList[j]
15
15
+
if a > b:
16
16
+
sortedList[i], sortedList[j] = b, a
17
17
+
sortPath.add((i, j))
18
18
+
if len(sortPath) > 1:
19
19
+
return False
20
20
+
if len(sortPath) == 1:
21
21
+
return list(sortPath.pop())
22
22
+
return False
23
23
+
24
24
+
25
25
+
print("Testing isNearlySorted()...", end="")
26
26
+
assert isNearlySorted([2, 7, 5, 4]) == [1, 3]
27
27
+
assert isNearlySorted([2, 3, 1]) == False
28
28
+
assert isNearlySorted([1, 2, 3]) == False
29
29
+
assert isNearlySorted([-1, 1, 0, 5]) == [1, 2]
30
30
+
assert isNearlySorted([5, 6, 9, 8, 8]) == [2, 4]
31
31
+
assert isNearlySorted([2, 1]) == [0, 1]
32
32
+
assert isNearlySorted([3, 3]) == False
33
33
+
assert isNearlySorted([2]) == False
34
34
+
assert isNearlySorted([]) == False
35
35
+
36
36
+
# Verify that the function is non-mutating
37
37
+
L = [2, 7, 5, 4]
38
38
+
isNearlySorted(L)
39
39
+
assert L == [2, 7, 5, 4]
40
40
+
print("Passed!")
+40
python/oct3/level2/repeatingPattern.py
···
1
1
+
from typing import List, Optional, TypeVar
2
2
+
3
3
+
4
4
+
T = TypeVar("T")
5
5
+
6
6
+
7
7
+
def repeatingPattern(L: List[T]) -> Optional[List[T]]:
8
8
+
"""Returns the smallest repeating pattern of a list or None if no pattern exists."""
9
9
+
if len(L) < 2:
10
10
+
return None
11
11
+
repeat_options = list(
12
12
+
reversed([i for i in range(2, max(len(L) // 2 + 1, 3)) if len(L) % i == 0])
13
13
+
)
14
14
+
M = [L[0]]
15
15
+
for repeat in repeat_options:
16
16
+
M = L[: len(L) // repeat]
17
17
+
if M * repeat == L:
18
18
+
return M
19
19
+
if len(M) > len(L):
20
20
+
return None
21
21
+
return None
22
22
+
23
23
+
24
24
+
print("Testing repeatingPattern()...", end="")
25
25
+
assert repeatingPattern([1, 2, 1, 2]) == [1, 2]
26
26
+
assert repeatingPattern([1, 2, 1, 2, 1, 2, 1, 2]) == [1, 2]
27
27
+
assert repeatingPattern([1, 2, 3, 1, 2, 3]) == [1, 2, 3]
28
28
+
assert repeatingPattern([1, 1]) == [1]
29
29
+
assert repeatingPattern([]) == None
30
30
+
assert repeatingPattern([42]) == None
31
31
+
assert repeatingPattern([1, 2, 3, 1, 2]) == None
32
32
+
assert repeatingPattern([121, 212, 12]) == None
33
33
+
L = [1, 2, 3, 4]
34
34
+
assert repeatingPattern(L * 20) == L
35
35
+
36
36
+
# Finally, verify that the function is non-mutating
37
37
+
L = [1, 2, 1, 2]
38
38
+
repeatingPattern(L)
39
39
+
assert L == [1, 2, 1, 2]
40
40
+
print("Passed!")
+37
python/oct3/level2/sortEvens.py
···
1
1
+
from typing import List
2
2
+
3
3
+
4
4
+
def sortEvens(L: List[int]):
5
5
+
"""Sorts the even elements of a list in ascending order."""
6
6
+
modifiable_indecies = [i for i, x in enumerate(L) if x % 2 == 0]
7
7
+
sorted_evens = sorted([L[i] for i in modifiable_indecies])
8
8
+
9
9
+
for i, x in enumerate(sorted_evens):
10
10
+
L[modifiable_indecies[i]] = x
11
11
+
12
12
+
13
13
+
print("Testing sortEvens()...", end="")
14
14
+
L = [1, 8, 4, 9, -2, 7]
15
15
+
assert sortEvens(L) == None
16
16
+
assert L == [1, -2, 4, 9, 8, 7]
17
17
+
18
18
+
L = [1, 4, 3, 2, 5]
19
19
+
assert sortEvens(L) == None
20
20
+
assert L == [1, 2, 3, 4, 5]
21
21
+
22
22
+
L = [1, 3, 2, 5]
23
23
+
assert sortEvens(L) == None
24
24
+
assert L == [1, 3, 2, 5]
25
25
+
26
26
+
L = [3, 6, 4, -2, 8, 4, 0, 5]
27
27
+
assert sortEvens(L) == None
28
28
+
assert L == [3, -2, 0, 4, 4, 6, 8, 5]
29
29
+
30
30
+
L = [1, 3, 5]
31
31
+
assert sortEvens(L) == None
32
32
+
assert L == [1, 3, 5]
33
33
+
34
34
+
L = []
35
35
+
assert sortEvens(L) == None
36
36
+
assert L == []
37
37
+
print("Passed!")
+57
python/oct3/level2/subCipher.py
···
1
1
+
from string import ascii_lowercase, ascii_uppercase
2
2
+
3
3
+
4
4
+
def encodeSubstitutionCipher(msg: str, key: str) -> str:
5
5
+
"""Encode a message using a substitution cipher."""
6
6
+
unique = set(msg)
7
7
+
for c in unique:
8
8
+
if c.isalpha():
9
9
+
if c.isupper():
10
10
+
encoded = key[ord(c) - ord("A")]
11
11
+
else:
12
12
+
encoded = key[ord(c) - ord("a")]
13
13
+
encoded = encoded.lower()
14
14
+
msg = msg.replace(c, encoded)
15
15
+
return msg
16
16
+
17
17
+
18
18
+
def decodeSubstitutionCipher(encodedMsg: str, key: str) -> str:
19
19
+
"""Decode a message using a substitution cipher."""
20
20
+
unique = set(encodedMsg)
21
21
+
for c in unique:
22
22
+
if c.isalpha():
23
23
+
if c.isupper():
24
24
+
decoded = ascii_uppercase[key.index(c.upper())]
25
25
+
else:
26
26
+
decoded = ascii_lowercase[key.index(c.upper())]
27
27
+
encodedMsg = encodedMsg.replace(c, decoded)
28
28
+
return encodedMsg
29
29
+
30
30
+
31
31
+
print("Testing encodeSubstitutionCipher()...", end="")
32
32
+
assert encodeSubstitutionCipher("CAB", "SQGYFEZXLANKJIMPURDCWTHVOB") == "GSQ"
33
33
+
assert encodeSubstitutionCipher("Cab Z?", "SQGYFEZXLANKJIMPURDCWTHVOB") == "Gsq B?"
34
34
+
assert (
35
35
+
encodeSubstitutionCipher("Hello, World!", "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
36
36
+
== "Hello, World!"
37
37
+
)
38
38
+
assert (
39
39
+
encodeSubstitutionCipher("42 - 0 = 42", "XHNSBMTOPQWGZDCEAVLKJYIURF")
40
40
+
== "42 - 0 = 42"
41
41
+
)
42
42
+
assert encodeSubstitutionCipher("", "XHNSBMTOPQWGZDCEAVLKJYIURF") == ""
43
43
+
44
44
+
45
45
+
print("Testing decodeSubstitutionCipher()...", end="")
46
46
+
assert decodeSubstitutionCipher("GSQ", "SQGYFEZXLANKJIMPURDCWTHVOB") == "CAB"
47
47
+
assert decodeSubstitutionCipher("Gsq B?", "SQGYFEZXLANKJIMPURDCWTHVOB") == "Cab Z?"
48
48
+
assert (
49
49
+
decodeSubstitutionCipher("Hello, World!", "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
50
50
+
== "Hello, World!"
51
51
+
)
52
52
+
assert (
53
53
+
decodeSubstitutionCipher("42 - 0 = 42", "XHNSBMTOPQWGZDCEAVLKJYIURF")
54
54
+
== "42 - 0 = 42"
55
55
+
)
56
56
+
assert decodeSubstitutionCipher("", "XHNSBMTOPQWGZDCEAVLKJYIURF") == ""
57
57
+
print("Passed!")