This repository was archived by the owner on Apr 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathset_permutations_combinations.py
More file actions
executable file
·68 lines (57 loc) · 1.71 KB
/
set_permutations_combinations.py
File metadata and controls
executable file
·68 lines (57 loc) · 1.71 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Apr 6 14:44:50 2021
@author: mahan
"""
def factorial(number):
def is_negative(number):
if number < 0:
raise ValueError("factorial cannot be calculated for a negative number!")
try:
is_negative(number)
except ValueError as err:
print(err)
else:
return 1 if number <= 1 else number * factorial(number - 1)
def get_ith_pemutations(list_char = ['1','2','a','b'], i=0):
l_char = list_char.copy()
permutation = []
while l_char:
count = factorial(len(l_char)-1)
select = i//count
permutation.append(l_char[select])
del(l_char[select])
i -= count * select
return permutation
def getElementFromEachList(l=[['1', '2'],['a', 'b']]):
if len(l) == 1:
return [[x] for x in l[0]]
result = []
for listElements in getElementFromEachList(l[1:]):
for fElement in l[0]:
result.append([fElement] + listElements)
return result
def getPermutations(l=['a','b','c'], r=2):
results = []
def getPermutationsRecr(l, com = []):
if len(com) == r:
results.append(com)
return
for e in l:
li = list(l)
li.remove(e)
getPermutationsRecr(li, com + [e])
getPermutationsRecr(l)
return results
def getCombinations(l=['a','b','c'], r=2):
results = []
def getCombinationsRecr(l, com = []):
if len(com) == r:
results.append(com)
return
for i, e in enumerate(l):
getCombinationsRecr(l[i+1:], com + [e])
getCombinationsRecr(l)
return results
print(getCombinations())