Files
CS412-Applied-Algorithms/Palindrome-Partitioning/README.md

64 lines
2.6 KiB
Markdown
Raw Normal View History

# Lab 4: Coding Palindrome Partitioning with Backtracking
## Description
In this lab you will develop a recursive algorithm for the palindrome partition
counting problem.
A **palindrome** is a string of letters that is the same backwards as forwards.
For example, "racecar", "eevee", and "12321" are all palindromes. A string with
a single character is trivially a palindrome.
A **palindromic partition** of a string is a partition of the string into
substrings whose concatenation is equal to the original string, but such that
every substring is itself a palindrome.
For example, the string, "seeks" can be broken up into the palindrome partition
["s", "ee", "k", "s"] or as ["s", "e", "e", "k", "s"]. Your task is to design
and implement a recursive algorithm that counts the number of palindromic
partitions of a given string.
## Input
Your input will begin with a single line containing a nonnegative integer n
followed by exactly n lines each of which contains an input string.
## Output
You should output **exactly** n lines, one for each input string with each
ending in a newline character. The value of each line should be the number of
unique palindromic partitions that can be made with the input string.
<table>
<tr>
<td>Sample Input</td>
<td>Sample Output</td>
</tr>
<tr>
<td><pre>3<br>abc<br>bcccb<br>seeks</pre></td>
<td><pre>1<br>5<br>2</pre></td>
</tr>
</table>
## Turning it in
**Submit your Python code as cs412_palindrome_count_bt.py to Gradescope.**
## Hints
- As always, when devising a recursive backtracking algorithm, think of your
output as a series of choices. Almost always you can get to the recursive
solution by having each recursive call brute-force make all possible next
choice and use the recursion fairy to handle the remaining choices.
- What are the choices here? Think about what you need to do to the string to
turn it into a palindromic partition--this will tell you exactly what a
"choice" is in terms of producing palindromic partitions.
- Don't worry about speed here. You are almost certainly going to have a rather
bad runtime. We'll fix this in a few weeks when we talk about dynamic
programming. You probably want to write a tiny helper predicate
isPalindrome(s) that returns true if s is a palindrome and false otherwise.
- How can you easily have a string evaluate to the reverse of itself? Recall
that slicing takes 3 values (start pos, end pos, step size). See this website
for details:
https://www.digitalocean.com/community/tutorials/how-to-index-and-slice-strings-in-python-3