From 4b0a6a01b051a4ebfbc17661d14cb23fe4f275fb Mon Sep 17 00:00:00 2001 From: Orangerot Date: Thu, 27 Jun 2024 11:30:16 +0200 Subject: Initial commit --- counting-bits/Cargo.toml | 8 ++++++++ counting-bits/src/main.rs | 23 +++++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 counting-bits/Cargo.toml create mode 100644 counting-bits/src/main.rs (limited to 'counting-bits') diff --git a/counting-bits/Cargo.toml b/counting-bits/Cargo.toml new file mode 100644 index 0000000..6354b0d --- /dev/null +++ b/counting-bits/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "counting-bits" +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/counting-bits/src/main.rs b/counting-bits/src/main.rs new file mode 100644 index 0000000..1203594 --- /dev/null +++ b/counting-bits/src/main.rs @@ -0,0 +1,23 @@ +fn main() { + let tests = [ + (2, vec![0,1,1]), + (5, vec![0,1,1,2,1,2]) + ]; + + for test in tests { + println!("{} is {:?} should be {:?}", test.0, count_bits(test.0), test.1); + } +} + +fn count_bits(n: i32) -> Vec { + let mut solution: Vec = Vec::new(); + for i in 0..=n { + let mut ones = 0; + for c in 0..32 { + ones += (1<