加倍一些钻石


25

问题

给定一个正整数n,其中n < 100

输出菱形图案,如下所示:

输入项 n=1

/\/\
\/\/

输入n=2

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

输入n=3

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

输入n=4

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

等等。

规则

  • 允许的程序和功能。
  • 允许尾随空格。
  • 没有/\允许的行上的前导空格。
  • 允许尾随换行符。
  • 以字节为单位的最短代码获胜

这可能很相关


2
@carusocomputing您现在正在幻觉中……
Outgolfer的Erik


1
@dzaima到它的沙箱!
Magic Octopus Urn

1
@carusocomputing当然,但是首先我必须弄清楚它的发生原因和方式:p
dzaima

Answers:


12

SOGL V0.12,24个字节

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

在这里尝试!

说明:

ā                         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

2
哇,这真是个恶心的命令。
Magic Octopus Urn

@carusocomputing也很新。相关文件。仍然必须弄清楚如何处理剩余的190个字符
dzaima

哇,所以您在SOGOL中有190个免费命令,您已经可以有效地打高尔夫球了吗?
Magic Octopus Urn

1
@carusocomputing我的意思是190个免费命令来洛尔
dzaima

2
@carusocomputing但有趣的是,(大约)未实现90/256个字符,并且61/256没有任何文档
dzaima

7

木炭30 27字节

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

在线尝试!链接是详细版本的代码。说明:木炭的绘制图元不能完全绘制钻石,因为对角线移动保持在相同奇偶性的正方形上。编辑:新的解决方案是绘制菱形的一侧,然后旋转整个画布以准备绘制另一侧,从而使菱形可以循环绘制。然后将此循环包含在一个循环中,以绘制每个钻石的所有内部钻石。最外面的循环将所有菱形彼此相邻。最终,图像被镜像。

请注意,此后已扩展了炭笔,可以使用来保存另一个字节Increment


您需要时,0.5字符移动在哪里:(
CalculatorFeline

6

APL(Dyalog)70 69 66字节

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

在线尝试!

假设⎕IO←0,这在许多系统上都是标准的,因此该程序的索引为0。

这是一个通过STDIN进行输入的交易。

说明

(有点过时)

注意,这是left参数,是right参数,⍺⍺是left运算符。

B是有助于镜像钻石的功能。它使用字符串作为右参数,并将反向函数作为左(B运算符)。

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

现在我们进入程序的主要部分。

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

必须确保为该范围内的每个元素创建的所有三角形都⌽⍳A具有相同的高度,以便以后可以将它们彼此串联。

                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

现在,该模式的左上部分已完成。剩下的就是先垂直翻转然后水平翻转。

(⊢⍪⊖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

就是这样!输出是一个带有/\s并用空格填充的字符矩阵。


6

05AB1E47 43 41 35 34 33 32字节

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

在线尝试!

(感谢@Emigna提出了3处改进,提供了4个字节)


此说明适用于早期版本,此后进行了几次迭代。

>                                          # [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.

您的钻石之间有空隙
LiefdeWen

@LiefdeWen是这个好吗?有尾随和换行符吗?
Magic Octopus Urn

您可以使用前缀η而不是后缀,因为它们与此字符串相同。
Emigna

¨这里相同并且€Rí
Emigna

@Emigna我打了一些球,但是谢谢!您将尝试一个33字节的答案,该答案有100%的不同:P?
Magic Octopus Urn

5

CJam65 63字节

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

在线尝试!

说明

在此说明中,我将输入数字称为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.

出于好奇,为什么要e#解释?
Magic Octopus Urn

1
@carusocomputing这是一条注释,因此您可以自己运行解释。不是真的有必要,但¯\ _(ツ)_ /¯
商务猫

1
@carusocomputing #不是CJam评论- sourceforge.net/p/cjam/wiki/Basic%20operators/#number-sign -即使是在许多其他语言。由于CJam是一种高尔夫语言,因此所有一个字符的命令都将用于适合高尔夫的功能。注释仅对不带代码的代码有用,因此它使用2个字符的序列,从而释放了1个字符的序列供其他使用
Joe

3

Python 2中152 147 143 140个字节

-1个字节,感谢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)

在线尝试!

这是通过将最大的菱形的内柱切成较小的柱来进行的,[0,..,n,n,..,0]用于控制要去除的柱的数量。


您可以通过更改r=r+r+=
musicman523 '17


3

Dyalog APL,46岁

{⊃,/⍵∘{'/ \'[2+((-⍪⊖)⌽,-)(-⍺)↑∘.≥⍨⍳⍵]}¨(⌽,⊢)⍳⍵}

欢迎使用PPCG,并且是一个不错的第一答案!看到这是dfn的样子,我在{}您的答案中添加了,因为它们必须包括在内。
Kritixi Lithos


By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.