1

The input is two integers in one line, x and y. I need to write a basically one-line program which does different things with x and y, and prints the result. Say, the output should be x + 1, y * y. Or list(range(x)), y // 2. Whatever operations but different. No def functions, also 'if' and 'for' are prohibited. As far as i understand, should look something like:

print(
    *map(
        lambda x: ??? ,
        map(
            int, input().split()
        )
    )
)

(But lambda can only do same thing to both inputs, right? ) I know it is possible and i've been thinking about this for three days to no avail. Most probable i miss something very obvious.

3
  • 2
    Right? Wrong. You can pass as many parameters to lambda as you like. Commented Sep 14, 2018 at 23:49
  • Input many parameters - yes. Output a tuple - yes. The problem is that output = fixed function of input. Normally one would simply write: x, y = map(int(input().split()) print(x+1) print(y*y) But it has to be done without intermediate x,y and in one line Commented Sep 14, 2018 at 23:59
  • *map(...) is what you need. Commented Sep 15, 2018 at 0:03

2 Answers 2

1

Your lambda function can take an x and y and turn them into pretty much any expression. Then you would call that function, doing the processing of the inputs outside of that lambda

print(*(lambda x, y: (x+1, y*y))(*map(int, input().split())))
print(*(lambda x, y: (list(range(x)), y//2))(*map(int, input().split())))
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I had a mental block indeed :) Very simple
0

This seems to work just fine:

print(
    *(lambda x: [int(x[0]) + 1, int(x[1]) * int(x[1])])(
        input().split()
    )
)

No fancy functional tricks or map are needed.

1 Comment

Thank you! I had a mental block indeed :) Several previous tasks in my course were heavy on map, filter etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.