21
\$\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\$

51 Answers 51

1
2
1
\$\begingroup\$

Clojure (107)

(pr(apply str(map #(apply str(repeat(Integer/parseInt(str(second %)))(first %)))(partition 2(read-line)))))

This feels exceptionally long for being Clojure, if someone can do better, please post it.

\$\endgroup\$
1
1
\$\begingroup\$

C# - 108 105 bytes

void f(){var s=Console.ReadLine();for(int i=0;i<s.Length;i+=2)Console.Write(new string(s[i],s[i+1]-48));}

Explanation:

void f()
{
    var s = Console.ReadLine(); // Read the input from STDIN.

    // Loop until the end of the string, and increment our index counter by 2 at each iteration.
    for (int i = 0; i < s.Length; i += 2) 

        Console.Write(new string(s[i], s[i + 1] - 48)); // Create (s[i + 1] - 48) copies of the current character, and print it.
        // s[i + 1] - 48 implicitly casts the character at s[i + 1] to its integer digit form. 
}
\$\endgroup\$
4
  • \$\begingroup\$ Please add an explanation to help people less familiar with C# although it may seem simple enough, short answers with no code do get auto-flagged \$\endgroup\$ Aug 22, 2016 at 11:31
  • \$\begingroup\$ @RohanJhunjhunwala Sure. I'll add it when I get home. My C++ answer will also have it then. \$\endgroup\$
    – Yytsi
    Aug 22, 2016 at 13:03
  • \$\begingroup\$ @RohanJhunjhunwala Explanation added. Does it look good to you? \$\endgroup\$
    – Yytsi
    Aug 22, 2016 at 20:57
  • 1
    \$\begingroup\$ Looks good, good work! \$\endgroup\$ Aug 22, 2016 at 21:01
1
\$\begingroup\$

JavaScript (ES6), 40 bytes

s=>s.replace(/../g,v=>v[0].repeat(v[1]))

f=
  s=>s.replace(/../g,v=>v[0].repeat(v[1]))
;
console.log(f(":144,1'1"))

\$\endgroup\$
1
\$\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\$
1
\$\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\$
1
\$\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\$
1
\$\begingroup\$

K (oK), 17 bytes

Solution:

,/{(.y)#x}.'0N 2#

Try it online!

Explanation:

,/{(.y)#x}.'0N 2# / the solution
            0N 2# / reshape input into Nx2 grid
  {      }.'      / apply lambda to each pair of inputs
       #x         / take from x
   (  )           / do this together
    .y            / value y (ie "5" => 5)
,/                / flatten
\$\endgroup\$
1
\$\begingroup\$

><>, 28 24 bytes

Saved 4 bytes thanks to Jo King!

/1-:?v
\$o:$<-*c4i;?(0:i

Try it online!

This was very interesting to golf the form. You can see my process below.

Alternatives

i:0(?;i4c*->:    ?v
           ^$o:$-1<

/?      <:-*c4i;?(0:i
\1-$:o$:^0

/?    :<-*c4i;?(0:i
\1-$:o$^.00

/?    :<-*c4ip1c;?(0:i
\1-c1go^.00

/?    :<-*c4ip1c;?(0:i
\1-c1go^.00

/?}-1<:-*c4i;?(0:i[0
\:o}:^.00

/?}-1<:-*c4i;?(0:i[0
\:o}:^00

0/?}-1<:-*c4i;?(0:i[
0\:o}:^
\$\endgroup\$
0
1
\$\begingroup\$

Perl 6, 20 bytes

{S:g{(.)(.)}=$0 x$1}

Try it online!

Anonymous codeblock containing a regex that replaces each pair of characters in the input with the first one repeated by the second.

\$\endgroup\$
1
\$\begingroup\$

8088 Assembly, IBM PC DOS, 24 bytes

Binary xxd dump:

00000000: d1ee add0 e88a c8ad 80ec 308a dcb4 0ecd  ..........0.....
00000010: 10fe cb75 f8e2 f0c3                      ...u....

Unassembled listing:

D1 EE       SHR  SI, 1          ; point SI to DOS PSP (080H) 
AD          LODSW               ; load input length into AL 
D0 E8       SHR  AL, 1          ; half input length to get number of CN pairs 
8A C8       MOV  CL, AL         ; move into counter 
        C_LOOP: 
AD          LODSW               ; Load pair: C = AL, N = AH 
80 EC 30    SUB  AH, '0'        ; ASCII convert N to binary 
8A DC       MOV  BL, AH         ; BL is counter for number of reps of C 
        N_LOOP: 
B4 0E       MOV  AH, 0EH        ; BIOS teletype output function 
CD 10       INT  10H            ; call BIOS, display C 
FE CB       DEC  BL             ; decrement loop counter 
75 F8       JNZ  N_LOOP         ; if end of N loop, move to next C 
E2 F0       LOOP C_LOOP         ; loop through all C's 
C3          RET                 ; return to DOS 

Standalone DOS executable program. Input via command line, output to console.

enter image description here

\$\endgroup\$
1
\$\begingroup\$

Brachylog, 11 bytes

ġ₂{ẹịᵗj₎}ᵐc

Try it online!

          c    The output is the concatenation of
  {     }ᵐ     the result of mapping
   ẹịᵗj₎       repeating the first element by the second converted to an integer
ġ₂             over the length-2 chunks of the input.
\$\endgroup\$
0
\$\begingroup\$

Senva, 33 bytes

I was first making a big answer with a large code into the back-memory, but finally I made that :

1'(`>0.>{@}0'{v}1'0,>48-0,-<~>$>$

Here is an explanation :

1'  // Go to the second cell, the first will be used to store an information
(   // Input a string
`   // Go to the last cell (at the last character of the string)
>   // Make a new cell after the string
0.  // Store a 0 to mark the end of the string
>   // Make a new cell
{@} // Store this adress into the back-memory
0'  // Go to the 1st cell
{v} // Paste the adress, now the 1st cell contains the adress which points after the '0' which marks the end of the string
1'  // Go to the 2nd cell (the beginning of the string)
0,  // While we do NOT encounter a zero (while the string is not terminated)
  >   // Go to the next cell (the repeat number)
  48- // Substract 48 to get the number (the string was stored as an ASCII chain)
  0,  // While this cell is not equal to 0 (higher than 0)
    - // Decrease the cell
    < // Go to the cell which contains the character to repeat
    ~ // Display it
    > // Go to the number
  $ // End of the loop
  > // Go to the next number
$ // End of the loop

So we make a loop across the string, and a second loop into which display the C character `` times.

\$\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\$
2
  • \$\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\$ Aug 23, 2016 at 7:41
  • \$\begingroup\$ @PeterTaylor Good catch, thanks! \$\endgroup\$
    – Yytsi
    Aug 23, 2016 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\$

Ahead, 12 bytes

~W:k-1'ii@j~

Explained

Single line Ahead boards are nice because you can explain them without some kind of diagram.

(Program effectively loops in reverse by floating)

~            float to next ~ (to end of line)
          ~  stop floating (end of line, turn around)
         j   jump next
       i     read char from input
        @    bounce here and die if input is empty, else continue west
      i      read digit char from input
   -1'       subtract '1 (49) from input char to get digit value
  :          dup first char that many times
 W           Write stack to output as chars
~            (repeat until head dies from empty input)

Try it online!

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

Clojure, 60 bytes

#(apply str(for[[c n](partition 2 %)_(range(-(int n)48))]c))

I assume we can take the input as an argument and return the string. If not this grows to 73 bytes:

#(pr(apply str(for[[c n](partition 2(read-line))_(range(-(int n)48))]c))
\$\endgroup\$
0
\$\begingroup\$

SmileBASIC, 54 bytes

LINPUT S$WHILE I<LEN(S$)?S$[I]*VAL(S$[I+1]);
I=I+2WEND

Alternative (same size):

LINPUT S$WHILE""<S$?SHIFT(S$)*VAL(S$[1]);
S$[0]="
WEND

Really wanted to write ?SHIFT(S$)*VAL(SHIFT(S$)) but expressions are evaluated right to left, so the shifts would happen in the wrong order...

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

Python 2, 40 bytes

f=lambda s:s and s[0]*int(s[1])+f(s[2:])

Try it online!

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

Gema, 17 16 characters

??=@repeat{$2;?}

Sample run:

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

Try it online!

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

Husk, 9 7 bytes

ṁΓ·RrC2

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ Use Γ to access the head and tail of a list more efficiently: Try it online! \$\endgroup\$
    – Zgarb
    Oct 12, 2020 at 8:00
  • \$\begingroup\$ @Zgarb I'd like to know more about how this works in chat. \$\endgroup\$
    – Razetime
    Oct 12, 2020 at 8:34
1
2

Your Answer

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

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