26
3
Take a square matrix containing positive integers as input, and calculate the "rotated sum" of the matrix.
Rotated sum:
Take the sum of the original matrix and the same matrix rotated 90, 180 and 270 degrees.
Suppose the matrix is:
2 5 8
3 12 8
6 6 10
then the rotated sum will be:
2 5 8 8 8 10 10 6 6 6 3 2
3 12 8 + 5 12 6 + 8 12 3 + 6 12 5 =
6 6 10 2 3 6 8 5 2 10 8 8
26 22 26
22 48 22
26 22 26
Test cases:
Input and output separated by dashes, different test cases separated by a newline. Test cases in more convenient formats can be found here.
1
-------------
4
1 3
2 4
-------------
10 10
10 10
14 6 7 14
6 12 13 13
6 2 3 10
5 1 12 12
-------------
45 37 24 45
24 30 30 37
37 30 30 24
45 24 37 45
14 2 5 10 2
18 9 12 1 9
3 1 5 11 14
13 20 7 19 12
2 1 9 5 6
-------------
24 29 31 41 24
41 49 31 49 29
31 31 20 31 31
29 49 31 49 41
24 41 31 29 24
f=lambda*l:l[3:]and[map(sum,zip(*d))for d in zip(*l)]or f(zip(*l[0][::-1]),*l)
saves two bytes with "normal" input. Try it online! – Dennis – 2018-01-20T13:21:59.677@Dennis Thank you! I thought
lambda*l
wasn't possible in Python 2 for some reason. – Mr. Xcoder – 2018-01-20T13:25:21.780You can't do
– Dennis – 2018-01-20T13:31:20.153x,*y=1,2,3
in Python 2.7 or[*x]
in Python 3.4, but starred expressions can be used for function arguments even in Python 1.6. Try it online!