Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [3, 2, 4, 1, 1, 5, 1, 2]
- ^
- [3, 2, 4]
- 3 -> [3, 2, 4]
- 2 -> [2, 4]
- 4 -> [4, 1, 1, 5]
- 1 -> [1]
- 1 -> [1]
- 5 -> [5, 1, 2]
- 1 -> [1]
- 2 -> [2]
- [3, 2, 4, 1, 1, 5, 1, 2]
- ^ ^ ^ ^ ^
- [3, 2, 4] -> 9
- [2, 4] -> 6
- [4, 1, 1, 5] -> 11
- [1] -> 1
- [1] -> 1
- [5, 1, 2] -> 8
- [1] -> 1
- [2] -> 2
- [9, 6, 11, 1, 1, 8, 1, 2]
- [1, 2, 3, 4, 5] -> [1, 5, 12, 9, 5]
- [3, 3, 3, 3, 3, 3, 3, 3] -> [9, 9, 9, 9, 9, 9, 6, 3]
- [5, 1, 2, 4, 1] -> [13, 1, 6, 5, 1]
- [1] -> [1]
- ṫJḣ"ḅ1
- ṫJḣ"ḅ1 Main link. Argument: A (array)
- J Index; yield the 1-based indices of A.
- ṫ Tail; map k to the postfix of A that begins with the k-th element.
- ḣ" Vectorized head; for each k in A, truncate the corr. postfix to length k.
- ḅ1 Convert the resulting slices from base 1 to integer.
- lambda X:[sum(X[i:i+k])for i,k in enumerate(X)]
- f l@(x:y)=sum(take x l):f y
- f x=x
- +/@{."_1].
- f =: +/@{."_1].
- f 3 2 4 1 1 5 1 2
- 9 6 11 1 1 8 1 2
- f 1 2 3 4 5
- 1 5 12 9 5
- +/@{."_1]. Input: A
- ]. Get each suffix of A from longest to shortest
- {."_1 For each value in A, take that many values from its corresponding suffix
- +/@ Sum that group of values taken from that suffix
- Return the sums
- d+
- $*
- M!&`b1(1)*(?<-1>,1+)*
- M%`1
- ¶
- ,
- Tr@Take[#,UpTo@#&@@#]&/@Drop[#,t-1]~Table~{t,Length@#}&
- f = %; f /@ {{1, 2, 3, 4, 5}, {3, 3, 3, 3, 3, 3, 3, 3}, {5, 1, 2, 4, 1}, {1}}
- (* {{1, 5, 12, 9, 5}, {9, 9, 9, 9, 9, 9, 6, 3}, {13, 1, 6, 5, 1}, {1}} *)
- [D¬£Oˆ¦Ž
- [ # infinite loop
- D # duplicate current list
- ¬ # get head of list
- £ # get that many elements from list
- O # sum
- ˆ # add to global array
- ¦ # remove first element of list
- Ž # break if stack is empty
- # implicitly push and print global array
- .es:Qk+b
- f(A)->put(1,1),L=lists,[L:sum(L:sublist(A,put(1,get(1)+1),X))||X<-A].
- FKo>i<s
- - o = 0
- F - for i in input:
- o - o+=1
- > - input[o:]
- i< - ^[:i]
- s - sum(^)
- Function e(g())
- Dim h()
- k=LBound(g)
- l=UBound(g)
- ReDim h(k To l)
- On Error Resume Next
- For i=k To l
- For j=i To i+g(i)-1
- h(i)=h(i)+g(j)
- Next
- Next
- e=h
- End Function
- ms<~tQ
- ms<~tQ
- ms<~tQdQ Implicit variable introduction
- Implicit: Q = eval(input())
- m Q Map d over the input, Q
- < Qd Take the first d elements of Q
- s Sum them
- ~tQ Afterwards, set Q to the tail of Q, removing the first element.
- let f(A:int[])=[for i in 0..A.Length-1->Seq.skip i A|>Seq.truncate A.[i]|>Seq.sum]
- 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
- int[]s(List<int>a)=>a.Select((n,i)=>a.GetRange(i,Math.Min(n,a.Count-i)).Sum()).ToArray();
- .v|h~l(A:Tc?;A?)b:0&~b.h~+A
- .v Input = Output = []
- | Or
- h~l A is a list, its length is the value of the first element of the Input
- (
- A:Tc? The concatenation of A with another list T results in the Input
- ; Or
- A? A = Input
- )
- b:0& Call recursively on Input minus the first element
- ~b. Output is the output of that call with an extra element at the beginning
- h~+A That extra element is the sum of the elements of A
- {+/¨⍵↑∘⌽¨⌽,⌽⍵}
- {⌽+/¨(-↑¨,)⌽⍵}
- <?foreach($a=$_GET[a]as$i=>$v)echo array_sum(array_slice($a,$i,$v)),"
- ";
- function f($a){foreach($a as$i=>$v)$r[]=array_sum(array_slice($a,$i,$v));return$r;}
- function p($a){foreach($c=$r=$a as$i=>$v)for($k=$i;$k--;)if(--$a[$k]>0)$r[$k]+=$v;return$r;}
- <?foreach($a=$r=$c=$_GET[a]as$i=>$v)for($k=$i;$k--;)if(--$c[$k]>0)$r[$k]+=$v;print_r($r);
- {sum each(til[count x],'x)sublist:x}
- q){sum each(til[count x],'x)sublist:x}3 2 4 1 1 5 1 2
- 9 6 11 1 1 8 1 2
- md{z:l-g:c;+c;q:c;};:1:l;
- m :1:l; #loop over entire input
- #set each value to...
- d{ } #the sum of...
- z:l-g:c:+c;q:c; #the input cropped to
- #the length of the value in the cell
- n=>_.From(n).Select((v,i)=>_.From(n).Slice(i,i+v).Sum()).ToArray()
- (defn f[[b & r]](concat[(apply + b(take(dec b)r))](if r(f r))))
- fGy+!-R0<G*!s
- f % Take input implicitly. Indices of nonzero elements: this gives [1 2 ... n]
- % where n is input size
- G % Push input again
- y % Push a copy of [1 2 ... n]
- + % Add. Gives [a+1 b+2...] where [a b...] is the input
- ! % Transpose into a column vector
- - % Subtraction with broadcast. Gives 2D array
- R % Keep upper triangular part, making the rest of entries 0
- 0< % True for negative entries. Each row corresponds to a substring sum.
- % For each row, this gives true for the entries of the input that make up
- % that substring sum. Each row is thus a mask to select entries of the input
- G % Push input again
- * % Multiply with broadcast. This multiplies the input times each row
- !s % Sum of each row. Implicitly display
- 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