Challenge problem 5 with Python: lambda ... I decided to copy without

kota9 Challenge 5 problems that software engineers should solve in 1 hour. I learned a lot. You think about recurrence like this.

Try to write using lambda

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']

You can append ...

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 ...

Isn't it a level that can be assigned?

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

Challenge problem 5 with Python: lambda ... I decided to copy without
I want to AWS Lambda with Python on Mac!
I tried to solve the problem with Python Vol.1
I want to debug with Python
Challenge Problem 5 with Python: lambda ... Don't go deep into oneliner ... Don't
I wanted to solve the ABC164 A ~ D problem with Python
I wanted to solve ABC160 with Python
I want to analyze logs with Python
I want to play with aws with python
Connect to s3 with AWS Lambda Python
I wanted to solve ABC172 with Python
I really wanted to copy with selenium
I want to do it with Python lambda Django, but I will stop
I want to use MATLAB feval with python
I wanted to solve NOMURA Contest 2020 with Python
I was able to recurse in Python: lambda
I want to make a game with Python
I wanted to install Python 3.4.3 with Homebrew + pyenv
I tried to get CloudWatch data with Python
I tried to output LLVM IR with Python
Sample to send slack notification with python lambda
I want to use Temporary Directory with Python2
I tried to automate sushi making with python
#Unresolved I want to compile gobject-introspection with Python3
Export RDS snapshot to S3 with Lambda (Python)
I want to solve APG4b with Python (Chapter 2)
Upload files to Google Drive with Lambda (Python)
Challenge AtCoder (ABC) 164 with Python! A ~ C problem
I want to write to a file with Python
[Patent analysis] I tried to make a patent map with Python without spending money
Try to solve the programming challenge book with python3
I want to handle optimization with python and cplex
I tried to implement Minesweeper on terminal with python
I tried to get started with blender python script_Part 01
The first algorithm to learn with Python: FizzBuzz problem
I tried to touch the CSV file with Python
I tried to draw a route map with Python
I tried to solve the soma cube with python
I tried to get started with blender python script_Part 02
I was addicted to scraping with Selenium (+ Python) in 2020
I tried to implement an artificial perceptron with python
I want to inherit to the back with python dataclass
I want to work with a robot in python.
Write multiple records to DynamoDB with Lambda (Python, JavaScript)
I tried to automatically generate a password with Python3
[ML Ops] I want to do multi-project with Python
I tried to analyze J League data with Python
I was able to repeat it in Python: lambda
I want to run a quantum computer with Python
I want to bind a local variable with lambda
I tried to solve AOJ's number theory with Python
The 15th offline real-time I tried to solve the problem of how to write with python
I tried to find the entropy of the image with python
I tried fp-growth with python
I tried scraping with Python
I want to be able to analyze data with Python (Part 3)
Connect to BigQuery with Python
I tried to simulate how the infection spreads with Python
I want to specify another version of Python with pyvenv
Operate TwitterBot with Lambda, Python
I wanted to solve the Panasonic Programming Contest 2020 with Python