add fraction maths (issue #293)

main
William Perron 2 years ago
parent bf9e06cc32
commit 02423a5d92

21
Cargo.lock generated

@ -13,18 +13,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]] [[package]]
name = "dice-stats" name = "empty-zeros"
version = "0.1.0" version = "0.1.0"
dependencies = [
"itertools",
"rand",
]
[[package]]
name = "either"
version = "1.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7fcaabb2fef8c910e7f4c7ce9f67a1283a1715879a7c230ca9d6d1ae31f16d91"
[[package]] [[package]]
name = "fraction-math" name = "fraction-math"
@ -41,15 +31,6 @@ dependencies = [
"wasi", "wasi",
] ]
[[package]]
name = "itertools"
version = "0.10.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473"
dependencies = [
"either",
]
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.139" version = "0.2.139"

@ -1,6 +1,7 @@
[workspace] [workspace]
members = [ members = [
"balance", "balance",
"empty-zeros",
"fraction-math", "fraction-math",
"parens", "parens",
"repeated-groups", "repeated-groups",

@ -0,0 +1,8 @@
[package]
name = "empty-zeros"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

@ -0,0 +1,77 @@
/// Given a non-empty array containing only non-negative integers, return the
/// list with trailing and leading zeroes removed.
///
/// Example:
///
/// ```
/// > removeZeroes([0, 0, 0, 3, 1, 4, 1, 5, 9, 0, 0, 0, 0])
/// > [3, 1, 4, 1, 5, 9]
///
/// > removeZeroes([0, 0, 0])
/// > []
///
/// > removeZeroes([8])
/// > [8]
/// ```
fn main() {
let mut v = vec![0, 0, 0, 3, 1, 4, 1, 5, 9, 0, 0, 0, 0];
println!("{:?}", remove_zeros(&mut v));
println!("{:?}", v);
}
fn remove_zeros(v: &mut Vec<usize>) {
if v.len() == 0 {
return;
}
let mut i = 0;
while i <= v.len() && v[0..i].iter().sum::<usize>() == 0 {
i += 1;
}
v.drain(..i - 1);
if v.len() == 0 {
return;
}
let mut j = v.len() - 1;
while v[j..].iter().sum::<usize>() == 0 {
j -= 1;
}
v.drain(j + 1..);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_remove_zeros() {
let mut v = vec![0, 0, 0, 3, 1, 4, 1, 5, 9, 0, 0, 0, 0];
remove_zeros(&mut v);
assert_eq!(vec![3, 1, 4, 1, 5, 9], v,);
let mut v = vec![0, 0, 0];
remove_zeros(&mut v);
assert_eq!(Vec::<usize>::new(), v);
let mut v = vec![8];
remove_zeros(&mut v);
assert_eq!(vec![8], v);
let mut v = vec![];
remove_zeros(&mut v);
assert_eq!(Vec::<usize>::new(), v);
let mut v = vec![0];
remove_zeros(&mut v);
assert_eq!(Vec::<usize>::new(), v);
let mut v = vec![1, 0, 0, 0];
remove_zeros(&mut v);
assert_eq!(vec![1], v);
}
}
Loading…
Cancel
Save