71
\$\begingroup\$

In this challenge, you must take multiline ASCII art as input, such as:

 OOOOOO  OOOOOO  OOOOOO  OOOOOOO      OOOOOO   OOOOOO  OO      OOOOOOO 
OO      OO    OO OO   OO OO          OO       OO    OO OO      OO      
OO      OO    OO OO   OO OOOOO       OO   OOO OO    OO OO      OOOOO   
OO      OO    OO OO   OO OO          OO    OO OO    OO OO      OO      
 OOOOOO  OOOOOO  OOOOOO  OOOOOOO      OOOOOO   OOOOOO  OOOOOOO OO      

And you will also take an integer as input. You must output the ASCII art enlarged by the amount specified with the integer. For example, if you used a second argument of 3, the output would be

   OOOOOOOOOOOOOOOOOO      OOOOOOOOOOOOOOOOOO      OOOOOOOOOOOOOOOOOO      OOOOOOOOOOOOOOOOOOOOO                  OOOOOOOOOOOOOOOOOO         OOOOOOOOOOOOOOOOOO      OOOOOO                  OOOOOOOOOOOOOOOOOOOOO
   OOOOOOOOOOOOOOOOOO      OOOOOOOOOOOOOOOOOO      OOOOOOOOOOOOOOOOOO      OOOOOOOOOOOOOOOOOOOOO                  OOOOOOOOOOOOOOOOOO         OOOOOOOOOOOOOOOOOO      OOOOOO                  OOOOOOOOOOOOOOOOOOOOO
   OOOOOOOOOOOOOOOOOO      OOOOOOOOOOOOOOOOOO      OOOOOOOOOOOOOOOOOO      OOOOOOOOOOOOOOOOOOOOO                  OOOOOOOOOOOOOOOOOO         OOOOOOOOOOOOOOOOOO      OOOOOO                  OOOOOOOOOOOOOOOOOOOOO
OOOOOO                  OOOOOO            OOOOOO   OOOOOO         OOOOOO   OOOOOO                              OOOOOO                     OOOOOO            OOOOOO   OOOOOO                  OOOOOO
OOOOOO                  OOOOOO            OOOOOO   OOOOOO         OOOOOO   OOOOOO                              OOOOOO                     OOOOOO            OOOOOO   OOOOOO                  OOOOOO
OOOOOO                  OOOOOO            OOOOOO   OOOOOO         OOOOOO   OOOOOO                              OOOOOO                     OOOOOO            OOOOOO   OOOOOO                  OOOOOO
OOOOOO                  OOOOOO            OOOOOO   OOOOOO         OOOOOO   OOOOOOOOOOOOOOO                     OOOOOO         OOOOOOOOO   OOOOOO            OOOOOO   OOOOOO                  OOOOOOOOOOOOOOO
OOOOOO                  OOOOOO            OOOOOO   OOOOOO         OOOOOO   OOOOOOOOOOOOOOO                     OOOOOO         OOOOOOOOO   OOOOOO            OOOOOO   OOOOOO                  OOOOOOOOOOOOOOO
OOOOOO                  OOOOOO            OOOOOO   OOOOOO         OOOOOO   OOOOOOOOOOOOOOO                     OOOOOO         OOOOOOOOO   OOOOOO            OOOOOO   OOOOOO                  OOOOOOOOOOOOOOO
OOOOOO                  OOOOOO            OOOOOO   OOOOOO         OOOOOO   OOOOOO                              OOOOOO            OOOOOO   OOOOOO            OOOOOO   OOOOOO                  OOOOOO
OOOOOO                  OOOOOO            OOOOOO   OOOOOO         OOOOOO   OOOOOO                              OOOOOO            OOOOOO   OOOOOO            OOOOOO   OOOOOO                  OOOOOO
OOOOOO                  OOOOOO            OOOOOO   OOOOOO         OOOOOO   OOOOOO                              OOOOOO            OOOOOO   OOOOOO            OOOOOO   OOOOOO                  OOOOOO
   OOOOOOOOOOOOOOOOOO      OOOOOOOOOOOOOOOOOO      OOOOOOOOOOOOOOOOOO      OOOOOOOOOOOOOOOOOOOOO                  OOOOOOOOOOOOOOOOOO         OOOOOOOOOOOOOOOOOO      OOOOOOOOOOOOOOOOOOOOO   OOOOOO
   OOOOOOOOOOOOOOOOOO      OOOOOOOOOOOOOOOOOO      OOOOOOOOOOOOOOOOOO      OOOOOOOOOOOOOOOOOOOOO                  OOOOOOOOOOOOOOOOOO         OOOOOOOOOOOOOOOOOO      OOOOOOOOOOOOOOOOOOOOO   OOOOOO
   OOOOOOOOOOOOOOOOOO      OOOOOOOOOOOOOOOOOO      OOOOOOOOOOOOOOOOOO      OOOOOOOOOOOOOOOOOOOOO                  OOOOOOOOOOOOOOOOOO         OOOOOOOOOOOOOOOOOO      OOOOOOOOOOOOOOOOOOOOO   OOOOOO

Specifically, each character must turn into an n by n box of that character, where n is the integer argument. For example, an input of

ab
cd

and 3 will result in

aaabbb
aaabbb
aaabbb
cccddd
cccddd
cccddd

This is , so the shortest code in bytes wins.

\$\endgroup\$
3
  • \$\begingroup\$ ascii art looks interesting on the questions list \$\endgroup\$
    – Justin
    Commented Jan 27, 2014 at 16:49
  • 7
    \$\begingroup\$ I think you should make a secondary/bonus round for a method that introduces proper aliasing into the enlarged art. Your giant code golf art is looking pretty blocky. \$\endgroup\$
    – AmeliaBR
    Commented Jan 28, 2014 at 1:58
  • 5
    \$\begingroup\$ @AmeliaBR See here. \$\endgroup\$
    – Howard
    Commented Jan 28, 2014 at 14:22

39 Answers 39

41
\$\begingroup\$

APL, 7 chars/bytes*

{⍺/⍺⌿⍵}

Function that takes the number and input string as parameters and returns the result:

      a
abcde
fghij
      2 {⍺/⍺⌿⍵} a
aabbccddee
aabbccddee
ffgghhiijj
ffgghhiijj
      3 {⍺/⍺⌿⍵} a
aaabbbcccdddeee
aaabbbcccdddeee
aaabbbcccdddeee
fffggghhhiiijjj
fffggghhhiiijjj
fffggghhhiiijjj

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
*: APL can be written in its own (legacy) single-byte charset that maps APL symbols to the upper 128 byte values. Therefore, for the purpose of scoring, a program of N chars that only uses ASCII characters and APL symbols can be considered to be N bytes long.

\$\endgroup\$
10
  • 1
    \$\begingroup\$ ... :O How does it work? \$\endgroup\$
    – Doorknob
    Commented Jan 27, 2014 at 21:44
  • 5
    \$\begingroup\$ @DoorknobofSnow The standard representation for a multiline string is APL is a rectangular character matrix (with trailing blanks if the lines have uneven width… yes, APL is that old.) The / function (not to be confused with the / operator… yes, same symbol) duplicates and/or removes columns from a matrix according to the left-side argument. If that is a scalar (simple number) then it's replicated for all input columns. Therefore 2/m is a matrix with all columns doubled. The variant does the same, but for rows (or first-axis planes in the general case.) \$\endgroup\$
    – Tobia
    Commented Jan 27, 2014 at 21:50
  • 6
    \$\begingroup\$ Beautiful language, isn't it? It's actually pretty readable, once you learn a few dozen symbols. Much more so than the ASCII line-noise languages you see around here… \$\endgroup\$
    – Tobia
    Commented Jan 27, 2014 at 21:58
  • \$\begingroup\$ Cool. Was ⌿ always in the language? It wasn't in the Gilman and Rose book, and an Iverson book on APL\360 mentions /[1] but not ⌿ . \$\endgroup\$ Commented Jan 28, 2014 at 9:35
  • 2
    \$\begingroup\$ Wow. That. Is. Just. Awesome. I. Am. Severely. Awestruck. By. This. Code. I would like to offer more than a mere +1, but my rep is quite low for a bounty yet. \$\endgroup\$ Commented Sep 29, 2016 at 12:07
35
\$\begingroup\$

GolfScript, 20 characters

n%(~{.{*}+@1/%n+*}+/

Takes all input from STDIN, first line is the scaling factor, the rest the multiline-input. You can try the example online.

Input

3
ab
cd

Output

aaabbb
aaabbb
aaabbb
cccddd
cccddd
cccddd

Code

n%            # Split the input into separate lines
(~            # Take the first line and evaluate (factor)
{             # {...}+/ adds the factor to the code block and loops over
              # the remaining lines (i.e. the factor will be the top of stack
              # for each line of input
   .{         # Same thing, duplicate factor and add it to code block and
              # loop over each character (1/ splits the string into chars)
     *        # multiply single-char string with factor
   }+@1/%
   n+         # Join string by adding a newline
   *          # Multiply the line by the factor (left from the . operation)
}+/
\$\endgroup\$
7
  • \$\begingroup\$ Hey! You've got a better input format, score and time than me :-/ +1 \$\endgroup\$ Commented Jan 27, 2014 at 6:14
  • \$\begingroup\$ Also, I'm kinda lost reading it. \$\endgroup\$ Commented Jan 27, 2014 at 6:17
  • 2
    \$\begingroup\$ @JanDvorak Added some explanation. \$\endgroup\$
    – Howard
    Commented Jan 27, 2014 at 6:22
  • \$\begingroup\$ Wow. Today I've learned 1/. I also need to use value function + more. \$\endgroup\$ Commented Jan 27, 2014 at 6:41
  • 2
    \$\begingroup\$ @CoryKlein: It would kind of help if you told us what error, but... *gazes into crystal ball* ...it may be that golfscript.rb is not marked executable, or that the first line has the wrong path to the Ruby interpreter for your system. Oh, and that echo should presumably be cat. Try cat inputFile | ruby golfscript.rb scriptFile (or just ruby golfscript.rb scriptFile < inputFile) and see if that works. \$\endgroup\$ Commented Jan 28, 2014 at 14:00
20
\$\begingroup\$

J, 20 17 characters

f=.([#&.|:#)];._2

Defines a verb f that does what is required. Usage:

   3 f a
aaaccc
aaaccc
aaaccc
bbbddd
bbbddd
bbbddd

where a is the input string.
On Windows an extra character is required to remove the \r:

f=.([#&.|:#)}:;._2

Explanation:

];._2 chops the input string into chunks based on the last character of the string, which in this case will be a \n. Windows has \r\n so we need to use }: to chop an extra character off: }:;._2. Cut verb documentation

The rest of the code (with the exception of the assignment f=.) is a fork.
It breaks down like this: [ #&.|: #

If a is our input string the calculation will be 3 # a (we'll call this result x), then 3 [ a (we'll call this result y) then y #&.|: x.

3 # a just makes three copies of every member of a. Copy verb documentation
This turns

ab
cd

into

aaabbb
cccddd

3 [ a just returns 3. Left verb documentation

Finally y #&.|: x is y copy under transpose x. The # works as before, but the &.|: tells J to transpose the input first and then transpose it back when it's finished. Under conjunction documentation, transpose verb documentation.

The transpose turns

aaabbb
cccddd

into

ac
ac
ac
bd
bd
bd

then the copy changes it to

aaaccc
aaaccc
aaaccc
bbbddd
bbbddd
bbbddd

and transposing it back gives you

aaabbb
aaabbb
aaabbb
cccddd
cccddd
cccddd
\$\endgroup\$
3
  • 4
    \$\begingroup\$ Windows sucks, eh? :P \$\endgroup\$
    – cjfaure
    Commented Jan 27, 2014 at 11:20
  • \$\begingroup\$ @Trimsty Indeed it does. \$\endgroup\$
    – Gareth
    Commented Jan 27, 2014 at 11:30
  • 1
    \$\begingroup\$ This is shorter: [##"1 Also, I'm not sure about the unwritten rules of golfing in J, but I'd say the string splitting and the assignment could be omitted, given the question's loose requirements. I certainly did so in my APL answer. Example: 2 ([##"1) 'abcde',:'fghij' \$\endgroup\$
    – Tobia
    Commented Jan 21, 2015 at 22:41
17
\$\begingroup\$

Haskell, 49 bytes

x=(=<<).replicate
e n=unlines.x n.map(x n).lines

The enlarge function is e, which takes a count and a string, and returns a string:

λ: putStrLn $ e 3 "ab\ncd\n"
aaabbb
aaabbb
aaabbb
cccddd
cccddd
cccddd
\$\endgroup\$
15
\$\begingroup\$

APL, 11 chars

@Gareth did essentially the same thing first, in J, so this APL entry is just for exhibition, not competition - he's the winner.

      E←{⍉⍺/⍉⍺/⍵}

Usage: magnification in left arg (⍺), art in the form of a 2d character matrix in right arg (⍵).
⍺/⍵ will replicate elements across each row of a vector or matrix (2/'O O' becomes 'OO OO').
⍉⍺/⍉ will transpose that, replicate the elements, transpose that.

(If we were going for clarity instead of program length, the code could have been E←{⍺/[1]⍺/⍵} )

      I←5 32⍴' OOOOOO  OOOOOO  OOOOOO  OOOOOOOOO      OO    OO OO   OO OO     OO      OO    OO OO   OO OOOOO  OO      OO    OO OO   OO OO      OOOOOO  OOOOOO  OOOOOO  OOOOOOO'
      I
 OOOOOO  OOOOOO  OOOOOO  OOOOOOO
OO      OO    OO OO   OO OO     
OO      OO    OO OO   OO OOOOO  
OO      OO    OO OO   OO OO     
 OOOOOO  OOOOOO  OOOOOO  OOOOOOO

      3 E I
   OOOOOOOOOOOOOOOOOO      OOOOOOOOOOOOOOOOOO      OOOOOOOOOOOOOOOOOO      OOOOOOOOOOOOOOOOOOOOO
   OOOOOOOOOOOOOOOOOO      OOOOOOOOOOOOOOOOOO      OOOOOOOOOOOOOOOOOO      OOOOOOOOOOOOOOOOOOOOO
   OOOOOOOOOOOOOOOOOO      OOOOOOOOOOOOOOOOOO      OOOOOOOOOOOOOOOOOO      OOOOOOOOOOOOOOOOOOOOO
OOOOOO                  OOOOOO            OOOOOO   OOOOOO         OOOOOO   OOOOOO               
OOOOOO                  OOOOOO            OOOOOO   OOOOOO         OOOOOO   OOOOOO               
OOOOOO                  OOOOOO            OOOOOO   OOOOOO         OOOOOO   OOOOOO               
OOOOOO                  OOOOOO            OOOOOO   OOOOOO         OOOOOO   OOOOOOOOOOOOOOO      
OOOOOO                  OOOOOO            OOOOOO   OOOOOO         OOOOOO   OOOOOOOOOOOOOOO      
OOOOOO                  OOOOOO            OOOOOO   OOOOOO         OOOOOO   OOOOOOOOOOOOOOO      
OOOOOO                  OOOOOO            OOOOOO   OOOOOO         OOOOOO   OOOOOO               
OOOOOO                  OOOOOO            OOOOOO   OOOOOO         OOOOOO   OOOOOO               
OOOOOO                  OOOOOO            OOOOOO   OOOOOO         OOOOOO   OOOOOO               
   OOOOOOOOOOOOOOOOOO      OOOOOOOOOOOOOOOOOO      OOOOOOOOOOOOOOOOOO      OOOOOOOOOOOOOOOOOOOOO
   OOOOOOOOOOOOOOOOOO      OOOOOOOOOOOOOOOOOO      OOOOOOOOOOOOOOOOOO      OOOOOOOOOOOOOOOOOOOOO
   OOOOOOOOOOOOOOOOOO      OOOOOOOOOOOOOOOOOO      OOOOOOOOOOOOOOOOOO      OOOOOOOOOOOOOOOOOOOOO
\$\endgroup\$
5
  • \$\begingroup\$ I should point out though, that I use 5 chars turning the string input into a character array, so you only beat me by 1 character really. :-) \$\endgroup\$
    – Gareth
    Commented Jan 27, 2014 at 16:28
  • 2
    \$\begingroup\$ J is hobbled by that pesky design goal in which operators are restricted to the ASCII character set. :) \$\endgroup\$ Commented Jan 27, 2014 at 16:59
  • 2
    \$\begingroup\$ Though, if we measure in bytes like the OP said, this is 44 bytes (implying unicode of course, not utf-8) and @Gareth's is still 17. That's the downside to languages that aren't restricted to ASCII. \$\endgroup\$
    – cjfaure
    Commented Jan 27, 2014 at 19:43
  • 5
    \$\begingroup\$ @Trimsty But APL has it's own character set which maps each character to a single byte, so arguably this is still 11 bytes. \$\endgroup\$
    – Volatility
    Commented Jan 27, 2014 at 23:03
  • 1
    \$\begingroup\$ @Volatility Hm. That's really weird, sorry, didn't know :P \$\endgroup\$
    – cjfaure
    Commented Jan 28, 2014 at 8:51
14
\$\begingroup\$

Bash/sed script, 48 chars

printf -vr %$1s
sed -n s/./${r// /&}/g${r// /;p}

Save as a script, chmod +x and run:

$ ./asciiexpand 3 <<< $'ab\ncd'
aaabbb
aaabbb
aaabbb
cccddd
cccddd
cccddd
$ 

How does it work?

The meat of it is in the sed command. For example, if n=3, then the sed command is expanded to something like:

sed -n 's/./&&&/g;p;p;p'

This compound sed command can be broken up into:

  • s/./&&&/g - this substitute command matches each character and replaces it with the character repeated 3 times
  • 3x p commands - this just prints the whole pattern space (i.e. the current line) 3 times

We pass -n to sed to tell it not to print anything unless explicitly told to, so we have full control of what is printed and how many times.

I couldn't figure out a quick way to generate arbitrary-length repeating strings directly in sed, so I used some bash tricks instead:

printf -vr "%3s"

This prints a string (not specified, i.e. empty), with 3 leading spaces, and assigns the result to the bash variable r.

We then use bash parameter expansion to transform this string of spaces to what we need substituted into the sed expression:

$ echo ${r// /&}
&&&
$ echo ${r// /;p}
;p;p;p
$ 

I was able to get away with removing quotes around the printf format specifier and the sed expression, as none of the characters within need escaping in the bash shell.

Character count:

$ submission='r=`printf %$1s`
> sed -n s/./${r// /&}/g${r// /;p}'
$ echo ${#submission}
48
$ 
\$\endgroup\$
9
  • \$\begingroup\$ I don't have an idea how this works but it works! \$\endgroup\$
    – Tomas
    Commented Jan 27, 2014 at 9:57
  • 1
    \$\begingroup\$ Variable n violates rule 1: “may NOT be hardcoded”. \$\endgroup\$
    – manatwork
    Commented Jan 27, 2014 at 10:32
  • 1
    \$\begingroup\$ Just use $1 and remove n=3;, you will even save 4 characters and maybe the {} in the printf too. \$\endgroup\$
    – Tomas
    Commented Jan 27, 2014 at 11:14
  • \$\begingroup\$ And as we can assume $1 will be numeric, the quotes around printf's argument are not necessary, reducing it to 52 characters: r=`printf %$1s`;sed -n "s/./${r// /&}/g${r// /;//p}". \$\endgroup\$
    – manatwork
    Commented Jan 27, 2014 at 13:42
  • 1
    \$\begingroup\$ @Tomas - You've probably figured out how it works by now, but I added a description in case you're interested. \$\endgroup\$ Commented Jan 28, 2014 at 0:44
9
\$\begingroup\$

PERL,  41 39  25   22 chars

PERL, simple and effective - created right for the task. When invoked with -pi3, where 3 is the parameter n:

s/./$&x$^I/ge;$_ x=$^I

Classical solution (39 chars):

$n=<>;print map{s/./$&x$n/ge;"$_"x$n}<>

Classical solution needs the n is specified in the first line of input, e.g.

3
ab
cd

Thanks @manatwork for the $& trick.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Cool. But you not need to capture the matched characters: s/./$&x$n/ge. \$\endgroup\$
    – manatwork
    Commented Jan 27, 2014 at 10:27
8
\$\begingroup\$

Ruby:  64  49 characters

All data received on STDIN: first line the scaling factor, then the ASCII art.

BEGIN{n=gets.to_i}
$_=[$_.gsub(/./){$&*n}]*n*$/

Sample run:

bash-4.2$ ruby -lpe 'BEGIN{n=gets.to_i};$_=[$_.gsub(/./){$&*n}]*n*$/' <<< '3
> ab
> cd'
aaabbb
aaabbb
aaabbb
cccddd
cccddd
cccddd

Ruby:  44  41 characters

Assuming that every input line is terminated with line separator. Thanks to @Ventero.

$.<2?n=$_.to_i: $><<$_.gsub(/./){$&*n}*n

Sample run:

bash-4.2$ ruby -ne '$.<2?n=$_.to_i: $><<$_.gsub(/./){$&*n}*n' <<< '3
> ab
> cd'
aaabbb
aaabbb
aaabbb
cccddd
cccddd
cccddd
\$\endgroup\$
2
  • 1
    \$\begingroup\$ A shorter way to set n: $.<2?n=$_.to_i: $><<$_.gsub(/./){$&*n}*n (sadly, the space after the colon is necessary, otherwise the parser gets confused). Run with ruby -n. \$\endgroup\$
    – Ventero
    Commented Jan 28, 2014 at 18:09
  • \$\begingroup\$ Doh. I tried dozens of approaches to shorten the assignment to n, but I missed the ternary operator. Thank you, @Ventero. \$\endgroup\$
    – manatwork
    Commented Jan 29, 2014 at 8:45
7
\$\begingroup\$

Python 3 - 84

Not the shortest but a different answer nonetheless. An interesting one-liner.

n=int(input())
for i in eval(input()):print(((''.join(j*n for j in i)+'\n')*n)[:-1])

Put in a number first, then the ASCII art as a Python list, e.g.:

C:\Users\User4\Desktop>c:/python33/python.exe golf.py
3
["aabb","bbcc","ccdd"]
aaaaaabbbbbb
aaaaaabbbbbb
aaaaaabbbbbb
bbbbbbcccccc
bbbbbbcccccc
bbbbbbcccccc
ccccccdddddd
ccccccdddddd
ccccccdddddd
\$\endgroup\$
5
\$\begingroup\$

GolfScript, 29 characters

{:i;n/{1/:c;{c{i*}/n}i*}/}:f;

This defines a block f, that when called will produce the desired output. It assumes arguments are on the stack (because that's basically how arguments are passed in GolfScript).

Ungolfed (does that even make sense? :P):

{:i;n/{ # for each line...
  1/:c; # save characters of string in c
  {     # i times...
    c{  # for each character...
      i*
    }/
    n
  }i*
}/}:f;


# Test run
"ab
cd" 3 f
\$\endgroup\$
5
\$\begingroup\$

Golfscript, 23 characters

~(:i;n/{{{.}i*}:c%c}%n*

I've decided to write an entire program because it has less overhead than even an anonymous function does:

  • ~(:i; - eval the input, then decrement the multiplier, store it as i and discard.
  • n/{...}%n* - split by newlines, map each line, join by newlines
    • {...}:c%c, - take a block, apply it on map-each element, then apply it to the whole line.
      • {.}i* - duplicate this element i times

Live demo: http://golfscript.apphb.com/?c=OyciYWJjCmRlZiIzJwp%2BKDppO24ve3t7Ln1pKn06YyVjfSVuKg%3D%3D

Example usage:

;'"abc
def"3'
~(:i;n/{{{.}i*}:c%c}%n*
\$\endgroup\$
5
\$\begingroup\$

Python 3 - 109 107 93

i=input;n,s,a=int(i()),i(),[]
while s:a+=[''.join(c*n for c in s)]*n;s=i()
print('\n'.join(a))

First, input the number, then the string. Should be self-explanatory...

Thanks to Waleed Khan for suggesting removing the []

Thanks to Volatility for suggesting having a be a list.

\$\endgroup\$
8
  • 7
    \$\begingroup\$ Just noticed; the nsa is watching your every input \$\endgroup\$
    – Justin
    Commented Jan 27, 2014 at 4:59
  • \$\begingroup\$ Shortest code in bytes. Wouldn't that then include newlines? Shouldn't your count be 112? \$\endgroup\$
    – cjfaure
    Commented Jan 27, 2014 at 10:55
  • \$\begingroup\$ Sorry, I meant 111. \$\endgroup\$
    – cjfaure
    Commented Jan 27, 2014 at 11:48
  • \$\begingroup\$ @Trimsty you are right; I thought wordcounter.net counted newlines. \$\endgroup\$
    – Justin
    Commented Jan 27, 2014 at 16:36
  • 2
    \$\begingroup\$ Make a into a list, do a+=[''.join(c*n for c in s)]*n, then do print('\n'.join(a)) at the end. That should work. \$\endgroup\$
    – Volatility
    Commented Jan 27, 2014 at 20:45
5
\$\begingroup\$

Java - 217

First try at golfing. Seems like Java isn't the language to do it in.

enum M{;public static void main(String[]a){int f=new Integer(a[0]),i=0,j,k;a=a[1].split("\n");for(;++i<a.length*f;System.out.println())for(j=0;++j<a[i/f].length();)for(k=0;k++<f;)System.out.print(a[i/f].charAt(j));}}

On Windows you need to replace "\n" with "\r\n".

java M 3 "ab
cd"
aaabbb
aaabbb
aaabbb
cccddd
cccddd
cccddd
\$\endgroup\$
12
  • 2
    \$\begingroup\$ The input characters need to expand downwards as well as across (so there should be 3 lines of aaabbb and 3 lines of cccddd in your output example above). \$\endgroup\$
    – Gareth
    Commented Jan 27, 2014 at 15:01
  • 2
    \$\begingroup\$ You can spare: 1 character by initializing i in the declaration instead of the for; 8 characters by using a instead of x (a=a[1].split("\n"), and change all x with a). \$\endgroup\$
    – manatwork
    Commented Jan 27, 2014 at 15:24
  • 3
    \$\begingroup\$ Yeah, with any language where just the declaration of main takes more bytes than entire programs in other languages, contestants are going to be getting bogeys. \$\endgroup\$ Commented Jan 28, 2014 at 0:39
  • 2
    \$\begingroup\$ Hm ok. Here is 3 chars saved by rearranging the outter for loop a little bit: enum M{;public static void main(String[]a){int f=Integer.valueOf(a[0]),i=-1,j,k;a=a[1].split("\n");for(;++i<a.length*f;System.out.println())for(j=0;j<a[i/f].length();j++)for(k=0;k++<f;)System.out.print(a[i/f].charAt(j));}} \$\endgroup\$
    – daniero
    Commented Jan 28, 2014 at 13:25
  • 3
    \$\begingroup\$ Another 4 bytes: Integer.valueOf(a[0]) -> new Integer(a[0]) \$\endgroup\$ Commented Jan 28, 2014 at 15:17
5
\$\begingroup\$

(Edit: This solution is now invalid because the semantics of the instruction has changed. I didn’t realise that I’d already made use of the instruction when I changed it. You can, however, fix ths program by simply changing it to the newer instruction .)

Sclipting, 19 characters

Expects the input to be separated by \n (no \r) and the first line to contain the multiplying factor.

겠坼銻標⑴가殲各標⓶各①復終겠併①復終

Explanation

겠坼 | split everything at \n
銻 | get first element (the factor)
標 | mark
⑴가殲 | bring list to front; remove first element (the factor)
各 | for each line...
    標⓶ | mark
    各①復終 | for each character, multiply it
    겠併 | add a newline and concatenate everything above the mark
    ①復 | multiply that
終 | end of loop

At the end, the stack will look like this:

[ factor, mark, line1, line2, line3, ... ]

Everything above the mark is automatically concatenated and output, the rest discarded.

\$\endgroup\$
7
  • \$\begingroup\$ 这太有意思了!你是原创者对吧? \$\endgroup\$ Commented Jan 28, 2014 at 21:50
  • \$\begingroup\$ @theGreenCabbage 对了. 踢木外 是 这个 电脑语言 的 创始人 \$\endgroup\$
    – Justin
    Commented Jan 29, 2014 at 7:09
  • 1
    \$\begingroup\$ @Timwi sorry for calling you 踢木外 (kick wood outside, pronounced tea-moo-why)... \$\endgroup\$
    – Justin
    Commented Jan 29, 2014 at 7:13
  • \$\begingroup\$ @Quincunx: Hehe, I found it funny :) But the final i is pronounced [iː], not [ɑi]; how would you write it in Chinese now? \$\endgroup\$
    – Timwi
    Commented Jan 30, 2014 at 14:09
  • \$\begingroup\$ @Timwi Thought that's how it is pronounced (but I couldn't think of a corresponding character). I wrote by searching and copy-pasting (on mdbg.net/chindict/chindict.php) \$\endgroup\$
    – Justin
    Commented Jan 30, 2014 at 18:29
3
\$\begingroup\$

J, 7 chars

([#"1#)

Exact same thing as in @Tobia's APL answer, but in ascii chars.

a =. ' 0 ',.'0 0',.' 0 '
a
 0 
0 0
 0 

2 ([#"1#) a
  00  
  00  
00  00
00  00
  00  
  00  
\$\endgroup\$
3
\$\begingroup\$

Powershell (96)

function m($n,$s){$s.split("`n")|%{$l="";$_.ToCharArray()|%{$c=$_;1..$n|%{$l+=$c}};1..$n|%{$l}}}

Thought I'd give this a shot in PS. Using piping into foreach's (%) to keep it short here, but the function and ToCharArray are making it take a hit it brevity.

To use it, you call it from the command line like this:

PS C:\> m 3 "ab
>> cd"
>>
aaabbb
aaabbb
aaabbb
cccddd
cccddd
cccddd

Here's the non-minimised version:

function f($n, $s)
{
    $s.split("`n") | % {            # split input into separate lines
        $l=""                       # initialize an output line
        $_.ToCharArray() | % {      # split line into chars
            $c=$_ ; 1..$n | % {     # add n chars to the line
                $l+=$c 
            } 
        }
        1..$n | % {$l}              # output the line n times
    }
}
\$\endgroup\$
1
  • 2
    \$\begingroup\$ Mazzy suggested function m($n,$s){-split$s|%{,(-join($_-split''|%{$_*$n}))*$n}} for 63 bytes \$\endgroup\$
    – ovs
    Commented Jun 4, 2018 at 9:36
2
\$\begingroup\$

Julia, 74 chars/bytes

julia> f(i,n)=print(prod([(prod(["$c"^n for c in l])*"\n")^3 for l in split(i)]))
f (generic function with 1 method)

julia> f("ab\ncd",3)
aaabbb
aaabbb
aaabbb
cccddd
cccddd
cccddd

7 less if I just return the string. julia> is the interactive prompt.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ This is the first Julia golf I've seen. \$\endgroup\$
    – cjfaure
    Commented Jan 28, 2014 at 8:51
  • \$\begingroup\$ @Trimsty codegolf.stackexchange.com/search?q=julia still very rare, but they have been a few (including by one of the designers of the language actually). \$\endgroup\$
    – plannapus
    Commented Jan 28, 2014 at 15:14
  • \$\begingroup\$ @plannapus Interesting! There's a new, rare esolang called ~-~! that I've used as well, though it's terrible for golfing. \$\endgroup\$
    – cjfaure
    Commented Jan 29, 2014 at 17:07
2
\$\begingroup\$

Canvas, 2 bytes

:*

Try it here!

\$\endgroup\$
2
\$\begingroup\$

Powershell, 54 bytes

param($s,$n)$s-replace'.',('$0'*$n)-split'
'|%{,$_*$n}

Test script:

$f = {

param($s,$n)$s-replace'.',('$0'*$n)-split'
'|%{,$_*$n}

}

@(
@"
ab
cd
"@
,
@"
 OOOOOO  OOOOOO  OOOOOO  OOOOOOO      OOOOOO   OOOOOO  OO      OOOOOOO
OO      OO    OO OO   OO OO          OO       OO    OO OO      OO
OO      OO    OO OO   OO OOOOO       OO   OOO OO    OO OO      OOOOO
OO      OO    OO OO   OO OO          OO    OO OO    OO OO      OO
 OOOOOO  OOOOOO  OOOOOO  OOOOOOO      OOOOOO   OOOOOO  OOOOOOO OO
"@
) | % {
    &$f $_ 3
}

Explanation:

  • $s-replace'.',('$0'*$n) repeats each symbol exept new line.
  • -split'``n' splits the wide string by new line
  • |%{,$_*$n} repeats each line as an array element and returns array

The result is an array of wide and repeated strings.

\$\endgroup\$
2
\$\begingroup\$

R, 83/72 bytes

Alt approach using regexps

If we're allowed a trailing newline, 72 bytes:

function(s,n)cat(gsub("([^
]*
)",r,gsub("([^
])",r<-strrep("\\1",n),s)))

Try it online!

Otherwise, 83 bytes:

function(s,n)write(rep(el(strsplit(gsub("([^\n])",strrep("\\1",n),s),"\n")),e=n),1)

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Vyxal j, 3 bytes

•εf

Try it Online!

I/O is as lists of lines. The j flag formats the output into ASCII art.

•   # Repeat each character of each line by the number
 ε  # Repeat each line by the number
  f # Flatten
\$\endgroup\$
1
\$\begingroup\$

K, 20

.....

{,/y#',:',/'y#'/:x} 
\$\endgroup\$
1
\$\begingroup\$

Extended BrainFuck : 158

{a<]<[->+}3>,>3+[->4+[-<<4->>]&a>+<<]<+[-<,[>10+[-<->>+<]<[[->>+<<]3>[->[->+&a&a#3<+>>]>>]<[-]<<]>>[>[-4<[<]>[.>]>>.>]4<[-]<[-]4>[-]]>>[->+<]>[-<+<+>>]4<+<]>]

It turns into:

Brainfuck: 185

>>>,>+++[->++++[-<<---->>]<]<[->+>+<<]<+[-<,[>++++++++++[-<->>+<]<[[->>+<<]>>>[->[->+<]<[->+<]<[->+<<<+>>]>>]<[-]<<]>>[>[-<<<<[<]>[.>]>>.>]<<<<[-]<[-]>>>>[-]]>>[->+<]>[-<+<+>>]<<<<+<]>]

It requires an interpreter that either has 0 or no change as EOF marker. beef, available from Ubuntu repos, works swell:

$ ( echo -n 2
> cat <<eof
> BBBB  RRRR   AAA  IIII NN  NN FFFFF * * * KK  KK
> BB BB RR RR AA AA  II  NNN NN FF      *   KK  KK
> BBBB  RRRR  AAAAA  II  NNNNNN FFFF  ***** KKKKK
> BB BB RR RR AA AA  II  NN NNN FF      *   KK  KK
> BBBB  RR RR AA AA IIII NN  NN FF    * * * KK  KK
> eof
> ) | beef double.bf
BBBBBBBB    RRRRRRRR      AAAAAA    IIIIIIII  NNNN    NNNN  FFFFFFFFFF  **  **  **  KKKK    KKKK
BBBBBBBB    RRRRRRRR      AAAAAA    IIIIIIII  NNNN    NNNN  FFFFFFFFFF  **  **  **  KKKK    KKKK
BBBB  BBBB  RRRR  RRRR  AAAA  AAAA    IIII    NNNNNN  NNNN  FFFF            **      KKKK    KKKK
BBBB  BBBB  RRRR  RRRR  AAAA  AAAA    IIII    NNNNNN  NNNN  FFFF            **      KKKK    KKKK
BBBBBBBB    RRRRRRRR    AAAAAAAAAA    IIII    NNNNNNNNNNNN  FFFFFFFF    **********  KKKKKKKKKK
BBBBBBBB    RRRRRRRR    AAAAAAAAAA    IIII    NNNNNNNNNNNN  FFFFFFFF    **********  KKKKKKKKKK
BBBB  BBBB  RRRR  RRRR  AAAA  AAAA    IIII    NNNN  NNNNNN  FFFF            **      KKKK    KKKK
BBBB  BBBB  RRRR  RRRR  AAAA  AAAA    IIII    NNNN  NNNNNN  FFFF            **      KKKK    KKKK
BBBBBBBB    RRRR  RRRR  AAAA  AAAA  IIIIIIII  NNNN    NNNN  FFFF        **  **  **  KKKK    KKKK
BBBBBBBB    RRRR  RRRR  AAAA  AAAA  IIIIIIII  NNNN    NNNN  FFFF        **  **  **  KKKK    KKKK

Ungolfed EBF code:

;;;; Multiply 
;;;; Takes a digit x and ASCII art on STDIN
;;;; Prints ASCI art scaled x times
;;;; Usage:
;;;; bf ebf.bf < multiply.ebf >multiply.bf
;;;; bf multiply.bf

;;; Memory map for this program
:zero
:str
:flag
:tmp
:dup    
:num

;;; Macros
    
;; EOL support. Comment out the body of the two 
;; macros below to only support 0 and no change
;; Some interpreters use -1
{eol_start
  +(- 
}

{eol_end 
   )
}
    
;; macro that reads a digit.
;; the actual number is one more than input
;; ^12 uses 3 cells from origin
{read_number
  ,                    ; read char
  ^1 3+(-^2 4+(-^ 4-)) ; reduce by 3*4*4=48
}

;; duplicate current element
;; to the left using the right as tmp
{copy_left 
  (-^1+)^1(-^0+<+)
}

;; Duplicate char n times while
;; shifting to the right
{handle_char
  $str(-$tmp+)               ; copy the char to tmp
  $dup(-$num(->+)            ; for each dup move num
        $dup(-$num+)         ; and dup one step to the right
        $tmp(-$dup+$str+)    ; and copy the char back to str and to dup
        @flag)               ; which is the new tmp (it gets shifted to the right)
  $tmp(-)                    ; clear char when done
}

{handle_linefeed
  $dup(-                     ; for each dup
    $zero[<]>                ; go to beginnning of string
    [.>]@str                 ; print string 
    $tmp.                    ; print newline
  )
  $zero[-]<[-]>@zero         ; clean last two chars
  $tmp(-)                    ; remove line feed
}


;;; Main program 

;; read number
$tmp &read_number
$tmp (-$dup+$num+)
;$tmp,[-] ; uncomment to require a newline before asci art
$flag+(-
  $str = ,
  ( ;; NB! The line containing EOF will not be printed!
    &eol_start
    $flag 10+(-$str-$tmp+)
    if $str is not linefeed (   
       &handle_char
    ) $tmp ( linefeed
       &handle_linefeed
    ) 
    $num &copy_left      ; we need two copies of the duplicate dupber
    $flag+               ; flag to 
    &eol_end
  )  
)
\$\endgroup\$
1
\$\begingroup\$

Rebol - 87 79

r: do input while[d: input][forskip d r[insert/dup d d/1 r - 1]loop r[print d]]

Nicely formatted version:

r: do input

while [d: input] [
    forskip d r [insert/dup d d/1 r - 1]
    loop r [print d]
]

Usage example:

rebol -qw --do 'Rebol[]r: do input while[d: input][forskip d r[insert/dup d d/1 r - 1]loop r[print d]]' <<< "3
ab
cd"
aaabbb
aaabbb
aaabbb
cccddd
cccddd
cccddd

NB. This code only works in Rebol 2 at this moment (The INPUT function used is not fully implemented in Rebol 3 yet).

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

R, 125 bytes

function(s,n,x=utf8ToInt(s),m=matrix(x[u<-x!=10],,sum(!u)+1))for(i in 1:ncol(m))cat(rep(intToUtf8(rep(m[,i],e=n)),n),sep="
")

Try it online!

Converts string to UTF8, puts into a matrix whose columns are lines of text (removing newlines), and then repeats each char and the generated lines before printing separeated by newlines.

\$\endgroup\$
2
  • \$\begingroup\$ congrats on 1K by the way! :-) \$\endgroup\$
    – Giuseppe
    Commented Jun 5, 2018 at 17:52
  • \$\begingroup\$ ty :-) ! It's great to see R answers to most questions and even multiple answers to some questions. \$\endgroup\$
    – JayCe
    Commented Jun 5, 2018 at 17:56
0
\$\begingroup\$

R - 89

M=function(I,N) cat(paste(lapply(lapply(strsplit(I,""),rep,e=N),paste,collapse=""),"\n"))

I <- c("aa  aa  aa", "bbb bbb bbb", "c c c c c c")
N <- 3

I
# aa  aa  aa
# bbb bbb bbb
# c c c c c c"

M(I,N)

# aaaaaa      aaaaaa      aaaaaa 
# bbbbbbbbb   bbbbbbbbb   bbbbbbbbb 
# ccc   ccc   ccc   ccc   ccc   ccc
\$\endgroup\$
6
  • \$\begingroup\$ Looks interesting. But I get an error: “Error: could not find function "str_split"”. Do I need to explicitly install the stringr package to be able to run your code? (Sorry, not familiar with R.) \$\endgroup\$
    – manatwork
    Commented Jan 27, 2014 at 14:38
  • \$\begingroup\$ isn't it missing a call to cat to print it as stdout? Additionally each element of the string should not just be repeated n times in length but be repeated also in breadth. \$\endgroup\$
    – plannapus
    Commented Jan 27, 2014 at 15:08
  • \$\begingroup\$ You can also golf your function a bit more: <- can be =, paste0 can here be replaced by paste since you're using the collapse argument anyway and the argument each can be shorten to e. See here for some basic ideas on golfing with R. \$\endgroup\$
    – plannapus
    Commented Jan 27, 2014 at 15:10
  • \$\begingroup\$ Otherwise, it's a nice attempt (my own shortest attempt is still in the 200 characters or so :) ) \$\endgroup\$
    – plannapus
    Commented Jan 27, 2014 at 15:15
  • \$\begingroup\$ @manatwork ok, no error anymore wihtout loading stringr \$\endgroup\$ Commented Jan 27, 2014 at 20:57
0
\$\begingroup\$

MATLAB: 20 characters

['' imresize(ans,n)]

This solution assumes that the image is stored in a file called startup.m and that it is allowed to give the amount of replications to matlab when calling it, this can be done by means of: !matlab -r "n=3"&

MATLAB, call without argument: 23 characters

load;['' imresize(s,n)]

Unlike the first solution, the latter two assume that the amount of replications may not be expected in the call. For these solutions the original string and number are expected in a file called matlab.mat in your current directory.

MATLAB, mathematical alternative: 27 characters

load;['' kron(s,ones(1,n))]
\$\endgroup\$
0
\$\begingroup\$

C# (177)

public string e(string i,int x){return string.Join("",i.Split('\n').Select(a=>string.Join("",Enumerable.Repeat(string.Join("",a.ToCharArray().Select(c=>new string(c,x))),x))));}

Expects input string "i" containing new lines "\n" as the delimiter.

Formatted

public string e(string i, int x)
{
    return string.Join("", i.Split('\n').Select(a => string.Join("", Enumerable.Repeat( string.Join("", a.ToCharArray().Select(c => new string(c, x))), x))));
 }
\$\endgroup\$
0
\$\begingroup\$

CJam, 14 bytes

CJam is newer than this challenge (and the main feature I'm using is actually very recent), so this answer wouldn't be eligible for being accepted - but it's not beating APL anyway, so...

l~qN/1$e*fe*N*

Test it here.

Explanation

With the new e* which repeats each character in a string, this is really straightforward:

l~             e# Read and eval the scale factor.
  qN/          e# Read the rest of the input as the ASCII art and split into lines
     1$e*      e# Copy the scale factor and repeat each line by it.
         fe*   e# Repeat the characters in each line by the scale factor.
            N* e# Join with newlines.
\$\endgroup\$
1
  • \$\begingroup\$ I realize this is an old answer, but you can save a byte by using a function instead: {N/1$fe*e*N*} \$\endgroup\$ Commented Sep 9, 2018 at 5:49
0
\$\begingroup\$

RProgN 100 Bytes (Windows) Non-Competing

'l' asoc
'i' asoc
i '[^
]' '%0' l rep replace '[^
]*' '%0
' l rep 'z' asoc z 1 z len 1 - sub replace

RProgN doesn't have character escapes as of writing, which makes any handling of new lines require a physical new line. As such, this answer (and any input) has to use CRLFs, instead of just LFs. :(

Also, the 'rep' function was only added after this challenge was issued, as such, this is non-competing.

\$\endgroup\$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.