I'm currently looking at this answer on Stackoverflow about finding combinations of k elements given an n long string. However while making some changes to it (to learn more about it, adapt it to my needs, and convert it to c), I found some problems.
Taking this piece of code:
def comb(sofar, rest, k):
if k == 0:
print(sofar)
else:
for i in range(len(rest)):
comb(sofar + rest[i], rest[i+1:], k-1)
and adding a variable "n" which is supposed to keep the length of the string:
def comb(sofar, rest, n, k):
if k == 0:
print(sofar)
else:
for i in range(0, n):
comb(sofar + rest[i], rest[i+1:], n-1, k-1)
Technically shouldn't these do the same thing? I'm getting a "string index out of range error" but should len(rest) be the same as "n"?
edit:
comb is called with:
comb("", "12345", 3)
comb2 is called with:
comb("", "12345", 5, 3)