Guest User

Untitled

a guest
Jan 21st, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. l~/{_1fb_,Y${(_@=}%:++z}2*;=
  2.  
  3. 4 [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16]
  4.  
  5. l~/ "Evaluates the input and split the array into chunks"
  6. "of size N where N is the first integer";
  7. { }2* "Run this code block 2 times";
  8. _1fb "Copy the 2D array and calculate sum of each row of copy";
  9. _, "Copy the array containing sum of each row and get"
  10. "its length. This is equal to N";
  11. Y${ }% "Run this code block for each array of the original"
  12. "2D array that we copied from stack";
  13. (_ "Put the length number to top of stack, decrement and"
  14. "copy that";
  15. @= "Take the element at that index from each row and put"
  16. "N back behind at second position in stack";
  17. :+ "Take sum of elements of the array. This is sum of"
  18. "one of the diagonals of the 2D array";
  19. + "Push diagonal sum to row sum array";
  20. z "Bring original array to top and transpose columns";
  21. ; "At this point, the stack contain 3 arrays:"
  22. " Array with sum of rows and main diagonal,"
  23. " Array with sum of columns and secondary diagonal and"
  24. " The original array. Pop the original array";
  25. = "Check if sum of rows + main diagonal array is equal to ";
  26. "sum of columns + secondary diagonal array";
  27.  
  28. STDIN: 4,[16,3,2,13,5,10,11,8,9,6,7,12,4,15,14,1]
  29. Output: True
  30.  
  31. ∧/2=/(+⌿x,⍉x),+/↑1 1∘⍉¨x(⌽x←⎕⍴⍨,⍨⎕)
  32.  
  33. d = Diagonal; r = Reverse; i = Input[];
  34. Length@Union[Tr /@ Join[p = Partition[i[[2]], i[[1]]],
  35. t = Transpose@p, {d@p}, {d@t}, {d@r@p}, {d@r@t}]] == 1
  36.  
  37. {4,{16, 3, 2, 13, 5, 10, 11, 8, 9, 6, 7, 12, 4, 15, 14, 1}}
  38.  
  39. ∧/2=/+⌿(1 1∘⍉∘⌽,1 1∘⍉,⍉,⊢)⎕⍴⍨,⍨⎕
  40.  
  41. {M←⍺ ⍺⍴⍵⋄d←M=⍉M⋄(⊃≡∪)((+/,+⌿)M),+/∘,¨d(⌽d)ר⊂M}
  42.  
  43. 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
  44. 1
  45.  
  46. ~]:q(/q(/zip+[q()/{(;}%]+[q((/);(;{(;}%]+{{+}*}%.&,2<q(2?,{)}%-!*
  47.  
  48. 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)
  49.  
  50. ô©O®øO®ÅO®Å/O)˜Ës{DgLQ*
Add Comment
Please, Sign In to add comment