给我建一座城堡楼梯!


13

您将得到一个由可打印ASCII组成的字符串(没有换行符)。您的任务是为我的城堡建造一个漂亮的楼梯。

如何建立一个漂亮的楼梯?

  • 首先,您应该获得String的所有旋转。例如,字符串abcd具有以下旋转角度:(abcd, bcda, cdab, dabc每个字符都移到末尾,直到到达最后一个字符为止)。

  • 现在,我们将每个旋转彼此叠加:

    abcd
    bcda
    cdab
    dabc
    
  • 我们不能真正爬上直墙,所以我们必须建造楼梯。这意味着您应该在每次旋转之前在旋转列表中为其索引添加一些空格:

    abcd
     bcda
      cdab
       dabc
    
  • 您还需要连接到我城堡另一侧的楼梯,因此您应该像下面这样建造楼梯,反转每次旋转并增加一些间距:

    abcd      dcba
     bcda    adcb
      cdab  badc
       dabccbad
    

这是,因此以字节为单位的最短代码获胜,并且适用标签的标准规则。


测试用例

  • 输入:abcd,输出:

    abcd      dcba
     bcda    adcb
      cdab  badc
       dabccbad
    
  • 输入:aaaa,输出:

    aaaa      aaaa
     aaaa    aaaa
      aaaa  aaaa
       aaaaaaaa
    
  • 输入:Code golf,输出(注意空格):

    Code golf                flog edoC
     ode golfC              Cflog edo 
      de golfCo            oCflog ed  
       e golfCod          doCflog e   
         golfCode        edoCflog     
         golfCode        edoCflog     
          olfCode g    g edoCflo      
           lfCode go  og edoCfl       
            fCode gollog edoCf
    


楼梯不应该开始上升然后下降,而不是下降然后上升吗?:P
史蒂芬(Stephen)

@StepHen出于这个挑战的目的,它不应该:p
Xcoder先生17年


dabC。-------
奥利弗·尼

Answers:




3

视网膜,47字节

.
$.`$* $&$'$`$.'$* ¶
%(`^
$_¶
O$^`.(?=.*$)

¶

在线尝试!说明:第一阶段通过考虑每个字符并创建等于当前位置的空格,然后是字符串的其余部分,然后是字符串的开头,然后是等于字符串的其余部分的空格,来创建左楼梯。脚本的其余部分在依次生成的每一行上运行。首先复制该行,然后将副本中的字符颠倒,然后将该行及其副本连接起来。


3

Python 3,89个字节

x=input()
l=len(x)
for i in range(l):y=x[i:]+x[:i];j=' '*i;print(j+y+'  '*(l+~i)+y[::-1])

在线尝试!

-1字节感谢ovs

-1字节感谢Lynn


l-i-1可以只是l+~i作为~i == -i-1
OVS

' '*(l+~i)*2等于'  '*(l+~i),短一个字节!
林恩


2

木炭23 21 20字节

FLθ«FLθ§θ⁺κι↘MLθ←»‖C

在线尝试!

也许可以打更多的高尔夫球,但我是从移动应用发布的。链接到详细版本


哦,顺便说一句,如果您至少不添加说明,请使用-a标志 pls
仅ASCII格式,

仅限@ASCII,对不起,我认为详细版本可以作为解释。
查理

等待什么nvm没看到
仅ASCII的

我当时认为情况并非如此,但如今,您可以用任意字符串填充多边形,并获得9个字节所需的精确结果:G→↘←Lθθ‖C
尼尔,

2

Haskell,80 79字节

(s:u)#t|q<-(t>>" ")++s:u++t++(u>>" ")=q++reverse q++'\n':u#(t++[s])
u#_=u
(#"")

在线尝试!

怎么运行的

(#"")                      -- start with the input string and an empty accumulator

(s:u)#t                    -- let s be the first char and u the rest of the input
                           -- string, and t the accumulator
    |q<-                   -- let q be half of the current line, i.e.
        (t>>" ")++         --   length of t times spaces
        s:u++              --   s and u (aka the input string)
        t++                --   the accumulator
        (u>>" ")           --   length of u times spaces
    = q ++ reverse q ++    -- the current line is q and q reversed
        '\n' :             -- and a newline
        u#(t++[s])         -- and a recursive call with u as the new input
                           -- string and s put at the end of t
_#_=[]                     -- base case: stop if the input string is empty

编辑:感谢@ØrjanJohansen提供了一个字节。


u#_=u保存一个字节。
与Orjan约翰森

@ØrjanJohansen:我首先有一个字符串列表,unlinesu#_=u没有键入check的地方,后来切换到构建单个字符串...谢谢!
nimi

2

J,34个字节

([,.|.)@(i.@#((' '#~[),[|.])"0 1])

不打高尔夫球

([,.|.) @ (i.@# ((' '#~[) , [|.])"0 1 ])

在线尝试!



1

Mathematica,119个字节

b=StringRotateLeft;j=Table;Column@j[""<>{" "~j~i,b[s=#,i],j["  ",t-i],b[StringReverse@s,-i]},{i,0,t=StringLength@#-1}]&

1

PHP,95字节

for($e=strlen($s=$argn);$i<$e;$s.=$s[$i],$s[$i++]=" ")echo$t=str_pad($s,2*$e-1),strrev($t),"
";

与管道一起运行-nR在线尝试

分解

for($e=strlen($s=$argn);    # import input
    $i<$e;                  # loop length times
    $s.=$s[$i],                 # 2. append current character
    $s[$i++]=" ")               # 3. set current character to space
    echo$t=str_pad($s,2*$e-1),  # 1. print string padded with length-1 spaces
        strrev($t),             #    print reverse
        "\n";                   #    print newline

1

Japt,22字节


l
VÇç +UéZn)+´Vç)ê1÷

领先的换行符是该程序的一部分。

在线尝试!

使用我的WIP CodePen 运行所有测试用例

说明

隐式:U=输入字符串。第一行为空白,不覆盖U

第二线隐式分配的长度(l的)UV

第三行:

VÇç +UéZn)+´Vç)ê1÷
VoZ{Zç +UéZn)+--Vç)ê1} · Ungolfed
VoZ{                 }   Create array [0, V) and map by...
    Zç                      The current value (Z) times " "
       +UéZn)               Concatenated with U rotated Z times left
             +--Vç)         Concatenated with --V times " ". This decrements V
                   ê1       Palindromize with repeated last char
                       · Join with newlines and implicitly output


1

Javascript(ES6),118个字节

s=>[...s].map((_,y)=>Array(l=(j=s.length)*4-2).fill().map((_,x)=>(x=x<l/2?x:l-x-1)>=y&y+j>x?s[x%j]:" ").join``).join`
`

示例代码段:

f=
s=>[...s].map((_,y)=>Array(l=(j=s.length)*4-2).fill().map((_,x)=>(x=x<l/2?x:l-x-1)>=y&y+j>x?s[x%j]:" ").join``).join`
`
o.innerText=f("Code golf")
<pre id=o>


1

Python 2中85 83个字节

  • 感谢@ovs 2个字节:l+~i并帮助我发现了多余的空间
x=input()
l=len(x)
for i in range(l):r=i*' '+x[i:]+x[:i]+(l+~i)*' ';print r+r[::-1]

在线尝试!


1
l-1-i可以只是l+~i作为~i == -i-1
OVS

1

8th173168字节

s:len n:1- ( >r dup s:len n:1- "" ( " " s:+ ) rot times dup 0 r@ s:slice -rot r> -1 s:slice s:+ s:+ dup s:rev swap . . cr null s:/ a:shift a:push "" a:join ) 0 rot loop

带注释的非高尔夫版本

: shifter \ s -- s
  null s:/     \ convert string into array
  a:shift      \ remove the first item in the array and put it on TOS
  a:push       \ append the former 1st item to array
  "" a:join    \ convert array into string
;

: stairway \ s -- s
  s:len n:1-
  (
    >r                       \ save loop index
    dup                      \ duplicate input string 
    s:len n:1-               \ get string length
    "" ( " " s:+ ) rot times \ make filler
    dup                      \ duplicate filler 
    0 r@ s:slice             \ make left filler
    -rot                     \ put left filler at proper position
    r> -1 s:slice            \ make right filler
    s:+ s:+                  \ build string ( 1st half of stairway )
    dup s:rev                \ build 2nd half 
    swap . . cr              \ print it
    shifter                  \ shift rotate 1st character
  ) 0 rot loop               \ loop from 0 to len(string)-1
;

用法和示例

ok> "abcd" s:len n:1- ( >r dup s:len n:1- "" ( " " s:+ ) rot times dup 0 r@ s:slice -rot r> -1 s:slice s:+ s:+ dup s:rev swap . . cr null s:/ a:shift a:push "" a:join ) 0 rot loop
abcd      dcba
 bcda    adcb 
  cdab  badc  
   dabccbad 

或更清楚

ok> "Code golf" stairway
Code golf                flog edoC
 ode golfC              Cflog edo 
  de golfCo            oCflog ed  
   e golfCod          doCflog e   
     golfCode        edoCflog     
     golfCode        edoCflog     
      olfCode g    g edoCflo      
       lfCode go  og edoCfl       
        fCode gollog edoCf 
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.