From 93d62f056f700a73b8afb1654da1901932ce491c Mon Sep 17 00:00:00 2001 From: AlphonseMehounme Date: Sat, 30 Nov 2024 19:56:29 +0100 Subject: [PATCH] add if-else, countdown and sum of evens --- Rust/rustifinity/challenges/countdown.rs | 15 +++++++++++++++ Rust/rustifinity/challenges/if-else.rs | 8 ++++++++ .../rustifinity/challenges/sum-of-even-numbers.rs | 9 +++++++++ 3 files changed, 32 insertions(+) create mode 100644 Rust/rustifinity/challenges/countdown.rs create mode 100644 Rust/rustifinity/challenges/if-else.rs create mode 100644 Rust/rustifinity/challenges/sum-of-even-numbers.rs diff --git a/Rust/rustifinity/challenges/countdown.rs b/Rust/rustifinity/challenges/countdown.rs new file mode 100644 index 0000000..c144711 --- /dev/null +++ b/Rust/rustifinity/challenges/countdown.rs @@ -0,0 +1,15 @@ +pub fn countdown(n: u32) -> Vec { + // TODO: Implement the countdown using a while loop +if n == 0 { + vec![0] + } else { + let mut vect : Vec = Vec::new(); + let mut m = n; + while m > 0 { + vect.push(m); + m -= 1; + } + vect.push(0); + vect + } +} diff --git a/Rust/rustifinity/challenges/if-else.rs b/Rust/rustifinity/challenges/if-else.rs new file mode 100644 index 0000000..7e71674 --- /dev/null +++ b/Rust/rustifinity/challenges/if-else.rs @@ -0,0 +1,8 @@ +pub fn is_even(n: i32) -> bool { + // Your code here... + if n % 2 == 0 { + true + } else { + false + } +} diff --git a/Rust/rustifinity/challenges/sum-of-even-numbers.rs b/Rust/rustifinity/challenges/sum-of-even-numbers.rs new file mode 100644 index 0000000..5588d75 --- /dev/null +++ b/Rust/rustifinity/challenges/sum-of-even-numbers.rs @@ -0,0 +1,9 @@ +pub fn sum_of_evens(start: i32, end: i32) -> i32 { + let mut sum = 0; + for num in start..=end + 1 { + if num % 2 == 0 { + sum += num; + } + } + sum +}