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

Update #24

Open
wants to merge 3 commits into
base: main
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
15 changes: 15 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[package]
name = "Rust-Bootcamp-2023"
version = "0.1.0"
edition = "2021"
authors = ["CocDap"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[workspace]
members = [
"./exercises/basic-of-rust",
"./exercises/ownership-borrowing"
]

[[test]]
name = "conditions"
path = "./exercises/basic-of-rust/src/conditions.rs"

[[test]]
name = "strings"
path = "./exercises/basic-of-rust/src/strings.rs"

[[test]]
name = "functions"
path = "./exercises/basic-of-rust/src/functions.rs"


[dependencies]
8 changes: 8 additions & 0 deletions exercises/basic-of-rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "basic-of-rust"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
23 changes: 23 additions & 0 deletions exercises/basic-of-rust/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
## Complete Basic of Rust exercises
### Conditions
+ Complete `Conditions` (If Else) Exercises in `exercises/basic-of-rust/src/conditions.rs`
+ Run tests to check your implementation

```
cargo test --test conditions
```

### String
+ Complete `String` Exercises in `exercises/basic-of-rust/src/strings.rs`
+ Run tests to check your implementation
```
cargo test --test strings
```

### Functions
+ Complete `Functions` (fn) Exercises in `exercises/basic-of-rust/src/functions.rs`
+ Run tests to check your implementation

```
cargo test --test functions
```
148 changes: 148 additions & 0 deletions exercises/basic-of-rust/src/conditions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
//Exercise 1
// Complete this function to return the bigger number!
// Do not use:
// - another function call
// - additional variables
pub fn bigger(a: i32, b: i32) -> i32 {
todo!()
}

//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!()
}

// Exercise 3
// Step 1: Make me compile!
// Step 2: Get the bar_for_fuzz and default_to_baz tests passing!

pub fn foo_if_fizz(fizzish: &str) -> &str {
if fizzish == "fizz" {
"foo"
} else {
1
}
}

// Exercise 4
// Determine if a given year is a leap year
// Implement logic
fn is_leap_year(year: i32) -> bool {
todo!()
}

// Exercise 5
// Calculate the factorial of a number
// Implement logic
fn factorial(n: u32) -> u32 {
todo!()
}

// Exercise 6
// Check if a number is prime
// Implement logic

fn is_prime(n: u32) -> bool {
todo!()
}


// Don't mind this for now :)
#[cfg(test)]
mod tests {
use super::*;

// Test for exercise 1
#[test]
fn ten_is_bigger_than_eight() {
assert_eq!(10, bigger(10, 8));
}
// Test for exercise 1
#[test]
fn fortytwo_is_bigger_than_thirtytwo() {
assert_eq!(42, bigger(32, 42));
}
// Test for exercise 2
#[test]
fn test_check_number_positive() {
let result = check_number(10);
assert_eq!(result, "Positive");
}
// Test for exercise 2
#[test]
fn test_check_number_negative() {
let result = check_number(-5);
assert_eq!(result, "Negative");
}
// Test for exercise 2
#[test]
fn test_check_number_zero() {
let result = check_number(0);
assert_eq!(result, "Zero");
}

// Test for exercise 3
#[test]
fn foo_for_fizz() {
assert_eq!(foo_if_fizz("fizz"), "foo")
}

// Test for exercise 3
#[test]
fn bar_for_fuzz() {
assert_eq!(foo_if_fizz("fuzz"), "bar")
}

// Test for exercise 3
#[test]
fn default_to_baz() {
assert_eq!(foo_if_fizz("literally anything"), "baz")
}

// Test for exercise 4
#[test]
fn test_leap_year() {
assert_eq!(is_leap_year(2020), true);
assert_eq!(is_leap_year(2000), true);
assert_eq!(is_leap_year(1600), true);
}

// Test for exercise 4
#[test]
fn test_non_leap_year() {
assert_eq!(is_leap_year(2021), false);
assert_eq!(is_leap_year(1900), false);
assert_eq!(is_leap_year(1800), false);
}

// Test for exercise 5
#[test]
fn test_factorial() {
assert_eq!(factorial(0), 1);
assert_eq!(factorial(1), 1);
assert_eq!(factorial(5), 120);
assert_eq!(factorial(10), 3628800);
}

// Test for exercise 6
#[test]
fn test_prime_number() {
assert_eq!(is_prime(2), true);
assert_eq!(is_prime(7), true);
assert_eq!(is_prime(13), true);
assert_eq!(is_prime(19), true);
}
// Test for exercise 6
#[test]
fn test_non_prime_number() {
assert_eq!(is_prime(1), false);
assert_eq!(is_prime(4), false);
assert_eq!(is_prime(10), false);
assert_eq!(is_prime(15), false);
}


}
97 changes: 97 additions & 0 deletions exercises/basic-of-rust/src/functions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Exercise 1
// Fix all errors
fn sum(x, y: i32) {
x + y;
}

//Exercise 2
// Input: Provide an arbitrary value of n
// Implement sum function: 1+2+3+..n
// Output: Calculate sum 1 to n
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
}

// 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!()
}

// Exercise 4
// Calculate the sum of all even numbers in a list
fn sum_even_numbers(numbers: &[i32]) -> i32 {
todo!()
}


#[cfg(test)]
mod tests {
use super::*;

// Test for exercise 1
#[test]
fn sum_should_work() {
let (x, y) = (1, 2);
let s = sum(x, y);

assert_eq!(s, 3);
}

// Test for exercise 2
#[test]
fn test_sum_0() {
let result = sum_one_to_n(0);

assert_eq!(result, 0);
}

// Test for exercise 2
#[test]
fn test_sum_1() {
let result = sum_one_to_n(1);

assert_eq!(result, 1);
}

// Test for exercise 2
#[test]
fn test_sum_100() {
let result = sum_one_to_n(100);

assert_eq!(result, 5050);
}

// Test for exercise 3
#[test]
fn test_calculate_average() {
// Test case 1: Non-empty slice
let numbers = [2.5, 4.8, 6.3, 1.7, 3.9];
let result = calculate_average(&numbers);
assert_eq!(result, 3.84);

}

// Test for exercise 3
#[test]
fn test_calculate_average_empty() {
// Test case 1: Non-empty slice
let numbers = [];
let result = calculate_average(&numbers);
assert_eq!(result, 0.0);

}

// Test for exercise 4
#[test]
fn test_sum_even_numbers() {
assert_eq!(sum_even_numbers(&[1, 2, 3, 4, 5, 6]), 12);
assert_eq!(sum_even_numbers(&[10, 20, 30, 40, 50]), 150);
assert_eq!(sum_even_numbers(&[15, 25, 35, 45, 55]), 0);
assert_eq!(sum_even_numbers(&[-2, 0, 2, 4, 6]), 10);
}
}
4 changes: 4 additions & 0 deletions exercises/basic-of-rust/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
mod conditions;
mod strings;
mod functions;

Loading