-1
\$\begingroup\$

This question already has an answer here:

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

\$\endgroup\$

marked as duplicate by DJMcMayhem code-golf Jul 4 '17 at 16:26

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 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\$ – HyperNeutrino Jul 4 '17 at 16:32
  • \$\begingroup\$ Can we assume that the input will always be at least 2? \$\endgroup\$ – dzaima Jul 4 '17 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\$ – HyperNeutrino Jul 5 '17 at 19:56
1
\$\begingroup\$

SOGL V0.12, 21 17 bytes

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

Try it Here!

Explanation:

1.⁰                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
\$\endgroup\$
0
\$\begingroup\$

JavaScript (ES6), 61 60 bytes

n=>`${h="-"[r="repeat"](n)}
${`|${" "[r](n-=2)}|
`[r](n)}`+h
\$\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

\$\endgroup\$
  • \$\begingroup\$ {'--| '[a⌊⌽⊖a←2⊥¨×⍳⌽⍵]} \$\endgroup\$ – ngn Jul 7 '17 at 8:51
  • \$\begingroup\$ @ngn I don't get it. Can you post a TryAPL or TIO link? \$\endgroup\$ – Adám Jul 7 '17 at 10:46
  • \$\begingroup\$ it's []io=0 tio.run/##SyzI0U2pTMzJT///qG9qZv6jtgkG/… \$\endgroup\$ – ngn Jul 7 '17 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\$ – Adám Jul 7 '17 at 13:47
  • \$\begingroup\$ I must have confused the two... well, still an improvement. \$\endgroup\$ – ngn Jul 7 '17 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.

\$\endgroup\$
  • \$\begingroup\$ I believe you meant *x at the end. \$\endgroup\$ – Fedone Jul 6 '17 at 9:36
  • \$\begingroup\$ @Fedone Yes, that is what I meant. Thanks. \$\endgroup\$ – HyperNeutrino Jul 6 '17 at 17:59

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