CMU Coding Bootcamp

feat: oct 3 level 4

thecoded.prof 89ac97c0 fd72f669

verified
+273 -1
+1 -1
career focus/cf_questions.md
··· 27 27 # Work Environment & Company Preferences 28 28 29 29 12. Do you prefer working in a startup, a large corporation, or a mid-sized company? 30 - - Not much preference here. 30 + - I don't love corporate environments. I'd rather something mid size or smaller 31 31 13. Do you want to work remotely, in-office, or in a hybrid setup? 32 32 - Remotely or Hybrid 33 33 14. What kind of work culture and values align with you?
+126
python/oct3/level4/getEvalSteps.py
··· 1 + from typing import List 2 + 3 + 4 + def getMostImportantOpIndex(parts: List[str]) -> int: 5 + """Returns the index of the most important operator in the given list of parts.""" 6 + index = len(parts) + 1 7 + higher_order_operator = 0 8 + 9 + if "^" in parts: 10 + higher_order_operator = 2 11 + index = parts.index("^") 12 + if "*" in parts and higher_order_operator < 2 and parts.index("*") < index: 13 + higher_order_operator = 1 14 + index = parts.index("*") 15 + if "/" in parts and higher_order_operator < 2 and parts.index("/") < index: 16 + higher_order_operator = 1 17 + index = parts.index("/") 18 + if "%" in parts and higher_order_operator < 2 and parts.index("%") < index: 19 + higher_order_operator = 1 20 + index = parts.index("%") 21 + if "+" in parts and higher_order_operator < 1 and parts.index("+") < index: 22 + index = parts.index("+") 23 + if "-" in parts and higher_order_operator < 1 and parts.index("-") < index: 24 + index = parts.index("-") 25 + return index 26 + 27 + 28 + def runStep(a: str, sign: str, b: str) -> str: 29 + """Runs a single step of the evaluation process.""" 30 + if sign == "+": 31 + return str(int(a) + int(b)) 32 + elif sign == "-": 33 + return str(int(a) - int(b)) 34 + elif sign == "*": 35 + return str(int(a) * int(b)) 36 + elif sign == "/": 37 + return str(int(a) // int(b)) 38 + elif sign == "%": 39 + return str(int(a) % int(b)) 40 + elif sign == "^": 41 + return str(int(a) ** int(b)) 42 + else: 43 + raise ValueError("Invalid operator") 44 + 45 + 46 + def getEvalSteps(expr: str) -> str: 47 + """Returns a string containing the evaluation steps of the given expression.""" 48 + padding = " " * (len(expr) + 1) + "= " 49 + 50 + expr = expr.strip() 51 + expr = expr.replace(" ", "") 52 + expr = expr.replace("**", "^") 53 + expr = expr.replace("//", "/") 54 + steps = [expr] 55 + 56 + expr_parts = [] 57 + cur_piece = "" 58 + for c in expr: 59 + if c.isdigit(): 60 + cur_piece += c 61 + else: 62 + expr_parts.append(cur_piece) 63 + expr_parts.append(c) 64 + cur_piece = "" 65 + expr_parts.append(cur_piece) 66 + while len(expr_parts) > 1: 67 + next_op = getMostImportantOpIndex(expr_parts) 68 + left = expr_parts.pop(next_op - 1) 69 + op = expr_parts.pop(next_op - 1) 70 + right = expr_parts.pop(next_op - 1) 71 + val = runStep(left, op, right) 72 + expr_parts.insert(next_op - 1, val) 73 + steps.append("".join(expr_parts)) 74 + steps[0] = steps[0] + " = " + steps.pop(1) 75 + steps = [step.replace("^", "**") for step in steps] 76 + steps = [step.replace("/", "//") for step in steps] 77 + steps = [padding + step if not i == 0 else step for i, step in enumerate(steps)] 78 + final = "\n".join(steps) + "\n" 79 + return final 80 + 81 + 82 + def testGetEvalSteps(): 83 + print("Testing getEvalSteps()...", end="") 84 + assert ( 85 + getEvalSteps("2+3*4-8**3%3") 86 + == """\ 87 + 2+3*4-8**3%3 = 2+3*4-512%3 88 + = 2+12-512%3 89 + = 2+12-2 90 + = 14-2 91 + = 12 92 + """ 93 + ) 94 + assert ( 95 + getEvalSteps("2**2**3+4-2//3") 96 + == """\ 97 + 2**2**3+4-2//3 = 4**3+4-2//3 98 + = 64+4-2//3 99 + = 64+4-0 100 + = 68-0 101 + = 68 102 + """ 103 + ) 104 + assert ( 105 + getEvalSteps("4//2**2*8-3+2") 106 + == """\ 107 + 4//2**2*8-3+2 = 4//4*8-3+2 108 + = 1*8-3+2 109 + = 8-3+2 110 + = 5+2 111 + = 7 112 + """ 113 + ) 114 + assert ( 115 + getEvalSteps("2**4//2-3+1") 116 + == """\ 117 + 2**4//2-3+1 = 16//2-3+1 118 + = 8-3+1 119 + = 5+1 120 + = 6 121 + """ 122 + ) 123 + print("Passed!") 124 + 125 + 126 + testGetEvalSteps()
+146
python/oct3/level4/runSimpleProgram.py
··· 1 + from typing import List 2 + 3 + 4 + def findLabel(lines: List[List[str]], label: str) -> int: 5 + """Find the line number of a label""" 6 + for i, line in enumerate(lines): 7 + if line[0] == label + ":": 8 + return i 9 + raise ValueError(f"Label '{label}' not found") 10 + 11 + 12 + def parse(args: List[int], values: List[int], arg: str) -> int: 13 + """Parse an argument string into an integer value""" 14 + arg_var = 0 15 + 16 + if arg.isdigit(): 17 + arg_var = int(arg) 18 + elif arg.startswith("L"): 19 + arg_var = values[int(arg[1:])] 20 + elif arg.startswith("A"): 21 + arg_var = args[int(arg[1:])] 22 + 23 + return arg_var 24 + 25 + 26 + def runL(args: List[int], values: List[int], line: List[str]): 27 + """Run a Set command""" 28 + out_var = int(line[0][1:]) 29 + op = line[1] 30 + 31 + if out_var >= len(values): 32 + values.extend([0] * (out_var - len(values) + 1)) 33 + 34 + if op.isdigit(): 35 + values[out_var] = int(op) 36 + else: 37 + op1, op2 = line[2], line[3] 38 + op1_val, op2_val = parse(args, values, op1), parse(args, values, op2) 39 + if op == "+": 40 + values[out_var] = op1_val + op2_val 41 + elif op == "-": 42 + values[out_var] = op1_val - op2_val 43 + else: 44 + raise ValueError(f"Invalid Operator: {op}") 45 + 46 + 47 + def runJMP(args: List[int], values: List[int], line: List[str], lines) -> int | None: 48 + """Return the target line number or None for a JMP command""" 49 + command = line[0] 50 + if command == "JMP": 51 + jump_targ = line[1] 52 + if jump_targ.isdigit(): 53 + target = int(line[1]) 54 + if target < 0 or target >= len(lines): 55 + raise ValueError(f"Invalid Jump Target: {target}") 56 + return target 57 + else: 58 + target = findLabel(lines, line[1]) 59 + if target < 0 or target >= len(lines): 60 + raise ValueError(f"Invalid Jump Target: {target}") 61 + return target 62 + else: 63 + expr = line[1] 64 + if command[3] == "+": 65 + var = int(expr[1]) 66 + if values[var] > 0: 67 + target = findLabel(lines, line[2]) 68 + if target < 0 or target >= len(lines): 69 + raise ValueError(f"Invalid Jump Target: {target}") 70 + return target 71 + return 72 + elif command[3] == "0": 73 + var = int(expr[1]) 74 + if values[var] == 0: 75 + target = findLabel(lines, line[2]) 76 + if target < 0 or target >= len(lines): 77 + raise ValueError(f"Invalid Jump Target: {target}") 78 + return target 79 + return 80 + else: 81 + raise ValueError(f"Invalid Jump Target: {line[0]}") 82 + 83 + 84 + def runSimpleProgram(program: str, args: List[int]): 85 + """Run a simple program with given arguments.""" 86 + lines = program.splitlines() 87 + lines = [line.strip() for line in lines if line.strip()] 88 + lines = [line.split() for line in lines if not line.startswith("!")] 89 + 90 + values: List[int] = [] 91 + target = None 92 + i = 0 93 + return_value = None 94 + while not return_value: 95 + line = lines[i] 96 + # print(f"executing {i}: {line}, L: {values}, A: {args}") 97 + command = line[0] 98 + if ":" in line or (target and target != i): 99 + i += 1 100 + continue 101 + if target == i: 102 + target = None 103 + i += 1 104 + if command.startswith("L"): 105 + runL(args, values, line) 106 + elif command.startswith("JMP"): 107 + jump_to = runJMP(args, values, line, lines) 108 + if jump_to != None: 109 + i = jump_to 110 + elif command == "RTN": 111 + val = line[1] 112 + # print(f"RTN {val} from A: {args}, L: {values}") 113 + return_value = parse(args, values, val) 114 + break 115 + return return_value 116 + 117 + 118 + def testRunSimpleProgram(): 119 + print("Testing runSimpleProgram()...", end="") 120 + largest = """! largest: Returns max(A0, A1) 121 + L0 - A0 A1 122 + JMP+ L0 a0 123 + RTN A1 124 + a0: 125 + RTN A0""" 126 + assert runSimpleProgram(largest, [5, 6]) == 6 127 + assert runSimpleProgram(largest, [6, 5]) == 6 128 + 129 + sumToN = """! SumToN: Returns 1 + ... + A0 130 + ! L0 is a counter, L1 is the result 131 + L0 0 132 + L1 0 133 + loop: 134 + L2 - L0 A0 135 + JMP0 L2 done 136 + L0 + L0 1 137 + L1 + L1 L0 138 + JMP loop 139 + done: 140 + RTN L1""" 141 + assert runSimpleProgram(sumToN, [5]) == 1 + 2 + 3 + 4 + 5 142 + assert runSimpleProgram(sumToN, [10]) == 10 * 11 // 2 143 + print("Passed!") 144 + 145 + 146 + testRunSimpleProgram()