I am trying to write a recursion function in python and after certain call it throws index out of bound. However, I am no able to undestand why?
Could someone help me understand ?
def find_combinations(str, remaining_char=""):
print("find_combinations(%s, %s)" % (str, remaining_char))
if len(str) == 0:
print(remaining_char)
else:
for i in range(len(str)):
chr = str[i] # at this line it shows index out of bound
remaining_char = remaining_char+chr
str = str[1:]
find_combinations(str, remaining_char)
find_combinations("ABCD")
Error:
find_combinations(BCD, A)
find_combinations(CD, AB)
find_combinations(D, ABC)
find_combinations(, ABCD)
ABCD
...
...
chr = str[i]
IndexError: string index out of range
strbut keep iterating up to the length of the originalstrof course you'll go out of bounds.