Il y a quelque temps, il y avait un défi à relever concernant la multiplication de chaînes. Cela nous a montré comment nous pouvons multiplier non seulement des nombres, mais aussi des chaînes. Cependant, nous ne pouvons toujours pas multiplier un nombre par une chaîne correctement. Il y a eu une tentative de le faire mais c'est évidemment faux. Nous devons régler cela!
Ta tâche:
Ecrivez une fonction ou un programme qui multiplie deux entrées, une chaîne et un entier. Pour (correctement) multiplier une chaîne par un entier, divisez la chaîne en caractères, répétez chaque caractère un nombre de fois égal à celui-ci, puis collez les caractères ensemble. Si le nombre entier est négatif, nous utilisons sa valeur absolue dans la première étape, puis inversons la chaîne. Si l'entrée est 0, rien ne sort (rien multiplié par 0 ne vaut rien).
Contribution:
Une chaîne composée uniquement de caractères ASCII imprimables et de nouvelles lignes, ainsi qu'un entier (négatif possible).
Sortie:
La chaîne multipliée par le nombre entier.
Exemples:
Hello World!, 3 --> HHHeeellllllooo WWWooorrrlllddd!!!
foo, 12 --> ffffffffffffoooooooooooooooooooooooo
String, -3 --> gggnnniiirrrtttSSS
This is a fun challenge, 0 -->
Hello
World!, 2 --> HHeelllloo
WWoorrlldd!!
Notation:
C'est le code-golf , le plus petit nombre d'octets gagne!
la source
Réponses:
Gelée ,
654 octetsEssayez-le en ligne!
Comment ça marche
la source
JavaScript (ES6), 63 octets
Prend la syntaxe de currying
(s)(n)
.Cas de test
Afficher l'extrait de code
la source
reduce
!Python 3 , 44 octets
Essayez-le en ligne!
la source
f(n,*s)
is considered validPython 2,
59575046 bytes-2 bytes thanks to Anders Kaseorg. -4 bytes thanks to Dennis.
Try it online!
la source
05AB1E, 10 bytes
Try it online!
la source
²0‹i
isn't the best route and come up with literally 0 alternatives.²0‹i
before and I always think there has to be something better.Ä.D)øJ¹0‹iR
is the best I can do without copying you, I think yours is optimized.è
here, though I can't find a way to apply it in this scenario. Would save a maximum of 1 byte, if that.SÂΛ@²Ä×J
, usingÎ
to push 0 and input works if you change the order. Saves 1 byte! (Also replaced the if, so it doesn't need to be closed)MATL, 9 bytes
Inputs are: number, then string.
Strings with newlines are input using char
10
as follows:['first line' 10 'second line']
.Try it online! Or verify all test cases.
Explanation
Consider inputs
-3
and'String'
.la source
Haskell,
4136 bytesTry it online!
Example usage:
f (-3) "abc"
yields"cccbbbaaa"
.Edit: -5 bytes thanks to xnor!
la source
(<*[1..n])
for((<$[1..n])=<<)
.V,
29, 23, 18, 17 bytesTry it online!
Hexdump:
Thanks to @nmjcman101 for saving 6 bytes, which encouraged me to save another 5!
The original revision was pretty terrible, but now I'm really proud of this answer because it handles negative numbers surprisingly well. (V has next to no numerical support and no support for negative numbers)
Explanation:
At this point, the buffer looks like this:
It's important to not the trailing newline, and that the cursor is on it.
la source
0
special cases in V came in super handy.R,
837876 bytesAnonymous function.
Frederic saved 3 bytes, Giuseppe saved
24.Explanation:
Tests:
la source
rep(foo,,,3)
orrep(foo,e=3)
(same lenght) ;-)a=
. Hence, I used the value ofa
as an argument to the reverse function ifi<0
, by having the conditional return the function (which is why I needed the backquotes). But I needed to also apply the identity function for thei>=0
case, so I used(
which is close enough.(
is in fact a function. R is weird.(
is semantically equivalent to the identityfunction(x)x
05AB1E, 10 bytes
Try it online!
Explanation
la source
PHP>=7.1, 65 bytes
PHP Sandbox Online
la source
$n<0
has the same value as$n<0?:0
but it's 3 bytes shorter :-)Brain-Flak (BrainHack),
154152 bytesTry it online!
Just here to give DJMcMayhem some competition. ;)
Explanation
Here's a modified version of DJMcMayhem's explanation
la source
J,
191513 bytesTry it online!
Explanation
la source
(#~|)A.~0-@>]
for 13 bytes#~ ::(|.@#~|)
Dyalog APL, 15 bytes
String as a left argument, number as a right argument.
Try it online!
How?
⍺/⍨
- repeat the string|⍵
- abs(number) times⌽⍣
- reverse if(⍵<0)
- the number is below 0la source
MATLAB, 37 bytes
This defines and anonymous function with inputs
s
: string andn
: number.Example runs:
la source
repelem
exists.repelem
in Octave, for nowBrain-Flak (Haskell),
202192 bytesTry it online!
This is probably the worst possible language to do it in, but it's done. Thanks to @Wheatwizard for providing the Haskell interpreter, which allows mixed input formats. This would be about 150 bytes longer without it.
Explanation:
la source
Java (OpenJDK 8),
9998898785 bytesTry it online!
la source
s[(n<0?-l-~i:i)/n]
i
in the conditions->n->{for(int l=s.length*(n<0?-n:n),i=0;i++<l;)System.out.print(s[(n<0?i-l:i-1)/n]);}
. Another byte can be saved by iterating from -l to 0 instead (s->n->{for(int i=s.length*(n<0?n:-n),r=n<0?0:~i;i++<0;)System.out.print(s[(i+r)/n]);}
).Octave, 49 bytes
Try it online!
I will provide an explanation tomorrow.
la source
Ruby, 59 +1 = 60 bytes
Uses
-n
flag.Try it online!
la source
eval$_
is shorter than$_.to_i
by 1 byte.String#chars
can also accept a block the same wayString#each_char
can. Finally, reverse the input before processing each character so you can print it directly instead (switching your flag to-n
). All of this combines to become 55+1=56 bytes.Charcoal, 16 bytes
Try it online! Link is to verbose version of code. Explanation:
la source
CJam, 9 bytes
Try it online!
la source
Japt, 12 bytes
Try it online!
Explanation
Implicit input of string
U
and integerV
.Map (
®
) each letter ofU
(implicitly) to itself repeated (p
)abs(V)
(Va
) times.Turn the string into an array of chars (
¬
) and reduce (r
) that with..."!+".slice(sign(V))
- this either reduces with+
→a + b
, or with!+
→b + a
.Thanks @Arnauld for the backwards-reduce idea!
la source
£gY*Vg)pVa
should lead to a shorter solution but my brain has shut down for the holidays so I can't quite figure it out. You may be able to do something with it, though.WendyScript, 46 bytes
Try it online!
Explanation (Ungolfed):
la source
C89 bytes
I saw Ben Perlin's version and wondered if you couldn't be shorter still and also have a full program; surely,
atoi()
andputchar()
aren't that expensive in terms of bytes? Seems I was right!la source
Pyth,
1311 bytesTry it!
-2 bytes thanks to @jacoblaw
explanation
old approach, 13 bytes
Try it!
la source
Python 3, 68 bytes
Try it online!
la source
If the integer is negative, we use its absolute value in the first step, and then reverse the string.
)
n<0 else
=>n<0else
QBIC, 32 bytes
Explanation
la source
Mathematica, 89 bytes
input
la source
Braingolf, 22 bytes
Try it online!
Eeh, not bad.
Takes input as an integer and an array of characters.
Alternatively:
Braingolf, 31 bytes
Try it online!
Takes input as an integer and a string
la source
C, 109 bytes
Starting with a function declaration that takes an int and a string and produces a string (it seems implied that memory is not preallocated and must be created) it seems that the straight-forward approach is shorter than any attempts at being cleaver that I had tried.
}
la source