怎么 反向 cumprod

假如我想要实现这样一个功能,请问应该怎么做呢?

[1,2,3,4] -> 反向 cumprod [1*2*3*4,2*3*4,3*4,4]

请先 登录 后评论

1 个回答

wale

可以先给x逆序排序,然后cumprod后再逆序排一下

x = [1,2,3,4]
x.sort(false).cumprod().sort(false)

如果x不是有序,可以用reverse函数反序,比如

x = [2,3,1,2] // [2*3*1*2, 3*1*2, 1*2, 2]
x.reverse().cumprod().reverse()
请先 登录 后评论