| Bytes | Lang | Time | Link |
|---|---|---|---|
| 095 | sed 4.2.2 | 250822T105412Z | Toby Spe |
| 029 | Dyalog APL | 250821T231421Z | Aaron |
| nan | 240622T102352Z | RARE Kpo | |
| nan | Pxem | 240621T141538Z | 鳴神裁四点一号 |
| 010 | Nekomata | 240621T030757Z | alephalp |
| 094 | PowerShell | 230417T202134Z | James Fl |
| 070 | C gcc | 221221T154550Z | l4m2 |
| 071 | JavaScript Node.js | 230414T151643Z | Fhuvi |
| 055 | Arturo | 230412T195107Z | chunes |
| 025 | K ngn/k | 230412T202454Z | coltim |
| 077 | Excel VBA | 221221T224519Z | Engineer |
| 086 | PHP | 221226T030654Z | James Ri |
| 119 | Python 3 | 221228T180626Z | Joao-3 |
| 028 | Vim | 221222T155436Z | D. Ben K |
| 007 | 05AB1E | 221221T135731Z | The Thon |
| 156 | Java 19 | 221227T034037Z | Jeremy |
| 076 | Haskell | 221226T041339Z | Roman Cz |
| 068 | Lua | 221223T094746Z | fartmast |
| 018 | ><> | 221223T032459Z | Jo King |
| 088 | Java | 221222T195840Z | Unmitiga |
| 011 | Pyth | 221222T195250Z | CursorCo |
| 011 | Brachylog | 221222T183006Z | DLosc |
| 057 | Bash extglob | 221222T165619Z | roblogic |
| 086 | SNOBOL4 CSNOBOL4 | 221221T140116Z | Giuseppe |
| 024 | Zsh extendedglob | 221222T123913Z | roblogic |
| 007 | Japt | 221221T123841Z | Shaggy |
| 054 | Mathematica 13.1 | 221222T104244Z | Chris De |
| 612 | Nibbles | 221222T074616Z | Dominic |
| 007 | Pip | 221221T123139Z | math sca |
| 057 | R | 221221T134406Z | pajonk |
| 158 | Bash | 221222T040429Z | cocomac- |
| 016 | J | 221222T031301Z | Conor O& |
| 057 | Python | 221221T222816Z | jezza_99 |
| 042 | Factor | 221222T000744Z | chunes |
| 016 | Perl 5 p | 221221T235853Z | Sisyphus |
| 008 | Husk | 221221T214459Z | Dominic |
| 016 | Ruby | 221221T194134Z | Jordan |
| 008 | Jelly | 221221T182103Z | Jonathan |
| 018 | Raku | 221221T173233Z | Sean |
| 063 | Dart 2.18.4 | 221221T164807Z | Alex |
| 027 | Julia 1.0 | 221221T162529Z | MarcMush |
| 020 | Charcoal | 221221T155520Z | Neil |
| 028 | ><> | 221221T152144Z | Emigna |
| 031 | JavaScript | 221221T124041Z | Shaggy |
| 090 | Python 3 | 221221T122507Z | U13-Forw |
| 055 | ><> | 221221T125301Z | mousetai |
| 008 | Retina 0.8.2 | 221221T123837Z | Luis Men |
| 006 | Retina 0.8.2 | 221221T123955Z | Neil |
| 005 | QuadR | 221221T123415Z | Adá |
| 007 | Vyxal s | 221221T121509Z | lyxal |
sed 4.2.2, 95 bytes
y/0123456789/a!@#$%^&*(/
:
s/a/<<123456789a01>/
s/(.)<.*\1(a?.).*>/\2/
t
y/!@#$%^&*(/123456789/
How it works
#!/usr/bin/sed -rf
# save existing digits
y/0123456789/a!@#$%^&*(/
# unary to decimal
:
s/a/<<123456789a01>/
s/(.)<.*\1(a?.).*>/\2/
t
# restore digits
y/!@#$%^&*(/123456789/
Dyalog APL, 29 chars
{∊{0∊⍎⍵:⍕≢⍵⋄⍵}¨⍵⊂⍨1,2≠/'0'=⍵}
'0'=⍵ # Find the character 0
2≠/ # Compare every two elements for inquality.
# This demarcates where we turn from not-0's to 0's
1, # Shift it over one and start with a 1
⍵⊂⍨ # Partition the input with that new mask.
# This is then chunks of 0's and chunks of not-0's
{ }¨ # For each of those chunks
0∊⍎⍵ # if I execute that string and its 0
:⍕≢⍵ # then return a string of its length
⋄⍵ # else just return it unmodified
∊ # Enlist to join it all as one string again
💎
Created with the help of Luminespire.
awk
(gawk-specific)
gawk 'ORS=length(RT)' RS=0+
1234523623441
123452362344
12
1123456789
1234567891
123456789
11121324151
Pxem, 11 + 52 = 63 bytes
.c.w.n\0.a.s
Filename
\1.rL.w.i.c0.x.t.e.c.-.m.oXX\1.a.c!.x.s\1.+XX\1.a\1.+.a.e
What I have golfed
- EOF case out of the loop
- How subroutine works
Pxem, 13 + 58 = 71 bytes
.s.c.w.n\0.a.s
Filename
\1.rL.w.i.c\1.+\1.y.e.d.a.c0.x.e.o.s\1.rXX1.a.c1.z.s\1.+XX0.a.a
How I golfed
- Push zero by
\1.rinstead ofaa.%. - Put number of zeros at bottom then digit or EOF above.
Pxem, 16 + 61 = 77 bytes
.c.w.naa.%.a.s.s
Filename
aa.%.t.w.i.c\1.y.m.e.d.a.c0.x.m.e.m.-.t.oXX1.a1.z\1.m.+.tXX.a.a
Explained
Filename.
aa.%.t Store 0 to register because it is initialized with null.
.w Loop
.i Push getchar
.c\1.y If EOF
.m.e.d Push register, call subroutine, exit
.a
.c0.x If top is greater than 48
.m.e Push register, call subroutine
.m.-.t Reset register to 0
.o putchar pop
XX1.a Push 49
1.z If pop is NOT 49
\1.m.+.t Increment register
XX.a
.a End loop
Content as subroutine. Different space with filename, when return stack is concatenated to caller's stack.
.c.w If top is not zero
.n print popped value as numeric integer
aa.%.a
.s.s Clean up
Nekomata, 10 bytes
ĉᵐ{ᵉĜ#Mĝ}j
Nekomata is still bad at string challenges.
ĉᵐ{ᵉĜ#Mĝ}j
ĉ Split into groups of identical characters
ᵐ{ } For each group:
ᵉ M Max of the following two:
Ĝ Convert string to number
# Length
ĝ Convert number to string
j Join
Using the fact that the length of a positive integer is always less than or equal to the integer itself.
PowerShell, 94 bytes
$r='';$_|sls '0+|[1-9]+'-a|% m*|%{$v=$_.value;('$r+=$v','$r+=$_.length')[$v-match"0+"]|iex};$r
Ungolfed
$args|%{
# empty string to append to
$r=''
# '0+|[1-9]+' will match full string but split by zeroes and non-zeroes
$_ | sls '0+|[1-9]+' -a | % m*| %{
# value is the matched string
$v=$_.value
# true = 1; false = 0; shorter than an if statement
# iex will execute the command in quotes
('$r+=$v','$r+=$_.length')[$v-match"0+"] | iex
}
# prints the results
$r
}
C (gcc), 70 bytes
n;f(char*a){*a-48?n=!printf(n?"%d":"%c",n?:*a++),*a&&f(a):f(a+!!++n);}
C (gcc), 62 bytes by c--
n;f(int*a){*a-48?n=!printf(n?"%d":a++,n),*a&&f(a):f(a+!!++n);}
JavaScript (Node.js), 71 bytes
Solution without using regexp
s=>s.split(0).map(c=>c?(a?a:"")+(a=1,c):(++a,""),a=0).join``+(--a?a:"")
Arturo, 55 bytes
$=>[chunk&=>[&]|map'g->(`0`=g\0)?->size g->join g|join]
$=>[ ; a function, assign input to &
chunk&=>[&] ; split input into blocks of equal, contiguous chars
| ; then
map'g-> ; map over each block and assign current elt to g
(`0`=g\0)? ; is the first element of g the character `0`?
->size g ; then map g to the length of g
->join g ; otherwise, map g to its joined string
| ; then
join ; join the block of strings and numbers to a string
] ; end function
K (ngn/k), 25 bytes
{,/$(.'x)|#'x:(&~=':x)_x}
x:(&~=':x)_xsplit the input where it changes, and reassign tox(.'x)|#'xtake the maximum of the chunk of input (converted to its corresponding integer) and its count,/$convert to strings and flatten (and implicitly return)
Excel VBA, 100 77 bytes
Saved 23 bytes thanks to a translation by Taylor Raine
For i=2^15-1To 1Step-1:[B1]=i:[A1]=[Substitute(A1,Rept(0,B1),B1)]:Next:[B1]="
Input is in the cell A1 of the active sheet. Output is in the same cell. The code is run from the immediate window in VBA. The only clever bit is that Excel only allows 32,767 characters in a cell and counting down from there is less bytes than counting down from the length of the input.
PHP, 110 96 94 86 bytes
try{for(;;)if(!~~$c=fgetc(STDIN))$x++;else{echo$x?:"",$c;$x=0;}}finally{echo$x?$x:"";}
Takes input via STDIN, outputs via STDOUT.
Python 3, 120 119 bytes
def f(s):
c=0;r=''
for d in s:
if d=='0':c+=1
else:r+=f'{c if c!=0 else""}'+d;c=0
return r+f'{c if c!=0 else""}'
Vim, 28 bytes
:s/0\+/\=len(submatch(0))/g
Enter
We cannot use 0* because it matches the empty string between digits.
05AB1E, 7 bytes
0ÃηRāR:
- -6 thanks to Kevin Cruijssen
Explanation
0ÃηRāR: # Implicit input
0Ã # List intersection with [0]
ηR # Reversed prefixes
āR # Reversed length range
: # Infinite replacement
Previous 13 byte answer
.γ}εD0.åig}}J # Implicit input
.γ} # Group by consecutive items
ε } # For each group:
D # Duplicate the group
0.åi } # If 0 is in the group:
g # Push its length
J # Join everything into a single string
Java 19, 156 161 162 bytes
interface A{static void main(String[] a){var i=0;var s="";for(var c:a[0].toCharArray())if(c==48)i++;else{s+=i>0?i+""+c:c;i=0;}System.out.print(i>0?s+i:s);}}
Without Java's golfing tax, 113 bytes
var i=0;var s="";for(var c:a[0].toCharArray())if(c==48)i++;else{s+=i>0?i+""+c:c;i=0;}System.out.print(i>0?s+i:s);
Edit: replaced '0' with 48 as suggested in the comments. Thanks!
Edit: String concatentation is shorter than doing var o=System.out;
Haskell, 76 bytes
import Data.List
r s=concat[case t!!0 of '0'->show$length t;_->t|t<-group s]
Lua 68 bytes
s=io.read("*a")for i=#s,1,-1 do s=s:gsub(("0"):rep(i),i)end print(s)
Java, 88 bytes
s->java.util.regex.Pattern.compile("0+").matcher(s).replaceAll(r->r.group().length()+"")
Pyth, 11 bytes
sm`|vedhdr8
Explanation
sm`|vedhdr8Q # implicitly add Q to the end
# implicitly assign Q = eval(input())
r8Q # length encode Q
s # concatenate together
m # map over lambda d
` # convert to string
| # short circuiting or
ved # evaluate the second element of d
hd # or take the first element of d
Brachylog, 11 bytes
ḅ{ị0&lṫ|}ᵐc
Explanation
I was hoping for some automatic number -> string conversion, but no dice.
ḅ{ị0&lṫ|}ᵐc
ḅ Break the input string into blocks of identical characters
{ }ᵐ Map this predicate to each block:
ị Convert to integer
0 Assert that the result is zero
&l If so, get the length of the block
ṫ and convert to string
| If that failed (because the number wasn't zero), return the block unchanged
c Concatenate the results together into a single string
SNOBOL4 (CSNOBOL4), 86 bytes
I =INPUT
R I ARB . L SPAN(0) . X REM . I :F(O)
O =O L SIZE(X) :(R)
O OUTPUT =O I
END
I =INPUT ;* read input
R I ;* Match in I:
ARB . L ;* ARBitrary characters (save as Left part of string) up to
SPAN(0) . X ;* 1 or more 0s (save as X)
REM . I ;* REMaining characters (save as I)
:F(O) ;* On no match, goto O
O =O L SIZE(X) :(R) ;* Append L and # of 0s to O and goto R
O OUTPUT =O I ;* Output O and any remaining characters of I that didn't match
END
Japt, 7 bytes
r+iT Èl
r+iT Èl :Implicit input of string
r :Replace
+i : "+" prepended with
T : 0, giving the RegEx /0+/g
È : Pass each match through a function
l : Length
Mathematica (13.1) 54 bytes
StringReplace[string,x:"0"..:>ToString@StringLength@x]
Nibbles, 7 6 bytes (12 nibbles)
+.`=$$?`r$@,
`=$ # chunk the input by
$ # itself,
. # now map over each chunk:
? # if
`r$ # its value as an integer is nonzero
@ # return itself
# otherwise
, # return its length
+ # finally, join it all together
Pip, 7 bytes
-1 byte by jezza_99 / DLosc
aR+X0#_
Wow pip is really good at regex
aR+X0#_
aR+X0 Replace all occurrences of (regexified zero with a +)
#_ with it's length
R, 64 58 57 bytes
Edit: -1 byte thanks to @Dominic van Essen.
\(x){regmatches(x,t)=Map(attr,t<-gregexpr("0+",x),"m")
x}
R has some weird string manipulation functions...
Bash - 181 158 chars
Edit 1: I used this trick for the for loop, remove whitespace, condence an if statement
n=0;for ((i=0;i<${#1};i++));{ c="${1:$i:1}";if [ $c = '0' ]; then ((n++)); else [ $n -ne 0 ] && { echo -n $n;n=0;};echo -n $c;fi;};[ $n -ne 0 ] && echo -n $n
Explanation
# a counter of how far into a sequence of zeros we are
# this is zero if we're not in a 'zero-sequence'
n=0
# iterate over all the characters
# $1 is the first function argument ($0 would be the script name)
for (( i=0; i<${#1}; i++ )); do
# get the current char
c="${1:$i:1}"
# if it is a zero, then increment the counter
if [ $c = '0' ]; then
((n++));
# if it is NOT a zero
else
# if a zero sequence is over (given that the counter is
# not equal to zero and the current char isn't zero)
if [ $n -ne 0 ]; then
# print the number of zeros in the sequence,
# and reset the counter to zero
echo -n $n;
n=0;
fi;
echo -n $c;
fi
done
# check if there was a zero sequence terminating the string,
# as we wouldn't otherwise check as there wouldn't be a non-zero
# char initiating the check
if [ $n -ne 0 ]; then echo -n $n; fi
I referred to this SO answer for how to iterate over a string in Bash and this AU answer for a short way to increment a variable
J, 16 bytes
'0+'":@#rxapply]
Fairly simple regex solution:
'0+' ":@#rxapply ]
'0+' rxapply ] apply to all relevant patterns in the input
# length
":@ casted to string
J, 47 bytes
' '-.~[:,(1,2~:/\'0'&=)([`(":@#)@.('0'&e.));.1]
Horrendously long non-regex solution. This is the only way I could really think of approaching this, since, afaik, J doesn't have a builtin for grouping according to a boolean matrix or similar.
Python, 62 57 bytes
lambda n:re.sub("0+",lambda s:str(len(s[0])),n)
import re
-5 bytes thanks to @xnor
Regex solution, port of @mathcat's Pip answer
Jelly, 8 bytes
Œgċ”0ȯƊ€
A full program that accepts a string of digit characters and prints the result.
How?
Œgċ”0ȯƊ€ - Main Link: list of characters, S
Œg - group runs
€ - for each group:
Ɗ - last three links as a monad - f(group):
”0 - literal '0' character
ċ - count occurrences
ȯ - logical OR (group)
- implicit, smashing print
Dart (2.18.4), 63 bytes
r(s)=>s.replaceAllMapped(RegExp('0+'),(m)=>'${m.end-m.start}');
String replaceZerosWithTheirConsecutiveCounts(String src) {
return src.replaceAllMapped(
RegExp('0+'),
(Match match) => '${match.end - match.start}',
);
}
Charcoal, 20 bytes
⭆⪪⁻⪫⪪SI⁰_0_×_²_∨№ι0ι
Try it online! Link is to verbose version of code. Explanation:
S Input string
⪪ Split on
⁰ Literal integer `0`
I Cast to string
⪫ Join with
_0_ Literal string `_0_`
⁻ Remove matching substrings
_ Literal string `_`
× Repeated
² Twice
⪪ Split on
_ Literal string `_`
⭆ Map over pieces and join
№ Count of
0 Literal string `0`
ι In current piece
∨ Logical Or
ι Current piece
String separators are only needed between consecutive strings so the code has been tweaked to avoid them to reduce the byte count. ⭆S∨Iι_0_ also works instead of ⪫⪪SI⁰_0_ for the same byte count.
Python 3, 91 90 bytes
lambda x:''.join(l>"0"and l or f'{len([*g])}'for l,g in groupby(x))
from itertools import*
-1 byte thanks to The Thonnu
I use the itertools module's groupby function to group consecutively.
Retina 0.8.2, 8 bytes
(0+)
$.1
How it works
Match runs of zeros (0+), capture each match in a group (( )), replace it with the length of the most recent capture ($.1).
Retina 0.8.2, 6 bytes
0+
$.&
Try it online! Link includes test cases. Explanation: Unary to decimal conversion.
Vyxal s, 7 bytes
ĠṠƛ0cßL
Explained
ĠṠƛ0cßL
Ġ # Group on consecutive items
Ṡ # join each into a single string
ƛ # To each group:
0c # if it contains 0:
ßL # push the length of the string
# the s flag joins into a single string
An alternate 8 byter that uses regex match replacement
‛0+$⁽Løṙ
Explained
‛0+$⁽Løṙ
‛0+ # The string "0+"
$ # pushed under the input
⁽L # a function object that returns the length of its argument
øṙ # replace instances of runs of 0 with their length
