This was written in answer to a leetcode problem: given a string, return the minimum number of cuts needed to partition the string into palindrome substrings.
It may not be the most efficient way, but I decided to explore the possibilities as a graph, the idea being to only add nodes that can possibly be in a path that creates palindromic substrings. As I build the graph level by level, all I need to do is track which level I am on (0 for root (i.e. the original string), 1 for children of root and so on), and once I hit a node whose value is a palindrome, the current level is the required answer.
The bug I am getting is that instead of returning the level, I get a return value of None. The bug only occurs for some inputs. I have tried entering different inputs but cannot see a pattern nevermind determining the cause. Via debugging I know that the return statement is being executed when expected. Why is this happening? What is the problem?
class Node:
def __init__(self, val='', children=None):
self.val = val
self.children = children if children else []
class Solution:
def minCut(self, s: str) -> int:
if s == s[::-1]:
return 0
def _create_graph(root, level):
n = len(root.val)
for i in range(1, n):
substr = root.val[:i]
if substr == substr[::-1]:
comp_of_substr = root.val[i:]
if comp_of_substr == comp_of_substr[::-1]:
return level # code is reaching here as expected, but None is being returned
child = Node(comp_of_substr)
root.children.append(child)
for child in root.children:
_create_graph(child, level + 1)
root = Node(s)
return _create_graph(root, 1)
I have made sure the code is reaching the return value and terminating there. A sample of the debug I did by print statements:
root.val='abccbc', num_cuts=1
n=6
i=1
substr='a'
comp_of_substr='bccbc'
child.val='bccbc' appended to root.val='abccbc' children
root.children = ['bccbc']
i=2
substr='ab'
root.children = ['bccbc']
i=3
substr='abc'
root.children = ['bccbc']
i=4
substr='abcc'
root.children = ['bccbc']
i=5
substr='abccb'
root.children = ['bccbc']
root.val='bccbc', num_cuts=2
n=5
i=1
substr='b'
comp_of_substr='ccbc'
child.val='ccbc' appended to root.val='bccbc' children
root.children = ['ccbc']
i=2
substr='bc'
root.children = ['ccbc']
i=3
substr='bcc'
root.children = ['ccbc']
i=4
substr='bccb'
comp_of_substr='c'
found solution with num_cuts=2 # the print statement that gave this is literally
# above the 'return level'
for child in root.children:you are ignoring the returned level from the recursive call, and below that loop you are exiting the function without returning anything (i.eNone)Nonebecause of this. I didn't analyse your function any further. If you don't wantNone, you will have to have areturnstatement in the case the firstforloop does not execute thereturnyou have there. This is not about printing, but about returning. You can print what you want, but if you don't return it, you'll getNone.forloop finishes and comes to the secondforloop? Can you point to the line of code where it returns anything? It just isn't there. There isn't areturnstatement there.