19
\$\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, 2016 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, 2016 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, 2016 at 3:20

42 Answers 42

1
2
1
\$\begingroup\$

Bash + GNU utilities, 29

grep -Eo [0-9]+|paste -sd+|bc

If it is required to support input with no numbers (e.g. a), then we can do this:

Bash + GNU utilities, 38

1 byte saved thanks to @TobySpeight.

(grep -Eo [0-9]+;echo 0)|paste -sd+|bc
\$\endgroup\$
4
  • \$\begingroup\$ This prints nothing for input a. We are currently discussing whether that's valid or not. \$\endgroup\$
    – Dennis
    Feb 13, 2016 at 0:16
  • \$\begingroup\$ @Dennis. Ok. I added another version to cover both eventualities. \$\endgroup\$ Feb 13, 2016 at 0:27
  • \$\begingroup\$ You could use ; instead of || to always add zero, for no harm. \$\endgroup\$ Feb 15, 2016 at 14:34
  • \$\begingroup\$ @TobySpeight Yes, thats good - thanks! \$\endgroup\$ Feb 15, 2016 at 18:01
1
\$\begingroup\$

Python 2, 70 bytes

I am putting a Python answer just for fun.

import re
j=0
for i in map(int,re.findall('\d+',input())):j+=i
print j

Very simple and uses regex to find all the numbers in the input. Try it online!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ In this context you have to use raw_input or switch to python3. A smaller version (py3, 56 bytes) : import re;print(sum(map(int,re.findall('\d+',input())))). \$\endgroup\$
    – Dica
    Feb 18, 2016 at 14:01
1
\$\begingroup\$

Oracle SQL 11.2, 105 bytes

SELECT NVL(SUM(TO_NUMBER(COLUMN_VALUE)),0)FROM XMLTABLE(('"'||regexp_replace(:1,'[a-zA-Z]','","')||'"'));

The regex convert alpha characters to ','

XMLTABLE create one row per item in the string using ',' as the separator.

SUM the rows to get the result.

NVL is needed to account for a string with no digit.

\$\endgroup\$
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
\$\begingroup\$

Pip, 6 bytes

$+a@XI

Try It Online!

Explanation

    XI  Built-in regex matching integers
   @    Find all matches in
  a     Command-line input
$+      Fold on addition
\$\endgroup\$
1
\$\begingroup\$

Japt v2.0a0 -x, 3 bytes

'Cause ETH's solution is a bit cheaty!

q\D

Try it

q\D     :Implicit input of string
q       :Split on
 \D     :  RegEx /\D/ (or /[^0-9]/)
        :Implicit output of sum of resulting array
\$\endgroup\$
1
\$\begingroup\$

J-uby, 19 bytes

~:scan&/\d+/|:sum+Z

Attempt This Online!

\$\endgroup\$
1
\$\begingroup\$

jq -R, 31 bytes

[splits("\\D")|tonumber?]|0+add

Try it online!

\$\endgroup\$
1
\$\begingroup\$

J + regex, 21 bytes

1#.0,'\d+'".&>@rxall]

Attempt This Online!

1#.0,'\d+'".&>@rxall]
     '\d+'     rxall]  NB. find all matches of /\d+/ in right arg ]
              @        NB. then execute the monadic verb...
          ".&>         NB. eval each boxed match and unbox result
   0,                  NB. prepend 0 in case of no match
1#.                    NB. sum integer array

If adding the script to the language isn't allowed, then that adds 14 bytes for a total of 35.

1#.0,'\d+'".&>@rxall(load'regex')]]

The only difference is the fork (load'regex')]] that loads the library and simply returns the function arg using dyadic ].

\$\endgroup\$
1
\$\begingroup\$

Uiua, 19 12 bytes

/+⊜parse≤@9.

Try it!

-7 thanks to Bubbler

/+⊜parse≤@9.
           .  # duplicate
        ≤@9   # where are the characters less than or equal to 9?
  ⊜parse      # partition to an array of integers
/+            # sum
\$\endgroup\$
2
  • 1
    \$\begingroup\$ ⊜ partition is the perfect built-in here, so /+⊜parse≤@9. works for 12. ≤@9 works because there are no letters below digits. \$\endgroup\$
    – Bubbler
    Oct 20, 2023 at 1:21
  • \$\begingroup\$ @Bubbler Nice. Thanks! \$\endgroup\$
    – chunes
    Oct 20, 2023 at 1:34
0
\$\begingroup\$

05AB1E (legacy), 3 bytes

á¡O

Try it online or verify all test cases.

In the new version of 05AB1E it would have been 2 bytes longer, because ¡ now supports substrings to split on (in the legacy version a string argument would be interpret as a list of characters implicitly) and will keep empty strings:

áS¡þO

Try it online or verify all test cases.

Explanation:

       # Example input: "a1wAD5qw45REs5Fw4eRQR33wqe4WE"

       # (legacy version)
á      # Only keep the letters of the (implicit) input-string
       #  STACK: "awADqwREsFweRQRwqeWE"
 ¡     # Split the (implicit) input-string on those characters
       #  STACK: ["1","5","45","5","4","33","4"]
  O    # Sum the remaining numbers
       #  STACK: 97
       # (which is output implicitly as result)

       # (new version)
á      # (same as above)
 S     # Split it to a list of characters
       #  STACK: ["a","w","A","D","q","w","R","E","s","F","w","e","R","Q","R","w","q","e","W","E"]
  ¡    # Split the (implicit) input-string on those characters
       #  STACK: ["","1","","","5","","45","","","5","","4","","","","33","","","4","",""]
   þ   # Remove all empty strings, by only keeping numbers
       #  STACK: ["1","5","45","5","4","33","4"]
    O  # (same as above)
\$\endgroup\$
0
\$\begingroup\$

q, 41 20 bytes

sum value .Q.n .Q.n?

k, 23 14 bytes

+/. .Q.n .Q.n?
\$\endgroup\$
1
2

Your Answer

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

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