Skip to content

Instantly share code, notes, and snippets.

@mgarod
Last active January 28, 2016 19:32
Show Gist options
  • Save mgarod/da786a596326ae04848f to your computer and use it in GitHub Desktop.
Save mgarod/da786a596326ae04848f to your computer and use it in GitHub Desktop.
you have a function that takes a positive integer N and another positive integer S that function should generate all the subsequences with size S of the numbers within the range [1, N]. you can either return a collection of these sequences or just print each one to the standard output. example: subsequences(3,2) yields: 1,2 1,3 2,3
def sub(N, S):
sub_helper(S, range(1,N+1), list())
def sub_helper(S, remainingList, subseq):
if S == 0:
print subseq
return
else:
for i in range(0, len(remainingList)-S+1):
subseq.append(remainingList[i])
sub_helper(S-1, remainingList[(i+1):], subseq)
subseq.pop(-1);
def go():
print "sub(4,2)"
sub(4,2)
print "\nsub(6,3)"
sub(6,3)
go()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment