I am trying to debug the nested for loop via Pycharm debugger... In the process of troubleshooting, I wanted to breakdown the loop into two individual loops and step through the code but I am having a hard time with this...
Here is the code block with list comprehension:
def letterCasePermutation(S):
res = ['']
for ch in S:
if ch.isalpha():
res = [i + j for i in res for j in [ch.upper(), ch.lower()]]
return res
result = letterCasePermutation("ab")
print(result) # expected result = ['AB', 'Ab', 'aB', 'ab']
In order to debug this code block I would like to break down the list comprehension to something like this:
def letterCasePermutation(S):
res = ['']
for ch in S:
if ch.isalpha():
# res = [i + j for i in res for j in [ch.upper(), ch.lower()]]
for i in res:
for j in [ch.upper(), ch.lower()]:
res.append(i + j)
return res
result = letterCasePermutation("ab")
print(result)
The above block results in an infinite loop error instead of providing the result like code block-1. expected result = ['AB', 'Ab', 'aB', 'ab']
I am not able to figure what I am missing. After spending considerable amount of time and still being stuck, I decided to post this question. Thanks for the help in advance.