全方位展开


14

输入:

一串

输出:

1)首先,我们采取在输入串的末尾删除字符,直到我们留下的长度为正方形(即1,4,9,16,25,36,等等)
因此,abcdefghijklmnopqrstuvwxyz(长度26)变为abcdefghijklmnopqrstuvwxy(长度25)。

2)然后我们将其放在一个正方形中,从左到右一次一行:

abcde
fghij
klmno
pqrst
uvwxy

3)我们在四个方向上都将其折叠,如下所示(我们不断展开,直到外部折叠的“块”不再具有内部字符可展开):

      m
     qrs
     l n
     ghi
    abcde
 ihgf   jihg
mn lk   on lm
 srqp   tsrq
    uvwxy
     qrs
     l n
     ghi
      m

需要注意的一些事情,当我们向外折叠时,我们基本上像这样镜像(为澄清起见添加了数字,这些数字表示这些示例中的“索引”):

当我们向外折叠时:

 123    to:   321 123
fghij         ihgf   j

当我们折叠右侧时:

 123    to:    123 321
fghij         f   jihg

当我们向上折叠时:

            3q
            2l
            1g
  b   to:    b
 1g         1
 2l         2
 3q         3
  v          v

当我们向下折叠时:

 b          b
1g         1
2l         2
3q         3
 v   to:    v
           3q
           2l
           1g

挑战规则:

  • 您可以假设输入将始终至少包含1个字符(也将是输出)。
  • 输出格式很灵活,因此您可以打印到STDOUT或STDERR。返回为字符串数组/列表或字符二维数组;带换行符的单个字符串;等等
  • 输入将仅包含字母数字字符(a-zA-Z0-9
  • 您还可以使用非字母数字字符来填充ASCII美术输出中和/或周围的空格,例如点.
  • 尾随空格和一条尾随换行符是可选的。
  • 我们继续展开,直到外侧的折叠“块”不再有中心展开。

通用规则:

  • 这是,因此最短答案以字节为单位。
    不要让代码高尔夫球语言阻止您使用非代码高尔夫球语言发布答案。尝试针对“任何”编程语言提出尽可能简短的答案。
  • 标准规则适用于您的答案,因此您可以使用STDIN / STDOUT,具有正确参数的函数/方法和返回类型的完整程序。你的来电。
  • 默认漏洞是禁止的。
  • 如果可能的话,请添加一个带有测试代码的链接。
  • 另外,如有必要,请添加说明。

测试用例:

Input: abcdefghijklmnopqrstuvwxy
Output:
      m
     qrs
     l n
     ghi
    abcde
 ihgf   jihg
mn lk   on lm
 srqp   tsrq
    uvwxy
     qrs
     l n
     ghi
      m

Input: A
Ouput:
A

Input: ThisIsATest
Output:
  I
 Thi
Is sI
 ATe
  I

Input: HowAboutAVeryLongExampleWhichIsAlsoAnEvenSquareInsteadOfOddOneAndExceeds64Chars
Output:

               An
               ch
              xamp
              i  I
              o  E
              quar
             steadO
             S    e
             s    v
             h    s
             E    l
             VeryLo
            HowAbout
      oLyreVA      noLyreV
  xampl    Eg      el    Examp
hci  Is    hW      As    hi  Ihc
nAo  Ev    sl      ev    so  EnA
  quare    Sn      Ie    Squar
      Odaetsn      fOdaets
            OddOneAn
             steadO
             S    e
             s    v
             h    s
             E    l
             VeryLo
              xamp
              i  I
              o  E
              quar
               An
               ch

Input: Lenght7
Output:
Le
ng

Input: abc
Output:
a

有一个在测试一个错误的“HowAboutAVeryLongExampleWhichIsAlsoAnEvenSquareInsteadOfOddOneAndExceeds64Chars”:“H” - >“我”靠近输出底部
NGN

Answers:


5

SOGL V0.12,75 个字节

l√u²m√lH»{ā;l⁾:A∫Ba{bIwFIWhFbž;FIbI@ž}};}¹K⁴{ē2\⌡±e{@Κ};⁴┼┼};0E{ē2\⌡№:h++}╚

在这里尝试!

这期望在堆栈上输入,因此为了易于使用,我添加了 ,一开始就了该内容。如果输入仅包含数字,则可能导致问题,因此这里 是一个测试套件。

70字节√lH»{ā;l⁾:A∫Ba{bIwFIWhFbž;FIbI@ž}};}¹K⁴{ē2\⌡±e{@Κ};⁴┼┼};0E{ē2\⌡№:h++}╚也可以,但是正如我现在才实现在字符串上,因此文档中没有提到它将占用我不会计算的长度。

说明:

creating a square from the input

l       get the length of the input
 √      get its square root
  u     floor that
   ²    square it
    m   mold the input to that length
     √  convert it to a square

creating the unfoldings of the square - the idea is to cut out the inner squares to a new array

lH»{                              } (length-1)//2 times do
    ā;                                push an empty array below ToS
      l⁾                              push ToS.length - 2 (ToS here is the square or the previous unfolding)
        :A                            save a copy of that in the variable A
          ∫B                    }     repeat that amount of times, saving iteration on B - cutting the inner square to the empty array
            a{                 }        variable A times do
              bIw                         get the b+1th row of the previous unfolding
                 FIW                      get the (current loops iteration + 1)th character of that
                    h                     swap the 2 items below ToS - so the stack now is [..., prevUnfolding, newArray, character]
                     Fbž                  at [current loops iteration; b] insert that character in the array
                        ;                 swap the top 2 items - the stack now is [..., newArray, prevUnfolding]
                         FIbI@ž           at [current loops iteration+1; b+1] insert a space
                                 ;    get the now not empty array ontop of the stack

add the horizontal unfoldings

¹                    wrap the stack in an array
 K                   push the 1st item of that, which will function as the canvas
  ⁴{              }  iterate over a copy of the remaining items
    ē2\⌡               repeat (e++ divides by 2) times (default for the variable E is the input, which defaults to 0)
        ±                reverse the array horizontally
         e{  }         repeat e times
           @Κ            add a space before ToS
              ;⁴┼┼     add that horizontally before and after the canvas

add the veertical unfoldings

;                get the copy of the foldings above the canvas
 0E              reset the variable E to 0
   {         }   iterate the copy of the foldings
    ē2\⌡           repeat (e++ divides by 2) times (default for the variable E is the input, which defaults to 0)
        №            reverse the array vertically
         :h++      add that vertically before and after the canvas
              ╚  center the canvas vertically

您的70字节版本有效,因为不再存在竞争问题
粗野的

@Shaggy 75字节版本仅是有效的,因为像以前一样,此挑战仅适用于数字。我之所以不计算75字节的版本,是因为我觉得它处于添加内置漏洞的漏洞之列
dzaima

4

木炭120个 109字节

AI§⪪IXLθ⁰·⁵.⁰ηFη⊞υ✂θ×ιηF⁴«AυεJ⁰¦⁰F÷⁺¹η²«F⁴«F⁻η⁺κꧧεκ⁺μκ↷A⮌EεEε§ξν嶻A⎇﹪ι²Eε⮌λ⮌εεA⎇‹ι²⁻⁺²⁺κκη⁻η⁺κκκ¿﹪ι²Mκ¹M¹κ

在线尝试!请注意,此后已更改为,链接反映了这一点。说明:

       θ          Input string
      L           Length
     X  ⁰·⁵       Raise to the power 0.5
    I             Cast to string
   ⪪       .      Split on the decimal point
  §         ⁰     Take the first element (integer part)
 I                Cast to integer
A            η    Assign to h

计算h = int(sqrt(len(q)))。(Floor尚未实施...)

Fη⊞υ✂θ×ιη

从输入中提取h长度的切片h。(实际上,我不会费心地将切片截断为一定长度h。)我使用for循环而不是a,Map因为我需要某处Assign的结果,Map并且在处理a时这是不平凡的Slice

F⁴«

展开发生4次,每个方向一次(编码时向下,向右,向上,向左)。此循环的循环变量为i

   Aυε

取得切片的字符串的副本。

   J⁰¦⁰

跳回到画布的原点,以便每个展开都在同一位置以h-by- hsquare 开始。

   F÷⁺¹η²«

重复(h+1)/2次数;每次展开一次,再加上原始方块一次。此循环的循环变量为k

          F⁴«

重复4次,对展开的正方形的每一侧一次。(我不使用循环变量l。)

             F⁻η⁺κκ         Loop h-2k times, loop variable `m`
                    §εκ     Take the `k`th row
                   §   ⁺μκ  Take the `k+m`th column
                            Implicitly print the character

打印展开的正方形的一侧。由于这是k展开的位置,因此正方形的边为h-2k,并且使字符k远离原始正方形的边缘。

枢轴准备好打印正方形的下一个侧面。

               Eε       Map over the array (element `m`, index `n`)
                 Eε     Map over the array (element `x`, index `p`)
                   §ξν  Take the `n`th element of `x`
              ⮌         Reverse
             A        ε Replace the array with the result

旋转切片的字符串。(是的,这是一个ξ。我不经常使用它!)Eη也可以用于外部Map。旋转还具有将数组的宽度截断为的便利副作用h

             ¶»

打印完一面后,光标移出正方形的边缘。少打印一个字符会使第1面的正方形打印失败,并且反过来也不会打高尔夫球。之前已进行过枢轴操作,打印换行符可方便地将光标移回角落。

            ﹪ι²         Take `i` modulo 2
           ⎇            Choose either
                   ⮌ε   Reverse the array
               Eε       Map over the array (element `l`, index `m`)
                 ⮌λ     Reverse each element
          A          ε  Replace the array with the result

垂直或水平翻转正方形。

           ⎇‹ι²                 If `i` < 2
                  ⁺κκ           Double `k`
                ⁺²              Add 2
               ⁻     η          Subtract `h`
                        ⁺κκ     Else double `k`
                      ⁻η        Subtract from `h`
          ≔                κ    Assign back to `k`.

计算下一个展开的位移。

           ﹪ι²          Take `i` modulo 2
          ¿             If not zero
              Mκ¹       `k` across and 1 down
                 M¹κ    Else 1 across and `k` down

根据需要水平或垂直移动到下一个展开。

这是指向97字节版本的链接,该版本通过使用所有最新的Charcoal功能获得,包括Floor在线试用!链接是详细版本的代码。


您确定这有效吗?TIO似乎引发了一个错误。
LyricLy

@LyricLy Bah,我以为我很聪明,但是没有真正检查它是否有效。我将还原更改。
尼尔

1
废话忘了让浮点数在切片oops中起作用
仅ASCII码,

@仅ASCII对我没有帮助,无论如何我都需要将整数截断为整数。
尼尔

对。好吧,我即将添加发言权,所以它不会成为太大问题:P
ASCII码,仅ASCII
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.