GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — algorithms/backtrack/permute.py at master · deba999/algorithms

Expand file tree

/

permute.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
41 lines (35 loc) · 844 Bytes
/

permute.py

Copy path

File metadata and controls

41 lines (35 loc) · 844 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# Given a collection of distinct numbers, return all possible permutations.
# For example,
# [1,2,3] have the following permutations:
# [
# [1,2,3],
# [1,3,2],
# [2,1,3],
# [2,3,1],
# [3,1,2],
# [3,2,1]
# ]
def permute(nums):
perms = [[]]
for n in nums:
new_perms = []
for perm in perms:
for i in range(len(perm) + 1):
new_perms.append(perm[:i] + [n] + perm[i:]) ###insert n
print(i, perm[:i], [n], perm[i:], ">>>>", new_perms)
perms = new_perms
return perms
# DFS Version
# def permute(nums):
# res = []
# dfs(res, nums, [])
# return res
# def dfs(res, nums, path):
# if not nums:
# res.append(path)
# for i in range(len(nums)):
# print(nums[:i]+nums[i+1:])
# dfs(res, nums[:i]+nums[i+1:], path+[nums[i]])
test = [1, 2, 3]
print(test)
print(permute(test))
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.