Created
July 16, 2020 09:22
-
-
Save subhanshuja/236d58f79543933da413b746087501cd to your computer and use it in GitHub Desktop.
porting javascript to python backtrack Road to Genius: superior #51
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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