20
\$\begingroup\$

Task

Given one non-whitespace printable character, make a 3x3 square representation of that input. For example, if the input is #, then the output is:

###
# #
###

Rules

  • The output format is strict, although a trailing newline is allowed. It means that the space in the middle is required, and also that the two newline characters separating the three lines are required.

Testcases

Input: #

Output:

###
# #
###

Input: A

Output:

AAA
A A
AAA

Input: 0

Output:

000
0 0
000

Scoring

This is . Shortest answer in bytes wins.

\$\endgroup\$
  • 2
    \$\begingroup\$ The fact that the size is fixed allows for some optimization. Answers form the linked challenge will probably not be competitive here. So I don't think it's a duplicate \$\endgroup\$ – Luis Mendo May 11 '17 at 14:33
  • 12
    \$\begingroup\$ I was the one who downvoted, for it for being a simple, boring challenge. I'm normally a fan of easy challenges, as they're a good place for new golfers to start but this just feels too easy. \$\endgroup\$ – Shaggy May 11 '17 at 14:56
  • 32
    \$\begingroup\$ @Ayoungcoder It is a perfectly valid reason to downvote a challenge. \$\endgroup\$ – Wheat Wizard May 11 '17 at 14:59
  • 2
    \$\begingroup\$ @Shaggy: In terms of difficulty, there's difficulty to write the program, and difficulty to golf the program. This program is easy to write, but I'm not so sure it's easy to golf it. \$\endgroup\$ – user62131 May 11 '17 at 15:33
  • 5
    \$\begingroup\$ In my opinion, this is a good challenge for people who are just getting started with code golfing. It's good to have a mix of difficulties. Overloading on any one type will be to the detriment of some part of the community. So, I'm glad this challenge was written. \$\endgroup\$ – isaacg May 13 '17 at 6:22

71 Answers 71

0
\$\begingroup\$

braingasm, 18 bytes

,...10..32..10....

Yeah... read a byte, print it three times, then print a newline (10), then the byte again, then a space (32), that byte again, another newline and that byte three more times...

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

Ruby, 23+1 = 24 bytes

Uses the -n flag for +1 byte.

puts$_*3,$_+' '+$_,$_*3

Try it online!

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

Haskell, 45 44 bytes

-1 byte thanks to Laikoni

Naive approach

f s=putStrLn$s:s:s:'\n':s:' ':s:'\n':s:s:[s]

For some reason

f s=putStr$s:s:s:'\n':s:' ':s:'\n':s:s:[s]

won't output the last line on repl.it, but if it's still valid it shaves off 2 bits for a total of 42

And

f s=print$s:s:s:'\n':s:' ':s:'\n':s:s:[s]

will print the answer as "sss\ns s\nsss" instead of properly linebreaking. If that's valid it shaves off 3 bits for a total of 41

There is a solution with replace that's 42 bits long ... but that's without counting the bytes needed to import the necessary module. For reference:

import Data.String.Utils
f s=putStrLn$replace "." s "...\n. .\n..."
\$\endgroup\$
  • \$\begingroup\$ You can shorten s:[] to [s]. \$\endgroup\$ – Laikoni May 20 '17 at 12:37
0
\$\begingroup\$

OCaml, 53 bytes

let f c=String.map(function '#'->c|c->c)"###
# #
###"

It's pretty bad. OCaml does not even have a replace function

Bash, 26 bytes

echo "$1$1$1
$1 $1
$1$1$1"

5 bytes less boring

\$\endgroup\$
  • \$\begingroup\$ Suggestion: Post you OCaml answer and Bash answers separately. \$\endgroup\$ – CalculatorFeline May 22 '17 at 16:29
0
\$\begingroup\$

Python 2, 34 bytes

lambda n:3*n+'\n'+n+" "+n+'\n'+3*n

Try it online!

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

Excel VBA, 27 Bytes

Anonymous VBE immediate window function that takes input from cell [A1] on the Application.ActiveSheet object and outputs a 'square' to the VBE immediate window.

a=[A1]:?a;a;a:?a" "a:?a;a;a
\$\endgroup\$
0
\$\begingroup\$

Syms, 21 bytes

{}<[[[++~[{& &}~[>~>>

Try it online!

\$\endgroup\$
  • \$\begingroup\$ oops, didnt allow trailing [] but never specified it, lucky ig guess :) \$\endgroup\$ – Luc H Sep 16 '17 at 6:37
  • \$\begingroup\$ Um why is -i on \$\endgroup\$ – CalculatorFeline Sep 25 '17 at 19:51
0
\$\begingroup\$

K (oK), 8 bytes

Solution:

3 3#5$4#

Try it online!

Example:

> 3 3#5$4#"@"
("@@@"
 "@ @"
 "@@@")

Explanation:

Evaluated right-to-left. Build 'AAAA', then 'AAAA ' and then shape into the 3x3 grid required.

3 3#5$4# / the solution
      4# / take 4 of whatever is to the right
    5$   / right pad with whitespace to length 5
3 3#     / shape into 3x3 grid

Bonus:

The solution is a polyglot for q/kdb+:

q)3 3#5$4#"*"
"***"
"* *"
"***"
\$\endgroup\$
  • \$\begingroup\$ cool way of doing it \$\endgroup\$ – Luc H Sep 26 '17 at 14:26
0
\$\begingroup\$

UGL, 23 Bytes

j"\n"++l*I3l++i" "il*i3

try it online (write the code to stdin. you can also use the link at the top of the answer to try online, that accepts ctrl+v)

Equivalent Python code:

cur_inp = ""
def inp():
    global cur_inp
    cur_inp = input()
    return cur_inp
"\n".join([inp*3]+[cur_inp+" "+cur_inp]+[cur_inp*3])

Using Python's string multiplication.

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

Dart, 30 bytes

f(s)=>'$s$s$s\n$s $s\n$s$s$s';

Try it online!

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

Javascript (in-console), 42 bytes

for(i=3;i;i--)console.log(i%2?'iii':'i i')

Alternatively, this abomination:

i=3;while(i--)console.log(i%2?'i i':'iii')

The previous JavaScript answer was really cool, but you still need extra JavaScript to print out the square into the console or into an HTML element. I'm going for the shortest code including printing. If you put this code in-between two script tags and save it as an HTML page (so you aren't putting the code directly into the console) it's still only 59 bytes, but you can just run it from the developer console for the 42 bytes.

Explanation

for loop

i has to remain true for the for loop to keep running. When it hits 0 it is false and so stops. Counting down instead of up lets us save 2 characters over i=1;i=3;i++.

The code also has no spaces or semicolons- bad, I know. But with just one line, it still works!

The ternary operator reads "If i divided by 2 equals 0 (so if i is 2), print iii; otherwise, print i i." This means only the second line will print i i.

while loop

The while loop was a test that didn't get any fewer bytes, but was interesting. i is just defined outside of the loop. I didn't know that you could iterate inside of the while statement; it still goes until i becomes 0 from what I can tell. I don't know if this works cross-browser, but it worked in Chrome. I can't help but think there's a way to shorten the while version more, but I haven't figured it out yet...

\$\endgroup\$
  • 1
    \$\begingroup\$ First of all, welcome to the site! This looks like a very thorough first answer. Second, excuse me for being JavaScript illiterate but how does this program take input? It appears to me, again I'm completely illiterate in Javascript, this always print a square of is or perhaps a square made of the numbers 2, 1, and 0. \$\endgroup\$ – Wheat Wizard Sep 18 '17 at 3:47
  • \$\begingroup\$ I think the program is supposed to take a character of input and then create a square consisting of that character. \$\endgroup\$ – Esolanging Fruit Sep 18 '17 at 4:53
  • \$\begingroup\$ Also, you can collapse the the --i and the i into the middle section of the for loop to save a byte (but you'll have to swap the branches on the ? statement). \$\endgroup\$ – Esolanging Fruit Sep 18 '17 at 4:54
  • \$\begingroup\$ @FunkyComputerMan Thank you! In JavaScript, surrounding values with single quotes makes them into strings. The string could have been anything besides 'i', I just chose 'i' for the heck of it (and I was brainstorming if I could somehow print the character as well as the variable name and use some sort of hack there). \$\endgroup\$ – Josh Powlison Sep 18 '17 at 21:05
  • \$\begingroup\$ @Challenger5 Comment 1: Looking at the description again, you may be right. "Given one non-whitespace printable character, make a 3x3 square representation of that input." But that could imply that input needs to be 1 variable, and I'm not sure that most of these answers follow that (that said, I'm not fluent in a lot of these languages, so I could just be unable to read them right). I'm not 100% sure, so I'll keep my answer here. Comment 2: Could you show me what the code would be? The shorthand you're referring to sounds familiar, but I'm struggling to find it online. \$\endgroup\$ – Josh Powlison Sep 18 '17 at 21:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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