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
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Repeat the characters

+5
−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      -> ""
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

14 answers

You are accessing this answer with a direct link, so it's being shown above all other answers regardless of its score. You can return to the normal view.

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

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

Just iterate directly (1 comment)
+3
−0

Haskell, 18 bytes

(.replicate).(>>=)

Try it online!

History
Why does this post require attention from curators or moderators?
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!

History
Why does this post require attention from curators or moderators?
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.

History
Why does this post require attention from curators or moderators?
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!

History
Why does this post require attention from curators or moderators?
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!

History
Why does this post require attention from curators or moderators?
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!

History
Why does this post require attention from curators or moderators?
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)
+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.

History
Why does this post require attention from curators or moderators?
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
History
Why does this post require attention from curators or moderators?
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...

History
Why does this post require attention from curators or moderators?
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.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+1
−0

shortC, 40 37 bytes

i;f(C*s,In){O;*s;s++)Oi=n;i--;P*s));}

Try it online!

A shortC conversion of @Lundin's C (gcc) implementation.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+0
−0

J, 1 byte

#

Try it online!

The copy verb. It works the exact same as APL and Jelly's replicate.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+0
−0

Ruby, 23 bytes

->{_1.gsub /./,'\0'*_2}

Try this online!

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »