From 02423a5d92d865e018f632a9dac4caa12073a091 Mon Sep 17 00:00:00 2001 From: William Perron Date: Wed, 3 May 2023 15:47:00 -0400 Subject: [PATCH] add fraction maths (issue #293) --- Cargo.lock | 21 +---------- Cargo.toml | 1 + empty-zeros/Cargo.toml | 8 +++++ empty-zeros/src/main.rs | 77 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 87 insertions(+), 20 deletions(-) create mode 100644 empty-zeros/Cargo.toml create mode 100644 empty-zeros/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index 55ffcc1..2bbdcb1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -13,18 +13,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] -name = "dice-stats" +name = "empty-zeros" 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]] name = "fraction-math" @@ -41,15 +31,6 @@ dependencies = [ "wasi", ] -[[package]] -name = "itertools" -version = "0.10.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473" -dependencies = [ - "either", -] - [[package]] name = "libc" version = "0.2.139" diff --git a/Cargo.toml b/Cargo.toml index ea46155..32c1b4c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,7 @@ [workspace] members = [ "balance", + "empty-zeros", "fraction-math", "parens", "repeated-groups", diff --git a/empty-zeros/Cargo.toml b/empty-zeros/Cargo.toml new file mode 100644 index 0000000..7fe336c --- /dev/null +++ b/empty-zeros/Cargo.toml @@ -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] diff --git a/empty-zeros/src/main.rs b/empty-zeros/src/main.rs new file mode 100644 index 0000000..2d62608 --- /dev/null +++ b/empty-zeros/src/main.rs @@ -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) { + if v.len() == 0 { + return; + } + + let mut i = 0; + while i <= v.len() && v[0..i].iter().sum::() == 0 { + i += 1; + } + + v.drain(..i - 1); + + if v.len() == 0 { + return; + } + + let mut j = v.len() - 1; + while v[j..].iter().sum::() == 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::::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::::new(), v); + + let mut v = vec![0]; + remove_zeros(&mut v); + assert_eq!(Vec::::new(), v); + + let mut v = vec![1, 0, 0, 0]; + remove_zeros(&mut v); + assert_eq!(vec![1], v); + } +}