-2
\$\begingroup\$

NOTE: This question is different than Draw an ASCII Rectangle because the user has to input only 1 number to generate a box, unlike the other question, where the user has to input the x and y size of the box.

Your task is to create a box based upon user input. Let's say I inputted 2. The box generated would look like:

--
--

As in, two by two long. Or, if I entered 7:

-------
| |
| |
| |
| |
| |
-------

So, seven by seven.

Notice the box generated above has 5 bars up and seven hyphens across. The hyphen directly above or below the bars does count as one vertical iteration.

One more example. I enter 5. The box generated:

-----
| |
| |
| |
-----

You can use any programming language you wish. The task is to be able to generate a box using the least possible amount of bytes.

Current leaderboard:

[1] dzaima in SOGL, 17 bytes

[2] Adam in APL, 27 bytes

[3] HyperNeutrino in Python, 51 bytes

asked Jul 4, 2017 at 16:20
\$\endgroup\$
3
  • 2
    \$\begingroup\$ @DJMcMayhem While I don't dispute your dupeuammer, this challenge is slightly different and offers some golfing opportunities because the characters are unique across rows, not columns, which is how most languages work. I trust your judgement more than mine though :P \$\endgroup\$ Commented Jul 4, 2017 at 16:32
  • \$\begingroup\$ Can we assume that the input will always be at least 2? \$\endgroup\$ Commented Jul 4, 2017 at 16:35
  • 1
    \$\begingroup\$ Your comment about the input consisting of only one number isn't a valid de-duplicating reason. If the other one requires both dimensions and this only requires one, then every solution there can be easily transferred to this question by simply repeating the left argument and using that as the right argument. See my above point though. \$\endgroup\$ Commented Jul 5, 2017 at 19:56

4 Answers 4

1
\$\begingroup\$

SOGL V0.12, (削除) 21 (削除ここまで) 17 bytes

1.0{1.┐∙ž}:┐┌ŗH╬8

Try it Here!

Explanation:

1.0 push 1, input and wrap in an array
 { } for each item in that array do
 1 push 1
 .┐∙ get an array of input amount of items of "|"
 ž at coordinates [currentArrayItem; 1] place those bars
 : duplicate it
 ┐┌ŗ replace "|" with "-"
 H rotate anti-clockwise
 ╬8 place the 2nd array over the 1st
answered Jul 4, 2017 at 16:25
\$\endgroup\$
0
\$\begingroup\$

JavaScript (ES6), (削除) 61 (削除ここまで) 60 bytes

n=>`${h="-"[r="repeat"](n)}
${`|${" "[r](n-=2)}|
`[r](n)}`+h
answered Jul 4, 2017 at 16:27
\$\endgroup\$
0
\$\begingroup\$

APL (Dyalog), 27 bytes

'-'⍪'-'⍪⍨'|','|',⍨''⍴⍨2⍴-∘2

Try it online!

-∘2 subtract two

2⍴ reshape into length 2

''⍴⍨ use that as height and width to reshape an empty string (padding with spaces)

'|',⍨ put stiles on the right

'|', put stiles on the left

'-'⍪⍨ put dashes below

'-'⍪ put dashes on top

answered Jul 4, 2017 at 16:24
\$\endgroup\$
5
  • \$\begingroup\$ {'--| '[a⌊⌽⊖a←2⊥¨×⍳⌽⍵]} \$\endgroup\$ Commented Jul 7, 2017 at 8:51
  • \$\begingroup\$ @ngn I don't get it. Can you post a TryAPL or TIO link? \$\endgroup\$ Commented Jul 7, 2017 at 10:46
  • \$\begingroup\$ it's []io=0 tio.run/##SyzI0U2pTMzJT///qG9qZv6jtgkG/… \$\endgroup\$ Commented Jul 7, 2017 at 13:44
  • \$\begingroup\$ @n It almost answers this then, alhough you can specify input in reverse, and use , saving 3 bytes. However, here there is only one input, so we need '--| '[a⌊⌽⊖a←2⊥¨×⍳2/⎕] \$\endgroup\$ Commented Jul 7, 2017 at 13:47
  • \$\begingroup\$ I must have confused the two... well, still an improvement. \$\endgroup\$ Commented Jul 7, 2017 at 15:08
0
\$\begingroup\$

Python, 51 bytes

lambda x:'-'*x+'\n'+('|'+' '*(x-2)+'|\n')*(x-2)+'-'*x

I tested this on my laptop and then manually wrote this answer on mobile so please tell me if this doesn't work.

answered Jul 4, 2017 at 16:27
\$\endgroup\$
2
  • \$\begingroup\$ I believe you meant *x at the end. \$\endgroup\$ Commented Jul 6, 2017 at 9:36
  • \$\begingroup\$ @Fedone Yes, that is what I meant. Thanks. \$\endgroup\$ Commented Jul 6, 2017 at 17:59

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.