Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.10 KB | None | 0 0
  1. [3, 2, 4, 1, 1, 5, 1, 2]
  2. ^
  3.  
  4. [3, 2, 4]
  5.  
  6. 3 -> [3, 2, 4]
  7. 2 -> [2, 4]
  8. 4 -> [4, 1, 1, 5]
  9. 1 -> [1]
  10. 1 -> [1]
  11. 5 -> [5, 1, 2]
  12. 1 -> [1]
  13. 2 -> [2]
  14.  
  15. [3, 2, 4, 1, 1, 5, 1, 2]
  16. ^ ^ ^ ^ ^
  17.  
  18. [3, 2, 4] -> 9
  19. [2, 4] -> 6
  20. [4, 1, 1, 5] -> 11
  21. [1] -> 1
  22. [1] -> 1
  23. [5, 1, 2] -> 8
  24. [1] -> 1
  25. [2] -> 2
  26.  
  27. [9, 6, 11, 1, 1, 8, 1, 2]
  28.  
  29. [1, 2, 3, 4, 5] -> [1, 5, 12, 9, 5]
  30. [3, 3, 3, 3, 3, 3, 3, 3] -> [9, 9, 9, 9, 9, 9, 6, 3]
  31. [5, 1, 2, 4, 1] -> [13, 1, 6, 5, 1]
  32. [1] -> [1]
  33.  
  34. ṫJḣ"ḅ1
  35.  
  36. ṫJḣ"ḅ1 Main link. Argument: A (array)
  37.  
  38. J Index; yield the 1-based indices of A.
  39. ṫ Tail; map k to the postfix of A that begins with the k-th element.
  40. ḣ" Vectorized head; for each k in A, truncate the corr. postfix to length k.
  41. ḅ1 Convert the resulting slices from base 1 to integer.
  42.  
  43. lambda X:[sum(X[i:i+k])for i,k in enumerate(X)]
  44.  
  45. f l@(x:y)=sum(take x l):f y
  46. f x=x
  47.  
  48. +/@{."_1].
  49.  
  50. f =: +/@{."_1].
  51. f 3 2 4 1 1 5 1 2
  52. 9 6 11 1 1 8 1 2
  53. f 1 2 3 4 5
  54. 1 5 12 9 5
  55.  
  56. +/@{."_1]. Input: A
  57. ]. Get each suffix of A from longest to shortest
  58. {."_1 For each value in A, take that many values from its corresponding suffix
  59. +/@ Sum that group of values taken from that suffix
  60. Return the sums
  61.  
  62. d+
  63. $*
  64. M!&`b1(1)*(?<-1>,1+)*
  65. M%`1
  66. ,
  67.  
  68. Tr@Take[#,UpTo@#&@@#]&/@Drop[#,t-1]~Table~{t,Length@#}&
  69.  
  70. f = %; f /@ {{1, 2, 3, 4, 5}, {3, 3, 3, 3, 3, 3, 3, 3}, {5, 1, 2, 4, 1}, {1}}
  71.  
  72. (* {{1, 5, 12, 9, 5}, {9, 9, 9, 9, 9, 9, 6, 3}, {13, 1, 6, 5, 1}, {1}} *)
  73.  
  74. [D¬£Oˆ¦Ž
  75.  
  76. [ # infinite loop
  77. D # duplicate current list
  78. ¬ # get head of list
  79. £ # get that many elements from list
  80. O # sum
  81. ˆ # add to global array
  82. ¦ # remove first element of list
  83. Ž # break if stack is empty
  84. # implicitly push and print global array
  85.  
  86. .es:Qk+b
  87.  
  88. f(A)->put(1,1),L=lists,[L:sum(L:sublist(A,put(1,get(1)+1),X))||X<-A].
  89.  
  90. FKo>i<s
  91.  
  92. - o = 0
  93. F - for i in input:
  94. o - o+=1
  95. > - input[o:]
  96. i< - ^[:i]
  97. s - sum(^)
  98.  
  99. Function e(g())
  100. Dim h()
  101. k=LBound(g)
  102. l=UBound(g)
  103. ReDim h(k To l)
  104. On Error Resume Next
  105. For i=k To l
  106. For j=i To i+g(i)-1
  107. h(i)=h(i)+g(j)
  108. Next
  109. Next
  110. e=h
  111. End Function
  112.  
  113. ms<~tQ
  114.  
  115. ms<~tQ
  116. ms<~tQdQ Implicit variable introduction
  117. Implicit: Q = eval(input())
  118. m Q Map d over the input, Q
  119. < Qd Take the first d elements of Q
  120. s Sum them
  121. ~tQ Afterwards, set Q to the tail of Q, removing the first element.
  122.  
  123. let f(A:int[])=[for i in 0..A.Length-1->Seq.skip i A|>Seq.truncate A.[i]|>Seq.sum]
  124.  
  125. f=([a,...t],n)=>a&&n?a+f(t,n-1):0;g=([a,...t],r=[])=>a?g(t,[...r,a+f(t,a-1)]):r
  126.  
  127. int[]s(List<int>a)=>a.Select((n,i)=>a.GetRange(i,Math.Min(n,a.Count-i)).Sum()).ToArray();
  128.  
  129. .v|h~l(A:Tc?;A?)b:0&~b.h~+A
  130.  
  131. .v Input = Output = []
  132. | Or
  133. h~l A is a list, its length is the value of the first element of the Input
  134. (
  135. A:Tc? The concatenation of A with another list T results in the Input
  136. ; Or
  137. A? A = Input
  138. )
  139. b:0& Call recursively on Input minus the first element
  140. ~b. Output is the output of that call with an extra element at the beginning
  141. h~+A That extra element is the sum of the elements of A
  142.  
  143. {+/¨⍵↑∘⌽¨⌽,⌽⍵}
  144.  
  145. {⌽+/¨(-↑¨,)⌽⍵}
  146.  
  147. <?foreach($a=$_GET[a]as$i=>$v)echo array_sum(array_slice($a,$i,$v)),"
  148. ";
  149.  
  150. function f($a){foreach($a as$i=>$v)$r[]=array_sum(array_slice($a,$i,$v));return$r;}
  151.  
  152. function p($a){foreach($c=$r=$a as$i=>$v)for($k=$i;$k--;)if(--$a[$k]>0)$r[$k]+=$v;return$r;}
  153.  
  154. <?foreach($a=$r=$c=$_GET[a]as$i=>$v)for($k=$i;$k--;)if(--$c[$k]>0)$r[$k]+=$v;print_r($r);
  155.  
  156. {sum each(til[count x],'x)sublist:x}
  157.  
  158. q){sum each(til[count x],'x)sublist:x}3 2 4 1 1 5 1 2
  159. 9 6 11 1 1 8 1 2
  160.  
  161. md{z:l-g:c;+c;q:c;};:1:l;
  162.  
  163. m :1:l; #loop over entire input
  164. #set each value to...
  165. d{ } #the sum of...
  166. z:l-g:c:+c;q:c; #the input cropped to
  167. #the length of the value in the cell
  168.  
  169. n=>_.From(n).Select((v,i)=>_.From(n).Slice(i,i+v).Sum()).ToArray()
  170.  
  171. (defn f[[b & r]](concat[(apply + b(take(dec b)r))](if r(f r))))
  172.  
  173. fGy+!-R0<G*!s
  174.  
  175. f % Take input implicitly. Indices of nonzero elements: this gives [1 2 ... n]
  176. % where n is input size
  177. G % Push input again
  178. y % Push a copy of [1 2 ... n]
  179. + % Add. Gives [a+1 b+2...] where [a b...] is the input
  180. ! % Transpose into a column vector
  181. - % Subtraction with broadcast. Gives 2D array
  182. R % Keep upper triangular part, making the rest of entries 0
  183. 0< % True for negative entries. Each row corresponds to a substring sum.
  184. % For each row, this gives true for the entries of the input that make up
  185. % that substring sum. Each row is thus a mask to select entries of the input
  186. G % Push input again
  187. * % Multiply with broadcast. This multiplies the input times each row
  188. !s % Sum of each row. Implicitly display
  189.  
  190. Console.Write(String.Join(",",a.Select((v,i)=>a.Skip(i).Take(v).Sum().ToString()).ToArray()));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement