12
\$\begingroup\$

Help! My device malfunctions and whenever I try to repeat a String, I get a messy results. Instead of repeating the same string N times, it fills an NxN square with each of its characters, and stacks the squares up.

For example, given the String "Test" and the number 2, instead of "TestTest", I get:

TT
TT
ee
ee
ss
ss
tt
tt

After I have seen this for a while, I started to like it. Your task today is to reproduce this strange behaviour. Given a non-empty string that consists of printable ASCII only, and a positive integer, output the String my malfunctioning device returns.

  • All standard rules apply.

  • The input and output may be handled through any reasonable mean.

  • This is , so the shortest code in bytes in each language wins.


Test Cases

Input 
Output

----------

"Test", 2

TT
TT
ee
ee
ss
ss
tt
tt

----------

"UuU", 3

UUU
UUU
UUU
uuu
uuu
uuu
UUU
UUU
UUU

----------

"A", 5

AAAAA
AAAAA
AAAAA
AAAAA
AAAAA

----------

You can find a larger test case here. Good luck and have fun golfing!

\$\endgroup\$
5
  • \$\begingroup\$ Borderline duplicate of Enlarge ASCII art \$\endgroup\$ Commented Sep 5, 2017 at 19:52
  • 1
    \$\begingroup\$ Is a "list of lines" instead of a string separated by newlines valid? \$\endgroup\$
    – geokavel
    Commented Sep 5, 2017 at 20:04
  • 26
    \$\begingroup\$ Hmm, I don't see how the "abuse its undefined behavior" from the title actually shows in the task. There is no undefined behavior, the task is to reproduce a quite specifically defined behavior. \$\endgroup\$ Commented Sep 5, 2017 at 21:24
  • 4
    \$\begingroup\$ it fills an NxN square - Not a correct statement. \$\endgroup\$ Commented Sep 6, 2017 at 19:30
  • \$\begingroup\$ Vyxal, 1 byte: * Only works on your machine. \$\endgroup\$ Commented Aug 16, 2023 at 18:32

36 Answers 36

1
2
0
\$\begingroup\$

C# (.NET Core), 68 + 18 bytes

a=>n=>new int[a.Length*n].Select((x,i)=>Enumerable.Repeat(a[i/n],n))

Also included in byte count:

using System.Linq;

Try it online!

Output is a collection of collections of characters (one collection for each line).

Explanation:

a => n =>                                // Take a string and a number
    new int[a.Length * n]                // Create new collection, 'n' times larger than 'a'
    .Select((x, i) =>                    // Replace every member with
        Enumerable.Repeat(a[i / n], n)   //     a collection of repeated character from 'a', based on index
    )
\$\endgroup\$
0
\$\begingroup\$

Clojure v1.8, 63 62 58 bytes

#(map(fn[l](dotimes[i %2](prn(apply str(repeat %2 l)))))%)

Try it online!

Explanation

(map ... s)           For every letter of the string 's'
(dotimes[i %2] ...)   Do 'n' times
(prn(apply str(repeat %2 l))) Repeat the letter 'n' times, combine to string and print
\$\endgroup\$
0
\$\begingroup\$

Bash, 49 bytes

sed s/./$(printf \&%.s `seq $[$1*$1]`)/g|fold -$1

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Python 2, 46 bytes

x,y=input()
for p in x:print'\n'.join([p*y]*y)

Try it online!

\$\endgroup\$
1
0
\$\begingroup\$

GNU APL, 11 bytes

⍪/⎕{⍺⍺⍴⍵}¨⍞

Left argument: number of repetitions. the string.

This uses to repeat each letter ( in the inner {} block) in ⍺x⍺ matrix.

Then we use ⍪/ to "laminate" them.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Which APL is this? If it works on GNU APL, you can make ⍺ ⍺ ⍺⍺. Also, it might be possible to make it ⍪/⎕{⍺⍺⍴⍵}¨⍞ by receiving input from STDIN (on GNU APL). (Fair warning, I haven't used gnu apl in a while. so this might be wrong.) \$\endgroup\$
    – Adalynn
    Commented Sep 9, 2017 at 12:54
  • \$\begingroup\$ Yeah, I just verified, ⍪/⎕{⍺⍺⍴⍵}¨⍞ does work on GNU APL. \$\endgroup\$
    – Adalynn
    Commented Sep 9, 2017 at 13:07
  • 1
    \$\begingroup\$ ⍪/⎕{⍺⍺⍴⍵}¨⍞ also works for 11 bytes, receiving input through STDIN (a line of text, then the number) \$\endgroup\$
    – Adalynn
    Commented Sep 9, 2017 at 17:18
0
\$\begingroup\$

MathGolf, 8 bytes

*mÅn+m*~

String input as a list of characters.

Try it online.

Explanation:

*        # Repeat each character in the second (implicit) input-list the first
         # (implicit) input-integer amount of times
 mÅn+    # Append a newline to each inner string:
 m       #  Map over each string,
  Å      #  using the following two characters as inner code-block:
   n     #   Push a newline character
    +    #   And append it to the string
     m*  # Repeat each inner string the first (implicit) input-integer amount of times
       ~ # Dump this list of lines with trailing newlines to the stack
         # (after which the entire stack is joined and output implicitly as result)
\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.