画一个梯子并滑动


24

您的任务是创建一个程序或函数,该程序或函数将n1到25(含)之间的自然数()作为输入,并打印带有n梯级数的滑梯和梯子的等轴测图。

梯子和滑轨规格

梯子始终位于左侧,幻灯片始终位于右侧。我们正在从梯子一侧查看它,因此幻灯片的一部分在前三层被遮盖了。梯级由四个破折号(----)表示,梯子的侧面由斜线(/\)表示。下图是表示具有五个梯级的幻灯片所需的空间模式的示意图。

Slide              Blank space count
    /----/\        1234/----/\ 
   /----/  \       123/----/12\ 
  /----/    \      12/----/1234\ 
 /----/ \    \     1/----/1\1234\ 
/----/   \    \    /----/123\1234\

例子

>>1
/----/\

>>3
  /----/\ 
 /----/  \ 
/----/    \ 

>>4
   /----/\ 
  /----/  \ 
 /----/    \ 
/----/ \    \ 

>>10
         /----/\ 
        /----/  \ 
       /----/    \ 
      /----/ \    \ 
     /----/   \    \ 
    /----/     \    \ 
   /----/       \    \ 
  /----/         \    \ 
 /----/           \    \ 
/----/             \    \ 

这是代码高尔夫球,因此字节数最少的答案会获胜。

注意:输出中可以使用尾随空格,只要空格不超过行的长度即可。


14
在幻灯片的结尾,您会摔断双腿!
致命

13
@Fatalize那将是...致命的。
法师

7
@Fatalize奖励积分,如果您在底部绘制池;)
地图学家

8
@atlasologist血泊?
尼尔,

抱歉,这是一个愚蠢的问题,但以下几个答案(Python,JavaScript)定义了一个返回字符串而不是打印字符串的函数。那是犹太洁食吗?
约旦

Answers:


17

视网膜81 72 52字节

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

.+
$* /\    \
^.

+` /(.+)$
$&¶$%`/  $1
/.{5}
/----/

在线尝试!

说明

该程序包括四个阶段,所有阶段都是正则表达式替换(具有几个视网膜特定的功能)。我将使用输入5作为示例进行解释。

阶段1

.+
$* /\    \

这会将输入n变成n空格,其后/\ \将成为梯形图/幻灯片的顶部:

     /\    \

现在,我们只显示完整的幻灯片,只用左手代表梯子/

第二阶段

^.

不幸的是,n空格超出了我们的需要,因此我们再次删除了第一个字符。现在我们得到:

    /\    \

第三阶段

+` /(.+)$
$&¶$%`/  $1

是时候扩展完整的结构了。知道顶部在哪里就足以构建整个东西,因为我们只需一次将其延伸一行,就可以将梯子和滑梯移动两个距离。

+告诉视网膜重复此阶段在一个循环中,直到输出停止变化(在这种情况下,因为正则表达式匹配停止)。至于正则表达式本身,我们只需匹配/最后一行及其后的所有内容,还匹配其前面的一个空格,这意味着一旦/到达第一列,该匹配项将不再匹配。

这是我们将其替换为的内容:

 $&    The match itself. We don't want to remove the line we already have.
 ¶     A linefeed, because we want to append a new line.
 $%`   This is a very recent addition to Retina: it's like the normal $` but
      is bounded by linefeeds. That means this inserts everything in front
      of the match which is on the same line. In particular this one space
      less than the indentation of the matched line, hence we are shifting
      the / one column left.
 /     A literal /, representing the left edge of the ladder.
>  <   Two spaces, so that we can shift the slide one column right.
 $1    Capturing group 1 which contains the slide and its separation from
      the ladder.

因此,在每次迭代中,这都会向字符串添加一行,直到最终得到以下结果:

    /\    \
   /  \    \
  /    \    \
 /      \    \
/        \    \

阶段4

/.{5}
/----/

剩下的就是使梯子正确。这非常简单,我们只需匹配/和后5个字符,然后插入正确的梯形图表示,从而覆盖已经存在的幻灯片或空格:

    /----/\
   /----/  \
  /----/    \
 /----/ \    \
/----/   \    \

9

V38,37,36,34,33,32,31,30 29字节

Àé r\2é/4é-òhYpX$2P^ò3GEòjlr\

在线尝试!

可能会赶上Osabie。比Osabie短一字节。\o/绑上2sable!少一个字节!

在其他新闻中,这绝对是我做过的最长的删除线标头。

说明:

Àé              "Insert 'arg1' spaces
   r\           "Turn the last one into a '\'
     2é/        "Insert 2 '/'
        4é-     "Insert 4 '-'

ò        ò      "Recursivly:
 h              "  Move one to the left
  Yp            "  Duplicate this line
    X           "  Delete one space from the left
     $2P        "  Paste two spaces at the end of this line
        ^       "  Move back to the beginning of this line.

这将一直运行到发生错误为止,这要归因于“向左移动”命令('h'),是'arg1'次。

现在我们只需要添加内腿

3GE             "Move to the second slash of line 3
   ò    ò       "Recursively: (The second 'ò' is implicit)
    jl          "  Move down and to the right
      r\        "  And replace the character under the cursor with a '\'

非竞争版本(28字节)


8

Pyth,39 35字节

VQ++*dt-QN"/----/">+*+ddN"\    \\"5

说明:

VQ                                  # Interate over 0 -> Q-1 (Q is the input)
  +                                 # Concatenate the 2 halfs of the slide
   +                                # Concatenate the whitespace block and the ladder
    *d                              # Multiply d (whitespace) by this number \/
      t-QN                          # Calculate the amount of spaces before : input - step of the iterarion -1
          "/----/"                  # Ladder
                  >               5 # Remove the first 5 chars  from the string generated in the following lines 
                   +                # Concatenate the whitespace block and the slide
                    *+ddN           # Multiply "+dd" (2 whitespace) by the step of the iterarion to generate the space between the ladder and the slide
                         "\    \\"  # Slide

在这里测试



7

PowerShell v2 +,99 90 82字节

param($n)1..$n|%{" "*($n-$_)+"/----/"+-join(" "*($_+$i++)+"\    \")[6..(6+$_+$i)]}

需要输入$n,从开始一个循环1$n|%{...}。每次迭代,我们都在构造一个字符串。我们从适当数量的空格" "*($n-$_)和梯子开始"/----/"

为此,我们添加了另一个字符串,将其切成薄片[...]-join重新组合在一起。第二个字符串是幻灯片,我们假定整个幻灯片始终可见。它是幻灯片开始前的空格数" "*($_+$i++),其次是幻灯片本身"\ \"。它被一个范围切成一个范围,该范围被计算为“空间加幻灯片”的一部分,该范围被梯子部分隐藏了。

例子

PS C:\Tools\Scripts\golfing> .\draw-a-ladder-and-slide.ps1 7
      /----/\
     /----/  \
    /----/    \
   /----/ \    \
  /----/   \    \
 /----/     \    \
/----/       \    \

PS C:\Tools\Scripts\golfing> .\draw-a-ladder-and-slide.ps1 15
              /----/\
             /----/  \
            /----/    \
           /----/ \    \
          /----/   \    \
         /----/     \    \
        /----/       \    \
       /----/         \    \
      /----/           \    \
     /----/             \    \
    /----/               \    \
   /----/                 \    \
  /----/                   \    \
 /----/                     \    \
/----/                       \    \

7
+1是因为目录中的斜线与测试输出中的斜线对齐。:D
DJMcMayhem

您的\golfing文件夹必须井井有条._.
Conor O'Brien

6

Python 2-79 76 75字节

x=input()
for i in range(x):print(x-i)*' '+'/----/'+(i*'  '+'\\    \\')[5:]

感谢Hubert Grzeskowiak使我“取消资格”,因为打印程序实际上节省了3个字节!也感谢EʀɪᴋGᴏʟғᴇʀ节省了1个字节!


真好!顺便说一句,我认为lambda可以,因为严格来说,这是一种功能。使用lambda可以保存换行符和空格:-)
Hubert Grzeskowiak

等等,我实际上是用lambda尝试过的,无法正常工作。我猜是印刷的BC吗?
Hubert

@HubertGrzeskowiak好吧,这取决于挑战规范。如果问题要求返回一个幻灯片的字符串表示形式的程序/函数,则我的任何一个答案都是有效的。正如您所指出的那样,由于它指定了“ print”,因此我要么需要命名lambda函数并输出对其的调用,要么执行上面做得更短的事情。
Cowabunghole '16

规范没有告诉该函数必须被命名,或被称为;-)
Hubert Grzeskowiak

将其转换为一个完整的方案:更换def f(x):x=input(),并删除第二行缩进。这样可以为您节省1个字节。
暴民埃里克(Erik the Outgolfer)'16年

6

Vim,71击键

这是一个很愚蠢的方法,但是有点有趣。输入以文本文件形式给出,其中一行包含整数。这可能很容易打高尔夫,但是现在就可以了(编辑:根据要求切换控制字符样式):

A@qyyPgg<c-v><c-v>GkI <c-v><esc>G$i  <c-v><esc><esc>
Tq"qDI/----/\^[lD@"ddh<c-v>god:%s/     \\/\\    \\<cr>

<c-v><esc>并且<cr>都是个人击键;ctrl + v,转义和回车(输入)。对于具有正确文字的简单摘要版本,以下是ladder.keys运行的文件xxd

00000000: 4140 7179 7950 6767 1616 476b 4920 161b  A@qyyPgg..GkI ..
00000010: 4724 6920 2016 1b1b 5471 2271 4449 2f2d  G$i  ...Tq"qDI/-
00000020: 2d2d 2d2f 5c1b 6c44 4022 6464 6816 676f  ---/\.lD@"ddh.go
00000030: 643a 2573 2f20 2020 2020 5c5c 2f5c 5c20  d:%s/     \\/\\
00000040: 2020 205c 5c0d 0d0a                         \\...

要进行尝试(假设使用适当的工具安装了nix),请执行以上操作,将其运行xxd -r并放入文件中ladder.keys。创建一个ladder.txt包含整数的文件。然后做:

vim -s ladder.keys -u NONE ladder.txt

我们通常写^V<c-v>这里。
Leaky Nun

@Jordan它们每个都是一个按键,将修改的按键(shift,ctrl或alt与另一个按键)算作一个按键。击键是Ctrl + v,转义和输入。
algmyr '16

@algmyr我的错。我之前看错了你的答案。
乔丹

6

bash,61岁

for((;i<$1;)){ printf "%$[$1+i]s\    \^M%$[$1-++i]s/----/\n";}

^M文字回车在哪里

$ ./ladder 1
/----/\
$ ./ladder 4
   /----/\
  /----/  \
 /----/    \
/----/ \    \
$ ./ladder 10
         /----/\
        /----/  \
       /----/    \
      /----/ \    \
     /----/   \    \
    /----/     \    \
   /----/       \    \
  /----/         \    \
 /----/           \    \
/----/             \    \

我认为您应该/在第4行中的“ \” 之间有一个空格,而在幻灯片边缘之间应少1个空格。
Leibrug '16

应该是4个破折号,而不是5个破折号
。– algmyr

@algmyr谢谢您节省了2个字节
izabera '16

5

JavaScript(ES6),79个字节

f=
n=>" ".repeat(n).replace(/./g,"$'/$`$`\\    \\\n").replace(/\/...../g,"/----/")
;
<input type=number min=0 oninput=o.textContent=f(this.value)><pre id=o>

通过使用一串n空格,然后进行一些奇异的替换来获得带有支撑的幻灯片,然后将其替换为梯子。


4

Ruby,61个字节

->n{n.times{|i|puts"%*s\\    \\\r%*s----/"% [n+i,"",n-i,?/]}}

不打高尔夫球

->(num_rows) {
  num_rows.times {|row_idx|
    puts "%*s\\    \\\r%*s----/" % [ num_rows + row_idx, "", num_rows - row_idx, "/" ]
  }
}

我可以使用保存两个字节 '%*s\ \^M%*s----/'^M格式为回车)格式字符串,但是Ruby会显示警告“ warning: encountered \r in middle of line, treated as a mere space”。¯\ _(ツ)_ /¯

先前的解决方案(64字节)

->n{n.times{|i|puts" "*(n+i)+"\\    \\\r"+" "*(n-i-1)+"/----/"}}

3

批次,194个字节

@echo off
for /l %%i in (1,1,%1)do call:l %1 %%i
exit/b
:l
set s=\    \
for /l %%j in (1,1,%2)do call set s=  %%s%%
set s=/----/%s:~7%
for /l %%j in (%2,1,%1)do call set s= %%s%%
echo%s%

事实证明,这样做相当简单:将幻灯片缩进,删除前7个字符,缩进梯子,删除前导空格。最后一点确实涉及一些技巧!


2

Java,116字节

c->{for(int i=0;i<c;i++)System.out.format("%"+(5+c-i)+"s%"+(i<3?i*2+1:2*(i-2))+"s%5s\n","/----/","\\",i<3?"":"\\");};

不幸的是,您无法[轻松]复制Java中的字符串,因此我最终滥用了format函数。


2

Scala,95个字节

def l(n:Int)=for(i<- 0 to n-1){println(" "*(n-i-1)+"/----/"+("  "*i+"\\    \\").substring(5))}

2

Haskell,81个字节

a n=[1..n]>>" "
f n=[1..n]>>=(\i->a(n-i)++"/----/"++drop 7(a(2*i)++"\\    \\\n"))

do i<-[1..n];a(n-i)++"/----/"++drop 7(a(2*i)++"\\ \\\n")保存两个字节。
林恩

0

eacal,非竞争性,386字节

init .
define @ curry push .
define ~ curry exec .--func
alias $ strap
alias ' string
set n set m cast number arg number 0
set s empty string
label l
@ get n
set n ~ dec
@ space
@ get n
$ ~ repeat
$ ' /----/
@ space
@ get m
@ get n
@ ~ sub
@ ~ dec
@ number 2
@ ~ mul
$ ~ repeat
$ ' \
$ newline
@ get n
@ number 0
if ~ more
goto l
@ $
@ regex gm '   ( {4})(?=.$)
@ '  \$1
print ~ replace

我正式使最冗长的语言成为可能。我开玩笑和讽刺地发表了评论。请冷静下来。有关如何在标头中链接的github repo中运行的说明。

不打高尔夫球

init .
set n set m cast number arg number 0
set s empty string
label loop
    push . get n
    set n exec .--func dec
    push . space
    push . get n
    strap exec .--func repeat
    strap string /----/
    push . space
    push . get m
    push . get n
    push . exec .--func sub
    push . exec .--func dec
    push . number 2
    push . exec .--func mul
    strap exec .--func repeat
    strap string \
    strap newline
    push . get n
    push . number 0
    if exec .--func more
        goto loop

push . strap
push . regex gm string   ( {4})(?=.$)
push . string  \$1
print exec .--func replace

1
我正式使最冗长的语言成为可能。 ”对不起,您听说过AppleScript吗?
Addison Crump

@VTCAKAVSMoACE好吧,让我们达成协议。您可以在applescript中回答此问题。然后,我会告诉您这比较冗长。
科纳·奥布莱恩

4
如果您无法解开它,它的详细程度还不够。
尼尔,2016年

@ VTC,TimmyD和Neil:Geez。你现在高兴了?
科纳·奥布莱恩

@CᴏɴᴏʀO'Bʀɪᴇɴ我们显然是在开玩笑,大声笑
Addison Crump
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.