-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy path(6 kyu) Easy Balance Checking.rs
More file actions
33 lines (26 loc) · 1.03 KB
/
(6 kyu) Easy Balance Checking.rs
File metadata and controls
33 lines (26 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
use regex::Regex;
fn balance(book: &str) -> String {
let re = Regex::new(r"[^\w\d\s\.]").unwrap();
let book = re.replace_all(book, "");
let lines = book.split('\n')
.filter(|x| !x.is_empty())
.collect::<Vec<&str>>();
let mut balance = lines[0].parse::<f64>().unwrap();
let mut prices = vec![];
let mut result = String::from(
format!("Original Balance: {:.2}\n", balance));
for row in lines[1..].iter() {
let row = row.split(" ").collect::<Vec<&str>>();
let (n, name, price) = (row[0], row[1], row[2]);
let price = price.parse::<f64>().unwrap();
result.push_str(&format!("{} {} {:.2} Balance {:.2}\n",
n, name, price, balance - price));
prices.push(price);
balance -= price;
}
let s = prices.iter().fold(0f64, |a, b| a + b);
let n = prices.len() as f64;
result.push_str(&format!("Total expense {:.2}\n", s));
result.push_str(&format!("Average expense {:.2}", s / n));
result
}