Skip to content

Commit b217173

Browse files
committed
itertool
1 parent 33e5c5a commit b217173

File tree

2 files changed

+45
-9
lines changed

2 files changed

+45
-9
lines changed

Notes/itertools/combination.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
# Combinations()
22
# 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
3+
# is used to generate all possible combinations of a specified length
4+
# from a given iterable
45
# (like a list, string, or tuple).
56
# 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.
7+
# combinations focus only on the selection of elements,
8+
# meaning the order does not matter.
9+
# It returns an iterator producing tuples,
10+
# each representing a unique combination of the input elements.
811

912

1013
# Example:
@@ -27,13 +30,17 @@
2730
# itertools.combinations()
2831
# generates all unordered pairs (length = 2) from the string "GeEK".
2932
# 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.
33+
# The order within each tuple doesn't matter and
34+
# no duplicate combinations (like ('e', 'G')) are included.
3135

3236
# Syntax of Itertools.Combinations()
3337
# itertools.combinations(iterable, r)
3438

3539
# Parameters:
3640

37-
# iterable: The input sequence (list, string, tuple, etc.) from which combinations are formed.
41+
# iterable: The input sequence (list, string, tuple, etc.)
42+
# from which combinations are formed.
3843
# 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.
44+
# Returns: An iterator that produces tuples,
45+
# each representing a unique combination of r elements from the iterable,
46+
# in the order they appear.

Notes/itertools/permutations.py

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
# Itertools.permutation()
22
# Itertools.permutation() function falls under the Combinatoric Generators.
3-
# The recursive generators that are used to simplify combinatorial constructs such as permutations, combinations,
3+
# The recursive generators that are used to simplify combinatorial constructs
4+
# such as permutations, combinations,
45
# and Cartesian products are called combinatoric iterators
56

6-
# The word “Permutation” it refers to all the possible combinations in which a set or string can be ordered or arranged.
7+
# The word “Permutation” it refers to all the possible combinations
8+
# in which a set or string can be ordered or arranged.
79
# Similarly here itertool.permutations() method provides us with all the possible arrangements
810
# that can be there for an iterator and all elements are assumed to be unique on the basis of their position
911
# and not by their value or category.
@@ -27,6 +29,32 @@
2729
for i in list(p):
2830
print(i) # print each value inside the permutation object
2931

32+
# Example:
33+
34+
# ('H', 'A', 'S', 'H')
35+
# ('H', 'A', 'H', 'S')
36+
# ('H', 'S', 'A', 'H')
37+
# ('H', 'S', 'H', 'A')
38+
# ('H', 'H', 'A', 'S')
39+
# ('H', 'H', 'S', 'A')
40+
# ('A', 'H', 'S', 'H')
41+
# ('A', 'H', 'H', 'S')
42+
# ('A', 'S', 'H', 'H')
43+
# ('A', 'S', 'H', 'H')
44+
# ('A', 'H', 'H', 'S')
45+
# ('A', 'H', 'S', 'H')
46+
# ('S', 'H', 'A', 'H')
47+
# ('S', 'H', 'H', 'A')
48+
# ('S', 'A', 'H', 'H')
49+
# ('S', 'A', 'H', 'H')
50+
# ('S', 'H', 'H', 'A')
51+
# ('S', 'H', 'A', 'H')
52+
# ('H', 'H', 'A', 'S')
53+
# ('H', 'H', 'S', 'A')
54+
# ('H', 'A', 'H', 'S')
55+
# ('H', 'A', 'S', 'H')
56+
# ('H', 'S', 'H', 'A')
57+
# ('H', 'S', 'A', 'H')
3058

3159
# Example:
3260
print("All the permutations of the given list is:")
@@ -46,7 +74,8 @@
4674

4775

4876
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
77+
print(list(permutations(range(3), 2)))
78+
# 2 is r -> refers to the length or dimension
5079

5180
# If r and length is mentioned here the output is
5281
# All the permutations of the given container is (range(3), 2):

0 commit comments

Comments
 (0)