26
\$\begingroup\$

Input

Your input is a list of single-digit numbers in any reasonable format, including a string of digits. The input will not be empty.

Output

Your output shall be the input list, but with each maximal run of nonzero digits being reversed.

Example

Consider the input

95883007414830
<---> <---->

where the nonzero runs have been marked with arrows. Reversing each of these runs, we obtain the output

38859003841470

Rules and scoring

You can write a full program or a function. The lowest byte count wins, and standard loopholes are disallowed.

This challenge is related, but much more complex, since it includes additional string processing.

Test cases

4 -> 4
00 -> 00
123 -> 321
0010 -> 0010
12000345 -> 21000543
18161604 -> 61618104
95883007414830 -> 38859003841470
010230456078912 -> 010320654021987
357509902003550 -> 575309902005530
2492882377675046 -> 5767732882942064
03026302053000357099 -> 03036202035000753099

Leaderboard

Here's a Stack Snippet to generate a leaderboard and list of winners by language. To make sure your answer shows up, start it with a header of the form

## Language, N bytes

You can keep old scores in the header by using the strikethrough tags: <s>57</s> will appear as (削除) 57 (削除ここまで).

/* Configuration */
var QUESTION_ID = 78510; // Obtain this from the url
// It will be like https://XYZ.stackexchange.com/questions/QUESTION_ID/... on any question page
var ANSWER_FILTER = "!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe";
var COMMENT_FILTER = "!)Q2B_A2kjfAiU78X(md6BoYk";
var OVERRIDE_USER = 32014; // This should be the user ID of the challenge author.
/* App */
var answers = [], answers_hash, answer_ids, answer_page = 1, more_answers = true, comment_page;
function answersUrl(index) {
 return "https://api.stackexchange.com/2.2/questions/" + QUESTION_ID + "/answers?page=" + index + "&pagesize=100&order=desc&sort=creation&site=codegolf&filter=" + ANSWER_FILTER;
}
function commentUrl(index, answers) {
 return "https://api.stackexchange.com/2.2/answers/" + answers.join(';') + "/comments?page=" + index + "&pagesize=100&order=desc&sort=creation&site=codegolf&filter=" + COMMENT_FILTER;
}
function getAnswers() {
 jQuery.ajax({
 url: answersUrl(answer_page++),
 method: "get",
 dataType: "jsonp",
 crossDomain: true,
 success: function (data) {
 answers.push.apply(answers, data.items);
 answers_hash = [];
 answer_ids = [];
 data.items.forEach(function(a) {
 a.comments = [];
 var id = +a.share_link.match(/\d+/);
 answer_ids.push(id);
 answers_hash[id] = a;
 });
 if (!data.has_more) more_answers = false;
 comment_page = 1;
 getComments();
 }
 });
}
function getComments() {
 jQuery.ajax({
 url: commentUrl(comment_page++, answer_ids),
 method: "get",
 dataType: "jsonp",
 crossDomain: true,
 success: function (data) {
 data.items.forEach(function(c) {
 if (c.owner.user_id === OVERRIDE_USER)
 answers_hash[c.post_id].comments.push(c);
 });
 if (data.has_more) getComments();
 else if (more_answers) getAnswers();
 else process();
 }
 }); 
}
getAnswers();
var SCORE_REG = /<h\d>\s*([^\n,]*[^\s,]),.*?(-?\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/;
var OVERRIDE_REG = /^Override\s*header:\s*/i;
function getAuthorName(a) {
 return a.owner.display_name;
}
function process() {
 var valid = [];
 
 answers.forEach(function(a) {
 var body = a.body;
 a.comments.forEach(function(c) {
 if(OVERRIDE_REG.test(c.body))
 body = '<h1>' + c.body.replace(OVERRIDE_REG, '') + '</h1>';
 });
 
 var match = body.match(SCORE_REG);
 if (match)
 valid.push({
 user: getAuthorName(a),
 size: +match[2],
 language: match[1],
 link: a.share_link,
 });
 
 });
 
 valid.sort(function (a, b) {
 var aB = a.size,
 bB = b.size;
 return aB - bB
 });
 var languages = {};
 var place = 1;
 var lastSize = null;
 var lastPlace = 1;
 valid.forEach(function (a) {
 if (a.size != lastSize)
 lastPlace = place;
 lastSize = a.size;
 ++place;
 
 var answer = jQuery("#answer-template").html();
 answer = answer.replace("{{PLACE}}", lastPlace + ".")
 .replace("{{NAME}}", a.user)
 .replace("{{LANGUAGE}}", a.language)
 .replace("{{SIZE}}", a.size)
 .replace("{{LINK}}", a.link);
 answer = jQuery(answer);
 jQuery("#answers").append(answer);
 var lang = a.language;
 if (! /<a/.test(lang)) lang = '<i>' + lang + '</i>';
 lang = jQuery(lang).text().toLowerCase();
 languages[lang] = languages[lang] || {lang: a.language, user: a.user, size: a.size, link: a.link, uniq: lang};
 });
 var langs = [];
 for (var lang in languages)
 if (languages.hasOwnProperty(lang))
 langs.push(languages[lang]);
 langs.sort(function (a, b) {
 if (a.uniq > b.uniq) return 1;
 if (a.uniq < b.uniq) return -1;
 return 0;
 });
 for (var i = 0; i < langs.length; ++i)
 {
 var language = jQuery("#language-template").html();
 var lang = langs[i];
 language = language.replace("{{LANGUAGE}}", lang.lang)
 .replace("{{NAME}}", lang.user)
 .replace("{{SIZE}}", lang.size)
 .replace("{{LINK}}", lang.link);
 language = jQuery(language);
 jQuery("#languages").append(language);
 }
}
body { text-align: left !important}
#answer-list {
 padding: 10px;
 width: 290px;
 float: left;
}
#language-list {
 padding: 10px;
 width: 290px;
 float: left;
}
table thead {
 font-weight: bold;
}
table td {
 padding: 5px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/Sites/codegolf/all.css?v=617d0685f6f3">
<div id="answer-list">
 <h2>Leaderboard</h2>
 <table class="answer-list">
 <thead>
 <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr>
 </thead>
 <tbody id="answers">
 </tbody>
 </table>
</div>
<div id="language-list">
 <h2>Winners by Language</h2>
 <table class="language-list">
 <thead>
 <tr><td>Language</td><td>User</td><td>Score</td></tr>
 </thead>
 <tbody id="languages">
 </tbody>
 </table>
</div>
<table style="display: none">
 <tbody id="answer-template">
 <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td><a href="{{LINK}}">{{SIZE}}</a></td></tr>
 </tbody>
</table>
<table style="display: none">
 <tbody id="language-template">
 <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td><a href="{{LINK}}">{{SIZE}}</a></td></tr>
 </tbody>
</table>

asked Apr 25, 2016 at 17:46
\$\endgroup\$

44 Answers 44

1
2
12
\$\begingroup\$

Pyth, 8

j0_Mcz0円

Split on zeros, reverse and join back on zeros.

Test Suite

answered Apr 25, 2016 at 17:50
\$\endgroup\$
8
\$\begingroup\$

IPOS, 5 bytes

'0!r%

% splits the input string on zeroes, applies the command r (reverse) to every substring and joins the result back on zeroes.

answered Apr 25, 2016 at 17:58
\$\endgroup\$
8
\$\begingroup\$

Retina, 15 bytes

S`0
O%^$`.
¶
0

Try it online! (Slightly modified to run all test cases at once.)

Explanation

S`0

Split the input around 0s that is, put each (possibly empty) run of non-zero digits on its own line.

O%^$`.

This reverses each line, by:

  • Applying the stage to each line individually with %.
  • Matching each character individually with ..
  • Sorting it by the result of the substitution ($) with the empty string (the empty second line). I.e. it doesn't sort at all, since all sort values are identical.
  • Then reverses the order of the sorted characters.

Finally:

¶
0

Turn the linefeeds back into 0s.

answered Apr 25, 2016 at 18:33
\$\endgroup\$
5
\$\begingroup\$

05AB1E, 6 bytes

Code:

0¡€R0ý

Explanation:

0¡ # Split on zeros
 €R # For each item in the array, reverse it
 0ý # Join by zeros

Uses CP-1252 encoding. Try it online!.

answered Apr 25, 2016 at 17:51
\$\endgroup\$
5
\$\begingroup\$

Julia, 30 bytes

s->replace(s,r"[^0]+",reverse)

This is an anonymous function that accepts a string and returns a string. To call it, assign it to a variable.

The replace function provides the ability to replace matches of a regular expression with the result of a function applied to each match. In this case we can get nonzeros using [^0]+ and replace those runs with the result of the reverse function applied to the matched text.

Verify all test cases online!

answered Apr 26, 2016 at 2:16
\$\endgroup\$
0
4
\$\begingroup\$

Ruby, 29 bytes

->x{x.gsub /[^0]*/,&:reverse}
answered Apr 25, 2016 at 18:01
\$\endgroup\$
4
\$\begingroup\$

Retina, (削除) 25 (削除ここまで) 24

O^$#`[^0](?=(.*?0)*)
$#1

Try it online

Saved 1 byte thanks to Martin!

Sort the nonzero digits by the number of zeros that follow the digit, in reverse order.

See Martin's solution for a clever use of per-line mode to get a shorter program!

answered Apr 25, 2016 at 18:27
\$\endgroup\$
4
\$\begingroup\$

Jolf, 8 bytes

RΜGi0λ_0

Try it here!

Explanation

RΜGi0λ_0
 Gi0 split input on zeroes
 Μ λ_ map over reversal as a lambda
R 0 join by zeroes

The code explanation looks sort of like a lambda if you squint.

An equivalent 8-byte answer:

RΜGi0 ̈́O0

Same thing, but it uses ̈́O (string mirror lambda) instead.

answered Apr 25, 2016 at 18:10
\$\endgroup\$
2
  • \$\begingroup\$ I'd say it looks more like some text in a funny shape when I squint at it. Maybe I should put this telescope away. \$\endgroup\$ Commented Apr 26, 2016 at 3:28
  • 1
    \$\begingroup\$ @cat No, no, keep it, but look at the lambda when you do. \$\endgroup\$ Commented Apr 26, 2016 at 3:29
3
\$\begingroup\$

Jelly, 5 bytes

ṣ0Uj0

Try it online!

answered Apr 25, 2016 at 19:06
\$\endgroup\$
3
\$\begingroup\$

Python, (削除) 58 (削除ここまで) 50 bytes

lambda x:"0".join([n[::-1] for n in x.split("0")])

Takes in a string x and splits on the zeros and reverses each element in the split and adds a zero and returns the this minus last zero.

answered Apr 25, 2016 at 19:34
\$\endgroup\$
3
  • 1
    \$\begingroup\$ Welcome to PPCG :) You should be able to do "0".join and then drop the addition and the slice. \$\endgroup\$ Commented Apr 25, 2016 at 19:37
  • 1
    \$\begingroup\$ No problem :) You can also remove the space between ] and for. You can check out our tips page for more pointers, if you'd like. \$\endgroup\$ Commented Apr 25, 2016 at 19:46
  • 8
    \$\begingroup\$ lambda x:"0".join(n[::-1]for n in x.split("0")) \$\endgroup\$ Commented Apr 25, 2016 at 20:04
3
\$\begingroup\$

Mathematica, 30 bytes

Join@@Reverse/@#~SplitBy~Sign&

e.g. Join@@Reverse/@#~SplitBy~Sign&[{1,2,3,0,0,5,9,0}] = {3, 2, 1, 0, 0, 9, 5, 0}

answered Apr 25, 2016 at 17:55
\$\endgroup\$
0
2
\$\begingroup\$

PowerShell v2+, 45 bytes

($args-split0|%{-join$_[$_.length..0]})-join0

Abusing the implicit casting like there's no tomorrow. Likely can't get much shorter, as there's no builtin for reverse that's shorter than this indexing trick.

Explanation

An example of how this works -- suppose 123045 was the input $args. After the -split on 0, the pipeline would contain an array (123,45). The first loop with |%{...} has the current element $_ equal to 123, which is then implicitly cast as a string, then reversed with the [] indexing. That makes it ('3','2','1') as a char-array. The loop's -join puts that back into a string "321" and leaves it on the pipeline. The next (last) loop iteration reverses the input to "54". So now our pipeline is "321", "54". That's encapsulated in parens () so it's reformed into an array, and re-joined back together with zeros to produce the resultant output string "321054". That's left on the pipeline and output to the console is implicit.

If there are subsequent zeros in the original input, the array would be populated with null elements, so there are the correct number of zeros on the output. For example, 1230045-split0 turns into (123,,45) and things continue as above.

answered Apr 25, 2016 at 18:19
\$\endgroup\$
2
\$\begingroup\$

Factor, 35 bytes

Beating Pyfon and Clojure, booyah!

[ "0"split [ reverse ] map "0"join ]

This anonymous function is a literal translation of this Python answer.

It's quite simple, just split the string on zeroes, reverse every element of the resulting array, and join all the elements (including zero-length strings) with "0".

Here's an example of it running on all test cases:

IN: scratchpad auto-use {
 "4"
 "00"
 "123"
 "0010"
 "12000345"
 "18161604"
 "95883007414830"
 "010230456078912"
 "357509902003550"
 "2492882377675046"
 "03026302053000357099"
 }
 [ 
 "0" split [ reverse ] map "0" join
 ]
 map .
{
 "4"
 "00"
 "321"
 "0010"
 "21000543"
 "61618104"
 "38859003841470"
 "010320654021987"
 "575309902005530"
 "5767732882942064"
 "03036202035000753099"
}

"00120" -> { "" "" "12" "" } -> { "" "" "21" "" } -> "00210"

answered Apr 25, 2016 at 20:18
\$\endgroup\$
0
2
\$\begingroup\$

Oracle SQL 11.2, (削除) 131 (削除ここまで) 123 bytes

Abusing XML functions.

SELECT LISTAGG(REVERSE(COLUMN_VALUE||''))WITHIN GROUP(ORDER BY rownum)FROM XMLTABLE(('"'||REPLACE(:1,'0','","0","')||'"'));
answered Apr 26, 2016 at 15:54
\$\endgroup\$
2
\$\begingroup\$

Haskell, 45 bytes

r%(0:t)=r++0:[]%t
r%(h:t)=(h:r)%t
r%_=r
([]%)

Recursively accumulates the reversed chunk so far in r, prepending it when a 0 is reached. When the remaining string is empty, it also discharges r.

The first two repeat some code, but I didn't find a shorter way to combine them (45 and 47 bytes):

r%(h:t)|h<1=r++h:[]%t|1>0=(h:r)%t
r%_=r
([]%)
r%l|0:t<-l=r++0:[]%t|h:t<-l=(h:r)%t
r%_=r
([]%)
answered Apr 25, 2016 at 20:18
\$\endgroup\$
1
  • \$\begingroup\$ I think you need one more pair of parenthesis in line #2: r%(h:t)=(h:r)%t \$\endgroup\$ Commented Apr 26, 2016 at 20:06
1
\$\begingroup\$

Pyke, 8 bytes

0円cm_0円J

Explanation:

0円c - input().split("0")
 m_ - map(reversed, ^)
 0円J - "0".join(^)

Try it here!

answered Apr 25, 2016 at 18:08
\$\endgroup\$
1
\$\begingroup\$

JavaScript (ES6), (削除) 50 (削除ここまで) 49 bytes

String version:

s=>s.replace(/[^0]+/g,r=>[...r].reverse().join``)

Saved a byte thanks to @Kevin Lau!

Array version (60 bytes):

a=>a.map((n,i)=>n?a[z-i+[...a,0].indexOf(0,i)]:(z=i,0),z=-1)
answered Apr 25, 2016 at 18:22
\$\endgroup\$
0
1
\$\begingroup\$

J, (削除) 20 (削除ここまで) 18 bytes

0}:@;@(<@|.;.1)@,]

Thanks to Zgarb for helping with this! Takes a space-separated list as a right argument.

-2 bytes thanks to Zgarb!

answered Apr 25, 2016 at 18:02
\$\endgroup\$
1
\$\begingroup\$

Clojure/ClojureScript, 44 chars

#(flatten(map reverse(partition-by pos? %)))

Same solution as others, just more verbose thanks to long function names. Wasn't going to post it because of that, but it beats some other answers so why not?

Works on any sequence type. In ClojureScript, this also works on strings since strings are processable as lists of characters, which are really just 1-length strings, which coerce to numbers for things like pos?.

answered Apr 25, 2016 at 19:15
\$\endgroup\$
1
\$\begingroup\$

Haskell, 46 bytes

import Data.Lists
(reverse=<<).split(oneOf"0")

Usage example: (reverse=<<).split(oneOf"0") $ "0123004500678090"-> "0321005400876090".

Sadly the split function requires the expensive import. Split the input list at every 0, e.g. split(oneOf"0") "0120030" -> ["","0","12","0","","0","3","0",""], reverse each chunk and concatenate into a single string.

answered Apr 25, 2016 at 19:34
\$\endgroup\$
1
\$\begingroup\$

F#, 103 bytes

let f x=x="";x.Split '0'|>Array.map(fun s->System.String(s|>Seq.toArray|>Array.rev))|>String.concat "0"
Conor O'Brien
40.4k3 gold badges96 silver badges182 bronze badges
answered Apr 26, 2016 at 2:25
\$\endgroup\$
1
\$\begingroup\$

Java, 179 bytes(with import)

import java.util.*;static void r(String s){StringJoiner x= new StringJoiner("0");for(String a:s.split("0",s.length())){x.add(new StringBuilder(a).reverse());}System.out.print(x);}

Takes in a string input and splits the characters by zero then adds them back by calling the add method on the StringJoiner class.

answered Apr 26, 2016 at 4:56
\$\endgroup\$
1
\$\begingroup\$

Perl, 22 bytes

Including +1 for -p option:

s/[^0]+/reverse$&/eg

This is a pretty trivial substitution - sorry to be so boring. Note that if your input is newline-terminated (e.g. using perl -pe 's/[^0]+/reverse$&/eg' <<<21000543 in Bash), it will catch the newline with the digits - use echo -n or printf to avoid that. Alternatively, for a cost of one additional byte, change the character class to [1-9], and you can provide many inputs, one per line.

answered Apr 27, 2016 at 9:17
\$\endgroup\$
1
\$\begingroup\$

C, 105 bytes

#define x(o)for(p=s;*s&&'0'o*s;++s);for(r=s;r-->p;putchar(*r));
f(s,p,r)char*s,*p,*r;{x(^)x(==)*s&&f(s);}

Call f with the list of digits as a nul-terminated string, and it will print the correct output.

Ungolfed and explained:

f(s,p,r)
char*s,*p,*r; // K&R style to avoid repeating `char`
{
 // x(^)x(==) expands to...
 // Find the next zero digit
 for(p = s; *s && '0' ^ *s; ++s)
 ;
 // Print that run backwards
 for(r = s; r-- > p; putchar(*r))
 ;
 // Find the next non-zero digit
 for(p = s; *s && '0' == *s; ++s)
 ;
 // Print that run backwards (no difference, it's all zeros)
 for(r = s; r-- > p; putchar(*r))
 ;
 // If there's more string, recurse
 *s && f(s);
}

See it live on Coliru

answered Dec 27, 2016 at 15:50
\$\endgroup\$
1
\$\begingroup\$

Husk, 4 bytes

ṁ↔ġ¬

Try it online! or verify all test cases

Input is a list of digits.

ġroups all adjacent runs of nonzero (¬=NOT) digits, then aps the 'reverse a list' (=REV) function across all groups, concatenating the result.

answered Apr 10, 2021 at 15:06
\$\endgroup\$
1
\$\begingroup\$

K (ngn/k), 10 bytes

"0"/|'"0"\

Try it online!

Similar to many of the other approaches; takes (implicit) input as a string of digits:

  • "0"\ split input on 0s
  • |' reverse each chunk
  • "0"/ join with 0s
answered Apr 10, 2021 at 15:55
\$\endgroup\$
1
\$\begingroup\$

Vyxal, 4 bytes

0ẆRṅ

Takes input as a string instead. If that's not allowed then the flag can be added.

Try it online!

Explanation:

0 # Push the literal digit 0
 Ẇ # Split and keep delimiter
 R # Reverse each one
 ṅ # Join by nothing
answered Nov 5, 2022 at 23:31
\$\endgroup\$
1
\$\begingroup\$

tinylisp 2, 58 bytes

(d R(\(D(A))(?(! D)A(?(h D)(R(t D)(c(h D)A))(, A(c 0(R(t D

Try It Online!

Ungolfed/explanation

Define reverse-runs as a function that takes a list of digits and optionally an accumulator. If the accumulator argument is not provided, it defaults to nil.

(def reverse-runs
 (lambda (digits (accum))

Base case: if digits is empty, return whatever is in the accumulator:

 (if (not digits)
 accum

Otherwise, if the next digit is truthy (non-zero), prepend it to the accumulator and recurse:

 (if (head digits)
 (reverse-runs
 (tail digits)
 (cons (head digits) accum))

Otherwise, the next digit is zero; prepend the accumulator and a 0 to a recursive call with an empty accumulator:

 (concat accum
 (cons 0
 (reverse-runs
 (tail digits))))))))
answered Aug 21, 2024 at 20:06
\$\endgroup\$
1
\$\begingroup\$

Nibbles, 7 nibbles (3.5 bytes)

\+\`=$/

Attempt This Online!

\+\`=$/ # full program
\+\`=$/$$ # with implicit variables shown
 `=$ # split the input into chunks
 # by the result of
 /$$ # dividing each element by itself
 # (so: nonzero/nonzero == 1,
 # and zero/zero == 0)
 \ # reverse the order of the chunks,
 + # flatten,
\ # and reverse
answered Aug 22, 2024 at 14:26
\$\endgroup\$
1
\$\begingroup\$

Uiua, 7 bytes

⍜⊜しろいしかく⍚⇌⊸±

Try it online: Uiua pad

Under taking the boxed chunks of where the sign of each number is one, reverse each box.

answered Aug 22, 2024 at 18:57
\$\endgroup\$
1
2

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.