# Challenge :

Given an integer n as input. Create a diamond that is 2x the given number n.

# Input :

Input is integer n and 2 < n ≤ 3000.

# Output :

Output will be a string and it will be in form of a diamond consisting of + with an addition line at the start showing n using +

# Examples :

D(3) :

+++
+
+++
+++++
+++++
+++
+

D(5) :

+++++
+
+++
+++++
+++++++
+++++++++
+++++++++
+++++++
+++++
+++
+

D(6) :

++++++
+
+++
+++++
+++++++
+++++++++
+++++++++++
+++++++++++
+++++++++
+++++++
+++++
+++
+



# Winning Criteria :

This is so shortest code in bytes for each programming language wins.

• May we take n in unary?
Commented Apr 19, 2018 at 7:16
• … using + as tally mark?
Commented Apr 19, 2018 at 7:17
• Can you add a test case where n is even? Commented Apr 19, 2018 at 10:09
• @Shaggy : sure why not. I will add that right away. Thanks Commented Apr 19, 2018 at 10:11
• This is a grenade. Commented Apr 21, 2018 at 23:12

# Attache, 62 bytes

{"+"*_+lf+UnGrid!Bounce=>"+ "[Table[>,1:_]|>~'#Reverse|>@N]}


Try it online!

A lambda which takes the integer as an argument.

## Example

A> n := 3
3
A> Table[>,1:n]
false false false
true false false
true  true false
A> Table[>,1:n]|>~'#Reverse
true  true false
true false false
false false false
false false false
true false false
true  true false
A> Table[>,1:n]|>~'#Reverse|>@N
1 1 0
1 0 0
0 0 0
0 0 0
1 0 0
1 1 0
A> "+ "[Table[>,1:n]|>~'#Reverse|>@N]
" " " " "+"
" " "+" "+"
"+" "+" "+"
"+" "+" "+"
" " "+" "+"
" " " " "+"
A> Bounce=>"+ "[Table[>,1:n]|>~'#Reverse|>@N]
" " " " "+" " " " "
" " "+" "+" "+" " "
"+" "+" "+" "+" "+"
"+" "+" "+" "+" "+"
" " "+" "+" "+" " "
" " " " "+" " " " "
A> UnGrid!Bounce=>"+ "[Table[>,1:n]|>~'#Reverse|>@N]
"  +  \n +++ \n+++++\n+++++\n +++ \n  +  "
A> lf+UnGrid!Bounce=>"+ "[Table[>,1:n]|>~'#Reverse|>@N]
"\n  +  \n +++ \n+++++\n+++++\n +++ \n  +  "
A> "+"*n+lf+UnGrid!Bounce=>"+ "[Table[>,1:n]|>~'#Reverse|>@N]
"+++\n  +  \n +++ \n+++++\n+++++\n +++ \n  +  "
A> Print[_]
+++
+
+++
+++++
+++++
+++
+
["+++\n  +  \n +++ \n+++++\n+++++\n +++ \n  +  "]
A>


# T-SQL, 152 bytes

Per our IO rules, input is taken via pre-existing table t with a integer field n.

DECLARE @n INT,@ INT=1,@k INT=1SELECT @n=n FROM t
PRINT REPLICATE('+',@n)a:PRINT SPACE(@n-@)+REPLICATE('+',2*@-1)IF @=@n SET @k-=1SET @+=@k IF @>0GOTO a


Manual counting loop, not very "SQL-like". Formatted:

DECLARE @n INT,@ INT=1,@k INT=1
SELECT @n=n FROM t
PRINT REPLICATE('+',@n)
a:
PRINT SPACE(@n-@)+REPLICATE('+',2*@-1)
IF @=@n SET @k-=1
SET @+=@k
IF @>0 GOTO a


# V, 20 bytes

é+ÄÀ­ñ>GMÙX2é+ÄHñÄÒ+


Try it online!

Hexdump:

00000000: e92b c4c0 adf1 3e47 4dd9 5832 e92b c448  .+....>GM.X2.+.H
00000010: f1c4 d22b                                ...+


Explanation:

é+                      " Insert a '+' character
Ä                     " Duplicate this line
À­                   " 'arg' - 1 times...
ñ          ñ       " Repeat the following code:
>G                "   Indent every line
M               "   Move to the center line
Ù              "   Duplicate this line
X             "   Delete the first character of it (the space)
2é+          "   Insert two '+' characters
Ä         "   Duplicate this new line we made
Ä      " After the loop is over, duplicate the top line
Ò+    " Replace every character on this line with a '+'


&.@@($c43-1)&>&1<[-1$c10@[-1$c32]&>@[-1$c43]&+2<][!$c10@[-1$c32]&+1>-2@[-1$c43]&<]  Try it online! Phooey is an extended form of Foo, adding input, sane loops, and some helper functions. # Swift, 218 169 bytes Sadly, I could not use map instead of for loops, since it timed out. func r(i:Int)->String{var l="";for j in(0...i*2){for k in(0..<i*2-1){l+=j==0&&k<i||j>0&&(j>i ?j-i-1:i-j)...(j>i ?i*3-1-j:i+j-2)~=k ?"+":" "};if j<i*2{l+="\n"}};return l}  Prettyfied: func r(i: Int) -> String { var l = ""; for j in (0 ... i * 2) { for k in (0 ..< i * 2 - 1) { l += (j == 0 && k < i) || (j > 0 && (j > i ? j - i - 1 : i - j) ... (j > i ? i * 3 - 1 - j : i + j - 2) ~= k) ? "+" : " " } if j < i * 2 { l += "\n" } } return l }  Try it online! # Retina, 35 bytes ¶<,-2\+ \+ (\++)$
$&$%"$1++ ^O  Takes unary + as input Try it online! # CJam, 34 bytes q~:X,'+X*n{_X-~' *\2*)'+*+}%_W%+:n  Try it online! Still wrapping my head around CJam, could probably be improved. # R, 141 139 137 135 126 bytes Another approach borrowing from the other R answer to this challenge by ngm as well as this other answer from Kirill L.: function(n)cat(c(g(n),format(sapply(c(1:n,n:1),function(i)g(2*i-1)),j="c",w=2*n-1)),sep=" ") g=function(x)intToUtf8(rep(43,x))  Try it online! Below a fully recursive solution growing the initial line and the diamond at each step: function(n)cat(" ",intToUtf8(rbind(t(f(n)[c(1:n,n:1),-1]),13)),sep="") f=function(n)"if"(n,{cat("+");rbind(cbind(32,f(n-1),32),43)},43)  Try it online! # Excel VBA, 89 bytes Anonymous VBE immediate window function that takes input as a unary number with + tally marks from range [A1] (ie [A1]="++++") that outputs to the VBE immediate window. ?[A1]:n=[Len(A1)]:For i=-n To n:i=i-(i=0):j=Abs(i):?Spc(j-1)Mid([A1&A1],1,2*(n-j)+1):Next  ### Alternate Version, 96 bytes Anonymous VBE immediate window function that takes input as a decimal number from range [A1] that outputs to the VBE immediate window. ?[Rept("+",A1)]:For i=[-A1]To[A1]:i=i-(i=0):[B1]=Abs(i):?Spc([B1]-1)[Rept("+",2*(A1-B1)+1)]:Next  # MY-BASIC, 126 bytes An anonymous function that takes input as a unary number with + tallys Input"",s$
n=Len(s$) Print s$;
For i=-n To n
j=Abs(i)
If i Then
For k=2 To j
Print" "
Next
Print Mid(s$+s$,1,2*(n-j)+1);
Next


Try it online!

0input"",s$:n=len(s$):?s$:fork=2ton:t$=t$+" ":nextk:fork=0-nton:f=abs(k):iffthen?mid$(t$+s$+s$,n-f+1,2*n-f) 1nextk  Try it online! # C (gcc), 111 bytes Rather than having two loops (one for [0..i-1] and [i-i..0]), I loop for 2*i iterations and compute the offsets from 0 for the first half and from 2*i for the last half. printf() takes care of the spacing! f(i,j,k){char s[9999];for(memset(s,43,j=2*i+1),k=-1;printf("%*.*s\n",i+k,~k?2*k+1:i,s),--j;)k=(j>i)?2*i-j:j-1;}  Try it online! • Suggest x-~x instead of 2*x+1 and j>i instead of (j>i) Commented Aug 4, 2018 at 8:15 # Jelly, 18 17 14 13 bytes ”+ẋṄ¹Ƥz⁶ŒBṚ;$


Try it online!

-1 byte thanks to Erik the Outgolfer

”+ẋṄ¹Ƥz⁶ŒBṚ;$”+ '+' literal ẋ repeat this <input> number of times: '+++' Ṅ print this string + a new line ¹Ƥ get the prefixes: ['+', '++', '+++'] z transpose and fill with... ⁶ space (' '). yields ['+++', ' ++', ' +'] ŒB palindromize each row: ['+++++', ' +++ ', ' + '] Ṛ;$      reverse and append with itself.

• ”  -> ⁶ Commented Jun 2, 2018 at 11:56
• @EriktheOutgolfer of course, thanks Commented Jun 2, 2018 at 15:41

# Oracle SQL, 166 bytes

select decode(x,1,lpad('x',n,'x'),lpad(' ',n-y,' ')||lpad('x',2*y-1,'x'))from(select level x,abs(trunc((level-2)/n)*(2*n+1)+1-level)y,n from t connect by level<2*n+2)


Test in SQL*PLus (This table is used as an input create table t(n) as select 5 from dual)

SQL> set pages 0 lines 2000 feedback off
2  from(select level x,abs(trunc((level-2)/n)*(2*n+1)+1-level)y,n from t connect by level<2*n+2)
3  /
xxxxx
x
xxx
xxxxx
xxxxxxx
xxxxxxxxx
xxxxxxxxx
xxxxxxx
xxxxx
xxx
x


# C# (Visual C# Interactive Compiler), 105 bytes

n=>new int[2*n+1].Select((_,i)=>new String('+',i<1?n:i>n?4*n-2*i+1:2*i-1).PadLeft(i<1?0:i>n?3*n-i:n+i-1))


Try it online!

The idea is to create an enumerator with length equal to the number of returned lines. Iterate over the enumerator and return strings of + characters with varying padding dependent upon the current index in the enumeration.

// n is the diamond size
n=>
// create an array to iterate over
new int[2*n+1]
// this version of select includes an index
.Select((_,i)=>
// create the diamond body
new String('+',i<1?n:i>n?4*n-2*i+1:2*i-1)


# Wolfram Language (Mathematica), 97 bytes

d[n_]:=Column[Row/@(Drop[Prepend[DiamondMatrix[n][[All,2;;]],Table[1,n]],{n+2}]/.{1->"+",0->""})]


Try it online!

# Java 11, 159155154 153 bytes

n->{var r="";for(int i=n,j;i-->0;r+=i>0?"\n":"")for(j=-n;++j<n;)r+=(j<0?-j:j)<n-i?"+":" ";return"+".repeat(n)+"\n"+r+"\n"+new StringBuffer(r).reverse();}


-5 bytes thanks to @ceilingcat.

Explanation:

Try it online.

n->{                       // Method with integer parameter and String return-type
var r="";                //  First-line String, starting empty
for(int i=n,j;i-->0      //  Loop i in the range (n,0]
;                    //    After every iteration:
r+=i>0?"\n":"")     //     Add a new-line, unless it's the last iteration of i
for(j=-n;++j<n;)       //   Inner loop j in the range (-n,n]
r+=                  //    Append the result with:
(j<0?-j:j)        //     If the absolute value of j
<n-i?             //     is smaller than the input n minus i:
"+"          //      Append a "+"
:             //     Else:
" ";         //      Append a space " " instead
return"+".repeat(n)+"\n" //  Return the input n amount of "+", plus a new-line,
+r+"\n"            //  plus the result-String, plus another new-line,
+new StringBuffer(r).reverse();}
//  plus the result-String again reversed


# Japt-R, 1816 15 bytes

õÈç+²Ãû ê ÕiUç+


Try it

# Zephyr, 241 bytes

input n as Integer
set s to""
for i from 1to n
set s to"+"+s
next
print s
for i from 1to n*2
set s to""
if i>n
set r to(i-n)-1
else
set r to n-i
end if
for j from 2to(n-r)*2
set s to"+"+s
next
for j from 1to r
set s to" "+s
next
print s
next


Try it online!

Not having a string multiplication operator really hurts here.

### Ungolfed/commented

# Input number
input n as Integer

# Build string of that many plusses in s
set s to ""
for i from 1 to n
set s to s + "+"
next
# Output it
print s

# Output diamond
for i from 1 to n * 2
# Build string for each line in s
set s to ""
# Set r to the number of spaces on the current line: n-1, ... , 1, 0, 0, 1, ... , n-1
if i > n
set r to (i - n) - 1
else
set r to n - i
end if
# Add that many spaces to s
for j from 1 to r
set s to s + " "
next
# Add (n - r) * 2 - 1 plusses to s
for j from 2 to (n - r) * 2
set s to s + "+"
next
# Output it
print s
next


# Regenerate, 70 bytes

(\+{$~1})( ( {#3-1}! {#1-1})(\+\+$4!\+)){#1}(
( $6!)(\+{#7-2}!$4)){#1}


Attempt This Online!

### Explanation

For clarity, space is replaced with _ and newline is replaced with ¶ in the explanation.

(\+{$~1})  Group 1: Repeat + a number of times equal to the input. (¶...){#1}  Group 2 generates the top half of the diamond. Repeat it (length of group 1) times. It contains groups 3 and 4. (_{#3-1}!_{#1-1})  Group 3: Add spaces, one less than the last time we matched group 3; or, if group 3 hasn't been matched yet, one less than the length of group 1. (\+\+$4!\+)


Group 4: Add plusses, two more than the last time we matched group 4; or, if group 4 hasn't been matched yet, a single plus.

(¶...){#1}


Group 5 generates the bottom half of the diamond. Repeat it (length of group 1) times. It contains groups 6 and 7.

(_$6!)  Group 6: Add spaces, one more than the last time we matched group 6; or, if group 6 hasn't been matched yet, none. (\+{#7-2}!$4)


Group 7: Add plusses, two less than the last time we matched group 7; or, if group 7 hasn't been matched yet, copy the last match of group 4.