diff --git a/Palindrome-Partitioning/cs412_palindrome_count_bt.py b/Palindrome-Partitioning/cs412_palindrome_partition_memoized_a.py similarity index 93% rename from Palindrome-Partitioning/cs412_palindrome_count_bt.py rename to Palindrome-Partitioning/cs412_palindrome_partition_memoized_a.py index ae7bf6d..acc041c 100644 --- a/Palindrome-Partitioning/cs412_palindrome_count_bt.py +++ b/Palindrome-Partitioning/cs412_palindrome_partition_memoized_a.py @@ -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] diff --git a/Palindrome-Partitioning/cs412_palindrome_partition_memoized_b.py b/Palindrome-Partitioning/cs412_palindrome_partition_memoized_b.py new file mode 100644 index 0000000..ae60dfe --- /dev/null +++ b/Palindrome-Partitioning/cs412_palindrome_partition_memoized_b.py @@ -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() diff --git a/Palindrome-Partitioning/inputs/sample2.txt b/Palindrome-Partitioning/inputs/sample2.txt new file mode 100644 index 0000000..cf2bfc2 --- /dev/null +++ b/Palindrome-Partitioning/inputs/sample2.txt @@ -0,0 +1,2 @@ +1 +eefffefeeefffefeffefefeeefefffefefefefefefffffffefeeee \ No newline at end of file