25
\$\begingroup\$

March 13 is recognized as National Jewel Day, which is the theme of this challenge. So, given an integer n where n is greater than 0, create an ASCII jewel. For example:

n = 1          n = 2             n = 3
                                       ______
                     ____             /      \
 __                 /    \            \      /
/  \                \    /             \    /
\  /                 \  /               \  /
 \/                   \/                 \/

The bottom is defined as the very bottom of the jewel to the highest pair of of \/. The rest is the top. For the above example where n = 1:

Bottom: \  /    Top:   __
         \/           /  \

As you can see, the bottom is made of n + 1 layers of \/ with (1 * lines from the bottom) * 2 spaces in between with a maximum of n lines from the bottom of the jewel. If we take the second jewel (n = 2), we can see that:

 ____
/    \      
\    /  2 (or n) layers from the bottom with 1*2*2 or 4 spaces in between \/
 \  /   1 layer from the bottom with 1*1*2 or 2 spaces in between \/
  \/    The bottom (0 layers) with 1*0*2 spaces or 0 spaces in between \/

The top is made of one pair of /\ with n*2 spaces in between with n*2 underscores on top.

Rules

  • Must be able to take in any nonzero positive integers as user input
  • Must create a jewel with the specs defined above (restated here):
    • The top is made of one pair of /\ with n*2 spaces in between with n*2 underscores on top.
    • The bottom is made of n + 1 layers of \/ with (1 * lines from the bottom) * 2 spaces in between with a maximum of n lines from the bottom of the jewel.
  • Trailing newlines after the jewel, or trailing spaces on each line are allowed.
  • No standard loopholes allowed

Winning Criteria

Least bytes wins!

\$\endgroup\$
  • 4
    \$\begingroup\$ Strictly speaking "nonzero positive" is redundant -- if you wanted to include 0, you'd have to say "nonnegative". \$\endgroup\$ – Fund Monica's Lawsuit Mar 26 '17 at 23:27
  • \$\begingroup\$ Can the answer be in PETSCII? \$\endgroup\$ – Shaun Bebbers Mar 27 '17 at 8:31
  • 3
    \$\begingroup\$ As the number gets higher the "jewels" start to look less like jewels and more like pizza slices, or maybe that's just lunch time talking. \$\endgroup\$ – Marijn Stevering Mar 27 '17 at 9:52

21 Answers 21

27
\$\begingroup\$

Charcoal, 17 bytes

Code:

NβG←β_↙↙¹→↘⁺β¹‖M→

Explanation:

Nβ                      # Place the input into β
   G←β_                 # Draw a line of length β with _ as the filling character
        ↙                # Move the cursor one down and one left
         ↙¹              # Draw a line from the cursor position to one position ↙
           →             # Move the cursor 1 to the right
             ⁺β¹         # Add one to the input and..
            ↘            # Create a line pointing ↘, with the size calculated above
                ‖M→     # Mirror to the right

A very neat command is ‖M, which also automatically mirrors / into \.

Uses the Charcoal encoding.

Try it online!

\$\endgroup\$
  • \$\begingroup\$ That mirror command is really cool! Does it mirror brackets and other characters too? And is there a way to override that behavior? \$\endgroup\$ – DJMcMayhem Mar 26 '17 at 20:59
  • 2
    \$\begingroup\$ @DJMcMayhem Yes and yes :) \$\endgroup\$ – Adnan Mar 26 '17 at 21:07
  • 27
    \$\begingroup\$ Lol, you made diamonds out of charcoals! \$\endgroup\$ – SteeveDroz Mar 27 '17 at 6:55
8
\$\begingroup\$

05AB1E, 27 20 bytes

ƒN·ð×…\ÿ/}¹·'_×)R.c

Try it online!

Explanation

ƒ                      # for N in range[0 ... n]
 N·ð×                  # push N*2 spaces
     …\ÿ/              # push the string "\ÿ/" with "ÿ" replaced by the spaces 
         }             # end loop
          Â            # push a reversed copy of the top of the stack 
                       # (the largest row of the bottom of the diamond)
           ¹·'_×       # push input*2 underscores
                )      # wrap the stack in a list
                 R     # reverse the list
                  .c   # join the list on newlines, padding each row to equal length
\$\endgroup\$
  • \$\begingroup\$ Haha, nice! I believe you can change D„/\„\/‡ to Â. \$\endgroup\$ – Adnan Mar 26 '17 at 16:25
  • \$\begingroup\$ @Adnan: Yes, I just realized that myself while working on an improvement :P \$\endgroup\$ – Emigna Mar 26 '17 at 16:27
8
\$\begingroup\$

Python 2, 101 98 95 bytes

lambda n:'\n'.join([' '+'__'*n,'/'+'  '*n+'\\']+[' '*i+'\\'+'  '*(n-i)+'/'for i in range(n+1)])

Try it Online!

Anonymous function that takes in a positive integer and returns a string

Python 3.6, 92 bytes (Thanks to Ben Frankel)

lambda n:f' {"__"*n}\n/{"  "*n}\\\n'+'\n'.join(' '*i+'\\'+'  '*(n-i)+'/'for i in range(n+1))

I couldn't find an online interpreter for this version, but it is a bit shorter due to f-strings in v3.6

\$\endgroup\$
  • \$\begingroup\$ You can save three bytes in Python 3.6: lambda n:f' {"__"*n}\n/{" "*n}\\\n'+'\n'.join(' '*i+'\\'+' '*(n-i)+'/'for i in range(n+1)). Taking advantage of f-strings. \$\endgroup\$ – Ben Frankel Mar 27 '17 at 4:32
  • \$\begingroup\$ I am pretty sure repl.it has a test suite for Python 3 \$\endgroup\$ – Anthony Pham Mar 27 '17 at 21:20
  • \$\begingroup\$ @AnthonyPham repl.it and TryItOnline both use Python 3.5, I've checked \$\endgroup\$ – math junkie Mar 27 '17 at 23:27
  • \$\begingroup\$ Whoa, finally! I wonder what took Python so long. Every language deserves string interpolation ... \$\endgroup\$ – Felix Dombek Mar 28 '17 at 2:53
7
\$\begingroup\$

PHP, 123 Bytes

echo($s=str_pad)(" ",$z=1+2*$a=$argv[1],_).$s("\n/",$z+1," ")."\\\n";for($i=0;$i<=$a;)echo$s($s("",$i)."\\",$z-$i++)."/\n";

143 Bytes first version

for(;$i<3+$a=$argv[1];$i++)echo 1-$i?str_pad("",$i?$i-2:1):"/",str_pad($i>1?"\\":"",$i<2?2*$a:2*($a-$i+2)+1,$i?" ":_),$i<2?$i?"\\":"":"/","\n";

Try it here!

\$\endgroup\$
  • \$\begingroup\$ Where can I try this? \$\endgroup\$ – Anthony Pham Mar 26 '17 at 16:15
  • \$\begingroup\$ @AnthonyPham Here. \$\endgroup\$ – Adnan Mar 26 '17 at 16:17
  • \$\begingroup\$ You can make it 119 bytes: ideone.com/RPCVZe \$\endgroup\$ – Tschallacka Mar 27 '17 at 14:21
  • \$\begingroup\$ @Tschallacka if I assume that I use only a Linux Sytem \$\endgroup\$ – Jörg Hülsermann Mar 27 '17 at 14:32
  • \$\begingroup\$ Well, as long as you don't edit with notepad.exe most editors have linux line endings... i.imgur.com/QZsmf4r.png windows console will happily display \n as a real newline. So yea, y ou can shave a few bytes of your answer. \$\endgroup\$ – Tschallacka Mar 27 '17 at 14:41
6
\$\begingroup\$

V, 28 27 26 bytes

1 bytes saved thanks to @DJMcMayhem by using > instead of É

Ài__<esc>É ÙÒ r/Á\Ùr\$r/òÙlxx>

<esc> is 0x1b

Try it online!

Hexdump:

00000000: c069 5f5f 1bc9 20d9 d220 722f c15c d972  .i__.. .. r/.\.r
00000010: 5c24 722f f2d9 6c78 783e                 \$r/..lxx>

Explanation

Top:

Ài__<esc>              " Write argument times __
É<space>               " Prepend a space to the line
Ù                      " Duplicate line below cursor, cursor also moves down
Ò<space>               " Replace every character with a space
r/                     " Change the first character in the line to a /
Á\                     " Append a \ to the end of the line

Bottom:

Ù                      " Duplicate
r\                     " Change the first character in the line to a \
$r/                    " Replace the last character with a /
ò                      " Until a breaking error occurs do:
  Ù                    "  Duplicate
  lxx                  "  Remove 2 middle characters (spaces)
  >                    "  Indent by one space (implicit ending >)
                       " Implicit ending ò
\$\endgroup\$
  • \$\begingroup\$ Nice answer! You can change É<space> to > which at the end of a macro is implicitly filled in to >> \$\endgroup\$ – DJMcMayhem Mar 26 '17 at 15:28
  • \$\begingroup\$ @DJMcMayhem Nice suggestion! So the > indents one space instead of by one tab? \$\endgroup\$ – Kritixi Lithos Mar 26 '17 at 15:32
  • \$\begingroup\$ Yep! That's cause I have set expandtab and set shiftwidth=1 \$\endgroup\$ – DJMcMayhem Mar 26 '17 at 15:39
5
\$\begingroup\$

Röda, 71 bytes

f n{a=" ";[a,"__"*n,"
/",a*2*n,`\
`];seq n,0|[a*(n-_),`\`,a*2*_1,"/
"]}

Try it online!

\$\endgroup\$
5
\$\begingroup\$

JavaScript (ES6), 80 bytes

f=
n=>` ${"_".repeat(n*2)}
/${s=" ".repeat(n)}${s}\\`+s.replace(/|/g,"\n$`\\$'$'/")
<input type=number oninput=o.textContent=f(this.value)><pre id=o>

\$\endgroup\$
3
\$\begingroup\$

Python 3, 107 105 Bytes

n,s=int(input())," "
print(s+n*"__","/"+n*2*s+"\\",*[i*s+"\\"+2*(n-i)*s+"/"for i in range(n+1)],sep="\n")

Takes an int from Stdin

\$\endgroup\$
3
\$\begingroup\$

MATL, 34 bytes

QE:qgOO(t~E3O(GQXy3*tPgEhv'_/\ 'w)

Try it at MATL Online!

Explanation

QE:qg   % Create array [0 1 1 ... 1 1] of size2*(n+1)
OO(     % Turns last 1 into a 0: [0 1 1 ... 1 0]
t~      % Duplicate and negate: [1 0 0 ... 0 1]
E3O(    % Multiply by 2, turn last 2 into 3: [2 0 0 ... 0 3]
GQXy    % Push identity matrix of size n+1
3*      % Multiply by 3
tPgE    % Duplicate, flip, turn 3 into 2
h       % Concatenate the two matrices horizontally
v       % Concatenate all arrays vertically into a matrix
'_/\ '  % Push this string
w)      % Index (modular, 1-based) with the matrix into the string. Implicitly display
\$\endgroup\$
3
\$\begingroup\$

PowerShell, 76, 74 bytes

param($n)" "+'_'*2*$n;"/$(' '*$n*2)\";$n..0|%{' '*($n-$_)+"\$(' '*$_*2)/"}

Note: the online example contains a bit of wrapping as a demonstration. Place in a PoSH function or script to execute.

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Welcome to PPCG! Nice first answer, and nice to see another PowerSheller around! You can save a couple bytes by using an incrementing variable in the loop -- ' '*$i++ instead of ' '*($n-$_). \$\endgroup\$ – AdmBorkBork Mar 27 '17 at 16:20
3
\$\begingroup\$

C, 131 bytes

i;f(n){for(printf(" ",i=0);i++<n*2;)printf("_");for(printf("\n/%*c\n",n*2+1,92,i=0);i++<n+1;)printf("%*c%*c\n",i,92,(n-i)*2+3,47);}

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Where can I test this? \$\endgroup\$ – Anthony Pham Mar 26 '17 at 15:54
  • \$\begingroup\$ @AnthonyPham Tio link added. \$\endgroup\$ – Steadybox Mar 26 '17 at 16:04
  • \$\begingroup\$ Nice approach using printf's width to fill in the spaces. You can save 9 more bytes if you create a macro for the printf, remove the first i=0 and add a new variable j instead of re-initializing i to 0 on the second run: i,j;f(n){for(p(" ");i++<n*2;p("_"));for(p("\n/%*c\n",n*2+1,92);j++<n+1;p("%*c%*c\n",j,92,(n-j)*2+3,47));} \$\endgroup\$ – Claudiu Mar 27 '17 at 12:21
  • \$\begingroup\$ @Claudiu Thanks, but then the function would produce correct output only when it is called for the first time, and IIRC that's against the rules here. The function should work no matter how many times it's called. \$\endgroup\$ – Steadybox Mar 27 '17 at 13:37
  • \$\begingroup\$ @Steadybox oh I see, sorry for that. Does this apply for all codegolf questions? Looking just at this specific question it doesn't seem that it wants multiple inputs. \$\endgroup\$ – Claudiu Mar 27 '17 at 14:29
2
\$\begingroup\$

Pyth, 44 Bytes

+" "*Q"__"++\/**2Qd\\jm+++*d\ \\**2-Qd\ \/hQ

try it!

explanation

The code consists of 3 parts:

+" "*Q"__"               # pretty straightforward " "+input()*"__"
++\/**2Qd\\              # d is defined as " ":  "/"+2*input()*d+"\"
jm+++*d\ \\**2-Qd\ \/hQ  # The third part is a bit more complex so I'll explain it further:

jm                   hQ  # Map some lambda function onto range(input()+1) and join the result on newlines
  +++*d\ \\**2-Qd\ \/    # Here d is the lambda argument (so I can't use it for spaces -.-) 
  +++*d\ \\**2-Qd\ \/    # In Python: d*" "+"\\"+2*(Q-d)*" "+"/"
\$\endgroup\$
2
\$\begingroup\$

Python3, 104 bytes

n=int(input());print(" "+"__"*n+"\n/"+"  "*n+"\\")
for i in range(n+1):print(" "*i+"\\"+"  "*(n-i)+"/")

The program takes an integer from STDIN and returns the jewel into STDOUT.

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

Pip, 43 bytes

42 bytes of code, +1 for -n flag.

Ps.'_Xa*2P"/\"JsXa*2sX_.'\.sXa-_X2.'/M,a+1

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

Explanation

Constructs the first two lines separately, then the rest of the jewel with a map operation:

Ps.'_Xa*2
      a*2  Cmdline arg, times 2
   '_X     That many underscore characters
 s.        Concatenated to a space character
P          Print (with newline)

P"/\"JsXa*2
        a*2  Cmdline arg, times 2
      sX     That many space characters
 "/\"J       Join the string "/\" with the above as the separator
P            Print (with newline)

sX_.'\.sXa-_X2.'/M,a+1
                  ,a+1  Numbers from 0 up to and including a
                 M      Map the following lambda function:
sX_                      Space, repeated (fn arg) times
   .'\                   Concatenate \
      .                  Concatenate:
       sXa-_              Space, repeated (a - (fn arg)) times
            X2            repeated twice
              .'/        Concatenate /
                         Print result list, newline separated (implicit, -n flag)

Another solution

Also 42+1 bytes, this time with the -l flag:

Ys.tAL'_.sX2+,a.0(yALRVyRXD1-_)R0'\R1'/ZDs

TIO

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

Pyth, 38 bytes

j[*yQ\_j*yQpd"/\\"jm+*\ dj*\ y-Qd"\/"h
\$\endgroup\$
2
\$\begingroup\$

C, 115 bytes

#define p printf(
i;j;f(n){for(p" ");i++<n;p"__"));for(p"\n/%*c",2*n+1,92);j<=n;p"\n%*c%*c",++j,92,n*2-j*2+3,47));}

Try it online!

C, 123 bytes

Though the challenge doesn't require it, at the expense of 8 bytes the function can be made reusable (the first solution saves 8 bytes by relying on the implicit initialization of global variables).

#define p printf(
i;f(n){for(i=0,p" ");i++<n;p"__"));for(i=0,p"\n/%*c\n",2*n+1,92);i<=n;p"%*c%*c\n",++i,92,n*2-i*2+3,47));}

Try it online!

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

Batch, 152 bytes

@set s=
@for /l %%i in (1,1,%1)do @call set s=  %%s%%
@echo  %s: =_%
@echo /%s%\
@set s=\%s%/
:l
@echo %s%
@if %s:~-2%==/ set s=%s:\  = \%&goto l

Tests:

n = 1
 __
/  \
\  /
 \/

n = 2
 ____
/    \
\    /
 \  /
  \/

n = 3
 ______
/      \
\      /
 \    /
  \  /
   \/
\$\endgroup\$
  • \$\begingroup\$ I will need a test suite to test this out. \$\endgroup\$ – Anthony Pham Mar 27 '17 at 21:19
2
\$\begingroup\$

C#, 187 bytes

I'm sure there is a more compact solution out there but this is my first attempt:

var a=" "+new string('_',2*n)+"\n/"+new string(' ',2*n)+"\\\n";for(int i=n;i>0;i--){a+=new string(' ',n-i)+"\\"+new string(' ',2*i)+"/\n";}a+=new string(' ',n)+"\\/";Console.Write(a);

Try it online..

\$\endgroup\$
  • \$\begingroup\$ I will need a test suite to test this out. \$\endgroup\$ – Anthony Pham Mar 27 '17 at 21:18
1
\$\begingroup\$

JavaScript (ES6), 93 bytes

n=>(` 0
/2\\`+`
1\\4/`.repeat(k=++n)).replace(/\d/g,c=>' _'[+!+c].repeat(c&1?k-n-2:+c+--n*2))

Demo

let f =

n=>(` 0
/2\\`+`
1\\4/`.repeat(k=++n)).replace(/\d/g,c=>' _'[+!+c].repeat(c&1?k-n-2:+c+--n*2))

console.log(f(1))
console.log(f(2))
console.log(f(3))

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

dc, 121 bytes

?d1+sa2*sb32P[0sq[lvPlq1+dsqlj>h]shlj0<h]srlbsj95svlrx2607Plbsj32svlrx[\]p0sd[ldsjlrx92Plbsjlrxlb2-sb[/]pld1+dsdla>k]dskx

Try it online!

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

Perl 5 109 94 + 1 (for flag -p) = 95 Bytes

Try it Online!

$l=$_*2;$s=" "."_"x$l."\n/"." "x$l."\\\n";$s.=" "x$_."\\"." "x($l-$_*2)."/\n"for 0..$_;print$s

Can be run like so:

perl -p <name of file> <<< n

Ungolfed

$l=$_*2;
$s=" "."_"x$l."\n/"." "x$l."\\\n";
$s.=" "x$_."\\"." "x($l-$_*2)."/\n"for 0..$_;
print$s

Explanation

#Sets $l to twice the value of the input 'n'
$l=$_*2;  

#Top 2 rows of jewel adding $l underscores then newline  
#followed by '/' and $l spaces.  The '\\\n' is an escaped '\' and a newline
$s=" "."_"x$l."\n/"." "x$l."\\\n";

#The meat of the jewel generation.  It contains a for-loop
#that iterates from 0 to $_ (the input value 'n')
#The loop uses its iterator value ($_ (which overrides the outer $_))
#to determine how many leading spaces it needs to apply.
#Then it adds a '\' with '\\' followed by $l-$_*2 number of spaces
#(the inside of the jewel).  Again, while under the umbrella of the for-loop,
#the $_ refers to the iterator value of the for-loop.
#After the inner spaces, it goes on to add in the '/' and a new line
$s.=" "x$_."\\"." "x($l-$_*2)."/\n"for 0..$_;

#Lastly, it prints the compiled Scalar value $s.  (In Perl, Strings are Scalar values or references
print$s
\$\endgroup\$
  • \$\begingroup\$ I will need a test suite to test this out as not everyone quite understands or has the ability to run this the way you have stated \$\endgroup\$ – Anthony Pham Mar 27 '17 at 21:19
  • \$\begingroup\$ @AnthonyPham I added the Try It Online link \$\endgroup\$ – CraigR8806 Mar 28 '17 at 14:50

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.