Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

complete exercises 1 #13

Open
wants to merge 1 commit into
base: class-1-exercises
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 38 additions & 8 deletions exercises/basic-of-rust/src/conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,24 @@
// - another function call
// - additional variables
pub fn bigger(a: i32, b: i32) -> i32 {
todo!()
if a > b {
return a;
}
return b;
}

//Exercise 2
// Input: Provide an arbitrary value of number
// 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
Expand All @@ -22,31 +31,52 @@ 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"
}
}

// Exercise 4
// 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
// Check if a number is prime
// 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;
}
}


Expand Down Expand Up @@ -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]
Expand Down
31 changes: 26 additions & 5 deletions exercises/basic-of-rust/src/functions.rs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -11,21 +11,42 @@ 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
// Input: list of arbitrary numbers
// 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
}


Expand Down
27 changes: 17 additions & 10 deletions exercises/basic-of-rust/src/strings.rs
Original file line number Diff line number Diff line change
@@ -1,45 +1,52 @@
// 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
}

// Exercise 4
// Reverse a string

fn reverse_string(input: &str) -> String {
todo!()
let reversed_chars: Vec<char> = 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)]
Expand Down Expand Up @@ -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);
}

Expand Down