zip() takes as its input iterables.
Is insight
type iterable
?
The following is a scrambled line of Python one liner:
sum([range(1, 6) zip(5, 0, -1)] a, b for a*b in) if
Some of the elements in this scrambled code are:
sum(), range(1,6), zip(), a*b, if, in
After reading the puzzle, the question "What is the noteworthiness of zip(5, 0, -1)
?" occurred to me.
Even though the line is scrambled, the meaning of zip()
remains a mystery to me.
>>> zip()
<zip object at 0x10d595100>
Since unraveling the mysteries of zip()
is required to assemble the pieces of this jigsaw puzzle, we embark on a journey to do so.
>>> zip(0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable
While poking at the function might return errors, the results can serve as valuable insight into the workings of zip()
. Unless we read the error message, we cannot adjust the code for a desired result. zip()
takes an iterable
input, but 0
is 'int' . Is 0
'int', or isn't int
iterable
?
>>> zip({1,2,3})
<zip object at 0x10d595240>
The set { x | x ∈ {1, 2, 3} } is iterable
, because it can be given to zip()
as its input.
for
, in
and []
suggest a list comprehension
zip()
will take an iterable
, range()
is iterable
>>> zip([a*b for a if b in range(0,6,-1)]
File "<stdin>", line 1
zip([a*b for a if b in range(0,6,-1)]
^
SyntaxError: invalid syntax
>>> [*zip([a for a in range(0,6)])]
[(0,), (1,), (2,), (3,), (4,), (5,)]
Whenever an a
value is generated in the range(0,6)
, it is enclosed in parentheses and given a comma as a companion.
Since inputing the iterable
[0, 1, 2, 3, 4, 5] into zip()
results as above, we can infer that zip()
puts parentheses around elements and appends them with a comma. Perhaps it creates tuples of elements.
I am stuck, so I will try to add sum()
to the line.
>>> sum(zip([a for a in range(0,6)]))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'tuple'
Before adding int
to tuple
, sum()
returns an error.
>>> [*zip([sum([(a * b) for a in range(6)]) for b in range(5) if 0-1])]
[(0,), (15,), (30,), (45,), (60,)]
This is a solution I came up with that uses all of the provided elements.
Wow! I asked the computer agent for the intended solution, and it is a cool one liner:
>>> sum([a * b for a, b in zip(range(1, 6), range(5, 0, -1))])
35
- Previous: When unscrambling jumbled significance.
- Next: Filter and taking breaks.