34
1
There was a challenge up a while ago about multiplying strings. It showed us how we can multiply not only numbers, but also strings. However, we still can't multiply a number by a string properly. There has been one attempt to do so but this is obviously wrong. We need to fix that!
Your Task:
Write a function or program that multiplies two inputs, a string and an integer. To (properly) multiply an string by an integer, you split the string into characters, repeat each character a number of times equal to the integer, and then stick the characters back together. If the integer is negative, we use its absolute value in the first step, and then reverse the string. If the input is 0, output nothing (anything multiplied by 0 equals nothing).
Input:
A string that consists solely of printable ASCII characters and newlines, and an integer (possible negative).
Output:
The string multiplied by the integer.
Examples:
Hello World!, 3 --> HHHeeellllllooo WWWooorrrlllddd!!!
foo, 12 --> ffffffffffffoooooooooooooooooooooooo
String, -3 --> gggnnniiirrrtttSSS
This is a fun challenge, 0 -->
Hello
World!, 2 --> HHeelllloo
WWoorrlldd!!
Scoring:
This is code-golf, lowest byte count wins!
4Can we assume the string is printable ASCII-only, plus newlines? – mbomb007 – 2017-07-11T21:45:57.267
Can we output a list of strings? – totallyhuman – 2017-07-11T21:50:15.280
Partial solution in Retina. Only works for positive values of the integer. I probably won't make time to finish it if someone wants to. https://tio.run/##K0otycxL/P8/Tk@bS0WLqzhBw97G9tA2PS1NLk6u6oQ4Qy6uWohgnCFQkFNDDyihYqhiyBWTEHdoWw0n1///RlweqTk5@Vzh@UU5KYoA
– mbomb007 – 2017-07-11T21:59:26.153@mbomb007, yes, sorry for taking so long about that. – Gryphon – 2017-08-02T00:08:13.717
@totallyhuman, no you may not. – Gryphon – 2017-08-08T03:52:35.180