27
\$\begingroup\$

I have a simple challenge for you this time. Given an array of positive integers A (or the equivalent in your language), replace each entry Ai with the sum of the next Ai elements of A, cycling back from the beginning if there are not enough items.

As usual, you can compete in any programming language and can take input and provide output through any standard method and in any reasonable format, while taking note that these loopholes are forbidden by default. You may optionally take the size of A as input too. This is , so the shortest submission (in bytes) for every language wins.

Examples / Test Cases

Given [1,3,4,5], your code should output [3,10,13,14], because 1 is replaced by 3, 3 is replaced by 4+5+1=10 (notice how it wrapped back from the start), 4 by 5+1+3+4=13 and 5 by 1+3+4+5+1=14.

Given [3,2,1,9], your program should produce [12,10,9,33], because we substitute 3 with 2+1+9=12, 2 with 1+9=10, 1 with 9 and 9 with 3+2+1+9+3+2+1+9+3=33 (notice how we wrapped back from the start more than once).

Some more test cases for you to choose from:

[4,3,2,1]                       -> [10,7,5,4]
[3,2,1,9]                       -> [12,10,9,33]
[1,3,4,5]                       -> [3,10,13,14]
[4,4,3,2,2]                     -> [11,11,8,6,8]
[3,5,3,2,1]                     -> [10,14,6,4,3]
[3,2,4,3,2,1,1]                 -> [9,7,7,4,2,1,3]
[7,8,6,5,4,3,2,1,5]             -> [29,33,20,15,11,8,6,5,30]
[28,2,4,2,3,2,3,4,5,3]          -> [137,6,10,5,9,7,12,38,39,34]
[1,2,3,4,5,4,3,2,1,2,3,4,3,2,1] -> [2,7,13,14,12,8,5,3,2,7,9,7,4,2,1]
\$\endgroup\$

35 Answers 35

1
2
0
\$\begingroup\$

Cjam, 23 bytes

_ee{~,\)f+1$f=:+\}/;]S*

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Factor + math.unicode sequences.repeating, 51 bytes

[ dup [ 1 + rotate swap cycle Σ ] with map-index ]

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Arturo, 55 bytes

$[a][i:1map a'x[sum map i..i+x-1'y[a\[y%size a]]i:i+1]]

Try it

$[a][                  ; a function taking an argument a
  i: 1                 ; assign 1 to i
  map a' x[            ; map over a and assign current element to x
    sum                ; sum
    map i..i+x-1 'y [  ; map over i to i+x-1, the indices in a that form our desired sum; assign current elt to y
      a\[y % size a]   ; index a at y modulo the length of a
    ]                  ; end map
    i: i + 1           ; increment i
  ]                    ; end map
]                      ; end function
 
\$\endgroup\$
0
\$\begingroup\$

Thunno, \$ 15\log_{256}(96)\approx \$ 12.35 bytes

AEeAuR1++z0sAIS

Attempt This Online!

\$\endgroup\$
0
\$\begingroup\$

Vyxal, 44 bitsv2, 5.5 bytes

ż~+rİṠ

Try it Online!

What is this goofy 10 byte nonsense I see?

Explained

ż~+rİṠ­⁡​‎‎⁡⁠⁡‏‏​⁡⁠⁡‌⁢​‎‎⁡⁠⁢‏⁠‎⁡⁠⁣‏‏​⁡⁠⁡‌⁣​‎‎⁡⁠⁤‏‏​⁡⁠⁡‌⁤​‎‎⁡⁠⁢⁡‏‏​⁡⁠⁡‌⁢⁡​‎‎⁡⁠⁢⁢‏‏​⁡⁠⁡‌­
ż       # ‎⁡Push the range [1, len(input)]
 ~+     # ‎⁢And add it to the input without popping
   r    # ‎⁣Vectorise range with the input. This essentially generates a list of indices of the next n items after each item in the input. 
    İ   # ‎⁤Index that list of indices into the input. 
     Ṡ  # ‎⁢⁡And sum each list. 
💎

Created with the help of Luminespire.

\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.