Pali-n-Drome此列表


13

面临的挑战是,将以下内容作为输入,以扩展回文法的实施:

  • n > 1和清单l

您的程序必须在水平和垂直方向上对列表进行回文,也就是说,它必须首先对列表本身进行回文处理,然后再对列表中的每个元素进行回文处理。或相反。在回钯化之前,请确保所有元素的长度均等。然后将按n顺序执行回文动作,直到达到所需的输出为止。显示预期输出的最简单方法是通过几个示例进行操作:


在上执行了一次迭代[123,456,789]

首先,您将列表palindromize到[123,456,789,456,123]

  • 如果这不是一个回文,如果结合在一起,就列表而言,它是一个回文。
  • [a,b,c]成为[a,b,c,b,a],因此LIST进行了Palindromized。

然后,您对每个list元素进行Palindromize [12321,45654,78987,45654,12321]

这就是每次迭代的执行方式,它本质上是全向回文。


鉴于n=1 and l=[123,456,789]

12321
45654
78987
45654
12321

给定 n=2 and l=[123,456,789]

123212321
456545654
789878987
456545654
123212321
456545654
789878987
456545654
123212321

鉴于n=1 and l=[3,2,1]

3
2
1
2
3

鉴于n=2 and l=["hat","mad"," a "]

hatahatah
madamadam
 a a a a 
madamadam
hatahatah
madamadam
 a a a a 
madamadam
hatahatah

鉴于n=2 and l=[" 3 ","2000"," 100"]

 3   3 3   3 
2000002000002
100 00100 001
2000002000002
 3   3 3   3 
2000002000002
100 00100 001
2000002000002
 3   3 3   3 

鉴于n=4 and l=["3 ","20","1 "]

3 3 3 3 3 3 3 3 3
20202020202020202
1 1 1 1 1 1 1 1 1
20202020202020202
3 3 3 3 3 3 3 3 3
20202020202020202
1 1 1 1 1 1 1 1 1
20202020202020202
3 3 3 3 3 3 3 3 3
20202020202020202
1 1 1 1 1 1 1 1 1
20202020202020202
3 3 3 3 3 3 3 3 3
20202020202020202
1 1 1 1 1 1 1 1 1
20202020202020202
3 3 3 3 3 3 3 3 3
20202020202020202
1 1 1 1 1 1 1 1 1
20202020202020202
3 3 3 3 3 3 3 3 3
20202020202020202
1 1 1 1 1 1 1 1 1
20202020202020202
3 3 3 3 3 3 3 3 3
20202020202020202
1 1 1 1 1 1 1 1 1
20202020202020202
3 3 3 3 3 3 3 3 3
20202020202020202
1 1 1 1 1 1 1 1 1
20202020202020202
3 3 3 3 3 3 3 3 3

鉴于n=3 and l=["_|__","__|_","___|"]

_|___|_|___|_|___|_|___|_
__|_|___|_|___|_|___|_|__
___|_____|_____|_____|___
__|_|___|_|___|_|___|_|__
_|___|_|___|_|___|_|___|_
__|_|___|_|___|_|___|_|__
___|_____|_____|_____|___
__|_|___|_|___|_|___|_|__
_|___|_|___|_|___|_|___|_
__|_|___|_|___|_|___|_|__
___|_____|_____|_____|___
__|_|___|_|___|_|___|_|__
_|___|_|___|_|___|_|___|_
__|_|___|_|___|_|___|_|__
___|_____|_____|_____|___
__|_|___|_|___|_|___|_|__
_|___|_|___|_|___|_|___|_

鉴于n=2 and l=["---|---","__|","___|","____|"]

---|-----|-----|-----|---
  __|   |__   __|   |__  
 ___|   |___ ___|   |___ 
 ____| |____ ____| |____ 
 ___|   |___ ___|   |___ 
  __|   |__   __|   |__  
---|-----|-----|-----|---
  __|   |__   __|   |__  
 ___|   |___ ___|   |___ 
 ____| |____ ____| |____ 
 ___|   |___ ___|   |___ 
  __|   |__   __|   |__  
---|-----|-----|-----|---

规则

  • n 将始终大于1。
  • l 将始终具有多个元素。
  • 的所有元素l都具有相同的长度。
  • 这是最短的解决方案,将被标记为获胜者。

9
如果我们不必填充元素,这将是一个更好的挑战。
mbomb007'3

2
@JonathanAllan可以说是全向回文或2D回文。我已经更新了说明;同样,填充可以防止一些较小的字符串已经是回文式的奇怪情况。
Magic Octopus Urn'Mar

1
@JonathanAllan就是列表,如果您正在查看列表作为要被Palindromized的项目。就像[@ 1,@ 2,@ 1]也是列表中的回文,而不是按元素...
Magic Octopus Urn

1
@JonathanAllan是的,从本质上讲,如果您愿意,可以这样看。
Magic Octopus Urn'Mar

1
最后一个示例需要填充。
乔纳森·艾伦

Answers:


9

05AB1E,4个字节

请注意,如果只需要一次迭代(n=1),那么程序将是回文û€û

Fû€û

在线尝试

F       Do n times
 û      Palindromize the list
  €û    Palindromize each element in the list

如果填充,输入仍然是程序的必需部分(11个字节):

€R.B€RIFû€û

我找不到找到更正当理由的捷径。左对齐和居中都很容易,但是由于某些原因,它更长。使用E²代替I也可以。


7

Python 2中71个 63字节

lambda x,n,f=lambda x:x+x[-2::-1]:eval('f(map(f,'*n+`x`+'))'*n)

在线尝试!

将回文函数分配给f,生成并评估以下模式(用于n=4
f(map(f,f(map(f,f(map(f,f(map(f,<input>))))))))


1
我想你的意思是分配。我不认为是动词,大声笑。
mbomb007'3

@ mbomb007韦尔普,时间以获得更多的咖啡〜

6

果冻,6 个字节

ŒḄŒB$¡

双向链接,或包含列表和的完整程序n

在线尝试!

使用Lynn出色的内置“反弹”两个版本。

ŒḄŒB$¡ - Main link: l, n
     ¡ - repeat n times
    $  -     last two links as a monad (firstly with l then the result...)
ŒḄ     -         bounce ("palindromise") the list
  ŒB   -         bounce the elements

5

Python 2,64个字节

h=lambda a:a+a[-2::-1]
f=lambda a,n:n and f(h(map(h,a)),n-1)or a

在线尝试!-页脚打印结果列表的每个元素,每行一个,“漂亮打印”。

h 是palindomisation函数,它将列表的所有元素从索引的最后一个(索引为-2)追加到输入(以大小为-1的步长)。

f呼叫h与调用的结果而h依次在每个元件上,降低了n由一个,并调用自身,直到n达到0时,在该点a是成品。


...而且我仍然忘记了f=递归函数,有一天我会记得。
乔纳森·艾伦

2

APL,15个字节

(Z¨Z←⊢,1↓⌽)⍣⎕⊢⎕

说明:

  • (... )⍣⎕⊢⎕:读取列表并N作为输入和运行N时间:
    • ⊢,1↓⌽:列表,后跟反向列表的末尾
    • Z←:将此功能存储在 Z
    • :并将其也应用于列表的每个元素

测试:

          (Z¨Z←⊢,1↓⌽)⍣⎕⊢⎕ 
    ⎕:
          '帽子''疯狂'''
    ⎕:
          2
    ──────────┬───────┬──────┬ ──────────┬───────┬────────┬
    │hatahatah│madamadam│aaaa│madamadam│hatahatah│madamadam│aaaa│madamadam│hatahatah│
    ──────────┴───────┴──────┴ ──────────┴───────┴────────┴

1

Groovy,66个字节

{x,n->f={z->z+z[z.size()-2..0]};n.times{x=f(x).collect{f(it)}};x}

1

Haskell,51个字节

x%n=iterate((++)<*>reverse.init)x!!n
x?n=(%n)<$>x%n

用法示例:["123","456","789"] ? 1-> ["12321","45654","78987","45654","12321"]在线尝试!

(++)<*>reverse.init从列表中创建回文,iterate(...)x一次又一次重复,并将中间结果收集在列表中,!!n选择此列表的第n个元素。(%n)<$>x%n对的正回文的每个元素进行一个正回文x


1

JavaScript(ES6),87个字节

f=(n,l,r=l=>[...a].reverse().slice(1))=>n--?f(l.concat(r(l)).map(s=>s+r(s).join``),n):l

1

,25字节

24个字节的代码,-l标志+1 。

Lq{gM:_.@>RV_gAL:@>RVg}g

将列表作为命令行参数,并将stdin中的数字n用作。在线尝试!

说明

                          g is list of cmdline args (implicit)
Lq{                   }   Read a line of input and loop that many times:
      _.@>RV_             Lambda function: take all but the first character (@>) of the
                           reverse (RV) of the argument (_), and concatenate that (.) to
                           the argument (_)
   gM:                    Map this function to g and assign the result back to g
                 @>RVg    Take all but the first element of the reverse of g
             gAL:         Append that list to g and assign the result back to g
                       g  After the loop, print g (each item on its own line due to -l)
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.