Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs

Dashboard
Notifications
Mark all as read
Challenges

Repeat the characters

+4
−0

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      -> ""
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

11 answers

+3
−0

Haskell, 18 bytes

(.replicate).(>>=)

Try it online!

Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+3
−0

Ruby, 34 29 bytes

->s,n{s.chars.map{_1*n}*""}

Try it online!

Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+2
−0

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.

Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+2
−0

JavaScript, 36 bytes

s=>n=>s.replace(/./g,`$&`.repeat(n))

Try it online!

Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+2
−0

Python 3, 34 bytes

lambda a,b:''.join(b*i for i in a)

Try it online!

Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+2
−0

Jelly, 1 byte

x

Try It Online!

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.

Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+2
−0

C (gcc), 56 bytes

i;f(s,n)char*s,n;{for(;*s;s++)for(i=n;i--;putchar(*s));}

Try it online!


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

Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+2
−0

Japt -m, 2 bytes

pV

Try it

pV     :Implicit map of first input string
p      :Repeat
 V     :Second input times
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+2
−0

APL (Dyalog Unicode), 1 byte

/

Try it online!

/ is called Replicate.

Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+2
−0

jq, 32 27 bytes

-5 bytes (thank you Razetime)

.n as$n|.s/""|map(.*$n)|add

Try it online!

Why does this post require moderator attention?
You might want to add some details to your flag.

2 comment threads

18 bytes: [Try it online!](https://tio.run/##yyr8/z9aQ69YX0lJMzpWSy8vtiYxJeX//2ouBQWlYiUrJY/UnJx8hfD8... (1 comment)
you can use `add` in place of join. (1 comment)
+1
−0

Python 3, 53 38 bytes

def f(x,y):
	for c in x:print(end=c*y)

Try it online!

I'm sure someone will find a lambda solution to this.

Golfed 15 bytes thanks to @Moshi's advice.

Why does this post require moderator attention?
You might want to add some details to your flag.

1 comment thread

Just iterate directly (1 comment)

Sign up to answer this question »

This community is part of the Codidact network. We have other communities too — take a look!

You can also join us in chat!

Want to advertise this community? Use our templates!