Tell me more ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

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.

share|improve this question
Should we print the result too? (You mention no I/O). – Dogbert Feb 8 '11 at 19:43
@Dogbert - I didn't think about that. Sorry, yes. I will update the post. – Anto Feb 8 '11 at 19:45
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 ;) – Anto Feb 8 '11 at 19:49
1  
Anto: A task where a solution has no observable side-effects isn't very nice, though. – Joey Feb 8 '11 at 21:00

9 Answers

up vote 7 down vote accepted

Perl, 15

Input in $_, sum in $c:

s/\d+/$c+=$&/ge
share|improve this answer

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.

share|improve this answer
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. – sepp2k Feb 8 '11 at 21:37
@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! – Ventero Feb 8 '11 at 21:48
+1, great use of eval and * '+' – Dogbert Feb 8 '11 at 21:55

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.

share|improve this answer

Python (60)

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

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.

share|improve this answer

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;
}
share|improve this answer

J - 40 38 characters

Lazy version. Requires the string library.

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

PHP - 37

Without printing;

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

With printing (38):

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

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.

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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