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\$
  • \$\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\$ – Martin Ender Sep 1 '15 at 11:48

33 Answers 33

11
\$\begingroup\$

Perl, 15

Input in $_, sum in $c:

s/\d+/$c+=$&/ge
| improve this answer | |
\$\endgroup\$
14
\$\begingroup\$

Ruby 1.9, 21 characters

eval a.scan(/\d+/)*?+

To print the solution to stdout, 2 additional characters are required:

p eval a.scan(/\d+/)*?+

And to read from stdin instead of using a predefined variable, another 3 characters have to be used:

p eval gets.scan(/\d+/)*?+

For Ruby 1.8, replace ?+ with "+" to get a working solution in 22 characters.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ The input is supposed to be taken from a variable, not stdin. Also scan is shorter than split. So your solution becomes eval s.scan(/\d+/)*?+ - 21 characters. \$\endgroup\$ – sepp2k Feb 8 '11 at 21:37
  • \$\begingroup\$ @sepp2k: Yeah, didn't read the description correctly. I'm just used to the other golf-tasks, where you usually have to read from stdin and print to stdout. Good point with scan, thanks! \$\endgroup\$ – Ventero Feb 8 '11 at 21:48
  • \$\begingroup\$ +1, great use of eval and * '+' \$\endgroup\$ – Dogbert Feb 8 '11 at 21:55
6
\$\begingroup\$

Ruby - 36 34 chars

s.scan(/\d+/).map(&:to_i).reduce:+

36 chars if you want the result printed.

p s.scan(/\d+/).map(&:to_i).reduce:+

Assumes the input is present as a string in s.

| improve this answer | |
\$\endgroup\$
6
\$\begingroup\$

Python (60)

import re;print sum(map(int,filter(len,re.split(r'\D',s))))
| improve this answer | |
\$\endgroup\$
4
\$\begingroup\$

JavaScript (ES6), 30

c=0,s.replace(/\d+/g,d=>c+=+d)

Annotated version:

// Store the sum.
c=0,
// Process every number found in the `s`.
s.replace(/\d+/g,
  // Convert the number into an integer.
  // Add it to the sum.
  d => c += +d
)
| improve this answer | |
\$\endgroup\$
3
\$\begingroup\$

Windows PowerShell, 23 25 29 31

With output.

$x-replace'\D','+0'|iex

In fact, without output is exactly the same, you'd just pipe it somewhere else where it's needed.

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

J - 40 38 characters

Lazy version. Requires the string library.

+/".(,' ',.~a.-.'0123456789')charsub y
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Supports Unicode. Supports encoding, come to think of it! \$\endgroup\$ – MPelletier Feb 8 '11 at 22:03
2
\$\begingroup\$

Java

out of the contest ;)

public static long sum(String s) {
    long sum = 0;
    String p = "";
    char[] ch = s.toCharArray();
    for (int i = 0; i < ch.length; i++) {
        boolean c = false;
        if (Character.isDigit(ch[i])) {
            if (i + 1 < ch.length) {
                if (Character.isDigit(ch[i + 1])) {
                    p += ch[i];
                    c = true;
                }
            }
            if (!c) {
                p += ch[i];
                sum += Integer.valueOf(p);
                p = "";
                c = false;
            }
        }
    }
    return sum;
}
| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

Labyrinth, 29 21 bytes

(Disclaimer: Labyrinth is newer than this challenge.)

Also, Labyrinth doesn't have variables, so I went with a normal input/output program.

)_"+`
( "?"
";;,;;(!@

This was fairly simple because of the way Labyrinth's input commands work. ? tries to read a signed integer from STDIN and stops at the first non-digit. If it can't read an integer (because the next character is a - not followed by a digit, or any other non-digit, or we've reached EOF), it will return 0 instead. , on the other hand reads any subsequent byte and pushes the byte value. If this one is called at EOF it will return -1 instead.

So here's some pseudocode for the solution:

running total = 0
while(true)
  while(true)
    try reading a non-zero integer N with ?
    if(N < 0)
      running total -= N
    else if(N > 0)
      running total += N
    else
      break
  // We've either read a zero or hit a something that isn't a number
  try reading a character with ,
  if(that returned -1)
    break
print running total

Dealing with negative numbers correctly complicates this solution quite a lot. If it weren't for those, I'd have this 8-byte solution:

?+
;,;!@
| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

gs2, 4 bytes

W#Θd

Encoded in CP437; the third byte is E9.

W reads all numbers /-?\d+/ from a string, maps absolute value, d sums.

(gs2, too, is newer than this challenge, but its read-nums command is a total coincidence.)

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

Perl 5 -MList::Util=sum -pF/\D+/, 8 bytes

$_=sum@F

Try it online!

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

05AB1E (legacy), 5 bytes

.œþOà

Try it online or verify a few more test cases. (Times out for the larger test cases due to the builtin.)

Explanation:

.œ     # Get all partitions of the (implicit) input-string,
       # which are all possible ways of dividing the input-strings into substrings
  þ    # Only leave the items consisting of digits for each partition
       # (in the new version of 05AB1E, an explicit `€` is required)
   O   # Sum each inner list
    à  # Pop and push its maximum
       # (after which the result is output implicitly)
| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

JavaScript [31 bytes]

eval(s.match(/\d+/g).join('+'))
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ I count it 31 bytes? \$\endgroup\$ – l4m2 Jun 16 at 7:30
  • \$\begingroup\$ @l4m2 Yes, you are right, fixed. \$\endgroup\$ – VisioN Jun 16 at 11:20
1
\$\begingroup\$

PHP - 37

Without printing;

<?array_sum(@split("[^0-9]+",`cat`));

With printing (38):

<?=array_sum(@split("[^0-9]+",`cat`));
| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Perl, 16 chars

s/\d+/$r+=$&/ge;

Takes input in $_, output goes on $r. Last semicolon is superfluous, but it will probably be needed when the program does more things. Add say$r for output.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Oops, didn't see your exact same answer when I posted. Though I counted one character more even without the semicolon. \$\endgroup\$ – J B Feb 8 '11 at 21:30
  • \$\begingroup\$ @J B: I can't count! :P. Actually, I made the mistake of echo'ing a double quoted string to wc -c. \$\endgroup\$ – ninjalj Feb 8 '11 at 21:33
1
\$\begingroup\$

R, 30

sum(scan(t=gsub("\\D"," ",x)))

Here, x is the name of the variable.

Example:

> x  <- "e7rde f ,fe 43 jfj 54f4sD"
> sum(scan(t=gsub("\\D"," ",x)))
Read 4 items
[1] 108
| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

J - 23 char

Not a winner, but we get to see a fairly rare primitive in action.

+/".(,_=_"."0 y)}y,:' '

Explained:

  • _"."0 y - For each character in the input string y, try to read it in as a number. If you can't, use the default value _ (infinity) instead.

  • ,_= - Check each result for equality to _, and then run the final array of 0s and 1s into a vector. ("."0 always adds one too many dimensions to the result, so we correct for that here.)

  • y,:' ' - Add a row of spaces beneath the input string.

  • } - Used as it is here, } is called Item Amend, and it uses the list of 0s and 1s on the left as indices to select the row to draw from in the right argument. So what happens is, for each column in the right side, we take the original character if it could be read in as a number, and otherwise we take the space beneath it. Hence, we cover up any non-numeric characters with spaces.

  • +/". - Now convert this entire string into an list of numbers, and sum them.

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Brachylog, 9 bytes

{~cịˢ+}ᶠ⌉

Try it online!

It's... inefficient... so I've neglected to test the longer cases. It appears to work, though. Takes input through the input variable, which just so happens to be the usual form of input anyhow.

        ⌉    The output is the largest
{     }ᶠ     possible
     +       sum of
   ịˢ        integers the string representations of which are some elements of
 ~c          a partition of the input.

ensures that numbers are maximally matched as well as never negative, since splitting a number up, invalidating a number, or turning it negative will never result in a larger sum.

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

C 96

Thanks to @ceilingcat for some very nice pieces of golfing - now even shorter

t;main(i,v)int**v;{for(char*q,*s=v[1];*s;q>s?t+=abs(i),s=q:s++)i=strtol(s,&q,0);printf("%d",t);}

Try it online!

An earlier 85 byte version that is cheating a bit by hardcoding the string inside the program:

t=0;main(i){for(char*q,*s;i=strtol(s,&q,0),*s;q>s?t+=abs(i),s=q:s++);printf("%d",t);}

To actually use the 85 byte program you need to assign the variable like so:

t=0;main(i){for(char*q,*s="text";i=strtol(s,&q,0),*s;q>s?t+=abs(i),s=q:s++);printf("%d",t);}
| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

K4, 21 bytes

Solution:

+/"J"$" "\:.Q.n .Q.n?

Explanation:

Not particularly pleased with this, but it works...

+/"J"$" "\:.Q.n .Q.n? / the solution
                .Q.n? / lookup right in the string "0123456789"
           .Q.n       / index back into "0123456789"
      " "\:           / split string on whitespace
  "J"$                / cast to numeric
+/                    / sum up

Extra:

  • +/0{.[{(0,. y)+1 10*x};(x;y);(+/x),0]}/ for 39 bytes which is 'nicer' but almost twice the count...
| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Bash + GNU utilities, 24 bytes

dc<<<`tr -c 0-9 +<<<$s`p

Try it online!

Input in the variable s, output on stdout.

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Funky, 38 bytes

s=>{n=0forv ins::gmatch"\\d+"n-=-v[0]}

Explained

s=>{                                 } $# A function taking argument "s"
    n=0                                $# Preset n to 0
              s::gmatch"\\d+"          $# An iterator function which finds all groups of digits in a string
       forv in                         $# Iterate through, setting v to the match
                             n-=       $# Subtract n by
                                -v[0]  $# The negative of the whole match, which implicitely numberfies it.
                                       $# Conveniently implcit return from last statement in the function.

Try it online!

| improve this answer | |
\$\endgroup\$
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

| improve this answer | |
\$\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.

| improve this 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.

| improve this answer | |
\$\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.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Equal length, but interesting: +/2⊃⍞⎕VFI⍨⎕AV~⎕D \$\endgroup\$ – Adám Jul 20 '16 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

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Perl - 24 chars

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

Input is in $_

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Actually, 14 bytes (non-competing)

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

Try it online!

This submission is non-competing because Actually is a fair bit newer than this challenge.

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
| improve this answer | |
\$\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!

| improve this answer | |
\$\endgroup\$

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.