25
\$\begingroup\$

Problem

Given a positive integer n where n < 100

Output a diamond pattern as follows:

Input n=1

/\/\
\/\/

Input n=2:

 /\      /\
//\\/\/\//\\
\\//\/\/\\//
 \/      \/

Input n=3:

  /\                /\
 //\\  /\      /\  //\\
///\\\//\\/\/\//\\///\\\
\\\///\\//\/\/\\//\\\///
 \\//  \/      \/  \\//
  \/                \/

Input n=4:

   /\                              /\
  //\\    /\                /\    //\\
 ///\\\  //\\  /\      /\  //\\  ///\\\
////\\\\///\\\//\\/\/\//\\///\\\////\\\\
\\\\////\\\///\\//\/\/\\//\\\///\\\\////
 \\\///  \\//  \/      \/  \\//  \\\///
  \\//    \/                \/    \\//
   \/                              \/

And so on.

Rules

  • Program and function allowed.
  • Trailing whitespace allowed.
  • Leading whitespace on lines with no / or \ allowed.
  • Trailing and leading newlines allowed.
  • Shortest code in bytes win

This is probably pretty related

\$\endgroup\$
  • 2
    \$\begingroup\$ @carusocomputing You're hallucinating right now... \$\endgroup\$ – Erik the Outgolfer Jun 22 '17 at 14:13
  • 9
    \$\begingroup\$ This happened. \$\endgroup\$ – dzaima Jun 22 '17 at 14:33
  • 1
    \$\begingroup\$ @dzaima to the sandbox with it! \$\endgroup\$ – Magic Octopus Urn Jun 22 '17 at 14:33
  • 1
    \$\begingroup\$ @carusocomputing Sure, but first I have to figure out why and how it happened :p \$\endgroup\$ – dzaima Jun 22 '17 at 14:34
  • 1
    \$\begingroup\$ Also related codegolf.stackexchange.com/q/56032/15599 \$\endgroup\$ – Level River St Jun 22 '17 at 21:52

10 Answers 10

12
\$\begingroup\$

SOGL V0.12, 24 bytes

ā.∫ā;∫ \*+}ø┼↔±╬¡;øΚ┼}╬³

Try it Here!

Explanation:

ā                         push an empty array (the main canvas)
 .∫                  }    iterate over the input, pushing 1-indexed iteration
   ā;                       push an empty array below the iteration
     ∫    }                 iterate over the iteration counter
       \*                     push current iteration amount of slashes
         +                    append those to the 2nd array
           ø┼               append nothing (so it'd space the array to a square)
             ↔±             reverse horizontally (swapping slashes)
               έ           quad-palindromize with 0 overlap and swapping characters as required
                 ;          get the canvas ontop
                  øΚ        prepend to it an empty line (so the now bigger romb would be one up)
                    ┼       append horizontally the canvas to the current romb
                      ╬³  palindromize horizontally with no overlap and swapping characters
\$\endgroup\$
  • 2
    \$\begingroup\$ Wow, , that's a sick command. \$\endgroup\$ – Magic Octopus Urn Jun 22 '17 at 15:16
  • \$\begingroup\$ @carusocomputing Pretty recent addition too. relevant file. Still have to figure out what to do with the remaining 190 characters \$\endgroup\$ – dzaima Jun 22 '17 at 15:19
  • \$\begingroup\$ Wow, so you have 190 free commands in SOGOL and you can already golf this efficiently? \$\endgroup\$ – Magic Octopus Urn Jun 22 '17 at 15:51
  • 1
    \$\begingroup\$ @carusocomputing I meant 190 free commands for lol \$\endgroup\$ – dzaima Jun 22 '17 at 15:52
  • 2
    \$\begingroup\$ @carusocomputing But as a fun fact, (approximately) 90/256 chars are not implemented and 61/256 don't have any documentation \$\endgroup\$ – dzaima Jun 22 '17 at 16:06
7
\$\begingroup\$

Charcoal, 30 27 bytes

F⁺¹N«Fι«F⁴«↙⁻ικ↑⟲T»←»Mι←»‖M

Try it online! Link is to verbose version of code. Explanation: Charcoal's drawing primitives can't quite draw a diamond, because the diagonal movements stay on squares of the same parity. Edit: The new solution is to draw one side of a diamond and then rotate the entire canvas ready to draw the next side, allowing a diamond to be drawn in a loop. This loop is then contained in a loop to draw all the inner diamonds for each diamond. The outermost loop draws all diamonds adjacent to each other. Finally the image is mirrored.

Note that Charcoal has since been extended and another byte could be saved by using Increment.

\$\endgroup\$
  • \$\begingroup\$ Where are the 0.5 char movements when you need them :( \$\endgroup\$ – CalculatorFeline Jun 22 '17 at 15:58
6
\$\begingroup\$

APL (Dyalog), 70 69 66 bytes

B←{'/\ '['\/'⍳⍺⍺⍵]}
C←⊢,⌽B
C(⊢⍪⊖B)⊃,/{C⊖A↑⊖' /'[⍵≤∘.+⍨⍳⍵+1]}¨⌽⍳A←⎕

Try it online!

Assumes ⎕IO←0, which is standard on many systems, so the program is 0-indexed.

This is a tradfn that takes input via STDIN.

Explanation

(slightly outdated)

Note that is the left argument, is the right argument and ⍺⍺ is the left operator.

B is a function that helps in mirroring the diamonds. It takes the string as the right argument and the reverse function as the left (so B is an operator).

B←{'/\ '['\/'⍳⍺⍺⍵]}
              ⍺⍺⍵            Apply ⍺⍺ on ⍵
         '\/'⍳               Find the index of the reflected string in '\/' (if the character is not found in `'\/'`, then return an index out of the bounds of the string, ie `2` if the character is a space)
   '/\ '[        ]           Use these indexes on '/\ ' to reflect the '/\' characters

And now we go to the main part of the program.

A←⎕              Assign the input to variable A
⍳                Create a range 0 .. A-1
⌽                Reverse it so that it becomes A-1 .. 0
¨                For each element do (the right argument is the element):
 ⍳⍵+1             Create a range 0 .. ⍵
 ∘.+⍨             Create an addition table using the range to result in a matrix like so:
                   0+0 0+1 0+2 .. 0+⍵
                   1+0 1+1 1+2 .. 1+⍵
                   2+0 2+1 2+2 .. 2+⍵
                   ...
                   ⍵+0 ⍵+1 ⍵+2 .. ⍵+⍵
 ⍵≤              The elements of the matrix that are greater than or equal to the ⍵,
                 this creates a triangle matrix that looks like this:
                   0 0 .. 0 1
                   0 0 .. 1 1
                   ..
                   1 1 .. 1 1
 ' /'[...]       Index it in ' /' to get a character matrix
                 (ie replace 0s with spaces and 1s with '/'s)
 ⊖               Flip this vertically
 A↑              Pad the top spaces

This is necessary to ensure that all the triangles created for every element in the range ⌽⍳A have the same height so that they can be later concatenated with each other.

 ⊖               Flip the matrix vertically again to go back to the original state
 (⊢,  )          Concatenate it with
    ⌽B           itself, but flipped horizontally
,/              Concatenate all triangles formed by the range operator
⊃               The resulting matrix is nested, so this operator "un-nests" it

Now the top left part of the pattern is complete. All that's remaining is to flip it vertically and then horizontally.

(⊢⍪⊖B)          Concatenate the resulting matrix with itself but flipped vertically
                (the vertically flipped matrix is concatenated below of the original matrix)
                Now the left part of the pattern is complete
(⊢,⌽B)         Concatenate the resulting matrix with itself flipped horizontally

And that's it! The output is a character matrix with /\s and padded with spaces.

\$\endgroup\$
6
\$\begingroup\$

05AB1E, 47 43 41 35 34 33 32 bytes

'/×ηηvy∞.C.Bø€∞¹NαGð.ø}})øíJ.B»∞

Try it online!

(-4 bytes thanks to @Emigna who suggested 3 improvements)


This explanation was for the earlier version, there have been a few iterations since then.

>                                          # [2]
 '/×                                       # ['//']
    η                                      # ['/','//']
     €η                                    # [['/'], ['/', '//']]
       vy                    }             # {For each element...}
         ∞                                 # Mirror horizontally.
          ¶¡                               # Split mirror on newlines.
            N£                             # Shave each diamond down a layer.
              .C                           # Horizontal center.
                .B                         # Pad for the transpose.
                  ø                        # Transpose.
                   €∞                      # Mirror each (vertically).
                     ¹NαFð.ø}              # Pad each list for transpose (verticaly).
                              )            # Wrap back to list...
                               €.B         # Pad each horizontally.
                                  ¦        # Remove the random numbers?
                                   ø       # Back to horizontal...
                                    €R     # Reverse to get correct order.
                                      J    # Join, no spaces.
                                       »   # Join newlines.
                                        ∞  # Final horizontal mirror.
\$\endgroup\$
  • \$\begingroup\$ There are spaces between your diamonds \$\endgroup\$ – LiefdeWen Jun 22 '17 at 14:30
  • \$\begingroup\$ @LiefdeWen is this okay? With trailing and preceeding newlines? \$\endgroup\$ – Magic Octopus Urn Jun 22 '17 at 15:15
  • \$\begingroup\$ You can use prefixes η instead of suffixes as they are the same for this string. \$\endgroup\$ – Emigna Jun 22 '17 at 15:35
  • \$\begingroup\$ is the same as ¨ here and €R is í. \$\endgroup\$ – Emigna Jun 22 '17 at 15:44
  • \$\begingroup\$ @Emigna I golfed some of that out, but thank you! You going to attempt a 33 byte answer that's 100% different :P? \$\endgroup\$ – Magic Octopus Urn Jun 22 '17 at 16:08
5
\$\begingroup\$

CJam, 65 63 bytes

q~_,:)_W%\+f{_2*S*a@2$-*\_,f{)'/*\Se[_W%'/'\er+}_W%Wf%+1$++}zN*

Try it online!

Explanation

In this explanation, I will refer to the input number as n.

q~        e# Read and eval the input (push n to the stack).
_,        e# Copy it an get the range [0 .. n-1].
:)        e# Increment each element to get [1 .. n].
_W%       e# Copy it and reverse it.
\+        e# Prepend the reverse to the original range, resulting in [n n-1 .. 1 1 .. n-1 n].
f{        e# Map over each number x in the range using n as an extra parameter:
 _2*S*a   e#  Push a string containing n*2 spaces, and wrap it in an array.
 @2$-     e#  Push n-x.
 *        e#  Repeat the space string from before n-x times.
 \        e#  Bring x back to the top.
 _,       e#  Copy it and get the range [0 .. x-1].
 f{       e#  Map over each number y in this range, using x as an extra parameter:
  )       e#   Increment y.
  '/*     e#   Repeat '/' y times.
  \Se[    e#   Pad the resulting string to length x by adding spaces to the left.
  _W%     e#   Copy the result and reverse it.
  '/'\er  e#   Replace '/' with '\' in that.
  +       e#   Concatenate to the other string. This forms one row of one diamond.
 }        e#  (end map, now we have the top half of a diamond of size x)
 _W%      e#  Copy the half-diamond and reverse it.
 Wf%      e#  Reverse each row.
 +        e#  Concatenate to the top half. Now we have a full diamond of size x.
 1$++     e#  Put the spaces from before at the beginning and end. This is to pad the top
          e#  and bottom of the smaller diamonds.
}         e# (end map)
z         e# Transpose.
N*        e# Join with newlines. Implicit output.
\$\endgroup\$
  • \$\begingroup\$ Out of curiosity, why e# in the explanation? \$\endgroup\$ – Magic Octopus Urn Jun 22 '17 at 15:48
  • 1
    \$\begingroup\$ @carusocomputing It's a comment, so you can run the explanation itself. Not really necessary but ¯\_(ツ)_/¯ \$\endgroup\$ – Business Cat Jun 22 '17 at 15:51
  • 1
    \$\begingroup\$ @carusocomputing # isn't a comment in CJam - sourceforge.net/p/cjam/wiki/Basic%20operators/#number-sign - even though it is in many other languages. Since CJam is a golfing language, all one-character commands are used for golf-appropriate functionality. Comments are only useful for ungolfed code, so it uses a 2-character sequence, thus freeing the one-character sequence up for something else \$\endgroup\$ – Joe Jun 23 '17 at 12:02
3
\$\begingroup\$

Python 2, 152 147 143 140 bytes

-1 byte thanks to musicman523

n=input()
r=range(n)
r+=r[::-1]
for x,i in enumerate(r):a,b='/\\\/'[i<x::2];s=' '*(n+~i);print''.join((s+a*n)[:n-j]+(b*-~i+s)[j:]for j in r)

Try it online!

This works by chopping the inner columns of the largest diamond to make the smaller ones, using [0,..,n,n,..,0] to control the amount of columns to remove.

\$\endgroup\$
  • \$\begingroup\$ You can get one cheap byte by changing r=r+ to r+= \$\endgroup\$ – musicman523 Jun 22 '17 at 15:12
3
\$\begingroup\$

Pyth, 35 32 bytes

j.tsm+Jm.[yQS*"\/"k\ Sd_M_Js__BS

Test suite

Done to see how my and @LeakyNun's approaches would differ.

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

Dyalog APL, 46

{⊃,/⍵∘{'/ \'[2+((-⍪⊖)⌽,-)(-⍺)↑∘.≥⍨⍳⍵]}¨(⌽,⊢)⍳⍵}
\$\endgroup\$
  • \$\begingroup\$ Welcome to PPCG and nice first answer! Seeing as how this is a dfn, I added the {} to your answer as they must be included. \$\endgroup\$ – Kritixi Lithos Jun 28 '17 at 9:28
2
\$\begingroup\$

Pyth, 49 bytes

LC.tb)L+_b.rb"\/"js_,J'MsMCyMy_Mm'Myd._._*\/Q__MJ

Try it online!

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

V, 38 bytes

Àá/Àá\ò2ÙlX$xòÍî
òYGpVæHÄÓ¯¨¯*Ü*©Ü/ ± 

Try it online!

\$\endgroup\$

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.