Shortest python A + B program with input and output

10

Some of you may say that I am wasting my time, but this task does not give me peace for more than 500 days. It is required to write a program in Python 3.x that takes two numbers as input and displays their sum. The numbers are given in the following format:

a b

Some example test cases:

100 500 -> 600
3 4     -> 7

The length of the code is calculated with the formula max(code length without spaces or tabs or newlines, code length / 4). I know only 2 solutions, each of them has a length of 36 characters:

print(sum(map(int, input().split())))

and:

print(eval(input().replace(' ', '+')))

Also I know, that the exists solution with length of 34 symbols. You can check it on this website.

Evgeny

Posted 2017-04-17T09:39:53.907

Reputation: 219

10

If spaces don't count, I have a 34-char solution (SHA1 is e3eb1f896cffc2dbff531ce5ba8fa25d34c22c76). I'm not sure though it would be good to publicly post a solution to what seems to be a continuing programming competition. Do we have a site policy on that?

– xnor – 2017-04-17T10:34:47.930

@ xnor, it's very old task, nobody can stop you to solve this problem here. – Evgeny – 2017-04-17T10:43:48.813

1@xnor as we already got an 34-char solution, would you mind posting yours? Claudio's SHA1 is different than yours – Felipe Nardi Batista – 2017-04-18T11:04:25.090

2@FelipeNardiBatista It's the same thing with double quotes. – xnor – 2017-04-18T21:55:02.430

Answers

5

Given the right hint toward the solution in the comment to the same question asked on stackoverflow ( see here ), I have got it right down to 34 and without any limitations on the input number or other tricks necessary:

print(eval(input().replace(*' +')))

Here a short summary of currently known different solutions:

print(eval(input().replace(' ','+'))) # 36

print(sum(map(int,input().split()))) # 36

print(eval(input().replace(*' +'))) # 34

print(sum(map(int,input()[::2]))) # 33 (limited to numbers between 0 and 9)

Maybe it could be considered cheating, but maybe not. At least there is no rule for this defined yet so let's include it into the collection of possible solutions:

import f;f.f() # 13

Required for this solution to work is a script f.py available in a search directory for modules of Python with following content:

def f():
    print(sum(map(int,input().split()))) 

Claudio

Posted 2017-04-17T09:39:53.907

Reputation: 174

If you read the question, it has to work on the given website, where there would not be an external script to import. – mbomb007 – 2017-04-17T20:35:04.747

@mbomb007 : I have read the question and it is not mentioned in the question, or can you provide a citation from the question stating that it is? – Claudio – 2017-04-17T20:41:25.667

1

"You can check it [(the problem)] on this website." And on the website, the specifications require input from STDIN. http://acmp.ru/index.asp?main=task&id_task=1 You don't get to create another module.

– mbomb007 – 2017-04-17T20:43:27.873

1@Claudio thank for the answer. The correct version is print(eval(input().replace(*' +'))) # 34 – Evgeny – 2017-04-17T22:29:26.263

@Claudio, can I close duplicate of this question on stackoverflow? – Evgeny – 2017-04-18T09:03:28.680

If you will be allowed to do that, why not? I can then see if my reputation there will decrease. Let's try :) . Personally I think that your posting at stackoveflow is valuable there, but most admins think probably it is not. My opinion is that diversity and duplicates are not necessary bad, but this is my personal opinion. Here some funny thing: as I wanted to make the answer 100% same as on stackoverflow my edit was rejected. I posted as guest but edited from new account I have created here on codegolf. Funny thing that I was not allowed to edit my own answer ... :D :D :D – Claudio – 2017-04-18T09:13:28.913

By the way: I have hard time to understand why the admins downvote and want get rid of some very interesting questions/answers. I am getting the feeling that under time pressure they reject what is hard to grasp or understand not able to see the positive potential behind it. I learned very much about Python syntax and standard library out of this question and wouldn't run into it if it was posted only here. Putting things in clear and unambiguous categories is hard to accomplish and in most cases pure subjective decision ... – Claudio – 2017-04-18T09:32:30.263

@Claudio I posted my question here because I was advised to do this on stackoverflow. Generally, I think people disagree with me on stackoverflow because the question not trivial and expect a lot of syntax knowledge. Also I agree with you, that question may be exists on both of network sites – Evgeny – 2017-04-18T12:11:20.680

@Claudio, if you are interesting in C++, I have the same question. I know the solution for Linux, but not for Windows. Let me know, I can post it. – Evgeny – 2017-04-18T12:15:14.850

@ЕвгенийКондратенко I am currently not into C++, also not into any questions without direct practical use and definitely not into Windows. From this perspective dealing with your question on stackoverflow was an absolute exception. I just wanted to see how good am I at Python and detected that I was not aware that the *[itemA,itemB] syntax apply also to characters in strings. That's it - now I'm done with riddles and Co for a while. Thanks for the offer (y) :) . – Claudio – 2017-04-18T13:00:15.800

@Claudio if it's possible, try to combine two Claudio into the one. – Evgeny – 2017-04-18T13:05:44.053