种植一棵树mo!


12

这是我的朋友托马斯。他是一半的树,一半的图释。

|    |
| :D |
|    |

他很孤独。让他结识一些朋友吧!


给定一个基于文本的表情符号作为输入(例如ಠ_ಠ:P>_>,不😀🤓🐦),输出对应的treemote。

Treemote的长度是多少个字符(基本上是字符串的大多数内置长度函数)。因此,ಠ_ಠ具有长度为3。

长度n为treemote的语法如下:

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

因此,任何长度为3的树状节点都将看起来像:

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

他的ceil(n/2)两边都有换行符分隔的主干段,每个主干段2 + n内都有空格。

挑战:给定基于文本的表情符号,输出相应的treemote。


其他规则:

  • 这是,这意味着我要您编写短代码。
  • 不允许出现标准漏洞。
  • 除非您的语言无法处理,否则您必须支持非ASCII字符。

测试用例:

^_^

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

\o/

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


(✿◠‿◠)

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


D:

|    |
| D: |
|    |


( ͡° ͜ʖ ͡°)


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

是否允许尾随换行符?
卡普

我们假设1个字符的图释不存在?
2013年

“基于文本的表情”是指“ ASCII表情”吗?
Downgoat

@唐老鸭号 参见ಠ_ಠ测试案例。
Rɪᴋᴇʀ

6
也许添加一个长度不是3的
测试用例

Answers:


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字节:

感谢Blue的技巧,它节省了2个字节!

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个unicode点范围之外的字符被视为大于1个字节(list('ಠ')yield ['\xe0', '\xb2', '\xa0'])。为此,我能想到的唯一解决方法是先使用解码输入utf-8,然后继续使用此utf-8解码后的字符串。

在线试用此Python 2版本!(爱迪生)


您可以摆脱(-(-q // 2))2个字节的括号。我认为通过切换到Python 2.7您可以再节省2个字节。它不需要第二个/ //,只需要在print语句中留一个空格。
蓝色

@Blue是的,您是对的,我可以删除第二对括号。不过,我需要第二次///小区分裂。
卡普

就像我说的那样,在Python 2.7中,整数除法是用1 /
Blue

@Blue哦,我想您是说在Python 3中不需要的。那么,在这种情况下,我将切换到Python2。谢谢!:)
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将两个字符≢⍵(U + 2262,U + 2375)错误显示为≢⍵(U + 2261,U + 0338,U + 2375),而不是̸≡⍵(U + 0338,U + 2262,U + 2375),因此这是一个Chrome的显示版本:{↑'|'{⍺⍵⍺}¨b,(⊂⍵),b←' '/⍨⌈0.5×̸̸≡⍵}


1
+1表示您的代码本身包含表情符号{⍺⍵⍺}¨b–
Value Ink,

3
啊,{⍺⍵⍺}¨b是“第五杯APL程序员竖起大拇指”表情符号。
林恩

2

V,60 57字节

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

不幸的是,就数学运算而言,V几乎没有。除法和ceil函数极大地增加了字节数。

由于其中包含许多讨厌的不可打印内容,因此这是一个可逆的十六进制转储:

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字符输入。您必须使用本地版本代替。谢谢,@丹尼斯!



1

Ruby,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来节省另外一堆字节
Neil

我认为这行不通,它只是尝试打印重复代码的源代码
巴林特

再试一次,我固定它@巴林特
康纳尔奥布莱恩

1

> <>,103字节

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

在线尝试!

此解决方案基于以下观察:每行由组成| <x> |<x>中线的模式为,其他行的空格数相同。

n从STDIN 读取输入(长度)后,程序将压入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表情符号被认为比其宽得多。
aragaer

1

PowerShell v3 +,72个字节

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

接受输入字符串$a。构造$b为($a.length长度+1右移一位,即除以2并乘以上限)重复(长度为1 的空管端字符串(中间有空格)和结尾的换行符)。然后输出的副本$b,输入字符串及其自己的管道,最后输出的副本$b

要求v​​3 +用于位移-shr运算符

例子

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


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


1

TSQL,96 88字节

DECLARE @ varchar(100)='^_^'

PRINT STUFF(REPLICATE('|'+SPACE(2+LEN(@))+'|
',LEN(@)*2-1),LEN(@)*(LEN(@)+5)-3,LEN(@),@)

在线尝试!

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.