Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
26
Task/Permutations/Python/permutations-2.py
Normal file
26
Task/Permutations/Python/permutations-2.py
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
def perm1(n):
|
||||
a = list(range(n))
|
||||
def sub(i):
|
||||
if i == n - 1:
|
||||
yield tuple(a)
|
||||
else:
|
||||
for k in range(i, n):
|
||||
a[i], a[k] = a[k], a[i]
|
||||
yield from sub(i + 1)
|
||||
a[i], a[k] = a[k], a[i]
|
||||
yield from sub(0)
|
||||
|
||||
def perm2(n):
|
||||
a = list(range(n))
|
||||
def sub(i):
|
||||
if i == n - 1:
|
||||
yield tuple(a)
|
||||
else:
|
||||
for k in range(i, n):
|
||||
a[i], a[k] = a[k], a[i]
|
||||
yield from sub(i + 1)
|
||||
x = a[i]
|
||||
for k in range(i + 1, n):
|
||||
a[k - 1] = a[k]
|
||||
a[n - 1] = x
|
||||
yield from sub(0)
|
||||
15
Task/Permutations/Python/permutations-3.py
Normal file
15
Task/Permutations/Python/permutations-3.py
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
for u in perm1(3): print(u)
|
||||
(0, 1, 2)
|
||||
(0, 2, 1)
|
||||
(1, 0, 2)
|
||||
(1, 2, 0)
|
||||
(2, 1, 0)
|
||||
(2, 0, 1)
|
||||
|
||||
for u in perm2(3): print(u)
|
||||
(0, 1, 2)
|
||||
(0, 2, 1)
|
||||
(1, 0, 2)
|
||||
(1, 2, 0)
|
||||
(2, 0, 1)
|
||||
(2, 1, 0)
|
||||
39
Task/Permutations/Python/permutations-4.py
Normal file
39
Task/Permutations/Python/permutations-4.py
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
def nextperm(a):
|
||||
n = len(a)
|
||||
i = n - 1
|
||||
while i > 0 and a[i - 1] > a[i]:
|
||||
i -= 1
|
||||
j = i
|
||||
k = n - 1
|
||||
while j < k:
|
||||
a[j], a[k] = a[k], a[j]
|
||||
j += 1
|
||||
k -= 1
|
||||
if i == 0:
|
||||
return False
|
||||
else:
|
||||
j = i
|
||||
while a[j] < a[i - 1]:
|
||||
j += 1
|
||||
a[i - 1], a[j] = a[j], a[i - 1]
|
||||
return True
|
||||
|
||||
def perm3(n):
|
||||
if type(n) is int:
|
||||
if n < 1:
|
||||
return []
|
||||
a = list(range(n))
|
||||
else:
|
||||
a = sorted(n)
|
||||
u = [tuple(a)]
|
||||
while nextperm(a):
|
||||
u.append(tuple(a))
|
||||
return u
|
||||
|
||||
for p in perm3(3): print(p)
|
||||
(0, 1, 2)
|
||||
(0, 2, 1)
|
||||
(1, 0, 2)
|
||||
(1, 2, 0)
|
||||
(2, 0, 1)
|
||||
(2, 1, 0)
|
||||
Loading…
Add table
Add a link
Reference in a new issue