六角形の三角形


20

|/\文字で構成される六角形の無限のタイル張りを想定します。

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

inputを指定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

回答:


8

Python 2、86バイト

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

オンラインでお試しください!

エリックのトリックの1つで、3バイトのゴルフをすることができました!ジョナサンアランのおかげで3バイト節約できました。

仕組み

まず、これはSTDINから入力を取得し、それを2つの別個の変数iとに割り当てますk。次に、変数iが真実である間、それをデクリメントし、それに応じて文字列を生成します。これは、入力からループするための省略形です-1から0まで。

文字列の生成

これをより多くの部分に分割します。

  • まず、で先頭のスペースを取得し" "*(k+~i)ます。以来i範囲でマッピングされている(入力、0] 、我々はからそれを減算する必要がありますk(私たちの安全に保存された元の入力)、デクリメントと何回というスペースを繰り返します。

  • +"\\"- "\"上のスペースに文字を追加します。

  • ' / |\ '[i%2::2]-2つの文字列、つまり"/ \ "" | "を、次の方法で生成します。

    • iが奇数の場合、i%21であるため[i%2::2]、インデックス1(0からインデックス)で始まる大きな文字列の各2文字を返します。

    • iが偶数の場合、i%21です。したがって、上記のメカニズムは、インデックス0で始まることを除いて同じことを行います。

  • +~-i*- "/ \ "または" | "i-1回、上記で生成された文字列を繰り返し、他の文字列に追加します。ビット演算子の利益(~-ビット単位の補数に相当私はから減算-1)、それはこの文脈で括弧を必要としないことです。

  • [:k-~i]-インデックスk-〜i = k-(-1-i)= k + 1 + iまで、上記で連結された文字列のすべての文字を取得します。

  • +"_/"[i>0:]-これは、追加されます"/"場合は、私≥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バイトでさらに良くなります:)
ジョナサンアラン

@JonathanAllan ...ありがとう!(!の説明になります...厳しい...やり直しが、ため息
ミスターXcoder

@JonathanAllan decrementステートメントの順序を逆にしない代替案を見つけまし
ミスターXcoder






2

パイソン2123 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の回答のように入力と文字列のフォーマットを使用して、大量のバイトを保存しました
  • Mr. Xcoderのおかげで2バイト節約

1
あなたは置き換えることによって、2つのバイトを保存することができ-1-a~a(私は私の答えで行ったように)。
ミスターXcoder

@ Mr.Xcoderありがとう:)
TFeld





0

ハスケル、 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は2バイトを節約しました。ありがとう!


([1,3..n-1]>>)の代わりに使用できます([1..div n 2]>>)
ライコニ



0

JavaScript(ES6)、89 85バイト

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

デモ




弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.