生日快乐V!


72

感谢@KritixiLithos 帮助我解决了这一挑战!


V是我编写的一种编程语言,因此我可以使用vim并将其扩展到代码高尔夫球挑战中。第一次提交是在2016年3月3日,这意味着今天V已满一岁!呜呼

在V成立的第一年,来自四个不同贡献者的176次提交,来自12个不同用户的140个答复以及太多重复的重复运算符无法计数。它有一个在线解释器,由@Dennis慷慨托管,自12月以来已运行了近8000次

让我们来挑战庆祝V的生日吧!由于V中的大多数功能在设计时都考虑了字符串操作和,因此,庆祝V的任何挑战都应该与ascii艺术有关,这似乎很自然。因此,您今天面临的挑战是将一个单词用作输入,然后将该单词重塑为V形。例如,输入“ Hello”应给出以下V:

Hello         olleH
 Hello       olleH
  Hello     olleH
   Hello   olleH
    Hello olleH
     HellolleH
      HellleH
       HeleH
        HeH
         H

以下是有关您的V外观的一些详细信息。如果输入字符串的长度为n个字符,则V应为多n*2行。第一行应包括:

<input string><(n*2) - 1 spaces><input string reversed>

在每一行中,在开头添加一个空格,并且字符串的两侧彼此相对移动,从而删除所有重叠的字符。直到最后一行,这只是输入的第一个字符。每行尾随空格都是可以接受的,尾随换行符也是允许的。

您可以假定输入将始终是可打印的ASCII,且其中没有任何空格,并且可以采用任何合理的方法进行输入和输出。以下是一些示例输入:

Happy:

Happy         yppaH
 Happy       yppaH
  Happy     yppaH
   Happy   yppaH
    Happy yppaH
     HappyppaH
      HapppaH
       HapaH
        HaH
         H

Birthday:

Birthday               yadhtriB
 Birthday             yadhtriB
  Birthday           yadhtriB
   Birthday         yadhtriB
    Birthday       yadhtriB
     Birthday     yadhtriB
      Birthday   yadhtriB
       Birthday yadhtriB
        BirthdayadhtriB
         BirthdadhtriB
          BirthdhtriB
           BirthtriB
            BirtriB
             BiriB
              BiB
               B

V!:

V!   !V
 V! !V
  V!V
   V

~:

~ ~
 ~

当然,由于这是,因此禁止了标准漏洞,并且您的目标是编写可能最短的程序来完成此任务。打高尔夫球快乐!


对于它的价值,我对vim回答很情有独钟,因此使用vim或V可以得到假想的加分,尽管任何语言都是可以接受的。:)


我可以在每个换行符后打印一个空字符(0x00)吗?
小麦巫师

@wheatwizard嗯。有点奇怪,但是我想只要输出在视觉上是相同的就可以了。
DJMcMayhem

21
5岁生日将是另外一回事!(以罗马数字表示)
艾伯特·伦肖

5
Vee对V语言的最良好祝愿:-)
Vee

Answers:


44

MATL21 14字节

MATL祝V生日快乐!

tnEXyY+c3MZvZ)

在线尝试!

说明

考虑输入

'Hello'

n=5。该代码使用size的单位矩阵计算此字符串的2D卷积2*n

[1 0 0 0 0 0 0 0 0 0;
 0 1 0 0 0 0 0 0 0 0;
 0 0 1 0 0 0 0 0 0 0;
 0 0 0 1 0 0 0 0 0 0;
 0 0 0 0 1 0 0 0 0 0;
 0 0 0 0 0 1 0 0 0 0;
 0 0 0 0 0 0 1 0 0 0;
 0 0 0 0 0 0 0 1 0 0;
 0 0 0 0 0 0 0 0 1 0;
 0 0 0 0 0 0 0 0 0 1]

卷积的结果转换为char并将char 0显示为空格

['Hello         ';
 ' Hello        ';
 '  Hello       ';
 '   Hello      ';
 '    Hello     ';
 '     Hello    ';
 '      Hello   ';
 '       Hello  ';
 '        Hello ';
 '         Hello']

然后[1, 2, ..., 2*n-1, 2*n, 2*n-1, ..., 2, 1]从此char矩阵中选择列,从而产生所需的结果:

['Hello         olleH';
 ' Hello       olleH ';
 '  Hello     olleH  ';
 '   Hello   olleH   ';
 '    Hello olleH    ';
 '     HellolleH     ';
 '      HellleH      ';
 '       HeleH       ';
 '        HeH        ';
 '         H         ']

注释代码

t      % Implicitly input string. Duplicate
nE     % Length, say n. Multiply by 2
Xy     % Identity matrix of that size
Y+     % 2D convolution. This converts chars to ASCII codes
c      % Convert to char
3M     % Push 2*n, again
Zv     % Push symmetric range [1, 2, ..., 2*n, 2*n-1, ..., 1]
Z)     % Apply as column indices. This reflects the first 2*n columns
       % symmetrically, and removes the rest. Implicitly display

非常有趣的方法!+1
seshoumara

3
@seshoumara谢谢!正如骗子所说的,卷积是成功的关键 :-)
Luis

38

V24,23,20字节

3Ù2Ò Íî
Xæ$òâÙHãêxx>

在线尝试!

现在,V具有一个“反向”运算符,这要短得多

与其他已回答的高尔夫语言相比,没有那么令人印象深刻,但是必须做到。十六进制转储:

00000000: 33d9 32d2 20cd ee0a 58e6 24f2 e2d9 48e3  3.2. ...X.$...H.
00000010: ea78 783e                                .xx>

说明:

3Ù                  " Make three extra copies of this current line
  2Ò                " Replace each character on this line and the next line with spaces
     Íî             " Join all lines together
X                   " Delete one space
 æ$                 " Reverse the word under the cursor

此时,缓冲区如下所示:

Happy         yppaH

不,我们将递归地建立三角形。

ò                   " Recursively:
 â                  "   Break if there is only one non-whitespace character on this line
  Ù                 "   Make a copy of this line
   H                "   Move to the first line
    ã               "   Move to the center of this line
     ê              "   Move to this column on the last line
      xx            "   Delete two characters
        >           "   Indent this line

这是我展示V的最喜欢功能之一的地方。很多命令都需要一个参数。例如,该>命令将根据参数缩进可变的行数:

>>    " Indent this line (this command is actually a synonym for '>_')
>j    " Indent this line and the line below
>k    " Indent this line and the line above
6>>   " Indent 6 lines
>}    " Indent to the end of this paragraph
>G    " Indent to the last line
...   " Many many many more

但是,如果大多数命令位于程序末尾且未指定,则将强制其以默认参数(通常为当前行)结尾。例如,V 在我们的递归循环中实际运行的是:

òâÙHãêxx>>ò

第二个ò是隐式填充的。很酷的事情是,隐式结束的命令应用了几层深度,因此即使我们只写了>V _,它也会隐式给出其参数,并且会缩进当前行。


我正在研究它,但我认为它更适合您回答!
nmjcman101 '17

29

Brainfuck,152字节

这是一个非常重要的时刻,我决定破解ol'BF解释器并尝试一下。

++++++++++[->+>+++<<]>>++>>+>>>,[[<]<<+>>>[>],]<[<]<<-[->+>>[>]>++++[-<++++++++>]<[<]<<]>[->++<]>[-<+>]<[<[-<+<.>>]<[->+<]>+>->>[.>]<[-]<[.<]<<<<<.>>>>]

有评论

++++++++++
[->+>+++<<] Insert 0 into the first buffer (Which we don't care about) 10 into the second and 30 into the thrd
>>++    Raise the third buffer to 32 making us our space
>   This buffer is reserved for the Insertable spaces counter
>
+>>>    Raise our incrementer This will be used to fill the other half of the string with spaces
,[  Read a byte
    [<]<<   Move to the back of the string buffer which is our incrementer
    +       increment it
    >>>[>]      And move to the next space of the string
    ,       And then read a new byte
]
<[<]<<-     Decrement the incrementer and begin to add spaces
[
    -       Decrement the incrementer
    >+      Raise the incrementer in the padding
    >>[>]   Move to a new part of the string buffer
    >++++[-<++++++++>]< Write a space
    [<]<<   Move all the way back to the string counter
]
BEGIN WRITING!!
>
[->++<]>[-<+>]<Double the incrementer
[
    <[  Move to the space counter
        -<+<.>> Decrement the space counter increment the temporary one to the left of it then print the space we stored to the left of that then return
    ]<[->+<]>+> Move the temporary space counter back
    -   Decrement the incrementer
    >>[.>]  Print every character from left to right
    <[-]    Snip the end off this
    <[.<]   Print every character from right to left
    <   Move back ot the incrementer
    <<<<.>>>> Print a newline aswell
]

在线尝试!


23

> <>,221字节

我花的方式太多时间在此。生日快乐,V!

l:2*01           p84*/v
 !@:$-1         /!?:$<
  1.v/ ^       v\~~>a
   vv\}o<     >ao  /
    1\/84/   :}o:$-
     \\!?<: l;!?\v
      p10-1:g10r\
       >  :>?\vv
        v:$-1/~
         </r+*
          </~
           l

您可以在线尝试使用它,但是获得此解释器并使用--play标志运行它会有趣得多

python3 fish.py v.fish -s "ppcg" --tick 0.05 --play

结果是下面的动画。

鱼跑的例子

(需要不到两分钟的时间)

说明

由于此答案的有趣之处在于将其包裹在V形状中,因此在此进行了解释。我们使用以下行编号版本作为参考。

1. l:2*01           p84*/v
2.  !@:$-1         /!?:$<
3.   1.v/ ^       v\~~>a
4.    vv\}o<     >ao  /
5.     1\/84/   :}o:$-
6.      \\!?<: l;!?\v
7.       p10-1:g10r\
8.        >  :>?\vv
9.         v:$-1/~
10.         </r+*
11.          </~
12.           l

有时,箭头(→↓←)用于指示到达摘要的方向。

  1. 初始化

       1.→l:2*01           p84*/v
       2.  !@:$-1   X     /!?:$<
       3.   1.            \~~>a
    

    第一行将2n推到[0,1],在堆栈上保留n并追加一个空格。接下来,我们向上走,然后绕到右边的第二行,我们将开始向左行。有一个循环,用于附加n + 1个空格。其工作原理如下。

                    Initial:                 "ppcg4 "
    !@:$-1 /!?:$<
               $     Swap.                   "ppcg 4"
              :      Duplicate.              "ppcg 44"
             ?       If more spaces to add:
        -1            Subtract 1.            "ppcg 3"
       $              Swap.                  "ppcg3 "
      :               Duplicate.             "ppcg3  "
     @                Rotate top 3.          "ppcg 3 "
    !                 Jump over stored value
                             and wrap around.
                    Else:
            /         Go to next part.
    

    完成此操作后,它会~~跳到第3行。在那里删除了顶部的两个堆栈元素(0和一个空格)(),然后跳转到X位置[10,1](a1.),继续向右。我们撞到了,然后/绕到第7行并开始主程序循环。

  2. 主循环(做2n次)

     6.              ;!?\
     7.       p10-1:g10r\   ←
    

    这是循环条件。首先,将堆叠翻转以进行打印。然后,我们从[1,0](01g)中获得计数器并存储一个递减的版本(:1-01p)。通过环绕并正确碰撞,我们遇到了终止程序的条件。如果不终止,则跳入第一个打印循环。

    • 第一个打印循环(左半部分)

      5.    1\   /   :}o:$-
      6.     \\!?<: l         ←
      

      我们从栈顶的长度开始,并执行以下代码,只要top元素不为0。

      1-$:o}
      
      1-        Subtract 1.    "ppcg3"
        $       Swap.          "ppc3g"
         :      Duplicate.     "ppc3gg"
          o     Output.        "ppc3g"
           }    Rotate right.  "gppc3"
      

      这将打印堆栈而不丢弃它。如果循环终止,我们将跳到第5行的右侧,为下一个打印循环做准备。

    • 准备右半

      5.  →    /84/   
      6.       \     
      7.            :    
      8.            >
      9.         v:$-1/~
      10.         </r+*
      11.          </~
      12.           l
      

      这是最难安装的零件之一。下面是剥离所有方向包装以指示发生情况的版本。

                   Initial stack:   "    gcpp0"
      84*+r~
      84*          Push 32 == " ".  "    gcpp0 "
         +         Add 32 and 0.    "    gcpp "
          r        Reverse.         " gcpp    "
           ~       Remove top.      " gcpp   "
      

      然后,我们增加要打印的内容的长度,并开始第二个打印循环(初始重复不是循环的一部分)。

    • 第二个打印循环(右半部分)

      3.     / ^ 
      4.     \}o<
      5.    
      6.           ↓   
      7.           
      8.       >  :>?\vv
      9.        v:$-1/~ 
      

      正在执行的代码与第一个打印循环中的代码完全相同,但o}由于存在可用位置,因此将代码放置得更远。完成后,我们还需要做一些事情,然后才能再次验证主循环不变性。在执行~了第9行后,我们将垂直环绕,最后到达以下代码。

                      ↓
      2.          X     / 
      3.  1.v/             >a
      4.              >ao  /
      

      首先ao将打印换行符。然后,我们弹起并从初始化后到达完全相同的位置,即跳到X


你应该让golfed版本的主要版本
可破坏柠檬

1
@DestructibleWatermelon,该文章更多地是关于V版本的,因为要用有限数量的可用字节将所有内容制成特定形状要困难得多。因此,下面将对V版本而不是普通版本进行说明。我可能稍后会打高尔夫球。
PidgeyUsedGust

这就是金子,只是金子
Christopher

我很欣赏这个答案使用的语言,其名称完全由旋转的“ V”组成。
Sellyme

19

Brain-Flak,486 + 1 = 489字节

Brain-Flak祝您生日快乐V!

还要感谢0',他提供了此答案中使用的一些代码

+1是由于-cASCII输入和输出所需的标志

((([]<{({}<>)<>}<>([]){({}[()]<(([][()])<{({}[()]<({}<>)<>>)}{}><>)<>({}<<>{({}[()]<({}<>)<>>)}{}><>)(({})<>)<>>)}{}([][][()]){({}[()]<((((()()()()){}){}){})>)}{}<>{({}<>)<>}<>>){}[()])<{((({})<((({}){}())){({}[()]<(({}<(({})<>)<>>)<(({}<({}<>)<>>[()])<<>({}<<>{({}[()]<({}<>)<>>)}{}>)<>>){({}[()]<({}<>)<>>)}{}<>>)>)}{}{}((()()()()()){})(<()>)<>>)<{({}[()]<({}<>)<>>)}{}{}{}{({}<>)<>}<>>[()])}{}>()())([][()]){{}(({}[()])<{({}[()]<((((()()()()){}){}){})>)}{}{({}<>)<>}{}>)([][()])}{}<>

在线尝试!

毫无疑问,这是我在Brain-Flak中所做的最困难的事情。

众所周知,Brain-Flak在复制和反转字符串方面非常可怕,而这个挑战除了复制和反转字符串外,什么都没有。

在不到一个小时的辛苦工作中,我设法得到几乎可以正常工作的代码片段,但是添加最后几个空格被证明是我在Brain-Flak中所做的最困难的事情之一。

说明

基本思想是,我们将首先创建V的顶部,每次迭代从中间删除两个字符,并在开头添加一个空格。

实际上,这变得非常困难。

存在用于复制和反向的现有算法,因此我使用其中一种算法在堆外创建代码的反向副本。完成这些操作后,我将2n-1空间放在原始堆栈的顶部,然后将堆栈外的堆栈移回堆栈上,以创建一个三明治。

测试1

现在我们有了第一行。现在我们要从开头删除两个字符,并在前面添加一个空格。事实证明这是最困难的部分。原因是我们实际上需要存储两个值,一个存储当前片段的深度,一个存储到要删除的V中心的深度。

这很难。

由于所有堆栈上都存在重复和反转,因此一直都在充分使用它们。这些堆栈上实际上没有任何地方可以放置任何东西。即使拥有世界上所有的Third Stack Magic,您也无法获得解决此问题所需的访问类型。

那么我们如何解决呢?简而言之,我们并不是真的。我们现在暂时忽略空格,并在以后对其进行修补,我们将在代码中添加零以标记空格打算到达的位置,但除此之外,我们实际上不会做任何事情。

因此,在每次迭代中,我们都会复制最后一次迭代并将其放到堆栈外。我们使用存储的深度将其拆分为一半,因此我们在右堆栈上保留了V的左半部分,在左堆栈上保留了V的右半部分。我们删除两个元素,然后将两个元素修补在一起。我们添加一个换行符以很好地度量并开始下一个迭代。每次到V中心的深度减小一,当它达到零时,我们就停止循环。

现在我们构造了大部分的V。但是,我们缺少适当的空间,我们的V当前(上下完全颠倒了)。

测试2

因此,我们将其翻转。要将其翻转到另一个堆栈上,我们必须将每个元素一个接一个地移动。当我们移动元素时,我们检查零。如果遇到一个空间,我们必须将这些空间放回它们所属的位置。我们剔除零并添加一堆空格。我们怎么知道多少?我们保持跟踪;与重复或反转操作不同,翻转堆栈是一项非常不繁重的任务,因此我们实际上具有存储和访问附加计数器的内存,以跟踪要添加的空间。每次添加一些空间时,计数器都会减一。计数器应在最后一个换行符(V的顶部)处达到零,因此我们可以开始打印了。

最后,我们清理掉一些东西,并终止程序以进行隐式输出。

测试3


令人印象深刻的是您设法使其完全正常工作!您是否认为可以通过使字节反转并添加-r标志来节省字节?
DJMcMayhem

@DJMcMayhem我不这么认为。反转过程和空格的插入是同时发生的,因此,如果添加了该-r标志,则实际上需要再次将其反转。我现在已经晚了,但我想明天我会努力尝试将高尔夫球击倒。如果我可以解决空间问题,那么我一定会使用该-r标志。
小麦巫师

16

果冻15 12字节

⁶ṁ⁸;;\Uz⁶ŒBY

在线尝试!

这个怎么运作

⁶ṁ⁸;;\Uz⁶ŒBY  Main link. Argument: s (string)

⁶ṁ            Mold ' ' like s, creating a string of len(s) spaces.
  ⁸;          Prepend s to the spaces.
    ;\        Cumulative concatenation, generating all prefixes.
      U       Upend; reverse each prefix.
       z⁶     Zip/transpose, filling empty spots with spaces.
         ŒB   Bounce; map each string t to t[:-1]+t[::-1].
           Y  Join, separating by linefeeds.

它是12个字符,但是是否有任何编码只能以12个字节出现?
卡巴斯德(Kasperd)

1
是的,Jelly使用它自己的自定义代码页
丹尼斯


16

JavaScript(ES6),108 106 98 94字节

f=
s=>s.repeat((j=l=s.length*4)*2).replace(/./g,_=>--j?s[j+j<l?j-i:l-i-j]||` `:(j=l,++i,`
`),i=1)
<input oninput=o.textContent=f(this.value)><pre id=o>


您能发表一篇说明这个的帖子吗?我对替换和正则表达式有些困惑。
Jacob Persi

@JacobPersi他们是一条红鲱鱼。我所需要的是尺寸的输出区域n*2n*4(包括在每行末尾的换行符)。然后,我计算应该出现在每个单元格中的字符。
尼尔

真好!您可以通过删除之间的换行符剃去一个字节f=s=>
yummypasta

@yummypasta这f=只是片段的一部分,而不是答案。因此,它不包含在字节数中。
尼尔

11

视网膜51 47字节

来自其他字符串处理语言的生日快乐!

字节数假定为ISO 8859-1编码。

$
$.`$* 
$
¶$`
O$^r`.\G

;{*`.¶

(\S.*).¶.
 $1¶

在线尝试!

说明

$
$.`$* 

通过匹配字符串的末尾,用检索字符串的长度,并用重复多次,这会追加n空格(其中n是字符串的长度)。$.`$*

$
¶$`

通过再次匹配字符串的结尾并将字符串本身插入,我们可以复制整个字符串(由换行符分隔)$`

O$^r`.\G

通过从右到左(r)匹配,然后一次匹配一个字符(.),但要确保它们都相邻(\G),来反转第二行。这样,比赛就不会超过换行符。然后将其用于排序阶段。通过使用排序模式($),但将每个匹配项替换为空字符串,则不会进行实际的排序。但是由于该^选项,匹配项最后会反转,从而反转整个第二行。

;{*`.¶

此阶段用于输出,并且还会影响程序的其余部分。{将剩余的阶段包装在一个循环中,重复执行直到这些阶段无法更改字符串(这将发生,因为最后一个阶段不再匹配)。该;禁止输出在程序的结尾。的*匝此阶段成干运行,这意味着阶段被处理,并且被打印的结果,但事后以前的字符串被恢复。

舞台本身仅删除换行符和前一个字符。这给了我们一行所需的输出(从第一行开始)。

(\S.*).¶.
 $1¶

最后,此阶段将每一行变成下一行。这是通过在第一个非空格字符前面插入一个空格,删除第一行的最后一个字符以及第二行的第一个字符来完成的。一旦第一行(与输出的最后一行相对应)上只剩下一个非空格字符,该过程就会停止。


很乐意对此进行解释。我知道sed的语法不太紧凑,但我的草稿却长了两倍。反转字符串并将第一条输出线放在一起是大多数。
seshoumara

@seshoumara当然,你去了。
Martin Ender

谢谢。现在,我知道一个双倍长度的sed脚本还不错:)),因为它会增加额外的s///字符,导致字符串变长,以及其他缺少Retina功能的操作。好读。+1
seshoumara

9

05AB1E,12个字节

Dgð×J.p€ûR.c

在线尝试!

说明

D             # duplicate input
 g            # length of copy
  ð×J         # append that many spaces to input
     .p       # get a list of all prefixes
       €û     # turn each into a palindrome
         R    # reverse the list
          .c  # pad each line until centered

或者从另一个方向获取相同的字节数。

Âsgú.sí€ûR.c

说明

             # push a reversed copy of input
 s            # swap the input to the top of the stack
  g           # get its length
   ú          # prepend that many spaces
    .s        # get a list of all suffixes
      í       # reverse each
       €û     # turn each into a palindrome
         R    # reverse the list
          .c  # pad each line until centered

2
如果您逐渐缩进注释,那么即使是源代码也看起来像是V :)

9

Japt,22 20 16 14 + 2个字节

Japt祝V高尔夫事业再创佳绩!

²¬£²îU²ç iYU)ê

需要-R标志。在线测试!

说明

这利用了我几天前添加的çî函数:

²¬£²îU²ç iYU)ê    Implicit: U = input string
²                 Double the input string.
 ¬                Split into chars.
  £               Map each char X and index Y by this function:
     U²             Take the input doubled.
       ç            Fill this with spaces.
         iYU        Insert the input at index Y.
    î       )       Mask: repeat that string until it reaches the length of
   ²                the input doubled.
                    This grabs the first U.length * 2 chars of the string.
             ê      Bounce the result ("abc" -> "abcba").
                  Implicit: output result of last expression, joined by newlines (-R flag)

丹尼斯的技术要长一个字节:

U+Uç)å+ mw y mê

5

GNU sed110100 +1(r标志)= 101字节

编辑:由于莱利短9字节

sed作为另一种字符串操作语言,祝V 一切顺利!

h;G
:;s:\n.: \n:;t
h;:r;s:(.)(\n.*):\2\1:;tr
H;x
s:\n\n ::
:V
h;s:\n::p;g
s:^: :;s:.\n.:\n:
/\n$/!tV

在线尝试!

说明:假设输入为最后一个测试用例('V!')。为了清楚起见,我将在每个步骤中显示模式空间,用'S'代替空格。

h;G                       # duplicate input to 2nd line: V!\nV!
:;s:\n.: \n:;t            # shift each char from 2nd line to 1st, as space: V!SS\n
h;:r;s:(.)(\n.*):\2\1:;tr # save pattern space, then loop to reverse it: \nSS!V
H;x                       # append reversed pattern to the one saved V!SS\n\n\nSS!V
s:\n\n ::                 # convert to format, use \n as side delimiter: V!SS\nS!V
:V                        # start loop 'V', that generates the remaining output
h;s:\n::p;g               # temporarily remove the side delimiter and print pattern
s:^: :;s:.\n.:\n:         # prepend space, delete char around both sides: SV!S\n!V
/\n$/!tV                  # repeat, till no char is left on the right side
                          # implicit printing of pattern left (last output line)

@Riley答案已更新,谢谢!
seshoumara

4

Python,110个字节

在线尝试!

我确定这不是最佳选择,但至少是Pythonic:

def f(s):n=len(s)*2-1;return''.join(i*' '+s[:n+1-i]+(n-2*i)*' '+s[n-i-1::-1]+'\n'for i in range(n))+n*' '+s[0]

4

Jolf,31个字节

Jolf谦虚地祝V生日快乐!

RΜwzΒώlid+γ_pq_ l+*␅Hi0ΒΒ␅ L_γ1S

在这里尝试! 应为0x05。

说明

RΜzΒώlid+γ_pq_ l+*␅Hi0ΒΒ␅ L_γ1S  i = input
     li                                  i.length
    ώ                                2 * 
   Β                             Β =
  z                              range(1, Β + 1)
 Μ     d                         map with: (S = index, from 0)
                +                 add:
                 *␅H               S spaces
                    i              and the input
               l                  slice (^) from
                     0Β            0 to Β
           pq_         Β␅         pad (^) with spaces to the right
         γ_                       γ = reverse (^)
        +                 L_γ1    γ + behead(γ)
R                             S  join with newlines

4

木炭,29字节

祝您生日快乐V,来自您的同事,令人失望的渴望这种挑战的ASCII艺术语言!

SσF…·¹Lσ«Fι§σκMι←↖»Fσ«Pσ↖»‖O→

在线尝试!

说明

我们的策略:打印V的左半部分,从底部开始到左上角;然后反映出来。

Sσ                                    Input σ as string
                                       The bottom len(σ) half-rows:
   F…·¹Lσ«           »               For ι in inclusive range from 1 to length(σ):
            Fι                          For κ in range(ι):
               §σκ                         Print character of σ at index κ
                  Mι←                   Move cursor ι spaces leftward
                      ↖                  Move cursor one space up and left
                                       The top len(σ) half-rows:
                        Fσ«    »      For each character ι in σ:
                            Pσ          Print σ without moving the cursor
                               ↖         Move cursor one space up and left
                                 ‖O→  Reflect the whole thing rightward, with overlap

(如果只有木炭进行了字符串切片……a,看来还没有实现。)


尽管Charcoal没有切片字符串,但确实有CycleChop,可用于提取字符串的开头,从而节省了4个字节。但是,有一种更好的方法可以节省9个字节。我认为当时还可以进行更多的节省:Reflect默认为正确反映,节省了另一个字节,并且其中一个变量已预定义到第一个输入,节省了两个字节。
尼尔

4

32 25字节

a.:sX#aL#a{OaDQaPRVaaPUs}

将输入字符串作为命令行参数。在线尝试!

说明

                           a is 1st cmdline arg, s is space (implicit)
     #a                    Len(a)
   sX                      Space, string-multiplied by the above
a.:                        Concatenate that to the end of a
       L#a{             }  Loop len(a) times (but NB, a is now twice as long as it was):
           Oa                Output a (no trailing newline)
             DQa             Dequeue one character from the end of a
                PRVa         Print reverse(a) (with trailing newline)
                    aPUs     Push one space to the front of a

4

带stringi包的R,225字节

library(stringi)
f=function(){
s=scan(,'',1,sep="\n")
m=2*nchar(s)
l=stri_pad(sapply(1:m-1,function(x)substr(paste(paste(rep(" ",x),collapse=""),s),1,m)),m,"right")
r=substr(stri_reverse(l),2,m)
message(paste0(l,r,"\n"))}
f()

如果您在交互式代码中运行R,请在粘贴我的答案后输入任何内容。您将需要安装stringi R软件包(我希望它不违反规则)。

说明:

基本思想是在左侧添加空格,然后将其切成正确的长度。之后,将其反转版本粘贴在右侧。这是该功能的更长的可读版本:

library(stringi)
make_V <- function(){                  # declaring the function
  string <- scan(, '', n=1, sep="\n")  # reading input
  max_length <- 2*nchar(string)        # number of chars in each half row

  # creating the left side of the V

  left <- stri_pad(                    
            sapply(1:max_length-1,     # for each row
                   function(x){     
                    substr(            
                      paste0(
                        paste0(rep(" ", x),
                               collapse=""), string), # add spaces to left side
                           1,
                           max_length) # cut the unneeded end
                    }),
            width=max_length,
            side="right")              # add spaces to the right side

  # creating the right side of the V

  right <- substr(stri_reverse(left), 2, max_length)

  # print it without any symbols before the strings 
  message(paste0(left, right, "\n"))
}

# run the function
make_V()

欢迎光临本站!:)
DJMcMayhem

4

Ruby,92 89 85字节

s=gets.chomp
s<<" "*n=s.size*2
n.times{s=s[0..(n-1)]
puts s+s.reverse[1..-1]
s=" "+s}

我的过程是在反转前半部分后,从每行的右半部分删除第一个字符。像这样:

Hello     |    olleH
 Hello    |   olleH 
  Hello   |  olleH  
   Hello  | olleH   
    Hello |olleH    
     Hello|lleH     
      Hell|leH      
       Hel|eH       
        He|H        
         H|         

我不习惯打高尔夫球,所以请告诉我是否有什么办法可以缩短高尔夫球时间。


欢迎来到该网站,这是一个不错的答案!不幸的是,我对红宝石一无所知,因此我无法提供任何提示。您也许可以在此页面上找到一些东西。
DJMcMayhem

谢谢!该页面上有很多有趣的事情,但我似乎已经做了很多。我确实意识到我可以通过副作用和操作顺序来节省一些字节。
user3334690

1
我认为[Ruby]只是Ruby,或多或少是众所周知的编程语言?
Rɪᴋᴇʀ

通过将其设置为lambda可以节省8个字节。
乔丹

4

批处理,186185字节

@set e=@set 
%e%/ps=
%e%t=%s%
%e%r=
:l
%e%s=%s% 
%e%r= %r%%t:~-1%
%e%t=%t:~,-1%
@if not "%t%"=="" goto l
:g
%e%r=%r:~1%
@echo %s%%r%
%e%s= %s:~,-1%
@if not "%r%"=="" goto g

1和6行有一个尾随空格。编辑:感谢@ ConorO'Brien,节省了1个字节。


1
这是保存字节的复杂方法。(创建别名@set 和删除@echo off,插入@必要的。
康纳奥布莱恩

@ ConorO'Brien谢谢,我从来没有想过8 sets可以为我节省足够的字节,使其值得。
尼尔

3

Haskell,76个字节

v是主要功能,接受String参数并给出String结果。

v i=unlines.r$i++(' '<$i)
r""=[]
r s|t<-init s=(s++reverse t):map(' ':)(r t)

在线尝试!

笔记:

  • i 是初始参数/输入。
  • s最初i带有length i空格。
  • v i调用r s,然后加入结果行。
  • r返回String行列表。
  • ts切掉最后一个字符。
  • 递归r t产生除第一行以外的行,减去每行的初始空间。

2
+1用于命名主要功能v。:D
DJMcMayhem

1
@DJMcMayhem:不将主要功能命名为一个字节长:unlines.r.((++)<*>(' '<$))
nimi

1
@nimi我以为他喜欢哪个,我选择的名称。从技术上讲是lambda关系...当我编写答案时,我不知道您可以对某些函数使用顶级声明,但不能命名主要函数。尽管我看到有人这样做,但我发现它有些令人不安。这些天,至少在GHCi中有效。
与Orjan约翰森

3

果冻,13 个字节

⁶ṁ;@µḣJUz⁶ŒBY

在线尝试!

怎么样?

⁶ṁ;@µḣJUz⁶ŒBY - Main link: string s
⁶             - space character
 ṁ            - mould like s: len(s) spaces
  ;@          - concatenate s with that
    µ         - monadic chain separation (call that t)
      J       - range(length(t))
     ḣ        - head (vectorises): list of prefixes from length to all
       U      - upend: reverse each of them
        z     - transpose with filler:
         ⁶    -     space: now a list of the left parts plus centre
          ŒB  - bounce each: reflect each one with only one copy of the rightmost character
            Y - join with line feeds
              - implicit print

相同的人认为很棒。:P
丹尼斯

哦,我没想到累积连接!木炭有一个V方向,也许对此有一些研究……
Jonathan Allan

3

Ruby,85 83个字节

编辑:删除多余的空格

s=ARGV[0];s+=' '*s.length;s.length.times{|i|puts s+s[i..-2].reverse;s=' '+s[0..-2]}

实际上,我发现很难用Ruby打高尔夫球。添加空格后,它扩展为一个非常可读的代码段:

s = ARGV[0]
s+=' ' * s.length

s.length.times do |i|
  puts s + s[i..-2].reverse
  s = ' ' + s[0..-2]
end

1
您可以像我一样通过将s.length设置为变量来节省一点时间吗?另外,我认为您应该考虑做尺寸而不是长度?
user3334690'3

按照@ user3334690的建议进行操作,并移动.times语句(79个字节):s=ARGV[0];(s+=' '*s.size).size.times{|i|puts s+s[i..-2].reverse;s=' '+s[0..-2]}
Conor O'Brien

通过将其设置为lambda可以节省五个字节。
约旦

3

MATLAB(R2016b),223个 183字节

r=input('');l=nnz(r)*2;for i=1:l;m=l+1-2*i;c={' '};s=cell(1,i-1);s(:)=c;x=cell(1,m);x(:)=c;e=r;y=flip(r);if(m<1);e=r(1:2*end-i+1);y=r(l/2+end-i:-1:1);end;disp(cell2mat([s e x y]));end

第一次打代码。欢迎小费!

程序输出:

MATLAB代码高尔夫

编辑:

感谢Luis Mendo,节省了40个字节。


2
欢迎使用PPCG,这是一个不错的第一答案!不幸的是,我对MATLAB一无所知,所以我无法帮助您,但是也许您会发现一些有用的技巧:-)
ETHproductions

1
默认情况下,允许输入包含其引号在内的字符串。所以,你可以删除's'input。另外,我看不到您为什么使用evalc(disp(...)),但我想您可以使用cell2mat 这种方式
Luis Mendo

1
此外,flip比短end:-1:1,请参见此处
Luis Mendo

3

PHP,95 92 85 80 78 77字节

注意:使用IBM-850编码

for($s.=strtr($s^$s=$argn,~ ,~▀);~$s[$i++];)echo$s,strrev($s=" $s"^$s^$s),~§;
          # Note that this ^ char is decimal 255 (negating it yields "\0")

像这样运行:

echo "Hello" | php -nR 'for($s.=strtr($s^$s=$argn,"\0",~▀);~$s[$i++];)echo$s,strrev($s=" $s"^$s^$s),~§;'
> Hello         olleH 
>  Hello       olleH  
>   Hello     olleH   
>    Hello   olleH    
>     Hello olleH     
>      HellolleH      
>       HellleH       
>        HeleH        
>         HeH         
>          H          

说明

for(
  $s.=strtr(             # Set string to the input, padded with spaces.
    $s^$s=$argn,         # Input negated with itself, leads to string with
                         # only null bytes with the same length.
    ~ ,                  # Replace null bytes...
    ~▀                   # ... with spaces.
  );
  ~$s[$i++];             # Iterate over the string by chars, works because 
                         # there are just as many lines as the padded
                         # string has chars.
)
  echo                   # Output!
    $s,                  # The string.
    strrev(              # The reverse of the string.
      $s=" $s"^$s^$s     # After each iteration, prefix string with a
    ),                   # space, and trim the last character.
    ~§;                  # Newline.

调整

  • 通过删除填充字符节省了3个字节(str_pad默认为空格,这是我们所需要的)
  • 通过对字符串使用二进制操作截断而不是保存7个字节 substr
  • 反向打印时旋转字符串可节省5个字节。无需打印退格键,但会导致每行尾随空格。
  • 使用更复杂但更短的方法通过填充字符串节省了2个字节。
  • 由于不需要考虑~"0"大小写(ASCII 207),因此节省了一个字节,因为可以假定所有输入都是可打印的ascii(Thx @Titus)

echo$s,strrev($s=" $s"^$s^$s),~§;节省5个字节。
泰特斯(Titus)

@Titus,谢谢。通常我会避免使用空格,但是OP表示可以接受

~$s[$i++]足够了(输入是可打印的ASCII,也是如此$s
Titus

@ Titus,thx,好消息。我倾向于在安全方面
进行

2

的JavaScript(ES6),169个 157字节

(-10字节,感谢Conor O'Brien)

V=(j,i=0,p="")=>i<(f=j.length)*2?V(j,-~i,p+" ".repeat(i)+j.slice(0,f-i*(i>f))+" ".repeat(i<f?(f-i)*2-1:0)+[...j.slice(0,f+~i*(i>=f))].reverse().join``+`
`):p

递归解决方案。我是JavaScript新手,请保持谦虚!任何打高尔夫球的技巧都将不胜感激。:)

当然,祝您生日快乐V

测试片段


1
这很好!通常,s.split("")可以更改为[...s],然后a.join("")可以a.join加上一对反引号。您可以用[r='repeat']和替换[r]为普通ol'repeat来额外节省3个字节slice
科纳·奥布莱恩

@ ConorO'Brien感谢您的提示!他们非常感谢。:)
R. Kap's

2

CJam,26个字节

您老朋友CJam生日快乐!

q_,2*:L,\f{LS*+m>L<_W%(;N}

在线尝试!

说明

q                           Push the input
 _,2*:L                     Push 2 times the length of the input, store it in L
       ,                    Take the range from 0 to L-1
        \                   Swap top stack elements
         f{                 Map over the range, using the input as an extra parameter
           LS*+               Append L spaces to the input
               m>             Rotate the resulting string right i positions (where i is the
                               current number being mapped)
                 L<           Take the first L characters of the string
                   _W%        Duplicate it and reverse it
                      (;      Remove the first character from the copy
                        N     Add a newline
                         }  (end of block)
                            (implicit output)

2

PowerShell,126个字节 124个字节

$l=($s="$args")|% Le*;$r=-join$s[-1..-$l];0..($l*2-1)|%{' '*$_+($s+' '*$l).substring(0,$l*2-$_)+(' '*$l+$r).substring($_+1)}

使用单个参数调用它,例如.\V.ps1 Hello

编辑:从AdmBorkBork中保存带提示的2个字节


1
A 在线试用!链接,以防您感兴趣。
丹尼斯

哦,我不知道那个小工具,谢谢!
托尔

嗨,您好!前面有几个小型高尔夫球场。将输入作为字符串,然后使用封装传递变量。保存两个字节。$l=($s="$args")|% Le*;
AdmBorkBork

哇,不知道那两次高尔夫,谢谢!
Tor


2

JavaScript(ES6),94个字节

f=(s,y=0,x=0,l=s.length*2-1)=>(s[(x>l?l*2-x:x)-y]||' ')+(x<l*2?f(s,y,x+1):y<l?`
`+f(s,y+1):'')

测试用例


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.