Partition into palindromes
Return every partition of text into palindromic substrings, in lexicographic list order. Empty text has one partition: [].
text = 'aab'
for end in range(1, len(text)+1):
piece = text[:end]
print(piece, piece == piece[::-1])Reason about the state
Backtrack over possible next cut positions, accepting only palindromic pieces. Copy the path at a complete partition and sort the final collection. Before writing code, identify what each variable means after one iteration. Walk through the example and explain why the next transition preserves that meaning. Then check the smallest permitted input and a case with repeated values. A correct answer follows the contract for every input, including the boundary cases in the checks. Keep any input mutation within the stated rules; return a fresh result when the task asks for one.
Your task
Return every partition of text into palindromic substrings, in lexicographic list order. Empty text has one partition: [].
The explanation opens here once every check passes.