17
\$\begingroup\$

Write the shortest code in the language of your choice to perform run length decoding of the given string.

The string will be supplied as input on stdin in the form

CNCNCNCNCNCNCNCN

where each C could be any printable ASCII character and each N is a digit 1 to 9 (inclusive).

Sample input:

:144,1'1

Corresponding output:

:4444,'
\$\endgroup\$

37 Answers 37

0
\$\begingroup\$

Gema, 17 characters

??=@repeat{$2;$1}

Sample run:

bash-4.3$ gema '??=@repeat{$2;$1}' <<< ":144,1'1"
:4444,'
\$\endgroup\$
0
\$\begingroup\$

C++ - 151 144 142 bytes

#include <iostream>
#include <string>
void f(){std::string p;std::cin>>p;for(int i=0,j;i<p.size();i+=2)for(j=48;j<p[i+1];j++)std::cout<<p[i];}

Way too long. I should probably just use an character pointer instead and forget the whole string thing.

2 bytes saved by @PeterTaylor!

Explanation:

void f()
{
    std::string p; // A string to hold the input.
    std::cin >> p; // Read the input from STDIN, and source to <p>.

    // Loop until the end of the string <p>, withs index jumps of 2 at each iteration.
    for(int i = 0, j; i < p.size(); i += 2)

        // Loop (p[i + 1] - 48) times, and print the current character each time.
        /* p[i + 1] - 48 is a neat way to convert the next character in the string
           to its integer digit from. */
        for(j = 48; j < p[i + 1]; j++)
            std::cout << p[i]; // Print the character we are at.
}
\$\endgroup\$
  • \$\begingroup\$ for(j=0;j<p[i+1]-48;j++): since you're not using j inside the loop, you can save two chars with for(j=48;j<p[i+1];j++) \$\endgroup\$ – Peter Taylor Aug 23 '16 at 7:41
  • \$\begingroup\$ @PeterTaylor Good catch, thanks! \$\endgroup\$ – Yytsi Aug 23 '16 at 7:45
0
\$\begingroup\$

><> - 36 Bytes

i:01-=?; l2=?v
v?=*68:ro:r-1<
>~~d0.

This solution can be run without using the input flag or preloading the stack, although both of those methods could cut down on bytes. If there is any interest, I can describe how it works to anyone who is curious.

Try it here!

\$\endgroup\$
0
\$\begingroup\$

JavaScript, 40 bytes

f=v=>v&&v[0].repeat(v[1])+f(v.slice(2))

Recursive. Expand first 2 characters and call self with remainder of string.

\$\endgroup\$
0
\$\begingroup\$

RProgN, 55 Bytes

S y = '' x = y L ¿ y pop y pop \ rep x . 'x' = y L } x

Explained

S y =               # Convert the input to a backwards stack, assign it to y.
'' x =              # Assign a blank string to x
y L                 # Push the length of y
¿                   # While the top of the stack is truthy, popping.
    y pop           # Push the top value of y to the stack.
    y pop           # Twice.
    \               # Swap them around, so we get C 1 instead of 1 C
    rep             # Repeat the string. (C 1 times in the above example)
    x . 'x' =       # Append it to the back of x, and re-assign it. We use the back because we're starting at the END of the string.
    y L             # Push the length of y for the next itteration of the loop.
}                   #
x                   # Push x, and implicitly print it.

Try it Online!

\$\endgroup\$
0
\$\begingroup\$

Jelly, 10 bytes

s2µṪV$€x@Ẏ

Try it online!

GOLFSCRIPT TIE JELLY DAT IMPOSSIBLE WAAAAAT!?

Jelly, 10 bytes

Ḋm2V€x@m2$

Try it online!

\$\endgroup\$
0
\$\begingroup\$

PHP, 51 bytes

for(;--$z?:($c=$argn[$i++]).$z=$argn[$i++];)echo$c;

Try it online!

PHP, 54 bytes

for(;~$c=$argn[$i++];)echo str_repeat($c,$argn[$i++]);

Try it online!

PHP, 54 bytes

for(;~$c=$argn[$i++];)echo str_pad($c,$argn[$i++],$c);

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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