CMU Coding Bootcamp

feat: oct 3 level 2

thecoded.prof b98b09c3 929cf363

verified
+174
+40
python/oct3/level2/isNearlySorted.py
··· 1 + from typing import Sequence, Literal, Set, Tuple 2 + from copy import deepcopy 3 + 4 + 5 + def isNearlySorted(L: Sequence[float]) -> Literal[False] | Sequence[float]: 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 + if not len(L): 8 + return False 9 + sortPath: Set[Tuple[int, int]] = set() 10 + sortedList = list(deepcopy(L)) 11 + for i in range(len(L) - 1): 12 + for j in range(len(L) - 1, i, -1): 13 + a = sortedList[i] 14 + b = sortedList[j] 15 + if a > b: 16 + sortedList[i], sortedList[j] = b, a 17 + sortPath.add((i, j)) 18 + if len(sortPath) > 1: 19 + return False 20 + if len(sortPath) == 1: 21 + return list(sortPath.pop()) 22 + return False 23 + 24 + 25 + print("Testing isNearlySorted()...", end="") 26 + assert isNearlySorted([2, 7, 5, 4]) == [1, 3] 27 + assert isNearlySorted([2, 3, 1]) == False 28 + assert isNearlySorted([1, 2, 3]) == False 29 + assert isNearlySorted([-1, 1, 0, 5]) == [1, 2] 30 + assert isNearlySorted([5, 6, 9, 8, 8]) == [2, 4] 31 + assert isNearlySorted([2, 1]) == [0, 1] 32 + assert isNearlySorted([3, 3]) == False 33 + assert isNearlySorted([2]) == False 34 + assert isNearlySorted([]) == False 35 + 36 + # Verify that the function is non-mutating 37 + L = [2, 7, 5, 4] 38 + isNearlySorted(L) 39 + assert L == [2, 7, 5, 4] 40 + print("Passed!")
+40
python/oct3/level2/repeatingPattern.py
··· 1 + from typing import List, Optional, TypeVar 2 + 3 + 4 + T = TypeVar("T") 5 + 6 + 7 + def repeatingPattern(L: List[T]) -> Optional[List[T]]: 8 + """Returns the smallest repeating pattern of a list or None if no pattern exists.""" 9 + if len(L) < 2: 10 + return None 11 + repeat_options = list( 12 + reversed([i for i in range(2, max(len(L) // 2 + 1, 3)) if len(L) % i == 0]) 13 + ) 14 + M = [L[0]] 15 + for repeat in repeat_options: 16 + M = L[: len(L) // repeat] 17 + if M * repeat == L: 18 + return M 19 + if len(M) > len(L): 20 + return None 21 + return None 22 + 23 + 24 + print("Testing repeatingPattern()...", end="") 25 + assert repeatingPattern([1, 2, 1, 2]) == [1, 2] 26 + assert repeatingPattern([1, 2, 1, 2, 1, 2, 1, 2]) == [1, 2] 27 + assert repeatingPattern([1, 2, 3, 1, 2, 3]) == [1, 2, 3] 28 + assert repeatingPattern([1, 1]) == [1] 29 + assert repeatingPattern([]) == None 30 + assert repeatingPattern([42]) == None 31 + assert repeatingPattern([1, 2, 3, 1, 2]) == None 32 + assert repeatingPattern([121, 212, 12]) == None 33 + L = [1, 2, 3, 4] 34 + assert repeatingPattern(L * 20) == L 35 + 36 + # Finally, verify that the function is non-mutating 37 + L = [1, 2, 1, 2] 38 + repeatingPattern(L) 39 + assert L == [1, 2, 1, 2] 40 + print("Passed!")
+37
python/oct3/level2/sortEvens.py
··· 1 + from typing import List 2 + 3 + 4 + def sortEvens(L: List[int]): 5 + """Sorts the even elements of a list in ascending order.""" 6 + modifiable_indecies = [i for i, x in enumerate(L) if x % 2 == 0] 7 + sorted_evens = sorted([L[i] for i in modifiable_indecies]) 8 + 9 + for i, x in enumerate(sorted_evens): 10 + L[modifiable_indecies[i]] = x 11 + 12 + 13 + print("Testing sortEvens()...", end="") 14 + L = [1, 8, 4, 9, -2, 7] 15 + assert sortEvens(L) == None 16 + assert L == [1, -2, 4, 9, 8, 7] 17 + 18 + L = [1, 4, 3, 2, 5] 19 + assert sortEvens(L) == None 20 + assert L == [1, 2, 3, 4, 5] 21 + 22 + L = [1, 3, 2, 5] 23 + assert sortEvens(L) == None 24 + assert L == [1, 3, 2, 5] 25 + 26 + L = [3, 6, 4, -2, 8, 4, 0, 5] 27 + assert sortEvens(L) == None 28 + assert L == [3, -2, 0, 4, 4, 6, 8, 5] 29 + 30 + L = [1, 3, 5] 31 + assert sortEvens(L) == None 32 + assert L == [1, 3, 5] 33 + 34 + L = [] 35 + assert sortEvens(L) == None 36 + assert L == [] 37 + print("Passed!")
+57
python/oct3/level2/subCipher.py
··· 1 + from string import ascii_lowercase, ascii_uppercase 2 + 3 + 4 + def encodeSubstitutionCipher(msg: str, key: str) -> str: 5 + """Encode a message using a substitution cipher.""" 6 + unique = set(msg) 7 + for c in unique: 8 + if c.isalpha(): 9 + if c.isupper(): 10 + encoded = key[ord(c) - ord("A")] 11 + else: 12 + encoded = key[ord(c) - ord("a")] 13 + encoded = encoded.lower() 14 + msg = msg.replace(c, encoded) 15 + return msg 16 + 17 + 18 + def decodeSubstitutionCipher(encodedMsg: str, key: str) -> str: 19 + """Decode a message using a substitution cipher.""" 20 + unique = set(encodedMsg) 21 + for c in unique: 22 + if c.isalpha(): 23 + if c.isupper(): 24 + decoded = ascii_uppercase[key.index(c.upper())] 25 + else: 26 + decoded = ascii_lowercase[key.index(c.upper())] 27 + encodedMsg = encodedMsg.replace(c, decoded) 28 + return encodedMsg 29 + 30 + 31 + print("Testing encodeSubstitutionCipher()...", end="") 32 + assert encodeSubstitutionCipher("CAB", "SQGYFEZXLANKJIMPURDCWTHVOB") == "GSQ" 33 + assert encodeSubstitutionCipher("Cab Z?", "SQGYFEZXLANKJIMPURDCWTHVOB") == "Gsq B?" 34 + assert ( 35 + encodeSubstitutionCipher("Hello, World!", "ABCDEFGHIJKLMNOPQRSTUVWXYZ") 36 + == "Hello, World!" 37 + ) 38 + assert ( 39 + encodeSubstitutionCipher("42 - 0 = 42", "XHNSBMTOPQWGZDCEAVLKJYIURF") 40 + == "42 - 0 = 42" 41 + ) 42 + assert encodeSubstitutionCipher("", "XHNSBMTOPQWGZDCEAVLKJYIURF") == "" 43 + 44 + 45 + print("Testing decodeSubstitutionCipher()...", end="") 46 + assert decodeSubstitutionCipher("GSQ", "SQGYFEZXLANKJIMPURDCWTHVOB") == "CAB" 47 + assert decodeSubstitutionCipher("Gsq B?", "SQGYFEZXLANKJIMPURDCWTHVOB") == "Cab Z?" 48 + assert ( 49 + decodeSubstitutionCipher("Hello, World!", "ABCDEFGHIJKLMNOPQRSTUVWXYZ") 50 + == "Hello, World!" 51 + ) 52 + assert ( 53 + decodeSubstitutionCipher("42 - 0 = 42", "XHNSBMTOPQWGZDCEAVLKJYIURF") 54 + == "42 - 0 = 42" 55 + ) 56 + assert decodeSubstitutionCipher("", "XHNSBMTOPQWGZDCEAVLKJYIURF") == "" 57 + print("Passed!")