Skip to content

Instantly share code, notes, and snippets.

@subhanshuja
Created July 16, 2020 09:22
Show Gist options
  • Save subhanshuja/236d58f79543933da413b746087501cd to your computer and use it in GitHub Desktop.
Save subhanshuja/236d58f79543933da413b746087501cd to your computer and use it in GitHub Desktop.
porting javascript to python backtrack Road to Genius: superior #51
# javascript reference: https://dev.to/codr/road-to-genius-superior-51-2c3f
# function backtrack(list, tempList, nums, start) {
# list.push([...tempList]);
# for(let i = start; i < nums.length; i++) {
# tempList.push(nums[i]);
# backtrack(list, tempList, nums, i + 1);
# tempList.pop();
# }
# }
# function subsets(nums) {
# const list = [];
# backtrack(list, [], nums, 0);
# return list;
# }
# let A = subsets([1, 2, 1]);
# A = A.length
# porting javascript to python\
def backtrack(lists, templist, nums, start):
lists.append(templist)
print(lists, templist, nums, start)
for index in range(start, len(nums)):
templist.append(nums[index])
backtrack(lists, templist, nums, index + 1)
templist.pop()
def subsets(nums):
lists = []
backtrack(lists, [], nums, 0)
return lists
if __name__ == "__main__":
test = subsets([1, 2, 1])
print(len(test))
print(test)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment