CMU Coding Bootcamp

feat: oct 2 level 4

thecoded.prof 747265f0 e533b263

verified
Changed files
+104
python
+25
python/oct2/level4/carrylessAdd.py
··· 1 + from typing import List 2 + 3 + 4 + def carrylessAdd(x: int, y: int) -> int: 5 + """Add two numbers without carrying.""" 6 + pos: List[int] = [] 7 + while x > 0 or y > 0: 8 + sum = (x % 10 + y % 10) % 10 9 + pos.append(sum) 10 + x //= 10 11 + y //= 10 12 + num = 0 13 + for idx, val in enumerate(pos): 14 + num += val * 10**idx 15 + return num 16 + 17 + 18 + print("Testing carrylessAdd()...", end="") 19 + assert carrylessAdd(8, 7) == 5 20 + assert carrylessAdd(785, 376) == 51 21 + assert carrylessAdd(0, 325) == 325 22 + assert carrylessAdd(30, 873) == 803 23 + assert carrylessAdd(873, 30) == 803 24 + assert carrylessAdd(100, 11) == 111 25 + print("Passed!")
+28
python/oct2/level4/carrylessMultiply.py
··· 1 + from typing import List 2 + 3 + 4 + def carrylessMultiply(x: int, y: int) -> int: 5 + """Multiply 2 numbers without carrying""" 6 + str_a = str(x)[::-1] 7 + str_b = str(y)[::-1] 8 + 9 + max_len = len(str_a) + len(str_b) 10 + pos: List[int] = [0] * max_len 11 + for i in range(len(str_a)): 12 + for j in range(len(str_b)): 13 + pnt = i + j 14 + digit_a = int(str_a[i]) 15 + digit_b = int(str_b[j]) 16 + pos[pnt] += digit_a * digit_b 17 + pos[pnt] %= 10 18 + result_str = "".join(str(digit) for digit in pos[::-1]).lstrip("0") or "0" 19 + return int(result_str) 20 + 21 + 22 + print("Testing carrylessMultiply()...", end="") 23 + assert carrylessMultiply(3, 3) == 9 24 + assert carrylessMultiply(4, 4) == 6 25 + assert carrylessMultiply(12345, 0) == 0 26 + assert carrylessMultiply(643, 59) == 417 27 + assert carrylessMultiply(6412, 387) == 807234 28 + print("Passed!")
+51
python/oct2/level4/routeCipher.py
··· 1 + def encodeRouteCipher(message: str, rows: int) -> str: 2 + """Encode a message using the Route Cipher.""" 3 + cur_char = "z" 4 + while len(message) % rows != 0: 5 + message += cur_char 6 + cur_char = chr(ord(cur_char) - 1) 7 + str_cols = [message[i : i + rows] for i in range(0, len(message), rows)] 8 + str_rows = ["".join(col) for col in zip(*str_cols)] 9 + encoded_message = f"{rows}" 10 + for idx, row in enumerate(str_rows): 11 + if idx % 2 == 0: 12 + encoded_message += row 13 + else: 14 + encoded_message += row[::-1] 15 + return encoded_message 16 + 17 + 18 + def decodeRouteCipher(encodedMessage: str) -> str: 19 + """Decode a message encoded using the Route Cipher.""" 20 + rows = int(encodedMessage[0]) 21 + rows_len = len(encodedMessage[1:]) // rows 22 + encoded_message = encodedMessage[1:] 23 + str_rows = [ 24 + encoded_message[i : i + rows_len] 25 + for i in range(0, len(encoded_message), rows_len) 26 + ] 27 + str_rows = [row[::-1] if idx % 2 == 1 else row for idx, row in enumerate(str_rows)] 28 + str_cols = ["".join(row) for row in zip(*str_rows)] 29 + decoded_string = "".join(str_cols) 30 + while decoded_string[-1].islower(): 31 + decoded_string = decoded_string[:-1] 32 + return decoded_string 33 + 34 + 35 + print("Testing encodeRouteCipher()...", end="") 36 + assert encodeRouteCipher("ASECRETMESSAGE", 4) == "4AREGESESETSzyAMC" 37 + assert encodeRouteCipher("ASECRETMESSAGE", 3) == "3ACTSGESMRSEEEAz" 38 + assert encodeRouteCipher("ASECRETMESSAGE", 5) == "5AESATSEMGEECRSz" 39 + assert encodeRouteCipher("ANOTHERSECRET", 4) == "4AHETzCENORRyxEST" 40 + 41 + 42 + print("Testing decodeRouteCipher()...", end="") 43 + assert decodeRouteCipher("4AREGESESETSzyAMC") == "ASECRETMESSAGE" 44 + assert decodeRouteCipher("3ACTSGESMRSEEEAz") == "ASECRETMESSAGE" 45 + assert decodeRouteCipher("5AESATSEMGEECRSz") == "ASECRETMESSAGE" 46 + assert decodeRouteCipher("4AHETzCENORRyxEST") == "ANOTHERSECRET" 47 + message = "SECRETSTUFFGOESHERE" 48 + encodedMessage = encodeRouteCipher(message, 6) 49 + plaintext = decodeRouteCipher(encodedMessage) 50 + assert plaintext == message 51 + print("Passed!")