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
-
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– hyperneutrino ♦2017年07月04日 16:32:55 +00:00Commented Jul 4, 2017 at 16:32
-
\$\begingroup\$ Can we assume that the input will always be at least 2? \$\endgroup\$dzaima– dzaima2017年07月04日 16:35:04 +00:00Commented 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\$hyperneutrino– hyperneutrino ♦2017年07月05日 19:56:23 +00:00Commented Jul 5, 2017 at 19:56
4 Answers 4
SOGL V0.12, (削除) 21 (削除ここまで) 17 bytes
1.0{1.┐∙ž}:┐┌ŗH╬8
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
JavaScript (ES6), (削除) 61 (削除ここまで) 60 bytes
n=>`${h="-"[r="repeat"](n)}
${`|${" "[r](n-=2)}|
`[r](n)}`+h
APL (Dyalog), 27 bytes
'-'⍪'-'⍪⍨'|','|',⍨''⍴⍨2⍴-∘2
-∘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
-
\$\begingroup\$
{'--| '[a⌊⌽⊖a←2⊥¨×⍳⌽⍵]}
\$\endgroup\$ngn– ngn2017年07月07日 08:51:18 +00:00Commented Jul 7, 2017 at 8:51 -
\$\begingroup\$ @ngn I don't get it. Can you post a TryAPL or TIO link? \$\endgroup\$Adám– Adám2017年07月07日 10:46:19 +00:00Commented Jul 7, 2017 at 10:46
-
\$\begingroup\$ it's []io=0 tio.run/##SyzI0U2pTMzJT///qG9qZv6jtgkG/… \$\endgroup\$ngn– ngn2017年07月07日 13:44:01 +00:00Commented Jul 7, 2017 at 13:44
-
-
\$\begingroup\$ I must have confused the two... well, still an improvement. \$\endgroup\$ngn– ngn2017年07月07日 15:08:48 +00:00Commented Jul 7, 2017 at 15:08
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.
-
\$\begingroup\$ I believe you meant
*x
at the end. \$\endgroup\$Fedone– Fedone2017年07月06日 09:36:18 +00:00Commented Jul 6, 2017 at 9:36 -
\$\begingroup\$ @Fedone Yes, that is what I meant. Thanks. \$\endgroup\$2017年07月06日 17:59:51 +00:00Commented Jul 6, 2017 at 17:59