kota9 Challenge 5 problems that software engineers should solve in 1 hour. I learned a lot. You think about recurrence like this.
So I gave up the challenge and decided to copy it for later study. It's not fun to just copy it, so it's just lambda binding again.
box=lambda *x:x
unbox=lambda x:x[-1]
do=box
switch=unbox
It is preparation. I want to use my own switch. Since I made it, I also use my own foldl.
foldl=(lambda f,acc,xs:
switch(
xs == [] and do( acc )
or do( foldl(f, f(acc, xs[0]), xs[1:]) )
)
)
With the function f_n that takes two integers together A function ff_n that takes a list of integers and attaches them together. It feels like int and str are not cool.
f_n=lambda x,y:int(str(x)+str(y))
ff_n=lambda xs:foldl(f_n,0,xs)
The main subject from here. I tried to make a list of integers for extensibility.
f_5=(lambda NUMS, n,M:
(lambda digits=NUMS[:n],
r_n=range(1,n),
out=[]:
(lambda n_digits=ff_n(digits):
(
out.append(str(n_digits)) if n_digits==M else None,
[[ out.append(x + "+" + str(ff_n(digits[-i:n])))
for x in f_5(NUMS, n-i , M - ff_n(digits[-i:n])) ] for i in r_n ],
[[ out.append(x + "-" + str(ff_n(digits[-i:n])))
for x in f_5(NUMS, n-i , M + ff_n(digits[-i:n])) ] for i in r_n ],
out,
)[-1]
)()
)()
)
ff_5=lambda NUMS, M: f_5(NUMS, len(NUMS), M)
NUMS=range(1,10)
print ff_5(NUMS,100)
did it. It seems that it can be a list of positive integers. If you try to find a pattern that becomes 1000 with a suitable number list
>>> ff_5([1,5,4,2,3,7,9,34,57,64,23],1000)
['15-4+2+37+934+57-64+23', '1+5-42-3+7+934+57+64-23']
Since the original is written with + =, I was a little worried about how to accumulate the return value. You can probably define [] in the argument out by default and add elements to it with append, right? When I wrote it, it seems that it works as it is. This is almost ...
For example
a=(lambda :
(lambda b=[]:
(b.append(1),
b.append(2),
b[0]+b[1],
)[-1]
)()
)
You can store the calculation results in an empty list and use them later. It's almost a local variable, isn't it? this. When I run it
>>> a()
3
You can reassign by popping and inserting. I won't do it. It's going to be crap.
Recommended Posts