I have the following snippet, that beautifully generates binary numbers by altering 0 and 1 in places of "?". But I don't understand how this piece of code works. Especially the line str_list[idx] = "?" . Why is the "?" begin restored in the idx position.
How should one think about this while writing a recursion?
Here is the code
def gen_bin(str_list):
if "?" not in str_list:
print "".join(str_list)
return
idx = str_list.index("?")
gen_bin(str_list[:idx] + ["0"] + str_list[idx+1:])
gen_bin(str_list[:idx] + ["1"] + str_list[idx+1:])
str_list[idx] = "?"
gen_bin(list("1??"))
Any advice on how to write such recursive functions will help me in becoming better.
Thanks for your time.,