Background
-rot transform (read as "minus-rot transform") is a sequence transformation I just invented. This transform is done by viewing the sequence as a stack in Forth or Factor (first term on the top) and repeatedly applying -rot +
. -rot
is a command that moves the top of the stack to two places down, and +
adds two numbers on the top of the stack.
For example, applying -rot transform to the sequence [1, 2, 3, 4, 5, 6, 7, 8, 9]
looks like this (the right side is the top of the stack in this example, to match Forth/Factor conventions):
initial: 9 8 7 6 5 4 3 2 1 <--
-rot : 9 8 7 6 5 4 1 3 2
+ : 9 8 7 6 5 4 1 5 <--
-rot : 9 8 7 6 5 5 4 1
+ : 9 8 7 6 5 5 5 <--
-rot : 9 8 7 6 5 5 5
+ : 9 8 7 6 5 10 <--
-rot : 9 8 7 10 6 5
+ : 9 8 7 10 11 <--
-rot : 9 8 11 7 10
+ : 9 8 11 17 <--
-rot : 9 17 8 11
+ : 9 17 19 <--
-rot : 19 9 17
+ : 19 26 <--
-rot
does not work when there are only two items on the stack, so the operation stops there. Then the top of the stack at the initial state and after each +
command are collected to form the resulting sequence of [1, 5, 5, 10, 11, 17, 19, 26]
. Note that it is one term shorter than the input sequence.
Task
Given a finite sequence of integers (of length ≥ 3), compute its -rot transform.
Standard code-golf rules apply. The shortest code in bytes wins.
Test cases
[1, 2, 3] -> [1, 5]
[1, -1, 1, -1, 1, -1, 1, -1] -> [1, 0, 0, 1, -1, 2, -2]
[1, 2, 3, 4, 5, 6, 7, 8, 9] -> [1, 5, 5, 10, 11, 17, 19, 26]
[1, 2, 4, 8, 16, 32, 64, 128] -> [1, 6, 9, 22, 41, 86, 169]
[1, 0, -1, 0, 1, 0, -1, 0, 1] -> [1, -1, 1, 0, 1, -1, 1, 0]