13
\$\begingroup\$

The challenge here is to extend an implementation of palindrome given the following as inputs:

  • n > 1 and a list l.

Your program must palindrome the list both vertically and horizontally, that is to say it must first palindrome the list itself, then each element in the list after; or the other way around. Before palindromization, all elements are ensured to be equal length. The palindrome action is then to be performed n times in sequence until the desired output is met. The easiest way to show the expected outputs is just to run through a few examples:


One iteration performed on [123,456,789]:

First you palindromize the list to [123,456,789,456,123].

  • While this is not a palindrome if joined together, it is a palindrome in terms of the list.
  • [a,b,c] became [a,b,c,b,a], so the LIST was palindromized.

Then, you palindromize each list element [12321,45654,78987,45654,12321].

This is how each iteration is performed, it's essentially an omnidirectional palindrome.


Given n=1 and l=[123,456,789]:

12321
45654
78987
45654
12321

Given n=2 and l=[123,456,789]

123212321
456545654
789878987
456545654
123212321
456545654
789878987
456545654
123212321

Given n=1 and l=[3,2,1]:

3
2
1
2
3

Given n=2 and l=["hat","mad"," a "]:

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

Given 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 

Given 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

Given n=3 and l=["_|__","__|_","___|"]:

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

Given n=2 and l=["---|---","__|","___|","____|"]:

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

Rules

  • n will always be greater than 1.
  • l will always have more than 1 element.
  • All elements of l are the same length.
  • This is shortest solution will be marked as winner.
\$\endgroup\$
  • 9
    \$\begingroup\$ This would be a better challenge if we didn't have to pad elements. \$\endgroup\$ – mbomb007 Mar 10 '17 at 17:01
  • 2
    \$\begingroup\$ @JonathanAllan it's an omnidirectional palindrome, or 2D palindrome you could say. I've updated the description; also, the padding prevents a few odd fringe cases where a smaller string is already a palindrome. \$\endgroup\$ – Magic Octopus Urn Mar 10 '17 at 17:07
  • 1
    \$\begingroup\$ @JonathanAllan it is in terms of the list, if you are looking at the LIST as the item to be palindromized. Just like [@1,@2,@1] is also a palindrome when looking at it as a list, not by the elements... \$\endgroup\$ – Magic Octopus Urn Mar 10 '17 at 17:08
  • 1
    \$\begingroup\$ @JonathanAllan yeah, essentially, you can look at it like that if you want. \$\endgroup\$ – Magic Octopus Urn Mar 10 '17 at 17:11
  • 1
    \$\begingroup\$ Last example requires padding. \$\endgroup\$ – Jonathan Allan Mar 10 '17 at 17:20
9
\$\begingroup\$

05AB1E, 4 bytes

Note that if only a single iteration was required (n=1), then the program would be the palindrome û€û.

Fû€û

Try it online

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

If padding the input was still a required part of the program (11 bytes):

€R.B€RIFû€û

I couldn't find a shorter way to right-justify. Left-justification and centering were all easy, but this was longer for some reason. Using E or ² instead of I also works.

\$\endgroup\$
7
\$\begingroup\$

Python 2, 71 63 bytes

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

Try it online!

Assign a palindrome function to f, generate and evaluate the following pattern (for n=4)
f(map(f,f(map(f,f(map(f,f(map(f,<input>))))))))

\$\endgroup\$
  • 1
    \$\begingroup\$ I think you mean assign. I don't think assing is a verb, lol. \$\endgroup\$ – mbomb007 Mar 10 '17 at 18:08
  • \$\begingroup\$ @mbomb007 welp, time to get more coffee~ \$\endgroup\$ – Rod Mar 10 '17 at 18:18
6
\$\begingroup\$

Jelly, 6 bytes

ŒḄŒB$¡

Dyadic link, or full program taking the list and n.

Try it online!

Using both versions of Lynn's fantastic built-in "bounce".

ŒḄŒ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
\$\endgroup\$
5
\$\begingroup\$

Python 2, 64 bytes

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

Try it online! - footer prints each of the elements of the resulting list, one per line, a "pretty print".

h is the palindomisation function, it appends to the input, all the elements of a list from the last but one, index -2, to the start in steps of size -1.

f calls h with the result of calling h on each element in turn, reduces n by one and calls itself until n reaches 0, at which point a is the finished product.

\$\endgroup\$
  • \$\begingroup\$ ...and I am still forgetting the f= for recursive functions, one day I'll remember. \$\endgroup\$ – Jonathan Allan Mar 10 '17 at 18:18
2
\$\begingroup\$

APL, 15 bytes

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

Explanation:

  • (...)⍣⎕⊢⎕: read the list and N as input, and run N times:
    • ⊢,1↓⌽: the list, followed by the tail of the reversed list
    • Z←: store this function in Z
    • : and apply it to each element of the list as well

Test:

          (Z¨Z←⊢,1↓⌽)⍣⎕⊢⎕ 
    ⎕:
          'hat' 'mad' ' a '
    ⎕:
          2
    ┌─────────┬─────────┬─────────┬─────────┬─────────┬─────────┬─────────┬─────────┬─────────┐
    │hatahatah│madamadam│ a a a a │madamadam│hatahatah│madamadam│ a a a a │madamadam│hatahatah│
    └─────────┴─────────┴─────────┴─────────┴─────────┴─────────┴─────────┴─────────┴─────────┘
\$\endgroup\$
1
\$\begingroup\$

Groovy, 66 bytes

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

Haskell, 51 bytes

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

Usage example: ["123","456","789"] ? 1 -> ["12321","45654","78987","45654","12321"]. Try it online!.

(++)<*>reverse.init makes a palindrome out of a list, iterate(...)x repeats this again and again and collects the intermediate results in a list, !!n picks the nth element of this list. (%n)<$>x%n makes a n-palindrom of each element of the n-palindrome of x.

\$\endgroup\$
1
\$\begingroup\$

JavaScript (ES6), 87 bytes

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

Pip, 25 bytes

24 bytes of code, +1 for -l flag.

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

Takes the list as command-line arguments and the number n from stdin. Try it online!

Explanation

                          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)
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.