I just saw your post way after the one from Pedrosky.
Its a bit easier to understand. Still try to get for each ma the numbers above each one to insert equations.
Its a bit easier to understand. Still try to get for each ma the numbers above each one to insert equations.
(Jun-25-2024, 02:00 PM)snippsat Wrote:(Jun-25-2024, 06:10 AM)Pedroski55 Wrote:Not the best way to move backward in Python there are own method for this.range(len(nums) - 1, -1, -1)]
>>> Quodown = [9, 7, 6, 9, 7, 6, 4, 3, 0] >>> reversed(Quodown) <list_reverseiterator object at 0x000001D98D2BC130> >>> list(reversed(Quodown)) [0, 3, 4, 6, 7, 9, 6, 7, 9] >>> # The more famous one in Python >>> Quodown[::-1] [0, 3, 4, 6, 7, 9, 6, 7, 9]So something like this.
Quodown = [9, 7, 6, 9, 7, 6, 4, 3, 0] # Calculate cumulative sums from end to start cumulative_sums = [] sumi = 0 for value in reversed(Quodown): sumi += value cumulative_sums.append(sumi) # Calculate moving averages based on cumulative sums moving_averages = [value / (index + 1) for index, value in enumerate(cumulative_sums)] for index, value in enumerate(cumulative_sums, start=1): print(f"Cumulative sum of the first {index} elements: {value}") # Print moving averages results print('-' * 40) for index, value in enumerate(moving_averages, start=1): print(f"Moving average of the first {index} elements: {value:.3f}")
Output:Cumulative sum of the first 1 elements: 0 Cumulative sum of the first 2 elements: 3 Cumulative sum of the first 3 elements: 7 Cumulative sum of the first 4 elements: 13 Cumulative sum of the first 5 elements: 20 Cumulative sum of the first 6 elements: 29 Cumulative sum of the first 7 elements: 35 Cumulative sum of the first 8 elements: 42 Cumulative sum of the first 9 elements: 51 ---------------------------------------- Moving average of the first 1 elements: 0.000 Moving average of the first 2 elements: 1.500 Moving average of the first 3 elements: 2.333 Moving average of the first 4 elements: 3.250 Moving average of the first 5 elements: 4.000 Moving average of the first 6 elements: 4.833 Moving average of the first 7 elements: 5.000 Moving average of the first 8 elements: 5.250 Moving average of the first 9 elements: 5.667