-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcombination.py
More file actions
49 lines (39 loc) · 1.07 KB
/
combination.py
File metadata and controls
49 lines (39 loc) · 1.07 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
def combination(string,mod):
if not string:
if not mod:
return []
return [mod]
ch = string[0]
left = combination(string[1:], mod+ch)
#ascii = combination(string[1:],mod+str(ord(ch)))
right =combination(string[1:],mod)
left+=right
#left+=ascii
return left
#print(combination("abbc",""))
def comination_iterative(string):
lst = [[]]
for i in string:
for j in range(len(lst)):
temp = []
temp+=lst[j]
temp.append(i)
lst.append(temp)
return lst[1:]
#print(comination_iterative([1,2,2]))
def comination_duplicate(string):
lst = [[]]
for i,_ in enumerate(string):
if (i >0 and string[i] == string[i-1]):
s = e
e = len(lst)
else:
s = 0
e = len(lst)
for j in range(s,e):
temp = []
temp+=lst[j]
temp.append(string[i])
lst.append(temp)
return lst[1:]
print(comination_duplicate([1,2,2]))