11
\$\begingroup\$

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.

\$\endgroup\$
4
  • 10
    \$\begingroup\$ 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? \$\endgroup\$
    – xnor
    Apr 17, 2017 at 10:34
  • \$\begingroup\$ @ xnor, it's very old task, nobody can stop you to solve this problem here. \$\endgroup\$
    – Evgeny
    Apr 17, 2017 at 10:43
  • 1
    \$\begingroup\$ @xnor as we already got an 34-char solution, would you mind posting yours? Claudio's SHA1 is different than yours \$\endgroup\$ Apr 18, 2017 at 11:04
  • 2
    \$\begingroup\$ @FelipeNardiBatista It's the same thing with double quotes. \$\endgroup\$
    – xnor
    Apr 18, 2017 at 21:55

1 Answer 1

6
\$\begingroup\$

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()))) 
\$\endgroup\$
11
  • 1
    \$\begingroup\$ If you read the question, it has to work on the given website, where there would not be an external script to import. \$\endgroup\$
    – mbomb007
    Apr 17, 2017 at 20:35
  • \$\begingroup\$ @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? \$\endgroup\$
    – Claudio
    Apr 17, 2017 at 20:41
  • 1
    \$\begingroup\$ "You can check it [(the problem)] on this website." And on the website, the specifications require input from STDIN. acmp.ru/index.asp?main=task&id_task=1 You don't get to create another module. \$\endgroup\$
    – mbomb007
    Apr 17, 2017 at 20:43
  • 1
    \$\begingroup\$ @Claudio thank for the answer. The correct version is print(eval(input().replace(*' +'))) # 34 \$\endgroup\$
    – Evgeny
    Apr 17, 2017 at 22:29
  • 1
    \$\begingroup\$ 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 \$\endgroup\$
    – Claudio
    Apr 18, 2017 at 9:13

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.