From 9b061851145a61450373719a99f349d5a8cea19b Mon Sep 17 00:00:00 2001 From: Mariano Date: Tue, 30 Jul 2024 11:17:39 +0200 Subject: [PATCH] Wrote program for Day 5 --- .../pair_of_socks/Cargo.toml | 6 ++++++ .../pair_of_socks/src/lib.rs | 12 ++++++++++++ .../pair_of_socks/tests/examples_sockpairs.rs | 16 ++++++++++++++++ 3 files changed, 34 insertions(+) create mode 100644 Week-01/Day-05_Pair-Of-Socks/pair_of_socks/Cargo.toml create mode 100644 Week-01/Day-05_Pair-Of-Socks/pair_of_socks/src/lib.rs create mode 100644 Week-01/Day-05_Pair-Of-Socks/pair_of_socks/tests/examples_sockpairs.rs diff --git a/Week-01/Day-05_Pair-Of-Socks/pair_of_socks/Cargo.toml b/Week-01/Day-05_Pair-Of-Socks/pair_of_socks/Cargo.toml new file mode 100644 index 0000000..ea49d63 --- /dev/null +++ b/Week-01/Day-05_Pair-Of-Socks/pair_of_socks/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "pair_of_socks" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/Week-01/Day-05_Pair-Of-Socks/pair_of_socks/src/lib.rs b/Week-01/Day-05_Pair-Of-Socks/pair_of_socks/src/lib.rs new file mode 100644 index 0000000..20b0f7e --- /dev/null +++ b/Week-01/Day-05_Pair-Of-Socks/pair_of_socks/src/lib.rs @@ -0,0 +1,12 @@ +pub fn sock_pairs(socks: &str) -> usize { + let mut sock_pairs = 0; + let mut socks = String::from(socks); + + while !socks.is_empty() { + let full_length = socks.len(); + socks = socks.replace(socks.chars().next().unwrap(), ""); + sock_pairs += (full_length - socks.len()) / 2; + } + + sock_pairs +} diff --git a/Week-01/Day-05_Pair-Of-Socks/pair_of_socks/tests/examples_sockpairs.rs b/Week-01/Day-05_Pair-Of-Socks/pair_of_socks/tests/examples_sockpairs.rs new file mode 100644 index 0000000..94f9d36 --- /dev/null +++ b/Week-01/Day-05_Pair-Of-Socks/pair_of_socks/tests/examples_sockpairs.rs @@ -0,0 +1,16 @@ +use pair_of_socks::sock_pairs; + +#[test] +fn one_sock_pair() { + assert_eq!(sock_pairs("AA"), 1); +} + +#[test] +fn two_sock_pair() { + assert_eq!(sock_pairs("ABABC"), 2); +} + +#[test] +fn four_sock_pair() { + assert_eq!(sock_pairs("CABBACCC"), 4); +}