Basically, what I want to do is similar to what the following Python function is doing:
def myfunction(mylist):
initial = [0]*len(mylist)
initial[0] = 1
for i in range(1, len(mylist)):
initial[i] = mylist[i - 1] * initial[i - 1]
I tried doing something like the code given below, however, it has a complexity more than linear time plus it doesn’t work properly as well since it seems to be using the i
value rather than the i-1
value of initial
.
def myfunction(mylist) do
initial = List.duplicate(0, len(mylist))
initial = List.update_at(initial, 0, &(&1 + 1))
1..len(mylist)-1
|> Enum.map(fn(i) ->
initial = List.update_at(initial, i, &(Enum.at(mylist, i-1) * &1))
initial
end)
end
Any help would be appreciated over here. Thank you!