Skip to content

Commit 2dac319

Browse files
authored
Merge pull request #119 from lironmiz/unit8-Ex8.2.3
Create unit8_ex8.2.3.py
2 parents 350917a + 8b14dbe commit 2dac319

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

unit8_ex8.2.3.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# exercise 8.2.3 from unit 8
2+
'''
3+
Write a function called mult_tuple defined as follows:
4+
5+
def mult_tuple(tuple1, tuple2):
6+
The function accepts as parameters two members of the tuple type.
7+
The function returns a tuple containing all the pairs that can be created from the members of the tuples passed as arguments.
8+
9+
Running examples of the mult_tuple function
10+
>>> first_tuple = (1, 2)
11+
>>> second_tuple = (4, 5)
12+
>>> mult_tuple(first_tuple, second_tuple)
13+
((1, 4), (4, 1), (1, 5), (5, 1), (2, 4), (4, 2), (2, 5), (5, 2))
14+
>>> first_tuple = (1, 2, 3)
15+
>>> second_tuple = (4, 5, 6)
16+
>>> mult_tuple(first_tuple, second_tuple)
17+
((1, 4), (1, 5), (1, 6), (2, 4), (2, 5), (2, 6), (3, 4), (3, 5), ( 3, 6), (4, 1), (5, 1), (6, 1), (4, 2), (5, 2), (6, 2), (4, 3), (5, 3), (6, 3))
18+
'''
19+
20+
def mult_tuple(tuple1, tuple2):
21+
def combinations(t1, t2):
22+
# create a list of tuples with all combinations of elements from t1 and t2
23+
comb = [(x, y) for x in t1 for y in t2]
24+
# add all combinations of elements from t2 and t1
25+
comb += [(y, x) for x in t1 for y in t2]
26+
return comb
27+
28+
return combinations(tuple1, tuple2)
29+

0 commit comments

Comments
 (0)