2. Advanced Operations on Built-In Data Structures
Activity 2.01: Permutation, Iterator, Lambda, and List
Solution:
These are the detailed steps to solve this activity:
- Look up the definition of
permutations
anddropwhile
fromitertools
. There is a way to look up the definition of a function inside Jupyter itself. Just type the function name, followed by ?, and press Shift + Enter:from itertools import permutations, dropwhile permutations? dropwhile?
You will see a long list of definitions after each
?
. We will skip it here. - Write an expression to generate all the possible three-digit numbers using
1
,2
, and3
:permutations(range(3))
The output (which will vary in your case) is as follows:
<itertools.permutations at 0x7f6c6c077af0>
- Loop over the iterator expression you generated before. Use the
print
method to print each element returned by the iterator. Useassert
andisinstance
to make sure that the elements are tuples:for number_tuple in permutations(range...