Palindrome Partitioning: Lab 6 changes

This commit is contained in:
2025-09-17 21:29:16 -04:00
parent 4ddf200373
commit 070d6dbe3e
3 changed files with 55 additions and 1 deletions

View File

@@ -31,7 +31,7 @@ def palindrome_partitions(s: str) -> int:
if is_palindrome(left):
if right not in memo:
memo[right] = palindrome_partitions(right)
memo[right] = recurse(right)
sum += memo[right]

View File

@@ -0,0 +1,52 @@
"""
name: Nicholas Tamassia
Honor Code and Acknowledgments:
This work complies with the JMU Honor Code.
Comments here on your code and submission.
"""
def is_palindrome(s: str, start: int, end: int) -> bool:
return s[start:end] == s[start:end][::-1]
def palindrome_partitions(s: str) -> int:
memo: list[int] = [-1] * (len(s) + 1)
def recurse(start: int) -> int:
nonlocal memo
if start == len(s):
return 1
if memo[start] != -1:
return memo[start]
sum = 0
for i in range(start + 1, len(s) + 1):
if is_palindrome(s, start, i):
sum += recurse(i)
memo[start] = sum
return sum
return recurse(0)
# All modules for CS 412 must include a main method that allows it
# to imported and invoked from other python scripts
def main():
n: int = int(input())
for i in range(0, n):
in_str = input().strip()
print(palindrome_partitions(in_str))
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,2 @@
1
eefffefeeefffefeffefefeeefefffefefefefefefffffffefeeee