17
\$\begingroup\$

Create a program which sums all integers found in a string which is set as a variable in the program (thus, the program doesn't have to handle any input). The integer numbers are separated by non-numericals (anything but 0, 1, 2, 3, ..., 9).

Examples:

  • e7rde f ,fe 43 jfj 54f4sD = 7+43+54+4=108
  • 5 = 5
  • 64 545,5445-32JIFk0ddk = 64+545+5445+32+0=6086
  • 0ab0 = 0+0 = 0

Extra notes:

  • Unicode support is not necessary, but allowed
  • -n (where n is an integer) is not counted as a negative n, but as a hyphen followed by n.

The answer may be printed on the screen (but not required).

Shortest answer (in characters) win.

\$\endgroup\$
5
  • \$\begingroup\$ Should we print the result too? (You mention no I/O). \$\endgroup\$
    – Dogbert
    Feb 8 '11 at 19:43
  • \$\begingroup\$ @Dogbert - I didn't think about that. Sorry, yes. I will update the post. \$\endgroup\$
    – Anto
    Feb 8 '11 at 19:45
  • \$\begingroup\$ Changed it as some people already had answers and didn't want to "hurt" them. I guess I should sleep now, so I will think a bit clearer ;) \$\endgroup\$
    – Anto
    Feb 8 '11 at 19:49
  • 3
    \$\begingroup\$ Anto: A task where a solution has no observable side-effects isn't very nice, though. \$\endgroup\$
    – Joey
    Feb 8 '11 at 21:00
  • \$\begingroup\$ An interesting test case I just ran into would be 5a-3 (my code would skip - if it follows a number immediately, but not if there was a non-number before it). \$\endgroup\$ Sep 1 '15 at 11:48

36 Answers 36

1
2
0
\$\begingroup\$

Perl - 24 chars

warn eval join'+',/\d+/g

Input is in $_

\$\endgroup\$
0
\$\begingroup\$

SNOBOL4 (CSNOBOL4), 76 bytes

	I =INPUT
S	I SPAN(&DIGITS) . X REM . I	:F(O)
	O =O + X	:(S)
O	OUTPUT =O
END

Try it online!

\$\endgroup\$
0
\$\begingroup\$

BASH (v5.0.17) + Unix utils, 40 34 bytes

"s" is the string

echo ${s//[!0-9]/ }|tr ' ' '+'|bc
\$\endgroup\$
1
  • \$\begingroup\$ tr -Cs 0-9 +|bc, depending on environments, to golf off; input from stdin. \$\endgroup\$ Aug 16 at 6:57
0
\$\begingroup\$

Actually, 14 bytes

9u▀8╙r♂┌-@s♂≈Σ

Try it online!

This program supports the CP437 code page for input.

Explanation:

9u▀8╙r♂┌-@s♂≈Σ
9u▀             base 10 digits (0-9)
   8╙r♂┌        all characters in CP437 (map(ord_cp437, range(2**8)))
        -       set difference
         @s     split input on any value in the resulting list
           ♂≈Σ  convert to ints and sum
\$\endgroup\$
0
\$\begingroup\$

Vyxal s, 8 bytes

`\D`ṡ';⌊

Try it Online!

Ugh we really need to make play nice with strings not containing numbers in v2.6; this would be 6 bytes if it didn't error.

Explained

`\D`ṡ';⌊
`\D`     # The string "\D" (a regex that looks for non-digits)
    ṡ    # Split the input on this regex
     ';  # Remove empty strings (because ⌊ on `` errors)
       ⌊ # Convert each to a number
         # Output the sum with the `s` flag  
\$\endgroup\$
0
\$\begingroup\$

Husk, 4 bytes

ṁiġ√

Try it online!

Explanation

ṁiġ√
  ġ√ group on isalpha
ṁ    sum
 i   the first integer in each string
\$\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.