22
\$\begingroup\$

TASK

The goal is to write a program that rotates any two-dimensional list by 45 degrees, it must be able to do this up to 7*45 (at once) before returning the list. The list will not necessarily be square or rectangular. You must include output for the examples in your answer. It must also work for cases that are not in the examples... circles, triangles etc. You cannot use a pre-existing function to do the whole thing.

All lists will have at least one axis of symmetry (N,S,E,W). All sublists are to be assumed as center-aligned. Odd-even lists will shift to the left one to align properly. See example 4 for gaps in the middle of a sublist.

INPUT

Your program will use a variable named l containing the list, and a variable named n specifying the amount the list will be rotated (n*45) (n will always be less than 7, and can be 0). It will have to accept l containing sublists of any printable data type (decimal, List, int, String[].. etc), but sublists will only contain one data type at a time.

You do not need to accept console input or use stdin. The lines specifying the test values of l and n are not included in the character count, but must be included in the submitted code.

OUTPUT

Your program must print the list in the correct orientation, NIL can be used to pad lists if you desire, but padding is not necessary (you get a smiley face if they are padded, though). Sub-lists do not have to be indented or separated by newlines as in the examples.

EXAMPLES

1

IN
l=
[[0 , 1 , 2],
 [3 , 4 , 5],
 [6 , 7 , 8]]
n=1

OUT
[    [0],
   [3 , 1],
 [6 , 4 , 2],
   [7 , 5],
     [8]    ]

2

IN
l=
[[a , b , c , d],
 [e , f , g , h]]
n=2

OUT
[[e , a],
 [f , b],
 [c , g],
 [h , d]]

3

IN
l=
[[A , B , C , D , E , F],
     [G , H , I , J],
         [K , L],
         [0 , 8],
         [M , N],
     [O , P , Q , R],
 [S , T , U , V , W , X]]
n=7

OUT
[          [F],
         [E],
       [D , J],
     [C , I],
   [B , H , L],
 [A , G , K , 8],
           [0 , N , R , X],
             [M , Q , W],
               [P , V],
             [O , U],
               [T],
             [U]          ]

4

IN
l=
[[9 , 8 , 7 , 6],
     [5],
 [4 , 3 , 2 , 1],
     [0]        ]
n=3

OUT
[  [0 , 4],
     [3],
   [2 , 5 , 9],
 [1 ,NIL, 8],
       [7],
     [6],     ]

5

IN
l=
[    [Q],
 [X ,NIL, Y],
     [Z]    ]
n=2

OUT
[    [X],
 [Z ,NIL, Q],
     [Y]     ]
\$\endgroup\$
  • 4
    \$\begingroup\$ Oooh. That's tough. Looks fun, though! \$\endgroup\$ – TheDoctor May 3 '14 at 14:25
  • 1
    \$\begingroup\$ Two questions: 1) We do not have to pad lists, right? 2) Do you really want us to rotate the list n times and not by n·45 °? I am asking because I am pretty certain that I would not obtain the result of example 3 by applying seven 45 ° rotations. \$\endgroup\$ – Wrzlprmft May 3 '14 at 21:54
  • \$\begingroup\$ No, you do not have to pad. The list should however be able to be arranged into the correct visual orientation, although it does not have to be output that way... the output will have no newlines. The list is rotated by n*45. \$\endgroup\$ – Οurous May 3 '14 at 23:19
8
\$\begingroup\$

Python – 234 201

# example for defining lists and n
l=[[1,2,3,4],
     [5],
   [6,7,8,9]]
n=1

# counting code
j=1j
m=max(map(len,l))+len(l)
M=range(-m,m)
e=enumerate
d=[[v for x in M for i,u in e(l)for k,v in e(u)if[1,1+j,j,j-1,-1,-j-1,-j,1-j][n]*(k-(len(u)-1)/2+j*i)==x+y*j]for y in M]
print[x for x in d if x]

Ungolfed Version

rotation = [1,1+1j,1j,1j-1,-1,-1j-1,-1j,1-1j][n]
m = max(map(len,l))+len(l)
output = []
for y in range(-m,m):
    line = []
    for x in range(-m,m):
        for i,sublist in enumerate(l):
            for k,entry in enumerate(sublist):
                if rotation * ( k-(len(sublist)-1)/2 + i*1j ) == x + y*1j:
                    line += [entry]
    if line != []:
        output += [line]
print output

This uses that multiplication (of a complex number) by a complex number corresponds to rotating and stretching. [1,1+1j,1j,1j-1,-1,-1j-1,-1j,1-1j] are complex numbers corresponding to the required angles and using the smallest scaling factor such that for an integer complex input the output is again integer complex.

\$\endgroup\$
  • 1
    \$\begingroup\$ I'm trying to understand how this works, but I get lost at the complex numbers. Could I ask for an explanation? \$\endgroup\$ – Οurous May 4 '14 at 10:12
  • 1
    \$\begingroup\$ @Ourous: Let x+iy=(x,y), then multiplying this by 1+i=(1,1), you get a rotation by 45 degrees. \$\endgroup\$ – Kyle Kanos May 4 '14 at 19:23
  • \$\begingroup\$ Great solution. I'm trying to adapt it to also insert the appropriate padding in the output lists, but am not having much luck. Is that a non-trivial addition? \$\endgroup\$ – tkocmathla Jul 16 '15 at 20:15
  • \$\begingroup\$ @tkocmathla: I did not test this, but try adding else: line += [None] after the fourth from the last line. \$\endgroup\$ – Wrzlprmft Jul 16 '15 at 21:32

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.