34
\$\begingroup\$

Your task: given a number n, generate a '+' sign that is n characters away from its center. If this is confusing, check out the test cases.

Standard methods of input: output must be a string or printed. Standard loopholes apply.

Input: 1
Output: +           ] 1 away from center `+`.

Input: 2
Output:  +          ] 2 away from center `+`.
        +++         ] 1 away from center `+`.  
         +

Input: 3
Output:   +         ] 3 away from center `+`.
          +
        +++++
          +
          +

This is , so shortest code wins!

\$\endgroup\$
6
  • \$\begingroup\$ I assume trailing spaces on each line are allowed so that the result is square, right? \$\endgroup\$
    – Luis Mendo
    Apr 11 '17 at 15:29
  • \$\begingroup\$ @LuisMendo Yes, that is fine. \$\endgroup\$
    – sporklpony
    Apr 11 '17 at 15:31
  • \$\begingroup\$ Related. \$\endgroup\$
    – Neil
    Apr 11 '17 at 19:29
  • 5
    \$\begingroup\$ "generate a '+' sign that is n characters away from its center" -- I don't understand this part. What center are you referring to? How can something be eccentric to itself? Please clarify. \$\endgroup\$
    – Wossname
    Apr 13 '17 at 6:43
  • 13
    \$\begingroup\$ It would have been a lot less confusing if the centre were 0 away from itself. \$\endgroup\$
    – OrangeDog
    Apr 13 '17 at 13:45

55 Answers 55

1
2
2
\$\begingroup\$

Brain-Flak, 216 + 1 = 217 bytes

+1 bytes from the -A flag

([{}]())(()()){<>(((((()()()()()){})){}()){}())<>(({}[()])<({}<(({})){({}()<(({}<<>(({}<(({})<>)>)<>)<>>)<{({}()<(((((()()){}){}){}){})>)}{}>)>)}{}>){(<{}(({}<<>({}<({}<>)>)>)<(({}){}){({}()<(({}))>)}{}>)>)}{}>)}{}{}

Try it online!

Explanation to come

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

Japt, 30 26 24 bytes

23 bytes of code, +1 for the -R flag.

Saved 4 bytes thanks to @ETHproductions!

V=U+´UVǦU?SpU +'+:'+pV

Try it online!

\$\endgroup\$
3
  • \$\begingroup\$ I think you can save a bunch by doing V=U+´U at the beginning and removing all those pesky -1s ;-) \$\endgroup\$ Apr 20 '17 at 20:49
  • \$\begingroup\$ @ETHproductions Great idea, thanks! I saved another byte by using Ç \$\endgroup\$
    – Oliver
    Apr 21 '17 at 3:43
  • \$\begingroup\$ And another byte by switching to ¦ \$\endgroup\$
    – Oliver
    Apr 21 '17 at 3:45
2
\$\begingroup\$

GolfScript, 27 bytes

~.(." "*"+"n++*.@2*("+"*n+\

Try it online!

Explanation and Example Execution:

                              # Input: 3
~.(.                          # Eval input, duplicate twice, decrement duplicates
                              # Stack: 3 2 2
    " "*                      # That many spaces
                              # Stack: 3 2 "  "
        "+"n++                # Followed by a + and a newline
                              # Stack: 3 2 "  +\n"
              *.              # Create n of those strings and duplicate it
                              # Stack: 3 "  +\n  +\n" "  +\n  +\n"
                @2*(          # Pull bottom to top, double it, decrement
                              # Stack: "  +\n  +\n" "  +\n  +\n" 5
                    "+"*n+    # That many +s followed by a newline
                              # Stack: "  +\n  +\n" "  +\n  +\n" "+++++\n"
                          \   # Swap last 2 items and implicityly print
                              # Stack: "  +\n  +\n" "+++++\n" "  +\n  +\n"
\$\endgroup\$
2
\$\begingroup\$

Vim, 36 bytes

<C-x>D@-i <esc>Y@-pVGy:1s/ /+/g
P:%s/.*/&+&

The byte-count includes a trailing newline. Try it online!

Explanation

<C-x>D

Decrement the input number n and delete it. The deleted text is stored in the "small deletion" register "-.

@-i <esc>

Insert a space "- times.

Y@-p

Yank that line, and then paste it "- times. We now have n lines of n-1 spaces each, with the cursor at the beginning of line 2.

VGy

Select all lines till the end of the file (i.e. n-1 lines, all but the first one) and yank them.

:1s/ /+/g

On line 1, replace each space with a plus sign. This command leaves the cursor on line 1.

P

Paste the previously yanked lines above the current line. We now have n-1 lines of spaces, one line of plus signs, and n-1 more lines of spaces.

:%s/.*/&+&

Replace each line with itself followed by a plus sign followed by itself again.

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

Pyke, 14 bytes

t.\+s FQt*\++s

Try it online!

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

J, 21 bytes

' +'{~+./~(0=}.,])i.n
\$\endgroup\$
1
\$\begingroup\$

8086 machine code, 57 bytes

00000000  a0 82 00 24 cf 98 48 89  c1 b4 4c e3 20 bf 39 01  |...$..H...L. .9.|
00000010  57 51 5a 52 51 b0 20 89  d1 f3 aa b8 2b 0a ab 59  |WQZRQ. .....+..Y|
00000020  e2 f2 c6 05 24 59 01 c9  b4 09 5a cd 21 41 b0 2b  |....$Y....Z.!A.+|
00000030  cd 29 e2 fa 4a cd 21 c3  0a                       |.)..J.!..|
00000039

Takes a number (one digit...) as command line argument.
How it works:

            |   use16
a0 82 00    |       mov al, byte [0x82]
24 cf       |       and al, not 0x30
98          |       cbw
48          |       dec ax
89 c1       |       mov cx, ax
b4 4c       |       mov ah, 0x4c
e3 20       |       jcxz a
bf 39 01    |       mov di, s+1
57          |       push di
51          |       push cx
5a          |       pop dx
52          |       push dx
51          |   @@: push cx
b0 20       |       mov al, ' '
89 d1       |       mov cx, dx
f3 aa       |       rep stosb
b8 2b 0a    |       mov ax, 0x0a2b
ab          |       stosw
59          |       pop cx
e2 f2       |       loop @b
c6 05 24    |       mov byte [di], 0x24
59          |       pop cx
01 c9       |       add cx, cx
b4 09       |       mov ah, 0x09
5a          |       pop dx
cd 21       |       int 0x21
41          |   a:  inc cx
b0 2b       |   @@: mov al, '+'
cd 29       |       int 0x29
e2 fa       |       loop @b
4a          |       dec dx
cd 21       |       int 0x21
c3          |       ret
0a          |   s db 0x0a
\$\endgroup\$
1
\$\begingroup\$

Rust, 89 bytes

|n|{for i in 1..2*n{if i==n{println!("{:+^1$}","",2*n-1)}else{println!("{:>1$}","+",n)}}}
\$\endgroup\$
1
\$\begingroup\$

05AB1E, 5 bytes (non-competing?)

'+90Λ

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ 4 bytes (and non-competing is no longer a thing) \$\endgroup\$ Dec 17 '19 at 9:15
1
\$\begingroup\$

J, 17 bytes

Anonymous tacit prefix function

' +'{~0=*/~@i:@<:

Try it online!

For example for 3:

<: decrement; 2

i: steps; [-2,-1,0,1,2]

*/~ times table; [[4,2,0,-2,-4],[2,1,0,-1,-2],[0,0,0,0,0],[-2,-1,0,1,2],[-4,-2,0,2,4]]

0= indicate where zero; [[0,0,1,0,0],[0,0,1,0,0],[1,1,1,1,1],[0,0,1,0,0],[0,0,1,0,0]]

" +"{~ use that to index into " +"; [" + "," + ","+++++"," + "," + "]

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

Keg, 39 37 bytes

;(:|:⑬*,\+,
,):⑨⑵;`+`*,
,(:|:⑬*,\+,
,

-2 bytes using built-ins

Try it online!

This attempt prints the isolated +s first, then the big row of +s then the final isolated +s.

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

Wren, 67 bytes

Fn.new{|n|(1...2*n).map{|i|i==n?"+"*(n+~-n):" "*~-n+"+"}.join("
")}

Try it online!

Explanation

Fn.new{|n|                                                      // New anonymous function with parameter n
          (1...2*n).map{|i|                                     // Foreach 1 -> 2n-1:
                           i==n?                                // If i equals n:
                                "+"*(n+~-n)                     // Return "+" 2n-1 times
                                           :" "*~-n             // Otherwise return space n-1 times
                                                   +"+"         // Add a trailing "+"
                                                       }.join("
")}                                                             // Join with newline
\$\endgroup\$
1
\$\begingroup\$

PowerShell, 47 bytes

param($n)($x=,(' '*--$n+'+')*$n)
'+'+'++'*$n
$x

Try it online!

This one is shorter than 48 bytes ¯\_(ツ)_/¯

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

Vyxal C, 12 bytes

\+*÷:?d›*Wøm

Try it Online!

-2 thanks to @astonearachnid. -1 thanks to lyxal.

\$\endgroup\$
4
  • \$\begingroup\$ 13 bytes: Try it Online! \$\endgroup\$ Jun 19 at 2:18
  • \$\begingroup\$ @astonearachnid Thanks! \$\endgroup\$
    – emanresu A
    Jun 19 at 3:37
  • \$\begingroup\$ 12 bytes \$\endgroup\$
    – lyxal
    Jun 19 at 3:44
  • \$\begingroup\$ @lyxal Thankss! \$\endgroup\$
    – emanresu A
    Jun 19 at 3:44
0
\$\begingroup\$

Charcoal, 26 25 bytes

NηA⁻η¹η××η²+P+Mη↙G↑⁺¹×η²+

Try it online!

Explanation:

Nη                   // Take the size as input.
A⁻η¹η                // Set η to η-1.
××η²+                // Print '+' η*2 times.
P+                   // Print '+'
Mη↙                 // Move η steps down to the left.
G↑⁺¹×η²+            // Print '+' η*2+1 times upwards from current position.
\$\endgroup\$
0
\$\begingroup\$

Common Lisp, SBCL, 69 bytes

(format t"~@?~@?~@?+
~@*~@?""~v@{~:*~v@t+
~}"(1-(read))"~1@*~v@{+~}")

Explanation

format        ;printing function
~@?           ;execute current argument as format string 
~@*           ;go to first argument in list, to reuse "~v@{~:*~v@t+
              ;~}"
~v@{~}        ;loop n times, where n is current argument
~:*           ;go back one argument - to reuse (1-(read))
~v@t          ;add n spaces where n is current argument
              ;if we were sure that we start on new line we could
              ;use ~vt, which moves to nth column
(1-(read))    ;take input and subtract 1 from it
~1@*          ;move to second argument, to reuse (1-(read))

"~@?~@?~@?+   ;main format string - calls others
~@*~@?"       ;prints one "+" and one newline
"~v@{~:*~v@t+ ;outputting input-1 lines of "+"s
~}"           ;preceded by correct amount of spaces
"~1@*~v@{+~}" ;outputting input-1 pluses. We do it two times
              ;and then add one + at the end. This way there
              ;is no need for argument like: (1-(* 2 INPUT))
              ;which would make me need a variable keeping INPUT

Ideas for improvement are welcomed.

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

Pyth, 24 bytes

+J*+++K*dQ\+Kb/Q2*\+hyQJ

Try it online!

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

Perl 5, 43 + 1 (-n) = 44 bytes

$,="+
";say@q=($"x--$_)x$_,'+'x(2*$_),@q,''

Try it online!

Independently came up with pretty much the same method as @Dada, but got it down by 1 byte.

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

Excel, 86 bytes

=REPT(REPT(" ",A1-1)&"+
",A1-1)&REPT("+",2*A1-1)&"
"&REPT(REPT(" ",A1-1)&"+
",A1-1)
\$\endgroup\$
0
\$\begingroup\$

Japt -R, 12 bytes

Look, Ma, no +!

çÄ ê
¬hNÉU û

Try it

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

APL (NARS2000), 25 characters:

{' +'[1+∨/¨⍵=∘.,⍨⍳⍵+⍵-1]}

APL (NARS2000), 26 characters:

{' +'[1+∨/¨⍵=∘.,⍨⍳¯1+2×⍵]}

Examples:

      {' +'[1+∨/¨⍵=∘.,⍨⍳⍵+⍵-1]} 1
+
      {' +'[1+∨/¨⍵=∘.,⍨⍳⍵+⍵-1]} 2
 + 
+++
 + 
      {' +'[1+∨/¨⍵=∘.,⍨⍳⍵+⍵-1]} 3
  +  
  +  
+++++
  +  
  +  
      {' +'[1+∨/¨⍵=∘.,⍨⍳⍵+⍵-1]} 4
   +   
   +   
   +   
+++++++
   +   
   +   
   +  

Rationale:

Take n = 4 as example. Objective is to generate an integer matrix as below:

1 1 1 2 1 1 1
1 1 1 2 1 1 1
1 1 1 2 1 1 1
2 2 2 2 2 2 2
1 1 1 2 1 1 1
1 1 1 2 1 1 1
1 1 1 2 1 1 1

Expression ⍵=∘.,⍨⍳⍵+⍵-1, when given ⍵ = 4, generates matrix of 2-tuple from integer vector 1..7:

 0 0  0 0  0 0  0 1  0 0  0 0  0 0 
 0 0  0 0  0 0  0 1  0 0  0 0  0 0 
 0 0  0 0  0 0  0 1  0 0  0 0  0 0 
 1 0  1 0  1 0  1 1  1 0  1 0  1 0 
 0 0  0 0  0 0  0 1  0 0  0 0  0 0 
 0 0  0 0  0 0  0 1  0 0  0 0  0 0 
 0 0  0 0  0 0  0 1  0 0  0 0  0 0 

Each cell is reduced by logical-or ∨/¨ to give boolean matrix:

0 0 0 1 0 0 0
0 0 0 1 0 0 0
0 0 0 1 0 0 0
1 1 1 1 1 1 1
0 0 0 1 0 0 0
0 0 0 1 0 0 0
0 0 0 1 0 0 0

Add 1 to this matrix and we have the desired array-indexing matrix.

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

Batch 97 bytes

requires windows 10 with virtual terminal support

Set x=%~1&cls&@Set/a c=x*2-1
@For /L %%i in (1 1 %c%)Do @Echo(ESC[%%i;%x%H+ESC[%x%;%%iH+ESC[%x%BESC[1G

ESC is the ANSI escape character, which doesn't paste well but can be copied from the raw paste data here.
Or it can be generated by opening the command prompt and typing >"%TEMP%\escape.chr" Echo(ALT+027 then pressing ENTER, or output using a for loop with Echo off:

(For /F "skip=4 DELIMS=" %a in ('Echo(prompt $E^|cmd')Do Echo(%a) >"%TEMP%\escape.chr"

How:
Iterates over the width of the string c and outputs c+ along the the x (y and x) intersection using virtual terminal sequences to move the cursor to the relevant coordinates.

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

Canvas, 8 7 bytes

+×:⤢n!┼

Try it here!

-1 byte from hakr14.

\$\endgroup\$
2
  • \$\begingroup\$ You can replace ;+* with :⤢ to save a byte. Also, how did I not know about ? \$\endgroup\$
    – hakr14
    Apr 29 at 21:12
  • \$\begingroup\$ @hakr14 I realized n's existence after a long time as well :) Talking with dzaima helps \$\endgroup\$
    – Razetime
    Apr 30 at 2:00
0
\$\begingroup\$

Regenerate, 38 bytes

(( {$~1-1}\+\n){$~1-1})\+{$~1*2-1}\n$1

Takes input as a command-line argument. Try it here!

Explanation

Space is represented by underscore for better visibility.

(( {$~1-1}\+\n){$~1-1})\+{$~1*2-1}\n$1
(                     )                 Group 1: the section above the center line
 (            )                          Group 2: one line of that section
  _{$~1-1}                                n-1 spaces
          \+\n                            followed by + and newline
               {$~1-1}                   Repeat n-1 times
                       \+{$~1*2-1}      n*2-1 + characters
                                  \n    followed by newline
                                    $1  Contents of group 1 again
\$\endgroup\$
0
\$\begingroup\$

Regenerate -a, 38 bytes

(({2,$~1} |\+|{2,$~1} )$2{$~1-2}!)\+$1

Takes input as a command-line argument. Try it here!

Explanation

Here, we're going to (ab)use Regenerate's backtracking algorithm. The regex describes several possibilities for a single line, and the -a flag instructs Regenerate to output all possible matches. I'm going to start with a slightly different version that doesn't work for input \$n=1\$, and then discuss how to deal with that corner case afterwards.

Space is represented by underscore for better visibility.

(({2,$~1} |\+|{2,$~1} )$2{$~1-2})\+$1
(                               )      Group 1: the part before the central + character:
                                       either n-1 spaces or n-1 + characters
 (                    )                 Group 2: which character to use
                                         Option 1:
  {2,$~1}                                 Repeat empty string between 2 and n times
                                          (this causes the backtracker to try n-1 different
                                          options, all of them equal to empty string)
         _                                followed by space
          |                              Option 2:
           \+                             + character
             |                           Option 3:
              {2,$~1}_                    Same deal as option 1
                       $2               Group 2 again...
                         {$~1-2}        ... repeated n-2 times, for a total of n-1
                                 \+    The central + character
                                   $1  followed by group 1 again

When backtracking over group 2, Regenerate will try space \$n-1\$ times, then +, then space another \$n-1\$ times. So the list of all possible matches will be \$n-1\$ lines of spaces with a + in the middle, one line of + characters with a + in the middle, and \$n-1\$ more lines of spaces with a + in the middle--exactly the output we want.

There is a problem with \$n=1\$, however. We want group 1 to match the empty string, so that the only possible match for the full regex is +. However, with input of \$1\$, group 1 fails to match. The culprit is $2{$~1-2}, "repeat group 2's contents \$n-2\$ times." It's not possible to repeat something \$-1\$ times, so the whole group fails.

We can get around this failure by providing empty string as another alternative inside group 1. However, using | here raises another issue: we don't want the empty string to be tried if the first alternative succeeded, only if it failed. (Since we're asking for all possible matches, we would get a stray + at the end of the output for \$n>1\$.)

The solution is to use !, a greedy version of |. If its left side matches at all, it takes the left side's match(es) and never tries the right side. It only tries the right side if its left side fails entirely. Thus, our final solution is:

((...)$2{$~1-2}!)\+$1
(               )      Group 1: the part before the central + character
 (...)                  Group 2: same options as before
      $2{$~1-2}         Repeat group 2 again n-2 times
               !        Or, iff that fails because n-2 < 0...
                        Empty string
                 \+    The central + character
                   $1  followed by group 1 again
\$\endgroup\$
1
2

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.