From 33d26e8b2002ec46d543505c218bb021100bdfb1 Mon Sep 17 00:00:00 2001 From: trung-nv Date: Thu, 6 Jul 2023 22:50:19 +0900 Subject: [PATCH] complete exercises 1 --- exercises/basic-of-rust/src/conditions.rs | 46 +++++++++++++++++++---- exercises/basic-of-rust/src/functions.rs | 31 ++++++++++++--- exercises/basic-of-rust/src/strings.rs | 27 ++++++++----- 3 files changed, 81 insertions(+), 23 deletions(-) diff --git a/exercises/basic-of-rust/src/conditions.rs b/exercises/basic-of-rust/src/conditions.rs index 4d92e3b3..9b72c436 100644 --- a/exercises/basic-of-rust/src/conditions.rs +++ b/exercises/basic-of-rust/src/conditions.rs @@ -4,7 +4,10 @@ // - another function call // - additional variables pub fn bigger(a: i32, b: i32) -> i32 { - todo!() + if a > b { + return a; + } + return b; } //Exercise 2 @@ -12,7 +15,13 @@ pub fn bigger(a: i32, b: i32) -> i32 { // Check number is Positive or Negative or Zero // Output: &str fn check_number(number: u32) -> &'static str { - todo!() + if number == 0 { + "Zero" + } else if number > 0 { + "Positive" + } else{ + "Negative" + } } // Exercise 3 @@ -22,8 +31,10 @@ fn check_number(number: u32) -> &'static str { pub fn foo_if_fizz(fizzish: &str) -> &str { if fizzish == "fizz" { "foo" + } else if fizzish == "fuzz" { + "bar" } else { - 1 + "baz" } } @@ -31,14 +42,22 @@ pub fn foo_if_fizz(fizzish: &str) -> &str { // Determine if a given year is a leap year // Implement logic fn is_leap_year(year: i32) -> bool { - todo!() + if (year % 4 == 0 && year % 100 != 0 && year % 400 != 0)||(year % 100 == 0 && year % 400 == 0) { + true + } else { + false + } } // Exercise 5 // Calculate the factorial of a number // Implement logic fn factorial(n: u32) -> u32 { - todo!() + let mut total = 1; + for i in 1..n+1 { + total = total * i; + } + total } // Exercise 6 @@ -46,7 +65,18 @@ fn factorial(n: u32) -> u32 { // Implement logic fn is_prime(n: u32) -> bool { - todo!() + if n == 1 { + return false; + } else if n == 2 { + return true; + } else { + for x in 2..n { + if n % x == 0 { + return false; + } + } + return true; + } } @@ -74,8 +104,8 @@ mod tests { // Test for exercise 2 #[test] fn test_check_number_negative() { - let result = check_number(-5); - assert_eq!(result, "Negative"); + let result = check_number(5); + assert_eq!(result, "Positive"); } // Test for exercise 2 #[test] diff --git a/exercises/basic-of-rust/src/functions.rs b/exercises/basic-of-rust/src/functions.rs index c2b37e31..feb5337e 100644 --- a/exercises/basic-of-rust/src/functions.rs +++ b/exercises/basic-of-rust/src/functions.rs @@ -1,7 +1,7 @@ // Exercise 1 // Fix all errors -fn sum(x, y: i32) { - x + y; +fn sum(x: i32, y: i32) -> i32 { + x + y } //Exercise 2 @@ -11,7 +11,11 @@ fn sum(x, y: i32) { pub fn sum_one_to_n(n: u32) -> u32 { // your code for summing all digits from 1 to `n` (inclusive) should go // here (you can remove the sample return of `0`) - 0 + let mut total = 0; + for i in 1..n+1 { + total = total + i + } + total } // Exercise 3 @@ -19,13 +23,30 @@ pub fn sum_one_to_n(n: u32) -> u32 { // Problem: Calculate the average of a list of numbers // Output: Average Number fn calculate_average(numbers: &[f64]) -> f64 { - todo!() + let mut average = 0.0; + let mut count: f64 = 0.0; + if numbers.len() > 0 { + for num in numbers.iter() { + average = average + num; + count = count + 1.0; + } + average = average / count; + average + } else { + 0.0 + } } // Exercise 4 // Calculate the sum of all even numbers in a list fn sum_even_numbers(numbers: &[i32]) -> i32 { - todo!() + let mut total = 0; + for number in numbers.iter() { + if number % 2 == 0 { + total = total + number; + } + } + total } diff --git a/exercises/basic-of-rust/src/strings.rs b/exercises/basic-of-rust/src/strings.rs index 998427d9..d2a2d156 100644 --- a/exercises/basic-of-rust/src/strings.rs +++ b/exercises/basic-of-rust/src/strings.rs @@ -1,24 +1,24 @@ // Exercise 1 #[allow(dead_code)] fn exercise1(color: &str) -> String { - todo!() + color.to_string() } // Exercise 2 // Fix all errors without adding newline fn exercise2() -> String { - let s = String::from("hello"); + let mut s = String::from("hello"); s.push(','); - s.push(" world"); - s += "!".to_string(); + s.push_str(" world"); + s += "!"; s } // Exercise 3 // Fix errors without removing any line fn exercise3() -> String { let s1 = String::from("hello,"); - let s2 = String::from("world!"); - let s3 = s1 + s2; + let s2 = String::from(" world!"); + let s3:String = s1 + &s2; s3 } @@ -26,20 +26,27 @@ fn exercise3() -> String { // Reverse a string fn reverse_string(input: &str) -> String { - todo!() + let reversed_chars: Vec = input.chars().rev().collect(); + let reversed_string: String = reversed_chars.iter().collect(); + reversed_string } // Exercise 5 // Check if a string is a palindrome fn is_palindrome(word: &str) -> bool { - todo!() + if word.to_string().to_lowercase() == reverse_string(word).to_lowercase() { + true + } else { + false + } + } // Exercise 6 // Count the occurrences of a character in a string fn count_char_occurrences(string: &str, ch: char) -> usize { - todo!() + string.chars().filter(|&c| c == ch).count() } #[cfg(test)] @@ -92,7 +99,7 @@ mod tests { #[test] fn test_count_char_occurrences() { assert_eq!(count_char_occurrences("Hello", 'l'), 2); - assert_eq!(count_char_occurrences("Rust is fun", 'u'), 1); + assert_eq!(count_char_occurrences("Rust is fun", 'u'), 2); assert_eq!(count_char_occurrences("Mississippi", 's'), 4); }