Skip to content

Commit dcdfd4b

Browse files
committed
Combination
1 parent 5316c13 commit dcdfd4b

File tree

2 files changed

+67
-25
lines changed

2 files changed

+67
-25
lines changed

Notes/itertools/combination.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Combinations()
2+
# The combinations() function in Python, part of the itertools module,
3+
# is used to generate all possible combinations of a specified length from a given iterable
4+
# (like a list, string, or tuple).
5+
# Unlike permutations, where the order does matter,
6+
# combinations focus only on the selection of elements, meaning the order does not matter.
7+
# It returns an iterator producing tuples, each representing a unique combination of the input elements.
8+
9+
10+
# Example:
11+
from itertools import combinations
12+
13+
a = "GeEK"
14+
# generate all combinations of length 2
15+
for j in combinations(a, 2):
16+
print(j)
17+
18+
# ('G', 'e')
19+
# ('G', 'E')
20+
# ('G', 'K')
21+
# ('e', 'E')
22+
# ('e', 'K')
23+
# ('E', 'K')
24+
25+
26+
# Explanation:
27+
# itertools.combinations()
28+
# generates all unordered pairs (length = 2) from the string "GeEK".
29+
# Each element is treated by its position and value.
30+
# The order within each tuple doesn't matter and no duplicate combinations (like ('e', 'G')) are included.
31+
32+
# Syntax of Itertools.Combinations()
33+
# itertools.combinations(iterable, r)
34+
35+
# Parameters:
36+
37+
# iterable: The input sequence (list, string, tuple, etc.) from which combinations are formed.
38+
# r: The length of each combination to be generated.
39+
# Returns: An iterator that produces tuples, each representing a unique combination of r elements from the iterable, in the order they appear.

Notes/itertools/permutations.py

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,52 @@
11
# Itertools.permutation()
2-
# Itertools.permutation() function falls under the Combinatoric Generators.
3-
# The recursive generators that are used to simplify combinatorial constructs such as permutations, combinations,
2+
# Itertools.permutation() function falls under the Combinatoric Generators.
3+
# The recursive generators that are used to simplify combinatorial constructs such as permutations, combinations,
44
# and Cartesian products are called combinatoric iterators
55

6-
# The word “Permutation” it refers to all the possible combinations in which a set or string can be ordered or arranged.
7-
# Similarly here itertool.permutations() method provides us with all the possible arrangements
8-
# that can be there for an iterator and all elements are assumed to be unique on the basis of their position
9-
# and not by their value or category.
6+
# The word “Permutation” it refers to all the possible combinations in which a set or string can be ordered or arranged.
7+
# Similarly here itertool.permutations() method provides us with all the possible arrangements
8+
# that can be there for an iterator and all elements are assumed to be unique on the basis of their position
9+
# and not by their value or category.
1010

11-
# All these permutations are provided in lexicographical order.
12-
# The function itertool.permutations() takes an iterator
13-
# and ‘r’ (length of permutation needed) as input and
14-
# assumes ‘r’ as default length of iterator
15-
# if not mentioned and returns all possible permutations of length ‘r’ each.
11+
# All these permutations are provided in lexicographical order.
12+
# The function itertool.permutations() takes an iterator
13+
# and ‘r’ (length of permutation needed) as input and
14+
# assumes ‘r’ as default length of iterator
15+
# if not mentioned and returns all possible permutations of length ‘r’ each.
1616

1717
# Syntax:
1818
# Permutations(iterator, r)
1919

2020
# Example:
21-
from itertools import permutations
22-
name="HASH"
23-
p=permutations(name) # it return permutation object <itertools.permutations object at 0x000001DED0B63100>
21+
from itertools import permutations
22+
23+
name = "HASH"
24+
p = permutations(
25+
name
26+
) # it return permutation object <itertools.permutations object at 0x000001DED0B63100>
2427
for i in list(p):
25-
print(i) # print each value inside the permutation object
28+
print(i) # print each value inside the permutation object
2629

2730

2831
# Example:
29-
print ("All the permutations of the given list is:")
30-
print (list(permutations([1, 'Spider'],2)))
31-
print()
32+
print("All the permutations of the given list is:")
33+
print(list(permutations([1, "Spider"], 2)))
34+
print()
3235

3336
# All the permutations of the given list is:
3437
# [(1, 'Spider'), ('Spider', 1)]
35-
3638

37-
print ("All the permutations of the given string is:")
38-
print (list(permutations('PT')))
39-
print()
39+
40+
print("All the permutations of the given string is:")
41+
print(list(permutations("PT")))
42+
print()
4043

4144
# All the permutations of the given string is:
4245
# [('P', 'T'), ('T', 'P')]
4346

4447

45-
print ("All the permutations of the given container is:")
46-
print(list(permutations(range(3), 2))) # 2 is r -> refers to the length or dimension
48+
print("All the permutations of the given container is:")
49+
print(list(permutations(range(3), 2))) # 2 is r -> refers to the length or dimension
4750

4851
# If r and length is mentioned here the output is
4952
# All the permutations of the given container is (range(3), 2):
@@ -56,7 +59,7 @@
5659

5760
# Example:
5861

59-
lst=["a","b","c"]
62+
lst = ["a", "b", "c"]
6063

6164
print("\nCombination of letters: ")
6265
for i in permutations(lst):

0 commit comments

Comments
 (0)