Consider the definition of a new datatype.
The new datatype is the inttuple
. An inttuple
is defined as being
either an integer, or a tuple consisting of inttuple
s. You see an
example of an inttuple
in the code block below.
>>> inttuple = (1, 2, (3, 4), 5, ((6, 7, 8, (9, 10), 11), 12, 13), ((14, 15, 16), (17, 18, 19, 20)))
Write a function flatten
that takes an inttuple
. The function must return a tuple
containing all integer values stores in the given inttuple
.
Tip
Since the
inttuple
is defined recursively, a recursive function is probably the right approach. If you skipped Chapter 101, you probably should skip this exercise too. Use theisinstance()
function (explained in Chapter 92) to determine whether you are dealing with an integer or a tuple.
>>> inttuple = (1, 2, (3, 4), 5, ((6, 7, 8, (9, 10), 11), 12, 13), ((14, 15, 16), (17, 18, 19, 20)))
>>> flatten(inttuple)
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)