ツリーモートを育てよう!


12

これは私の友人トーマスです。彼は半分の木、半分顔文字です。

|    |
| :D |
|    |

彼は孤独です。彼に友達を作ろう!


入力としてテキストベースの絵文字を所定の(例えばಠ_ಠ:P>_>、ではない😀🤓または🐦)、出力対応treemote。

treemoteの長さは、lonの文字数です(基本的に、文字列のほとんどの組み込みの長さ関数)。だから、ಠ_ಠ長さ3を有しています。

長さのツリーモートの構文nは次のとおりです。

|< 2+n spaces>| * ceil(n/2)
| <emote> | (note the spaces)
|< 2+n spaces>| * ceil(n/2)

したがって、長さ3のツリーモートは次のようになります。

|     |
|     |
| ಠ_ಠ |
|     |
|     |

彼がしているceil(n/2)改行は、両側に各トランクセグメントを分離2 + n内部のスペースを。

課題:テキストベースの絵文字を指定して、対応するツリーモートを出力します。


その他の規則:

  • これはです。つまり、短いコードを書いてほしいのです。
  • 標準の抜け穴は許可されません。
  • 言語で処理できない場合を除き、非ASCII文字をサポートする必要があります。

テストケース:

^_^

|     |
|     |
| ^_^ |
|     |
|     |

\o/

|     |
|     |
| \o/ |
|     |
|     |


(✿◠‿◠)

|        |
|        |
|        |
| (✿◠‿◠) |
|        |
|        |
|        |


D:

|    |
| D: |
|    |


( ͡° ͜ʖ ͡°)


|             |
|             |
|             |
|             |
|             |
|             |
| ( ͡° ͜ʖ ͡°) |
|             |
|             |
|             |
|             |
|             |
|             |

末尾の改行は許可されますか?
R.ガプス

1文字の絵文字が存在しないと仮定しますか?
アダム

「テキストベースの絵文字」とは、アスキーの絵文字を意味しますか?
ダウンゴート

@Downgoat no。ಠ_ಠテストケースをご覧ください。
Rɪᴋᴇʀ

6
たぶん、3以外の長さのテストケースを追加します
...-SuperJedi224

回答:


4

05AB1E27 25バイト

コード:

g©Ìð×"|ÿ|
"®;îש„| ¹s¶®J

説明:

g                  # Push the length of the input string.
 ©                 # Copy that to the register.
  Ì                # Increment by 2.
   ð×              # Multiply by spaces.
     "|ÿ|\n"       # ÿ is used for interpolation and push the string "|spaces|\n".
                   #
 ®                 # Retrieve the value from the register.
  ;î               # Divide by 2 and round up.
    ×              # Multiply that by "|spaces|".
     ©             # Copy this into the register.
      „|           # Push the string "| ".
         Â         # Bifurcate, pushing the string and the string reversed.
          ¹s       # Push input and swap.
            ¶      # Push a newline character.
             ®J    # Retrieve the value from the register and join everything in the stack.
                   # Implicitly output this.

CP-1252エンコードを使用します。オンラインでお試しください!


入力( ͡° ͜ʖ ͡°)すると面白い結果が返されます。
ショーンワイルド

@ShaunWildうん、それは奇妙なテストケースです。なぜなら、( ͡° ͜ʖ ͡°)それ自体は11文字の長さですが、8文字の長さに見えます。
アドナン

4

Python 3.5、76 75 73バイト:

2バイトを節約したヒントBlueに感謝します!

def i(n):q=len(n);z=('|'+' '*(2+q)+'|\n')*-(-q//2);print(z+'| '+n+' |\n'+z)

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

また、これは非競合のPython 2.7.5バージョンです。これは87バイトではるかに長いため です。

def i(n):q=len(n.decode('utf-8'));z=('|'+' '*(2+q)+'|\n')*-(-q/2);print z+'| '+n+' |\n'+z

これは、Python 2のデフォルトエンコーディングがasciiであり、したがって、128ユニコードポイント範囲外の文字は1バイト以上としてカウントされる(list('ಠ')yields ['\xe0', '\xb2', '\xa0'])ためです。私がこれについて考えることができる唯一の回避策は、最初にを使用して入力をデコードしutf-8、次にこのutf-8デコードされた文字列に進むことでした。

このPython 2バージョンをオンラインでお試しください!(イデオン)


2バイトで(-(-q // 2))の周りの括弧を取り除くことができます。Python 2.7に切り替えると、さらに2バイト節約できると思います。//の2番目の/は不要で、printステートメントにスペースのみが必要です。
ブルー

@Blueええ、そのとおりです。2番目のかっこを削除できます。しかし、私はない秒が必要///行うために切り上げ部門を。
R.ガプス

私が言っていたように、Pythonで2.7整数除算は、1 /で行われる
ブルー

@ブルーああ、私はあなたがPython 3でそれを必要としないと言っていたと仮定しました。まあ、その場合は、Python 2に切り替えます。ヒントをありがとう!:)
R. Kap

@Blueその場合、Python 2でどのように浮動小数点除算を行いますか?
R. Kap

3

Dyalog APL37 34 33バイト

{↑'|'{⍺⍵⍺}¨b,(⊂⍵),b←' '/⍨⌈0.5×≢⍵}

Chromeユーザー:脚注を参照*

テストケース

      f←{↑'|'{⍺⍵⍺}¨b,(⊂⍵),b←' '/⍨⌈0.5×≢⍵}

      f,'☺' ⍝ the , is necessary to create a 1 char string instead of a character scalar
|   |
| ☺ |
|   |
      f':D'
|    |
| :D |
|    |
      f'^_^'
|     |
|     |
| ^_^ |
|     |
|     |

* Chromeでは、2つの文字≢⍵(U + 2262、U + 2375)が(U + 0338、U + 2262、U + 2375)≢⍵ではなく(U + 2261、U + 0338、U + 2375)と誤って表示される̸≡⍵ため、 Chromeの表示バージョン:{↑'|'{⍺⍵⍺}¨b,(⊂⍵),b←' '/⍨⌈0.5×̸̸≡⍵}


1
コード自体{⍺⍵⍺} B内の絵文字を有するため+1
値インク

3
ああ、{⍺⍵⍺}¨b「5杯目のコーヒーを飲んでいるAPLプログラマー」の絵文字です。
リン

2

V、60 57バイト

I| A |ByWo=ceil(len(""")/2.0)
 dF.d0kwviWr Yu@-pH@-P

残念なことに、Vは数学的操作に関してほとんど何もありません。除算および上限機能により、バイト数が大幅に増加しました。

これには多くの厄介な印刷できないものが含まれているため、ここに可逆的なhexdumpがあります。

00000000: 497c 201b 4120 7c1b 4279 576f 123d 6365  I| .A |.ByWo.=ce
00000010: 696c 286c 656e 2822 1222 2229 2f32 2e30  il(len("."")/2.0
00000020: 290d 201b 6446 2e64 306b 7776 6957 7220  ). .dF.d0kwviWr 
00000030: 5975 402d 7048 402d 50                   Yu@-pH@-P

説明:

I| A |                                #Add surrounding bars
      B                               #Move back
       yW                             #Yank a word
         o                            #Open a new line
          <C-r>=                      #Evaluate

          <C-r>"                      #Insert the yanked text into our evaluation
ceil(len("      ")/2.0)<cr>           #Evaluate ceil(len(text)/2) and insert it

 dF.                                  #Append a space and delete backward to a (.)
                                      #By default, this will be in register "-
    d0                                #Delete this number into register a
      kw                              #Move up, and forward a word
        viWr                          #Replace the emoticon with spaces
             Yu                       #Yank this line, and undo 
                                      #(so we can get the emoticon back)
               @-p                    #Paste this text "- times.
                  H                   #Move to the beginning
                   @-P                #Paste this text "- times behind the cursor.

算術演算子があった場合、何バイトだったでしょうか?
アドナン

@Adnan言うのは難しいです。なぜなら、私は彼らがどのように見えるかを正確に知らないからです。楽観的に私は約30バイトと言いますか?
ジェームズ

2

Vitsy、43バイト

IV2m3mz4m2m
3mV\D4m
V1+2/\[1m]
' || '
}}ZaO

説明:

IV2m3mz4m2m

I            Grab the length of the input string.
 V           Save that value to a global final variable.
  2m         Call the method at line index 2.
    3m       Call the method at line index 3.
      z      Push the entire input to the stack.
       4m    Call the method at line index 4.
         2m  Call the method at line index 2.

3mV\D4m

3m           Call the method at line index 3.
  V          Push the global variable to the stack.
   \D        Duplicate the top item on the stack that many times.
     4m      Call the method at line index 4.

V1+2/\[1m]

V            Push the global variable to the stack.
 1+          Add one to the top value.
             REASONING: We want ceil(V/2), and since repeat commands follow the floor value of repeats, we want ceil(V/2)+.5, so we add one to make this work right.
   2/        Divide by two.
     \[1m]   Call method 1 that top value of the stack times.

' || '

' || '       Push ' || ', the string, to the stack.

}}ZaO

}}           Push the bottom item of the stack to the top twice.
  Z          Output everything in the stack.
   aO        Output a newline.

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

TIOのバグにより、Unicode文字を使用した入力は機能しないことに注意してください。それらの代わりにローカルバージョンを使用する必要があります。ありがとう、@ Dennis!



1

ルビー、57バイト

整数除算のトリックを使用し、Rubyのputs関数の癖を利用します。

->e{s=e.size+1;puts k=[?|+' '*-~s+?|]*(s/2),"| #{e} |",k}

1

JavaScriptのES6、83の 78バイト

e=>(a=`| ${" ".repeat(n=e.length)} |
`.repeat(Math.ceil(n/2)))+`| ${e} |
${a}`

を必要としないので、f=2バイト節約できます。eテンプレートの内側とa外側に移動して、さらに2バイト節約します。繰り返しに2を追加する代わりにスペースを挿入して、さらに2バイトを節約します。ビットシフトを使用して2で除算することにより、バイトの別の束を保存します
ニール

私はそれだけで、繰り返しのソースコードを印刷しようとすると、この作品を考えていない
バリント

もう一度試して、@Bálintで修正しました
コナー・オブライエン

1

> <>、103バイト

i:0(?\
}$&1[\~rl:::2%+*:2,
1-:?!\" "$
]{\  \~
?!\$}1-:
~&\
?!\l$:@-[l2)
~]\
}}\" || "
?!\ol
8.>]l?!;ao2

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

このソリューションは、各行がで構成されるという観察に基づいています| <x> |。ここで、<x>は、中央の行のパターンと、他の行の同数のスペースです。

nSTDINから入力(長さ)を読み取った後、プログラムはn*(n+(n%2))スペースをプッシュします。その後、スタックは半分の回数ロールされます。次に、nキャラクターを除くすべてが新しいスタックにプルされ、スタックのスタックはnスペースまたはパターン自体(中央のスタックのみ)で。出力ステップでは、現在のスタックの内容が|とで囲まれて印刷されます|


1

C、89バイト

f;main(int c,char**a){for(c=strlen(*++a)+1;f<(c|1);)printf("|%*s |\n",c,f++==c/2?*a:"");}

ただし、非ASCII絵文字を処理するかどうかはわかりません。


非ASCII絵文字でテストできますか?
Rɪᴋᴇʀ

試しましたが、結果は良くありませんでした- strlen最初のゼロバイトまでバイトをカウントし、結果として非ASCII絵文字は彼らよりもはるかに広いと見なされます。
アラガエル

1

PowerShell v3 +、72バイト

param($a)$b=("| "+(" "*($l=$a.length))+" |`n")*($l+1-shr1);"$b| $a |";$b

入力文字列を取ります$a。構築$b(空パイプエンド文字列として(と$a.length)の中間にスペースと末尾の改行)が繰り返し時間(長さ+ 1は、2とceiling'dで割った、即ち、右のビットシフト)。次に、のコピーを出力します$bし、独自のパイプを使用して入力文字列を最後に$b再びコピーを出力します。

ビットシフトにはv3 +が必要 -shr演算子にです。

PS C:\Tools\Scripts\golfing> .\grow-a-treemote.ps1 '>:-|'
|      |
|      |
| >:-| |
|      |
|      |


PS C:\Tools\Scripts\golfing> .\grow-a-treemote.ps1 '>:`-('
|       |
|       |
|       |
| >:`-( |
|       |
|       |
|       |


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