Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- l~/{_1fb_,Y${(_@=}%:++z}2*;=
- 4 [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16]
- l~/ "Evaluates the input and split the array into chunks"
- "of size N where N is the first integer";
- { }2* "Run this code block 2 times";
- _1fb "Copy the 2D array and calculate sum of each row of copy";
- _, "Copy the array containing sum of each row and get"
- "its length. This is equal to N";
- Y${ }% "Run this code block for each array of the original"
- "2D array that we copied from stack";
- (_ "Put the length number to top of stack, decrement and"
- "copy that";
- @= "Take the element at that index from each row and put"
- "N back behind at second position in stack";
- :+ "Take sum of elements of the array. This is sum of"
- "one of the diagonals of the 2D array";
- + "Push diagonal sum to row sum array";
- z "Bring original array to top and transpose columns";
- ; "At this point, the stack contain 3 arrays:"
- " Array with sum of rows and main diagonal,"
- " Array with sum of columns and secondary diagonal and"
- " The original array. Pop the original array";
- = "Check if sum of rows + main diagonal array is equal to ";
- "sum of columns + secondary diagonal array";
- STDIN: 4,[16,3,2,13,5,10,11,8,9,6,7,12,4,15,14,1]
- Output: True
- ∧/2=/(+⌿x,⍉x),+/↑1 1∘⍉¨x(⌽x←⎕⍴⍨,⍨⎕)
- d = Diagonal; r = Reverse; i = Input[];
- Length@Union[Tr /@ Join[p = Partition[i[[2]], i[[1]]],
- t = Transpose@p, {d@p}, {d@t}, {d@r@p}, {d@r@t}]] == 1
- {4,{16, 3, 2, 13, 5, 10, 11, 8, 9, 6, 7, 12, 4, 15, 14, 1}}
- ∧/2=/+⌿(1 1∘⍉∘⌽,1 1∘⍉,⍉,⊢)⎕⍴⍨,⍨⎕
- {M←⍺ ⍺⍴⍵⋄d←M=⍉M⋄(⊃≡∪)((+/,+⌿)M),+/∘,¨d(⌽d)ר⊂M}
- 4{M←⍺ ⍺⍴⍵⋄d←M=⍉M⋄(⊃≡∪)((+/,+⌿)M),+/∘,¨d(⌽d)ר⊂M}16 3 2 13 5 10 11 8 9 6 7 12 4 15 14 1
- 1
- ~]:q(/q(/zip+[q()/{(;}%]+[q((/);(;{(;}%]+{{+}*}%.&,2<q(2?,{)}%-!*
- s=io.read(1)v=io.read(2)d=0 r=0 for i=1,#s do t=0 for j = 1, #s do t=t+s[i][j]end d=d+s[i][i] r=r+s[i][#s-i+1]if t ~= v then o=true end end if d~=v and r~= v then o=true end print(not o)
- ô©O®øO®ÅO®Å/O)˜Ës{DgLQ*
Add Comment
Please, Sign In to add comment