'Convert nested loop to map/reduce/filter
As a part of learning python map/reduce/filter methods I've got an exercise- to convert function to one line code using map/reduce/filter only. This is the function that need to be converted:
def func5(n):
l = []
for i in range(n):
j = 0
while j < i:
if j % 2 == 0:
l.append(j + 5)
elif j % 3 == 0:
l.append(j // 2)
elif j % 5 == 2:
l.append(j)
j += 1
return l
So I think that I've done the if-else correct, but I don't know what is the syntax to create nested loop in lambda commands (we should not use for loops). My code so far:
l = list(map(lambda x: x+5 if x % 2 ==0 else x//2 if x % 3 ==0 else x if x % 5 ==2 ,_______))
Solution 1:[1]
All-in in a single line as required... reduce to concatenate lists (by default available only in the functools module, not anymore as build-in), filter to remove the side effects of the inline conditions (see my comment).
import functools as fc
n = 10
a = fc.reduce(lambda i, j: i+j, map(lambda i: list(filter(None, map(lambda j: j+5 if j%2==0 else (j//2 if j%3==0 else (j if j%5==2 else None)), range(i)))), range(n)))
a = list(a)
print(a)
print(a == func5(n))
Output
[5, 5, 5, 7, 5, 7, 1, 5, 7, 1, 9, 5, 7, 1, 9, 5, 7, 1, 9, 11, 5, 7, 1, 9, 11, 7, 5, 7, 1, 9, 11, 7, 13]
True
Solution 2:[2]
In list comprehension form:
[
x
for i in range(n)
for j in range(i)
for x in (
[j + 5]
if j % 2 == 0
else [j // 2]
if j % 3 == 0
else [j]
if j % 5 == 2
else []
)
]
...which can be converted to two reduces:
reduce(
lambda xs, i: xs
+ reduce(
lambda ys, j: ys
+ (
[j + 5]
if j % 2 == 0
else [j // 2]
if j % 3 == 0
else [j]
if j % 5 == 2
else []
),
range(i),
[],
),
range(n),
[],
)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | cards |
| Solution 2 |
