Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 03b41ba

Browse files
file
1 parent 4bc2865 commit 03b41ba

File tree

5 files changed

+87
-0
lines changed

5 files changed

+87
-0
lines changed

‎Itertools/Compress the String!.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from itertools import groupby
2+
ls=list(map(int, " ".join(input()).split(" ")))
3+
for key, group in(groupby(ls)):
4+
print((len((list(group))), key),end=" ")
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from itertools import combinations
2+
st,*r = input().split()
3+
for i in range(1,int(r[0])+1):
4+
for j in list(combinations(sorted(st),i)):
5+
print(*j,sep="")
6+
7+
8+
# these are the more ways to find permutations
9+
10+
#>>> from itertools import combinations
11+
#>>>
12+
#>>> print list(combinations('12345',2))
13+
#[('1', '2'), ('1', '3'), ('1', '4'), ('1', '5'), ('2', '3'), ('2', '4'), ('2', '5'), ('3', '4'), ('3', '5'), ('4', '5')]
14+
#>>>
15+
#>>> A = [1,1,3,3,3]
16+
#>>> print list(combinations(A,4))
17+
#[(1, 1, 3, 3), (1, 1, 3, 3), (1, 1, 3, 3), (1, 3, 3, 3), (1, 3, 3, 3)]
18+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from itertools import combinations_with_replacement
2+
st,*r = input().split()
3+
for j in list(combinations_with_replacement(sorted(st),int(r[0]))):
4+
print(*j,sep="")
5+
6+
7+
# these are the more ways to find permutations
8+
9+
#>>> from itertools import combinations_with_replacement
10+
#>>>
11+
#>>> print list(combinations_with_replacement('12345',2))
12+
#[('1', '1'), ('1', '2'), ('1', '3'), ('1', '4'), ('1', '5'), ('2', '2'), ('2', '3'), ('2', '4'), ('2', '5'), ('3', '3'), ('3', '4'), ('3', '5'), ('4', '4'), ('4', '5'), ('5', '5')]
13+
#>>>
14+
#>>> A = [1,1,3,3,3]
15+
#>>> print list(combinations(A,2))
16+
#[(1, 1), (1, 3), (1, 3), (1, 3), (1, 3), (1, 3), (1, 3), (3, 3), (3, 3), (3, 3)]
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from itertools import permutations
2+
st,*r = input().split()
3+
for i in list(sorted(permutations(st,int(r[0])))):
4+
print(*i,sep="")
5+
6+
7+
# these are the more ways to find permutations
8+
9+
#>>> from itertools import permutations
10+
#>>> print permutations(['1','2','3'])
11+
#<itertools.permutations object at 0x02A45210>
12+
#>>>
13+
#>>> print list(permutations(['1','2','3']))
14+
#[('1', '2', '3'), ('1', '3', '2'), ('2', '1', '3'), ('2', '3', '1'), ('3', '1', '2'), ('3', '2', '1')]
15+
#>>>
16+
#>>> print list(permutations(['1','2','3'],2))
17+
#[('1', '2'), ('1', '3'), ('2', '1'), ('2', '3'), ('3', '1'), ('3', '2')]
18+
#>>>
19+
#>>> print list(permutations('abc',3))
20+
#[('a', 'b', 'c'), ('a', 'c', 'b'), ('b', 'a', 'c'), ('b', 'c', 'a'), ('c', 'a', 'b'), ('c', 'b', 'a')]
21+
22+
23+
24+
#we can also chang our for loop like: for i in list(permutations(sorted(st),int(r[0]))):

‎Itertools/itertools.product().py‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from itertools import product
2+
3+
a= list(map(int, input().split()))
4+
b= list(map(int, input().split()))
5+
6+
print(*(list(product(a,b))))
7+
8+
9+
# There are many ways to do product.
10+
11+
#>>> from itertools import product
12+
#>>>
13+
#>>> print list(product([1,2,3],repeat = 2))
14+
#[(1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3)]
15+
#>>>
16+
#>>> print list(product([1,2,3],[3,4]))
17+
#[(1, 3), (1, 4), (2, 3), (2, 4), (3, 3), (3, 4)]
18+
#>>>
19+
#>>> A = [[1,2,3],[3,4,5]]
20+
#>>> print list(product(*A))
21+
#[(1, 3), (1, 4), (1, 5), (2, 3), (2, 4), (2, 5), (3, 3), (3, 4), (3, 5)]
22+
#>>>
23+
#>>> B = [[1,2,3],[3,4,5],[7,8]]
24+
#>>> print list(product(*B))
25+
#[(1, 3, 7), (1, 3, 8), (1, 4, 7), (1, 4, 8), (1, 5, 7), (1, 5, 8), (2, 3, 7), (2, 3, 8), (2, 4, 7), (2, 4, 8), (2, 5, 7), (2, 5, 8), (3, 3, 7), (3, 3, 8), (3, 4, 7), (3, 4, 8), (3, 5, 7), (3, 5, 8)]

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /