20
\$\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
    Commented Feb 8, 2011 at 19:43
  • \$\begingroup\$ @Dogbert - I didn't think about that. Sorry, yes. I will update the post. \$\endgroup\$
    – Anto
    Commented Feb 8, 2011 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
    Commented Feb 8, 2011 at 19:49
  • 3
    \$\begingroup\$ Anto: A task where a solution has no observable side-effects isn't very nice, though. \$\endgroup\$
    – Joey
    Commented Feb 8, 2011 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\$ Commented Sep 1, 2015 at 11:48

39 Answers 39

1
2
0
\$\begingroup\$

Smalltalk (Smalltalk/X) (51 chars)

using the regex pakage:

(s regex:'\d+' matchesCollect:[:n|n asNumber])sum

w.o. regex:

((s asCollectionOfSubCollectionsSeparatedByAnyForWhich:[:c|c isDigit not]) map:#asNumber)sum

input in s

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

Javascript - 43 chars

I know it's long, but there was no JS solution so :)

c=0
a=a.split(/[^\d]/g)
for(i in a)c+=+a[i]

ais the string. c contains answer.

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

Tcl, 30

expr [regsub -all \\D+ $a.0 +]

It assumes that the input is in the variable $a (formally, in a) and stores the answer in the interpreter result. I/O is left as an exercise.

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

APL, 16 bytes

{+/⍎b\⍵/⍨b←⍵∊⎕d}

⎕d is a built-in containing the digits (0-9). b is assigned to a vector of 0/1 where 1 is given to the characters that are digits. b is used to compress the given character array and then reused to expand it, which inserts blanks. is APL's eval which converts a string into a vector a integers, in this case. +/ computes the sum.

\$\endgroup\$
1
  • \$\begingroup\$ Equal length, but interesting: +/2⊃⍞⎕VFI⍨⎕AV~⎕D \$\endgroup\$
    – Adám
    Commented Jul 20, 2016 at 14:46
0
\$\begingroup\$

Swift 3, 78

s.characters.split{!("0"..."9"~=$0)}.flatMap{Int(String($0))}.reduce(0){$0+$1}

where s is the string

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

Arturo, 29 bytes

$=>[match&{/\d+}do~"∑|<=|"]

Try it

$=>[             ; a function
    match&{/\d+} ; get the numbers in the input as a block of strings; push to stack
    do           ; eval
    ~            ; interpolate
    "∑|<=|"      ; sum TOS
]                ; end function
\$\endgroup\$
0
\$\begingroup\$

Thunno, \$ 9 \log_{256}(96) \approx \$ 7.41 bytes

"\d+"AfiS

Attempt This Online! or verify all test cases

Explanation

"\d+"AfiS  # Implicit input
"\d+"Af    # Regex findall "\d+"
       i   # Convert to integers
        S  # Sum
\$\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.