Repeat the characters
Given a string and a non-negative integer $n$, output a new string in which each character is repeated $n$ times.
Test cases:
"abc", 1 -> "abc"
"Hello", 0 -> ""
"double", 2 -> "ddoouubbllee"
"Hi", 4 -> "HHHHiiii"
"", 20 -> ""
[Ruby], 34 29 bytes ```ruby …
3y ago
[Haskell], 18 bytes …
3y ago
[Python 3], 34 bytes …
3y ago
Rockstar, 90 bytes list …
3y ago
JavaScript, 36 bytes …
3y ago
Japt `-m`, 2 bytes pV …
3y ago
[C (gcc)], 56 bytes …
3y ago
[jq], 32 27 bytes -5 bytes (t …
3y ago
[Jelly], 1 byte x Tr …
3y ago
[APL (Dyalog Unicode)], 1 byte …
3y ago
[shortC], 40 37 bytes i …
3y ago
[Python 3], 53 38 bytes …
3y ago
J, 1 byte ``` # ``` Tr …
3y ago
Ruby, 23 bytes ```ruby ->{ …
3y ago
14 answers
Rockstar, 90 bytes
listen to S
split S
listen to N
O's""
while S
roll S into C
let C be*N-0
let O be+C
say O
Try it here - code will need to be pasted in; s
goes on the first line of input (leave blank for the empty string) and n
goes on the second line.
0 comment threads
C (gcc), 56 bytes
i;f(s,n)char*s,n;{for(;*s;s++)for(i=n;i--;putchar(*s));}
My attempt at recursion ended up at 61 bytes:
o;f(s,n)char*s,n;{for(o=n;*s&&n--;putchar(*s));*s&&f(++s,o);}
I still think there's probably a more elegant way to solve both loops with recursion, but I can't spot it...
0 comment threads
Jelly, 1 byte
x
x
is Jelly's replicate atom. Since Jelly is inspired by languages like J, APL, etc, it is no surprise that is has an atom for a fairly important function.
0 comment threads
jq, 32 27 bytes
.n as$n|.s/""|map(.*$n)|add
shortC, 40 37 bytes
i;f(C*s,In){O;*s;s++)Oi=n;i--;P*s));}
A shortC conversion of @Lundin's C (gcc) implementation.
0 comment threads