CMU Coding Bootcamp

feat: oct 2 level 3

thecoded.prof e533b263 8bd24ae4

verified
Changed files
+111
python
+35
python/oct2/level3/isWordLadder.py
··· 1 + def isWordLadder(s: str) -> bool: 2 + """Check if a string is a word ladder.""" 3 + words = s.split("-") 4 + if len(words) < 2: 5 + return False 6 + if len(words[0]) != len(words[-1]): 7 + return False 8 + if len(set(words)) != len(words): 9 + return False 10 + for i in range(len(words) - 1): 11 + if len(words[i]) != len(words[i + 1]): 12 + return False 13 + diff = 0 14 + for j in range(len(words[i])): 15 + if words[i][j] != words[i + 1][j]: 16 + diff += 1 17 + if diff != 1: 18 + return False 19 + return True 20 + 21 + 22 + print("Testing isWordLadder()...", end="") 23 + assert isWordLadder("dog-cog-cot-cat") == True 24 + assert isWordLadder("cat-bat") == True 25 + assert isWordLadder("cold-cord-card-ward-warm") == True 26 + assert isWordLadder("toggle-goggle-google") == True 27 + assert isWordLadder("cold-cord-card-warm") == False 28 + assert isWordLadder("cat-bat-cat") == False # duplicate word 29 + assert isWordLadder("cat-bat-") == False 30 + assert isWordLadder("cat-cats") == False 31 + assert isWordLadder("cat-cabs") == False 32 + assert isWordLadder("cat") == False # just one word 33 + assert isWordLadder("") == False # no words 34 + assert isWordLadder("-") == False # no words 35 + print("Passed!")
+19
python/oct2/level3/longestSubpalindrome.py
··· 1 + def longestSubpalindrome(s: str) -> str: 2 + """Find the longest subpalindrome (doesn't have to be centered) in a string.""" 3 + longest = "" 4 + for i in range(len(s)): 5 + for j in range(len(s), i - 1, -1): 6 + forwards = s[i:j] 7 + if forwards == forwards[::-1] and len(forwards) >= len(longest): 8 + longest = s[i:j] 9 + return longest 10 + 11 + 12 + print("Testing longestSubpalindrome()...", end="") 13 + assert longestSubpalindrome("ab-4-be!!!") == "b-4-b" 14 + assert longestSubpalindrome("abcbce") == "cbc" 15 + assert longestSubpalindrome("aba") == "aba" 16 + assert longestSubpalindrome("a") == "a" 17 + assert longestSubpalindrome("abd") == "d" 18 + assert longestSubpalindrome("kP:p") == "p" 19 + print("Passed!")
+57
python/oct2/level3/stripComments.py
··· 1 + def stripComments(code: str) -> str: 2 + """Remove comments from a Python code string.""" 3 + lines = code.split("\n") 4 + lines = [line.split("#")[0].rstrip() for line in lines] 5 + lines = [line for line in lines if line] 6 + lines = [f"{line}\n" for line in lines] 7 + return "".join(lines) 8 + 9 + 10 + print("Testing stripComments()...", end="") 11 + code = """\ 12 + # here's a comment! 13 + def foo(x): 14 + return x + 1 # here's another one 15 + """ 16 + result = """\ 17 + def foo(x): 18 + return x + 1 19 + """ 20 + assert stripComments(code) == result 21 + 22 + code = """\ 23 + def g(x): 24 + # Here are some comments 25 + # which must be removed 26 + # by stripComments 27 + return x * 7 28 + """ 29 + result = """\ 30 + def g(x): 31 + return x * 7 32 + """ 33 + assert stripComments(code) == result 34 + 35 + code = """\ 36 + def doIHaveAnyComments(): 37 + return 'No' 38 + """ 39 + result = """\ 40 + def doIHaveAnyComments(): 41 + return 'No' 42 + """ 43 + assert stripComments(code) == result 44 + 45 + code = """\ 46 + def f(x): 47 + #This function returns x + 5 48 + return x + 5 49 + """ 50 + result = """\ 51 + def f(x): 52 + return x + 5 53 + """ 54 + assert stripComments(code) == result 55 + 56 + assert stripComments("") == "" 57 + print("Passed!")