From e533b2633a7032f2a922e7521f153a86391ffb57 Mon Sep 17 00:00:00 2001 From: Samuel Shuert Date: Thu, 2 Oct 2025 15:21:09 +0000 Subject: [PATCH] feat: oct 2 level 3 --- python/oct2/level3/isWordLadder.py | 35 +++++++++++++ python/oct2/level3/longestSubpalindrome.py | 19 ++++++++ python/oct2/level3/stripComments.py | 57 ++++++++++++++++++++++ 3 files changed, 111 insertions(+) create mode 100644 python/oct2/level3/isWordLadder.py create mode 100644 python/oct2/level3/longestSubpalindrome.py create mode 100644 python/oct2/level3/stripComments.py diff --git a/python/oct2/level3/isWordLadder.py b/python/oct2/level3/isWordLadder.py new file mode 100644 index 0000000..fa7a400 --- /dev/null +++ b/python/oct2/level3/isWordLadder.py @@ -0,0 +1,35 @@ +def isWordLadder(s: str) -> bool: + """Check if a string is a word ladder.""" + words = s.split("-") + if len(words) < 2: + return False + if len(words[0]) != len(words[-1]): + return False + if len(set(words)) != len(words): + return False + for i in range(len(words) - 1): + if len(words[i]) != len(words[i + 1]): + return False + diff = 0 + for j in range(len(words[i])): + if words[i][j] != words[i + 1][j]: + diff += 1 + if diff != 1: + return False + return True + + +print("Testing isWordLadder()...", end="") +assert isWordLadder("dog-cog-cot-cat") == True +assert isWordLadder("cat-bat") == True +assert isWordLadder("cold-cord-card-ward-warm") == True +assert isWordLadder("toggle-goggle-google") == True +assert isWordLadder("cold-cord-card-warm") == False +assert isWordLadder("cat-bat-cat") == False # duplicate word +assert isWordLadder("cat-bat-") == False +assert isWordLadder("cat-cats") == False +assert isWordLadder("cat-cabs") == False +assert isWordLadder("cat") == False # just one word +assert isWordLadder("") == False # no words +assert isWordLadder("-") == False # no words +print("Passed!") diff --git a/python/oct2/level3/longestSubpalindrome.py b/python/oct2/level3/longestSubpalindrome.py new file mode 100644 index 0000000..df75bb9 --- /dev/null +++ b/python/oct2/level3/longestSubpalindrome.py @@ -0,0 +1,19 @@ +def longestSubpalindrome(s: str) -> str: + """Find the longest subpalindrome (doesn't have to be centered) in a string.""" + longest = "" + for i in range(len(s)): + for j in range(len(s), i - 1, -1): + forwards = s[i:j] + if forwards == forwards[::-1] and len(forwards) >= len(longest): + longest = s[i:j] + return longest + + +print("Testing longestSubpalindrome()...", end="") +assert longestSubpalindrome("ab-4-be!!!") == "b-4-b" +assert longestSubpalindrome("abcbce") == "cbc" +assert longestSubpalindrome("aba") == "aba" +assert longestSubpalindrome("a") == "a" +assert longestSubpalindrome("abd") == "d" +assert longestSubpalindrome("kP:p") == "p" +print("Passed!") diff --git a/python/oct2/level3/stripComments.py b/python/oct2/level3/stripComments.py new file mode 100644 index 0000000..c95adce --- /dev/null +++ b/python/oct2/level3/stripComments.py @@ -0,0 +1,57 @@ +def stripComments(code: str) -> str: + """Remove comments from a Python code string.""" + lines = code.split("\n") + lines = [line.split("#")[0].rstrip() for line in lines] + lines = [line for line in lines if line] + lines = [f"{line}\n" for line in lines] + return "".join(lines) + + +print("Testing stripComments()...", end="") +code = """\ +# here's a comment! +def foo(x): + return x + 1 # here's another one +""" +result = """\ +def foo(x): + return x + 1 +""" +assert stripComments(code) == result + +code = """\ +def g(x): +# Here are some comments +# which must be removed +# by stripComments + return x * 7 +""" +result = """\ +def g(x): + return x * 7 +""" +assert stripComments(code) == result + +code = """\ +def doIHaveAnyComments(): + return 'No' +""" +result = """\ +def doIHaveAnyComments(): + return 'No' +""" +assert stripComments(code) == result + +code = """\ +def f(x): +#This function returns x + 5 + return x + 5 +""" +result = """\ +def f(x): + return x + 5 +""" +assert stripComments(code) == result + +assert stripComments("") == "" +print("Passed!") -- 2.51.2