六边形的三角形


20

假设由|/\字符组成的六边形的无限平铺。

 / \ / \ / \ / \
|   |   |   |   |
 \ / \ / \ / \ /  etc.
  |   |   |   |
   \ / \ / \ /

给定输入n > 0,输出该平铺的三角形部分,如以下示例所示,并_在六边形的中间锚定一个:

n=1
\_/

n=2
\/ \/
 \_/

n=3
\  |  /
 \/ \/
  \_/

n=4
\/ \ / \/
 \  |  /
  \/ \/
   \_/

n=5
\  |   |  /
 \/ \ / \/
  \  |  /
   \/ \/
    \_/

n=6
\/ \ / \ / \/
 \  |   |  /
  \/ \ / \/
   \  |  /
    \/ \/
     \_/

n=7
\  |   |   |  /
 \/ \ / \ / \/
  \  |   |  /
   \/ \ / \/
    \  |  /
     \/ \/
      \_/

n=8
\/ \ / \ / \ / \/
 \  |   |   |  /
  \/ \ / \ / \/
   \  |   |  /
    \/ \ / \/
     \  |  /
      \/ \/
       \_/

and so on

规则

  • 前导/尾随换行符或其他空格是可选的,只要字符正确排列即可。
  • 完整的程序或功能都是可以接受的。如果是函数,则可以返回输出而不是打印输出。
  • 可以将输出输出到控制台,另存为图像,以字符串列表形式返回等。
  • 标准漏洞禁止出现。
  • 这是因此所有常见的高​​尔夫规则都适用,并且最短的代码(以字节为单位)获胜。

如果沿结果的顶部计数的端点的数目,将得到A029578与(天然与偶数交错数)的偏移量4 243648510612714,...
工程师吐司

“保存为图像”是什么意思?这是被标记为ascii-art吗?
tsh

@tsh对于HyperCard之类的东西,在画布上的输出等同于“ stdout”输出。我不是挑剔如何显示输出。
AdmBorkBork,

Answers:


8

Python 2 2,86个字节

i=k=input()
while i:i-=1;print(" "*(k+~i)+"\\"+i*' /  |\  '[i%2::2])[:k-~i]+"_/"[i>0:]

在线尝试!

Erik的花样之一使我可以打3个字节!由于乔纳森·艾伦节省了3个字节。

如何运作

首先,它从STDIN获取输入,并将其分配给两个单独的变量ik。然后,当变量i为真时,我们将其递减并相应地生成字符串。这是从输入开始循环的简写-从1一直到0。

生成字符串

我将其分成更多部分:

  • 首先,通过获得领先的间距" "*(k+~i)。由于i是通过范围(input,0]映射的,因此必须从k(安全存储的原始输入)中减去它,然后递减并重复多次。

  • +"\\"-将字符添加"\"到上方空格。

  • ' / |\ '[i%2::2]- 以以下方式生成我们的两个字符串,即"/ \ "" | "

    • 如果i为奇数,则i%21,因此[i%2::2]从索引1(0索引)开始返回较大字符串的每2个字符。

    • 如果i为偶数,则i%21,因此上述机制除了从索引0开始之外,其余功能相同。

  • +~-i*-重复上面生成的字符串,"/ \ "或者" | "i-1次,并将其附加到其他字符串。按位运算符(~-按位补码,等价于-1减去i)的好处是,在这种情况下,不需要括号。

  • [:k-~i]-获取上面串联的字符串的所有字符,直到索引k-〜i = k-(-1-i)= k +1 + i为止。

  • +"_/"[i>0:]-仅"/"i≥1时才加,否则追加_/

完整的示例/执行细节

让我们举一个例子,说明输入4时事物如何工作:

i=k=input()        # i and k are assigned to 4.
while i:           # Starts the loop. The initial value of i is 4.
i-=1;              # Decrement i. i is now 3.
" "*(k+~i)         # A space repeated k - 1 - i = 4 - 1 - 3 = 0 times.
+"\\"              # Plus the character "\". CS (Current string): "\".
' /  |\  '[i%2::2] # The string ' /  |\  '[3%2::2] = ' /  |\  '[1::2] = "/ \ ".
i*                 # ^ repeated i = 3 times: "/ \ / \ / \ ".
+                  # And concatenate. CS: "\/ \ / \ / \ "
[:k-~i]            # Get the characters of ^ up to index k + 1 + i = 4 + 1 + 3 = 8.
                   # CS: "\/ \ / \".
+"_/"[i>0:]        # Append "_/"[i>0:] = "_/"[3>0:] = "_/"[1:] = "/".
                   # CS: "\/ \ / \/".
print              # Output the result "\/ \ / \/".
while i:           # i is truthy (> 0), thus we loop again.
i-=1;              # Decrement i. i becomes 2.
" "*(k+~i)         # " " repeated 4 - 2 - 1 = 1 time. 
+"\\"              # Plus "\". CS: " \".
' /  |\  '[i%2::2] # ' /  |\  '[2%2::2] = ' /  |\  '[::2] = "  | ".
+i*                # Repeat i = 2 times and append: "  | ". CS: " \  |  |".
[:k-~i]            # CS up until k + 1 + i = 4 + 2 + 1 = 7. CS: " \  |  ".
+"_/"[i>0:]        # Append "/". CS: " \  |  /".
print              # Outputs the CS: " \  |  /".
while i:           # i is truthy (> 0), thus we loop again.
i-=1;              # Decrement i. i is now 1.
" "*(k+~i)         # " " repeated 4 - 1 - 1 = 2 times. 
+"\\"              # Plus "\". CS: "  \".
' /  |\  '[i%2::2] # ' /  |\  '[2%2::2] = ' /  |\  '[::2] = "/ \ ".
+i*                # Repeat i = 1 time and append: "/ \ ". CS: "  \/ \ ".
[:k-~i]            # CS up until k + i + 1 = 4 + 2 = 6. CS: "  \/ \".
+"_/"[i>0:]        # Append "/". CS: "  \/ \/".
print              # Outputs the CS: "  \/ \/".
while i:           # i is truthy (> 0), thus we loop again.
i-=1;              # Decrement i. i is now 0.
" "*(k+~i)         # " " repeated 4 - 1 - 0 = 3 times. 
+"\\"              # Plus "\". CS: "   \".
' /  |\  '[i%2::2] # ' /  |\  '[1%2::2] = ' /  |\  '[1::2] = "  | ".
+i*                # Repeat i = 0 times and append: "   \". CS: "   \".
[:k-~i]            # CS up until k + i + 1 = 4 + 0 + 1 = 5. CS: "   \".
+"_/"[i>0:]        # Append "_/" (because i > 0 is False since i == 0). CS: "  \_/".
print              # Outputs the CS: "  \_/".
while i:           # i == 0, hence the condition is falsy and the loop ends. 
                   # Program terminates.

将移至i-=1循环的开头,并使用略有不同的右侧形式将其减小到87个字节
乔纳森·艾伦

...实际上使用类似您的右侧编队的格式甚至更好,为86个字节:)
Jonathan Allan

@JonathanAllan ...谢谢!(尽管重做解释将是……艰难!…… 叹气
Xcoder先生17年

@JonathanAllan我找到了一个不会反转减量语句顺序的替代方法
Xcoder先生17年



2

Mathematica,131个字节

Join[Table[s=StringRiffle@Table[If[OddQ@i,"/ \\"," | "],⌈i/2⌉];""<>{"\\",If[!OddQ@i,{" ",s," "},s],"/"},{i,#-1,1,-1}],{"\_/"}]&   


返回字符串列表

在线尝试!




2

Python 2中123 112 110 109 100 98 96个字节

i=n=input()
while i:a=i%2;print' '*(n-i)+'\%s/'%['_',((-~i/2)*'/   \  |'[a::2])[a:~a]][i>1];i-=1

在线尝试!

  • 按照Rod的答案,通过使用输入和字符串格式保存了一堆字节
  • 多亏了Xcoder先生,节省了2个字节

1
您可以通过更换节省2个字节-1-a~a(正如我在我的答案一样)。
Xcoder先生17年

@ Mr.Xcoder谢谢:)
TF

1

Python 2,103个字节

i=n=input()
while i:print' '*(n-i)+'\%s/'%' '.join(['/\\'*(-~i/2),['_',' '+'| '*(i/2)][i>1]][i%2]);i-=1

在线尝试!





0

Haskell,101 99字节

j 1=["\\_/"]
j n|r<-([1,3..n-1]>>)=('\\':cycle[init$r"/ \\ ",' ':r" |  "]!!n++"/"):map(' ':)(j$n-1)

返回行列表。

在线尝试!

怎么运行的:

j 1=["\\_/"]               -- base case, n=1

j n                        -- for all other n
   |r<-([1,3..n-1]>>)      -- let r be the function that makes n/2 copies of
                           -- it's argument
   =                       -- the result is
      '\\':                --  a backslash, followed by
      cycle[  ]!!n         --  the inner part, which is
          init$r"/ \\ "    --    all but the last char of some copies of
                           --    "/ \ " for even line numbers, or
          ' ':r" |  "      --    some copies of " |  " prepended by a space
                           --    for odd line numbers
                           --    (chosen by indexing an infinite list of
                           --     both values alternating)   
      ++"/"                --  followed by a slash
    :                      --  and append a
               j$n-1        --  recursive call with n-1
      map(' ':)            --  where each line is prepended by a space

编辑:@Laikoni保存了两个字节。谢谢!


([1,3..n-1]>>)可以代替([1..div n 2]>>)
Laikoni '17

0

Java(OpenJDK 8)315306字节

i->{String r="";int j=0,k,u=i*2;char[][]c=new char[i][u+1];c[i-1][i]=95;for(;j<i;r+="".valueOf(c[j++]).replace('\0',' ')+"\n")for(k=0;k<u+1;k++){if(k==j)c[j][k]=92;if(k==u-j)c[j][k]=47;if(k>j&k<u-j)if((i-j)%2<1)c[j][k]=(k-j-1)%2<1?(char)(47+((k-j-1)/2)%2*45):32;else if((k-j-1)%4==2)c[j][k]='|';}return r;}

在线尝试!



0

JavaScript(ES6),89 85字节

f=(y,s='\\')=>--y?s+(y&1?' / \\':' |  ').repeat(y).slice(~y-y)+`/
`+f(y,' '+s):s+'_/'

演示版



0

PHP,89 + 1字节

while($k=$argn-$n)echo($p=str_pad)("",$n++),$p("\\",2*$k,$k>1?$k&1?"  | ":"/ \ ":_),"/
";

与管道一起运行-nR在线尝试


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.