How to resolve the algorithm Averages/Simple moving average step by step in the Rust programming language
How to resolve the algorithm Averages/Simple moving average step by step in the Rust programming language
Table of Contents
Problem Statement
Computing the simple moving average of a series of numbers. Create a stateful function/class/instance that takes a period and returns a routine that takes a number as argument and returns a simple moving average of its arguments so far. A simple moving average is a method for computing an average of a stream of numbers by only averaging the last P numbers from the stream, where P is known as the period. It can be implemented by calling an initialing routine with P as its argument, I(P), which should then return a routine that when called with individual, successive members of a stream of numbers, computes the mean of (up to), the last P of them, lets call this SMA(). The word stateful in the task description refers to the need for SMA() to remember certain information between calls to it:
Stateful also means that successive calls to I(), the initializer, should return separate routines that do not share saved state so they could be used on two independent streams of data. Pseudo-code for an implementation of SMA is:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Averages/Simple moving average step by step in the Rust programming language
Source code in the rust programming language
struct SimpleMovingAverage {
period: usize,
numbers: Vec<usize>
}
impl SimpleMovingAverage {
fn new(p: usize) -> SimpleMovingAverage {
SimpleMovingAverage {
period: p,
numbers: Vec::new()
}
}
fn add_number(&mut self, number: usize) -> f64 {
self.numbers.push(number);
if self.numbers.len() > self.period {
self.numbers.remove(0);
}
if self.numbers.is_empty() {
return 0f64;
}else {
let sum = self.numbers.iter().fold(0, |acc, x| acc+x);
return sum as f64 / self.numbers.len() as f64;
}
}
}
fn main() {
for period in [3, 5].iter() {
println!("Moving average with period {}", period);
let mut sma = SimpleMovingAverage::new(*period);
for i in [1, 2, 3, 4, 5, 5, 4, 3, 2, 1].iter() {
println!("Number: {} | Average: {}", i, sma.add_number(*i));
}
}
}
use std::collections::VecDeque;
struct SimpleMovingAverage {
period: usize,
numbers: VecDeque<usize>
}
impl SimpleMovingAverage {
fn new(p: usize) -> SimpleMovingAverage {
SimpleMovingAverage {
period: p,
numbers: VecDeque::new()
}
}
fn add_number(&mut self, number: usize) -> f64 {
self.numbers.push_back(number);
if self.numbers.len() > self.period {
self.numbers.pop_front();
}
if self.numbers.is_empty() {
return 0f64;
}else {
let sum = self.numbers.iter().fold(0, |acc, x| acc+x);
return sum as f64 / self.numbers.len() as f64;
}
}
}
fn main() {
for period in [3, 5].iter() {
println!("Moving average with period {}", period);
let mut sma = SimpleMovingAverage::new(*period);
for i in [1, 2, 3, 4, 5, 5, 4, 3, 2, 1].iter() {
println!("Number: {} | Average: {}", i, sma.add_number(*i));
}
}
}
You may also check:How to resolve the algorithm Tokenize a string step by step in the Wren programming language
You may also check:How to resolve the algorithm Rename a file step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Introspection step by step in the Inform 7 programming language
You may also check:How to resolve the algorithm Sorting algorithms/Stooge sort step by step in the R programming language
You may also check:How to resolve the algorithm N'th step by step in the BCPL programming language