CMU Coding Bootcamp

feat: oct 2 level 2

thecoded.prof 8bd24ae4 e56923ae

verified
+1 -1
python/oct2/level1/sameChars.py
··· 20 20 assert sameChars("123", "123") == True 21 21 assert sameChars("", "a") == False 22 22 assert sameChars("", "") == True 23 - assert sameChars(14, "14") == False 23 + # assert sameChars(14, "14") == False # this check passes but if I don't comment it I get an editor warning since I've typed the function 24 24 print("Passed!")
+17
python/oct2/level2/addUpcBarcodeCheckDigit.py
··· 1 + def addUpcBarcodeCheckDigit(barcode: str) -> str: 2 + """Adds the check digit to a UPC barcode.""" 3 + evenChecks = [int(digit) for i, digit in enumerate(barcode) if i % 2 == 0] 4 + oddChecks = [int(digit) for i, digit in enumerate(barcode) if i % 2 != 0] 5 + evenTotal = sum(int(digit) for digit in evenChecks) * 3 6 + oddTotal = sum(int(digit) for digit in oddChecks) 7 + checkDigit = 10 - (evenTotal + oddTotal) % 10 8 + return barcode + (str(checkDigit) if checkDigit != 10 else "0") 9 + 10 + 11 + print("Testing addUpcBarcodeCheckDigit()...", end="") 12 + assert addUpcBarcodeCheckDigit("03600029145") == "036000291452" 13 + assert addUpcBarcodeCheckDigit("11111111111") == "111111111117" 14 + assert addUpcBarcodeCheckDigit("23232323232") == "232323232329" 15 + assert addUpcBarcodeCheckDigit("12345678900") == "123456789005" 16 + assert addUpcBarcodeCheckDigit("00000000000") == "000000000000" 17 + print("Passed!")
+50
python/oct2/level2/simpleCipher.py
··· 1 + def encodeSimpleCipher(s: str) -> str: 2 + """ 3 + Encode a string using this formula. 4 + - s[i] = 2*i 5 + - if 2*i > len(s), s[i] = (2*i) % len(s) 6 + """ 7 + encoded = list(s) 8 + for i in range(len(s)): 9 + j = (2 * i) % len(s) 10 + cj = encoded[j] 11 + encoded[j] = encoded[i] 12 + encoded[i] = cj 13 + return "".join(encoded) 14 + 15 + 16 + def decodeSimpleCipher(s: str) -> str: 17 + """Decode a string encoded from encodeSimpleCipher""" 18 + decoded = list(s) 19 + for i in range(len(s) - 1, -1, -1): 20 + j = (2 * i) % len(s) 21 + cj = decoded[j] 22 + decoded[j] = decoded[i] 23 + decoded[i] = cj 24 + return "".join(decoded) 25 + 26 + 27 + print("Testing encodeSimpleCipher()...", end="") 28 + assert encodeSimpleCipher("AB") == "BA" 29 + assert encodeSimpleCipher("ABC") == "ABC" 30 + assert encodeSimpleCipher("ABCD") == "BCDA" 31 + assert encodeSimpleCipher("ABCDEFGH") == "BCFGDEHA" 32 + assert encodeSimpleCipher("SECRET MESSAGE") == "MCE SSTSAEREEG" 33 + assert ( 34 + encodeSimpleCipher("THIS IS A LONGER SECRET MESSAGE JUST FOR YOU!") 35 + == "T SAENG SR MGARJ H U!SSTE TFECORIIEYSELUOOS" 36 + ) 37 + print("Passed!") 38 + 39 + 40 + print("Testing decodeSimpleCipher()...", end="") 41 + assert decodeSimpleCipher("BA") == "AB" 42 + assert decodeSimpleCipher("ABC") == "ABC" 43 + assert decodeSimpleCipher("BCDA") == "ABCD" 44 + assert decodeSimpleCipher("BCFGDEHA") == "ABCDEFGH" 45 + assert decodeSimpleCipher("MCE SSTSAEREEG") == "SECRET MESSAGE" 46 + assert ( 47 + decodeSimpleCipher("T SAENG SR MGARJ H U!SSTE TFECORIIEYSELUOOS") 48 + == "THIS IS A LONGER SECRET MESSAGE JUST FOR YOU!" 49 + ) 50 + print("Passed!")
+45
python/oct2/level2/spellCheck.py
··· 1 + def spellCheck(lookupWord: str, words: str) -> str | None: 2 + """Spell check a word against a list of words.""" 3 + map = {} 4 + for word in words.split(): 5 + map[word] = levenshtein(lookupWord, word) 6 + min_distance = min(map.values()) 7 + if min_distance >= 3: 8 + return None 9 + return ",".join([word for word in map.keys() if map[word] == min_distance]) 10 + 11 + 12 + # from: https://en.wikibooks.org/wiki/Algorithm_Implementation/Strings/Levenshtein_distance#Python 13 + def levenshtein(s1: str, s2: str) -> int: 14 + if len(s1) < len(s2): 15 + return levenshtein(s2, s1) 16 + 17 + # len(s1) >= len(s2) 18 + if len(s2) == 0: 19 + return len(s1) 20 + 21 + previous_row = range(len(s2) + 1) 22 + for i, c1 in enumerate(s1): 23 + current_row = [i + 1] 24 + for j, c2 in enumerate(s2): 25 + insertions = ( 26 + previous_row[j + 1] + 1 27 + ) # j+1 instead of j since previous_row and current_row are one character longer 28 + deletions = current_row[j] + 1 # than s2 29 + substitutions = previous_row[j] + (c1 != c2) 30 + current_row.append(min(insertions, deletions, substitutions)) 31 + previous_row = current_row 32 + 33 + return previous_row[-1] 34 + 35 + 36 + print("Testing spellCheck()...", end="") 37 + words = "cat cow dog frog" 38 + assert spellCheck("frog", words) == "frog" # frog is correct 39 + assert spellCheck("cats", words) == "cat" # cat is distance 1 40 + assert spellCheck("caw", words) == "cat,cow" # cat and cow are distance 1 41 + assert spellCheck("drog", words) == "dog,frog" # dog and frog are distance 1 42 + assert spellCheck("kit", words) == "cat" # cat is distance 2 43 + assert spellCheck("ack", words) == None # cat, cow, and dog are 44 + # distance 3 (too far) 45 + print("Passed!")
+52
python/oct2/level2/wrapText.py
··· 1 + def wrapText(text: str, lineSize: int) -> str: 2 + """Wrap text to a specified line size.""" 3 + lines = [] 4 + words = text.split() 5 + currentLine = "" 6 + for word in words: 7 + if len(currentLine) + len(word) > lineSize: 8 + currentLine = currentLine.rstrip() 9 + currentLine = currentLine.ljust(lineSize) 10 + currentLine = f"|{currentLine}|" 11 + lines.append(currentLine) 12 + currentLine = f"{word} " 13 + else: 14 + currentLine += f"{word} " 15 + else: 16 + currentLine = currentLine.rstrip() 17 + currentLine = currentLine.ljust(lineSize) 18 + currentLine = f"|{currentLine}|" 19 + lines.append(currentLine) 20 + return "\n".join(lines) 21 + 22 + 23 + print("Testing wrapText()...", end="") 24 + text = """\ 25 + This is some sample text. It is just sample text. 26 + Nothing more than sample text. Really, that's it.""" 27 + 28 + textWrappedAt20 = """\ 29 + |This is some sample | 30 + |text. It is just | 31 + |sample text. Nothing| 32 + |more than sample | 33 + |text. Really, that's| 34 + |it. |""" 35 + 36 + assert wrapText(text, 20) == textWrappedAt20 37 + 38 + textWrappedAt30 = """\ 39 + |This is some sample text. It | 40 + |is just sample text. Nothing | 41 + |more than sample text. Really,| 42 + |that's it. |""" 43 + 44 + assert wrapText(text, 30) == textWrappedAt30 45 + 46 + textWrappedAt40 = """\ 47 + |This is some sample text. It is just | 48 + |sample text. Nothing more than sample | 49 + |text. Really, that's it. |""" 50 + 51 + assert wrapText(text, 40) == textWrappedAt40 52 + print("Passed!")