Reverse positive runs

22

2

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>

Zgarb

Posted 2016-04-25T17:46:15.370

Reputation: 39 083

Answers

11

Pyth, 8

j0_Mcz\0

Split on zeros, reverse and join back on zeros.

Test Suite

FryAmTheEggman

Posted 2016-04-25T17:46:15.370

Reputation: 16 206

8

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.

Denker

Posted 2016-04-25T17:46:15.370

Reputation: 6 639

7

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.

Martin Ender

Posted 2016-04-25T17:46:15.370

Reputation: 184 808

5

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!.

Adnan

Posted 2016-04-25T17:46:15.370

Reputation: 41 965

5

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!

Alex A.

Posted 2016-04-25T17:46:15.370

Reputation: 23 761

4

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.

Conor O'Brien

Posted 2016-04-25T17:46:15.370

Reputation: 36 228

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. – cat – 2016-04-26T03:28:50.110

1@cat No, no, keep it, but look at the lambda when you do. – Conor O'Brien – 2016-04-26T03:29:56.563

4

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!

FryAmTheEggman

Posted 2016-04-25T17:46:15.370

Reputation: 16 206

3

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}

feersum

Posted 2016-04-25T17:46:15.370

Reputation: 29 566

3

Ruby, 29 bytes

->x{x.gsub /[^0]*/,&:reverse}

Doorknob

Posted 2016-04-25T17:46:15.370

Reputation: 68 138

3

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.

1232

Posted 2016-04-25T17:46:15.370

Reputation: 41

1Welcome to PPCG :) You should be able to do "0".join and then drop the addition and the slice. – FryAmTheEggman – 2016-04-25T19:37:04.143

@FryAmTheEggman thanks for the tip – 1232 – 2016-04-25T19:44:13.610

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.

– FryAmTheEggman – 2016-04-25T19:46:11.237

7lambda x:"0".join(n[::-1]for n in x.split("0")) – orlp – 2016-04-25T20:04:10.853

2

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.

AdmBorkBork

Posted 2016-04-25T17:46:15.370

Reputation: 41 581

2

Jelly, 5 bytes

ṣ0Uj0

Try it online!

Dennis

Posted 2016-04-25T17:46:15.370

Reputation: 196 637

2

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
([]%)

xnor

Posted 2016-04-25T17:46:15.370

Reputation: 115 687

I think you need one more pair of parenthesis in line #2: r%(h:t)=(h:r)%t – nimi – 2016-04-26T20:06:51.203

2

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"

cat

Posted 2016-04-25T17:46:15.370

Reputation: 4 989

1

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

Quentin

Posted 2016-04-25T17:46:15.370

Reputation: 1 187

1

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!

Conor O'Brien

Posted 2016-04-25T17:46:15.370

Reputation: 36 228

1

Pyke, 8 bytes

\0cm_\0J

Explanation:

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

Try it here!

Blue

Posted 2016-04-25T17:46:15.370

Reputation: 26 661

1

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)

user81655

Posted 2016-04-25T17:46:15.370

Reputation: 10 181

1

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?.

MattPutnam

Posted 2016-04-25T17:46:15.370

Reputation: 521

1

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.

nimi

Posted 2016-04-25T17:46:15.370

Reputation: 34 639

1

F#, 103 bytes

let f x=x="";x.Split '0'|>Array.map(fun s->System.String(s|>Seq.toArray|>Array.rev))|>String.concat "0"

David Conrad

Posted 2016-04-25T17:46:15.370

Reputation: 1 037

1

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.

1232

Posted 2016-04-25T17:46:15.370

Reputation: 41

1

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","')||'"'));

Jeto

Posted 2016-04-25T17:46:15.370

Reputation: 1 601

1

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.

Toby Speight

Posted 2016-04-25T17:46:15.370

Reputation: 5 058

0

Clojure, 37 bytes

#(mapcat reverse(partition-by #{0}%))

Uses #{0} instead of pos? (save 1 byte by concatenating with %) and using mapcat instead of (flatten(map. Still longer than Factor.

NikoNyrh

Posted 2016-04-25T17:46:15.370

Reputation: 2 361

0

Wonder, 23 bytes

rstr[`[^0]*`g;'><"".rev

Usage:

(rstr[`[^0]*`g;'><"".rev)1230123

Reverses all parts of the input that match the global regex [^0]*.

Mama Fun Roll

Posted 2016-04-25T17:46:15.370

Reputation: 7 234

0

Perl 5, 52 bytes

A subroutine:

{$_=pop;y/0/ /;map$i.=reverse,split/\b/;$i=~y/ /0/r}

msh210

Posted 2016-04-25T17:46:15.370

Reputation: 3 094

-a doesn't work (in Strawberry at least) when the input string ends in 0, since split/\b/ includes the $/ with the 0. – msh210 – 2016-04-25T20:03:42.777

0

Actually, 22 bytes

k"a%sa"%'0@s♂R'0j#pXdX

This actually made me notice that there's a bug in the split command - it doesn't preserve empty splits. As a workaround, I surround the input string with as before doing the split, reverse, and join, then remove the as at the end. Input is taken as a string, output is a list of single-character strings.

Try it online

Explanation:

k"a%sa"%'0@s♂R'0j#pXdX
k"a%sa"%                surround input with "a" characters
        '0@s            split on 0
            ♂R          reverse each piece
              '0j       join on 0
                 #pXdX  listify, remove first and last element (the "a"s)

Mego

Posted 2016-04-25T17:46:15.370

Reputation: 32 998

0

C#, 131 bytes ##

solution flawed!

string j(string i){foreach(var s in i.Split('0'))if(s!="")i=i.Replace(s,new string(s.ToCharArray().Reverse().ToArray()));return i;}

ungolfed:

string j(string input)
    {

        string[] a = input.Split('0');

        foreach (string s in a)
        {
            if (s!="")
            input=input.Replace(s, new string(s.ToCharArray().Reverse().ToArray()));
        }

        return input;
    }

downrep_nation

Posted 2016-04-25T17:46:15.370

Reputation: 1 152

1I believe this method has a bug when one string to be reversed is a subset of another. If given the input 01201230, this would return 02102130. This is because the String.Replace method replaces all occurrences of the first string with the second. This bug would also be caused if one pattern shows up when reversed (0120210 would return 0120120). – Xynariz – 2016-04-26T18:02:23.253

I didn't even think of that.. – downrep_nation – 2016-04-26T18:13:41.953

0

C#, 133 bytes


Golfed

String m(String s){for(int i=0,p=i;i<s.Length;i++){if(s[i]=='0'){p=i+1;}else{s=s.Insert(p,s[i].ToString()).Remove(i+1,1);}}return s;}

Ungolfed

String m( String s ) {
    // i = Index on the String
    // p = Pointer of the last '0' found
    for (int i = 0, p = i; i < s.Length; i++) {
        if (s[ i ] == '0') {
            // Move the 'p' pointer to the index after this '0'
            p = i + 1;
        } else {
            // Insert the Char at the Index location to the Pointer location 
            //    and remove the one at the Index Location + 1,
            //    since the String was incremented by one char due to the 'Insert()'.
            s = s.Insert( p, s[ i ].ToString() )
                 .Remove( i + 1, 1 );
        }
    }

    return s;
}

Full code

using System;
using System.Collections.Generic;

namespace Namespace {
    class Program {
        static void Main( String[] args ) {
            List<String> ls = new List<String>() {
                "4",
                "00",
                "123",
                "0010",
                "12000345",
                "18161604",
                "95883007414830",
                "010230456078912",
                "357509902003550",
                "2492882377675046",
                "03026302053000357099"
            };

            foreach (String s in ls) {
                String f = m( s );

                Console.WriteLine( String.Format( "{0,30}", s.Replace( "0", "0 " ) ) );
                Console.WriteLine( String.Format( "{0,30}", f.Replace( "0", "0 " ) ) );
                Console.WriteLine( "" );
            }

            Console.ReadLine();
        }

        static String m( String s ) {
            for (Int32 i = 0, p = i; i < s.Length; i++) {
                if (s[ i ] == '0') {
                    p = i + 1;
                } else {
                    s = s.Insert( p, s[ i ].ToString() ).Remove( i + 1, 1 );
                }
            }

            return s;
        }
    }
}

auhmaan

Posted 2016-04-25T17:46:15.370

Reputation: 906

Just noticed now that @downrep_nation had already posted here a solution for C#, that beats my code by 2 bytes... ( It can be more.... ) – auhmaan – 2016-04-27T09:20:39.490

0

Java, 126

a->{String r="",t[]=a.split("0");for(String s:t){r+=new StringBuilder(s).reverse()+"0";}return r.substring(0, r.length()-1);}

HopefullyHelpful

Posted 2016-04-25T17:46:15.370

Reputation: 208

If you declare the s variable at the front with r and t[], can you omit the type declaration of s in the for loop? – Cyoce – 2016-05-24T18:19:51.497

Nope, I checked it, enhanced for loops need a newly declared variable. – HopefullyHelpful – 2016-05-25T00:59:21.977

huh. That's weird. Oh well – Cyoce – 2016-05-25T05:53:49.330