| Bytes | Lang | Time | Link |
|---|---|---|---|
| 040 | APLNARS | 250727T101329Z | Rosario |
| 213 | Desmos | 250726T231815Z | ErikDaPa |
| 008 | Vyxal 3 | 250724T172431Z | Themooni |
| 061 | Swift 6 | 240908T003452Z | macOSist |
| 008 | Uiua | 231020T005208Z | chunes |
| 019 | AWK | 250228T170231Z | xrs |
| 053 | Python 3.8 | 240906T133825Z | devoid |
| 034 | JavaScript Node.js | 240906T170530Z | l4m2 |
| 029 | Arturo | 240822T135640Z | chunes |
| 021 | J + regex | 221129T011127Z | south |
| 031 | jq R | 221128T153819Z | pmf |
| 020 | q | 221128T045909Z | cillianr |
| 019 | Juby | 221031T173447Z | Jordan |
| 003 | Japt v2.0a0 x | 221102T151519Z | Shaggy |
| 003 | 05AB1E legacy | 221102T160237Z | Kevin Cr |
| 006 | Pip | 221102T154524Z | DLosc |
| 036 | Factor + math.unicode sorting.human | 221031T214015Z | chunes |
| 023 | Ruby n | 221031T173046Z | Jordan |
| 004 | Vyxal s | 221031T172326Z | pacman25 |
| 071 | Python 2 | 160219T141745Z | LeeNever |
| 105 | Oracle SQL 11.2 | 160219T081414Z | Jeto |
| 043 | R | 160218T125818Z | slamball |
| 070 | Python 2 | 160217T094336Z | TanMath |
| 029 | Bash + GNU utilities | 160212T230828Z | Digital |
| 051 | Lua | 160215T154913Z | Katenkyo |
| 025 | POSIX sh + tr + dc | 160215T153450Z | Toby Spe |
| 045 | Ruby | 160215T125047Z | ryantk |
| 050 | R | 160214T220346Z | mnel |
| 008 | MATL | 160212T154755Z | Luis Men |
| 018 | Perl 6 | 160212T182307Z | Brad Gil |
| 035 | JavaScript ES6 | 160212T182441Z | ETHprodu |
| 035 | Clojure/ClojureScript | 160212T193808Z | MattPutn |
| 051 | Mathematica | 160212T155858Z | A Simmon |
| 106 | TIBasic | 160212T182723Z | Timtech |
| 035 | Julia | 160212T182426Z | Alex A. |
| 070 | Java | 160212T182235Z | hyperneu |
| 008 | Labyrinth | 160212T164127Z | Martin E |
| 013 | Seriously | 160212T171910Z | user4594 |
| 004 | Gol><> | 160212T171216Z | randomra |
| 002 | Japt | 160212T170247Z | ETHprodu |
| 039 | Javascript | 160212T154758Z | removed |
| 013 | CJam | 160212T152709Z | GamrCorp |
| 064 | PHP | 160212T163037Z | ʰᵈˑ |
| 002 | GS2 | 160212T162349Z | Dennis |
| nan | Perl | 160212T161855Z | Andreas |
| 006 | Jelly | 160212T155234Z | Dennis |
| 039 | Gema | 160212T161535Z | manatwor |
| 026 | PowerShell | 160212T152151Z | AdmBorkB |
| 2211 | Retina | 160212T153519Z | FryAmThe |
| 010 | Pyth | 160212T151610Z | Doorknob |
APL(NARS), 40 chars
{w←'0',⍵⋄+/⍎¨k/⍨{⍵⊆⎕D}¨k←w⊂⍨+\2≠/0,w∊⎕D}
I change the input in the way not find the zilde or void set in the middle of the computation. I find a way to use the partition char '⊂'.
Add to the array of input '0', call that array w, make a partition of w with elements that are or only numbers or only letters, in that partition eliminate elements are formed from letters (element =list of letter), for each in the array result apply command execute (⍎) sum the array result.
test:
q←{w←'0',⍵⋄+/⍎¨k/⍨{⍵⊆⎕D}¨k←w⊂⍨+\2≠/0,w∊⎕D}
q'a'
0
q'0'
0
q'5'
5
q'10'
10
q'a0A'
0
q'1a1'
2
q'11a1'
12
q'4aaaa65uiuou5i5iii'
79
For a speech on easily of read or understand, it seems better use one function that generate partitions not from one array of numbers, but from one binary array... This is my first try, the function p
p←{⍺⊂⍨1++\2≠/0,⍵}
p has as the array to build the partitions in ⍺, and one array of binary that say where are the borders of partition (where are the ones) in ⍵. It has this input output
p←{⍺⊂⍨1++\2≠/0,⍵}
12 13 14 p 0 0 0
┌1──────────┐
│┌3────────┐│
││ 12 13 14││
│└~────────┘2
└∊──────────┘
12 13 14 p 1 1 1
┌1──────────┐
│┌3────────┐│
││ 12 13 14││
│└~────────┘2
└∊──────────┘
12 13 15 p 0 1 1
┌2─────────────┐
│┌1──┐ ┌2─────┐│
││ 12│ │ 13 15││
│└~──┘ └~─────┘2
└∊─────────────┘
12 13 15 p 1 0 1
┌3────────────────┐
│┌1──┐ ┌1──┐ ┌1──┐│
││ 12│ │ 13│ │ 15││
│└~──┘ └~──┘ └~──┘2
└∊────────────────┘
12 13 15 p 1 1 0
┌2─────────────┐
│┌2─────┐ ┌1──┐│
││ 12 13│ │ 15││
│└~─────┘ └~──┘2
└∊─────────────┘
12 13 15 p 1 0 0
┌2─────────────┐
│┌1──┐ ┌2─────┐│
││ 12│ │ 13 15││
│└~──┘ └~─────┘2
└∊─────────────┘
12 13 14 p 0 1 1
┌2─────────────┐
│┌1──┐ ┌2─────┐│
││ 12│ │ 13 14││
│└~──┘ └~─────┘2
└∊─────────────┘
12 13 15 p 0 1 0
┌3────────────────┐
│┌1──┐ ┌1──┐ ┌1──┐│
││ 12│ │ 13│ │ 15││
│└~──┘ └~──┘ └~──┘2
└∊────────────────┘
12 13 15 p 0 0 1
┌2─────────────┐
│┌2─────┐ ┌1──┐│
││ 12 13│ │ 15││
│└~─────┘ └~──┘2
└∊─────────────┘
The function above exercise would be:
S←{w←'0',⍵⋄+/⍎¨k/⍨{⍵⊆⎕D}¨k←w p w∊⎕D}
S'4aaaa65uiuou5i5iii'
79
S'4a'
4
S'a'
0
Desmos, 213 bytes
m=0^{max(x-58,0)}
H=(x-48)m/m => keep only the #s
I=[1...H.count][H=H] => idxs of #s
k=I.count
A=join(0,\left\{k>1:∑_{n=1}^{[1...max(k-1,1)]}sign(I[2...]-I-1)[n],[]\right\}) => grouping mask
D=H[H=H][A=i] => at each item in the mask...
f(x)=[total(D10^{[D.count-1...0]})fori=[0...A.max]].total => group those #s by the mask and convert to dec, then finally sum
Vyxal 3, 8 bytes
"\d+"⊢E∑
"\d+"⊢E∑
∑ # sum
E # atoi
"\d+"⊢ # regex matches of /\d+/
💎
Created with the help of Luminespire.
<script type="vyxal3">
"\d+"⊢E∑
</script>
<script>
args=[["a1wAD5qw45REs5Fw4eRQR33wqe4WE"],["a"],["\"0\""],["\"5\""],["\"10\""],["A0a"],["1a1"],["11a1"],["4dasQWE65asAs5dAa5dWD"],["100a10"]]
</script>
<script src="https://themoonisacheese.github.io/snippeterpreter/snippet.js" type="module"/>
Swift 6, 63 61 bytes
{($0+"").split(separator:/[^\d]+/).map{Int($0)!}.reduce(0,+)}
AWK, 24 19 bytes
{a+=$1}END{print a}
{for(;i++<NF;)j+=$i}$0=j
There's a little trickery to cause AWK to only process numbers as fields.
JavaScript (Node.js), 34 bytes
s=>eval(s.replace(/\D|$/g,' +')+0)
Like ETHproductions's answer but extra push is possible
Not work as intended if leading 0 exist, as it reads as octal
J + regex, 21 bytes
1#.0,'\d+'".&>@rxall]
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 ].
Japt v2.0a0 -x, 3 bytes
'Cause ETH's solution is a bit cheaty!
q\D
q\D :Implicit input of string
q :Split on
\D : RegEx /\D/ (or /[^0-9]/)
:Implicit output of sum of resulting array
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)
Pip, 6 bytes
$+a@XI
Explanation
XI Built-in regex matching integers
@ Find all matches in
a Command-line input
$+ Fold on addition
Factor + math.unicode sorting.human, 36 bytes
[ find-numbers [ real? ] filter Σ ]
! "11a1"
find-numbers ! { "" 11 "a" 1 }
[ real? ] filter ! { 11 1 }
Σ ! 12
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
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.
R, 46 43 bytes
sum(strtoi(strsplit(scan(,''),'\\D')[[1]]))
Explanation
scan(,'') # Take the input string
strsplit( ,'\\D') # Returns list of all numeric parts of the string
[[1]] # Unlists to character vector
strtoi( ) # Converts to numeric vector
sum( ) # Sums the numbers
Sample run
> sum(strtoi(strsplit(scan(,''),'\\D')[[1]]))
1: a1wAD5qw45REs5Fw4eRQR33wqe4WE
2:
Read 1 item
[1] 97
Edit: Replaced [^0-9] with \\D.
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!
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
Lua, 51 Bytes
Pretty short for once! Even shorter than Java! The input must be a command-line argument for it to work.
a=0 arg[1]:gsub("%d+",function(c)a=a+c end)print(a)
Ungolfed
a=0 -- Initialize the sum at 0
arg[1]:gsub("%d+", -- capture each group of digits in the string
function(c) -- and apply an anonymous function to each of them
a=a+c -- sums a with the latest group captured
end)
print(a) -- output a
POSIX sh + tr + dc, 27 25 bytes
dc -e "0d`tr -sc 0-9 +`p"
Converts any run of non-digits (including the terminating newline) to + operator, pushes two zeros onto the stack (in case input begins with non-digit), adds them all and prints the result. There may be an extra zero remaining at bottom of stack, but we don't care about that.
Ruby 45 bytes
$*[0].split(/[a-z]/i).map(&:to_i).inject 0,:+
(First attempt at work, will revisit this)
R, 50 bytes
Requires having gsubfn installed
sum(gsubfn::strapply(scan(,''),'\\d+',strtoi)[[1]])
Uses strtoi to coerce to numeric
MATL, 8 bytes
1Y4XXXUs
1Y4 % predefined literal: '\d+'
XX % implicit input. Match regular expression. Returns a cell array of strings
% representing numbers
XU % convert each string to a double. Returns a numeric array
s % sum of numeric array
Perl 6, 18 bytes
{[+] .comb(/\d+/)}
{[+] .split(/\D/)}
Usage:
my &code = {[+] .comb(/\d+/)}
say code 'a'; # 0
say code '0'; # 0
say code '5'; # 5
say code '10'; # 10
say code 'a0A'; # 0
say code '1a1'; # 2
say code '11a1'; # 12
say code '4dasQWE65asAs5dAa5dWD'; # 79
say code 'a1wAD5qw45REs5Fw4eRQR33wqe4WE'; # 97
JavaScript ES6, 35 bytes
s=>eval(s.replace(/\D+/g,'+')+'.0')
How it works
First, we replace each string of non-digits with "+". There are basically four different ways that this could end up:
1. 1b23c456 => 1+23+456
2. a1b23c456 => +1+23+456
3. 1b23c456d => 1+23+456+
4. a1b23c456d => +1+23+456+
Cases 1 and 2 are taken care of already. But we somehow need to fix the last + so that it doesn't cause an error. We could remove it with .replace(/\+$,""), but that's too expensive. We could append a 0 to the end, but that would affect the last number if the string does not end with a +. A compromise is to append .0, which is both a valid number on its own and doesn't affect the value of other integers.
Here's a few other values that would work as well:
.0
-0
+0
-""
-[]
0/10
0e-1
.1-.1
Alternate version, also 35 bytes
s=>s.replace(/\d+/g,d=>t+=+d,t=0)|t
Another alternate version, 36 bytes
s=>s.split(/\D/).map(d=>t+=+d,t=0)|t
Clojure/ClojureScript, 35 bytes
#(apply +(map int(re-seq #"\d+"%)))
Mathematica, 51 bytes
Total@ToExpression@StringCases[#,DigitCharacter..]&
Catching the wrong end of the verbose Mathematica builtins. 1 Byte off with the help of @DavidC
TI-Basic, 106 bytes
Works on TI-83/84 calculators!
Input Str1
"{0,→Str2
Str1+"N0→Str1
For(I,1,length(Ans
sub(Str1,I,1
If inString("0123456789",Ans
Then
Str2+Ans→Str2
Else
If ","≠sub(Str2,length(Str2),1
Str2+","→Str2
End
End
sum(expr(Ans
Julia, 35 bytes
s->sum(parse,matchall(r"\d+","0"s))
This is an anonymous function that accepts a string and returns an integer. To call it, assign it to a variable.
We use matchall to get an array consisting of matches of the regular expression \d+, which are just the integers in the string. We have to tack on a 0 to the front of the string, otherwise for cases like "a", we'd be summing over an empty array, which causes an error. We then apply parse to each string match, which converts to integers, and take the sum.
Java, 70 bytes
s->{int n=0;for(String i:s.split("\\D+"))n+=Long.valueOf(i);return n;}
Labyrinth, 8 bytes
Take that, Pyth...
?+
;,;!@
Explanation
The usual primer (stolen from Sp3000):
- Labyrinth is 2D and stack-based. Stacks have an infinite number of zeroes on the bottom.
- When the instruction pointer reaches a junction, it checks the top of the stack to determine where to turn next. Negative is left, zero is forward and positive is right.
What comes in really handy here is that Labyrinth has two different input commands, , and ?. The former reads a single byte from STDIN, or -1 at EOF. The latter reads an integer from STDIN. It does so skipping everything that isn't a number and then reads the first decimal number it finds. This one returns 0 at EOF, so we can't use it to check for EOF reliably here.
The main loop of the program is this compact bit:
?+
;,
With ? we read an integer (ignoring all letters), with + we add it to the running total (which starts out as one of the implicit zeroes at the stack bottom). Then we read another character with , to check for EOF. As long as we're not at EOF, the read character will be a letter which has a positive character code, so the IP turns right (from its point of view; i.e. west). ; discards the character because we don't need it and then we enter the loop again.
Once we're at EOF, , pushes a -1 so the IP turns left (east) instead. ; again discards that -1, ! prints the running total as an integer and @ terminates the program.
Seriously, 13 bytes
,ú;û+@s`≈`MΣl
Explanation:
,ú;û+@s`≈`MΣl
, push input
ú;û+ push "abc...zABC...Z" (uppercase and lowercase English letters)
@s split on letters
`≈`M convert to ints
Σ sum
l length (does nothing to an integer, pushes 0 if an empty list is left, in the case where the string is all letters)
Japt, 2 bytes
Nx
How it works
N // Implicit: N = (parse input for numbers, "strings", and [arrays])
x // Sum. Implicit output.
Javascript, 32 39 bytes
s=>eval((s.match(/\d+/g)||[0]).join`+`)
f=
s=>eval((s.match(/\d+/g)||[0]).join`+`)
F=s=>document.body.innerHTML+='<pre>f(\''+s+'\') -> '+f(s)+'\n</pre>'
F('a')
F('5')
F('10')
F('1a1')
F('11a1')
F('4dasQWE65asAs5dAa5dWD')
F('a1wAD5qw45REs5Fw4eRQR33wqe4WE')
CJam, 13 bytes
Fixed to work with input without numbers thanks to Dennis! Also saved a byte by replacing the letters array with an array of ASCII above code point 64. And then another byte saved by Dennis!
q_A,s-Ser~]1b
Simple transliteration from letters to spaces, then eval and sum. Try it online.
PHP, 64 bytes
<?php preg_match_all("/\d+/",$argv[1],$a);echo array_sum($a[0]);
Run it as
php -f filterOutAndAddUp.php <test_case>
GS2, 2 bytes
Wd
How it works
W Read all numbers.
For input x, this executes map(int, re.findall(r'-?\d+', x)) internally.
d Compute their sum.
Perl, 21 + 1 = 22 bytes
$_=eval join"+",/\d+/g
Requires the -p flag:
$ perl -pe'$_=eval join"+",/\d+/g' <<< 'a1wAD5qw45REs5Fw4eRQR33wqe4WE'
97
Jelly, 6 bytes
&-ṣ-ḌS
How it works
&-ṣ-ḌS Main link. Input: L (string)
&- Take the bitwise AND of L's characters and -1.
This attempts to cast to int, so '0' & -1 -> 0 & -1 -> 0.
On failure, it returns the integer argument (if any), so 'a' & -1 -> -1.
ṣ- Split the resulting list at occurrences of -1.
Ḍ Convert each chunk from decimal to integer. In particular, [] -> 0.
S Compute the sum of the results.
Gema, 39 characters
<D>=@set{s;@add{${s;};$0}}
?=
\Z=${s;0}
Sample run:
bash-4.3$ gema '<D>=@set{s;@add{${s;};$0}};?=;\Z=${s;0}' <<< 'a1wAD5qw45REs5Fw4eRQR33wqe4WE'
PowerShell, 28 26 bytes
$args-replace"\D",'+0'|iex
Takes input $args then does a regex -replace to swap the letters with +0, then pipes that to iex (short for Invoke-Expression and similar to eval).
PS C:\Tools\Scripts\golfing> .\filter-out-and-add-up.ps1 'a1wAD5qw45REs5Fw4eRQR33wqe4WE'
97
Alternatively
If you're OK with some extraneous output, you can do the following, also at 28 26 bytes:
$args-split"\D"|measure -s
This will take the input string $args and -split it into an array-of-strings on the non-numbers (removing them in the process). For example, 1a2b33 would turn into ['1','2','33']. We pipe that to Measure-Object with the -Sum parameter. Output would be like the below:
PS C:\Tools\Scripts\golfing> .\filter-out-and-add-up.ps1 'a1wAD5qw45REs5Fw4eRQR33wqe4WE'
Count : 21
Average :
Sum : 97
Maximum :
Minimum :
Property :
Edit -- durr, don't need the [ ] in the regex since I'm no longer specifying a list of possible matches ...
Retina, 22 11
\d+
$0$*1
1
11 bytes (!) saved thanks to Martin!
Basically just decimal to unary then count the 1s.
Pyth, 12 11 10 bytes
ssM:z"\D"3
z autoinitialized to input()
: "\D"3 split on non-digits
sM convert all elements of resulting array to ints
s sum
Fortunately, s (convert to int) returns 0 when applied to the empty string, so I don't have to worry about the fact that split("a1b", "\D+") returns ["", "1", ""]. Similarly, split("a", "\D+") returns ["", ""].
This even allows me to split on every non-digit individually, since 1 + 0 + 0 + 0 + 0 + 2 is the same thing as 1 + 2.
Thanks to Thomas Kwa for a byte!