16
\$\begingroup\$

Task

The task is very simple. Given a non-empty string containing numbers, uppercase and lowercase letters, output the sum of the remaining numbers. For example:

a1wAD5qw45REs5Fw4eRQR33wqe4WE

Filtering out all the letters would result into:

 1   5  45   5  4    33   4

The sum of these numbers is 1 + 5 + 45 + 5 + 4 + 33 + 4 = 97. So the output would be 97.

Test Cases

a > 0
0 > 0
5 > 5
10 > 10
a0A > 0
1a1 > 2
11a1 > 12
4dasQWE65asAs5dAa5dWD > 79
a1wAD5qw45REs5Fw4eRQR33wqe4WE > 97

This is , so the submission with the least amount of bytes wins!

\$\endgroup\$
3
  • \$\begingroup\$ I knew I had written that Labyrinth program before... here is the same challenge but with negative numbers as well (which makes a surprisingly big difference for some languages, so I don't think they're dupes). \$\endgroup\$ Feb 12 '16 at 18:06
  • \$\begingroup\$ @MartinBüttner Looks like that one doesn't include negative numbers: "-n (where n is an integer) is not counted as a negative n, but as a hyphen followed by n." \$\endgroup\$
    – Paul
    Feb 13 '16 at 3:18
  • \$\begingroup\$ Oh, I see what you mean. You're saying it has hyphens and this one doesn't. \$\endgroup\$
    – Paul
    Feb 13 '16 at 3:20

31 Answers 31

1
2
1
\$\begingroup\$

Python 2, 71 bytes

I just had to make one-liner, though it costs a whole byte more than @TanMath's answer

sum(map(int,"".join([i*i.isdigit()or" "for i in raw_input()]).split()))

Explaination:

raw_input()                             - Input
"".join([i*i.isdigit()or" "for i in _]) - Replace non-digits with spaces 
map(int, _.split())                     - Split on spaces, and convert to number
sum(_)                                  - Sum the resulting list
\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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