diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..c41cc9e3 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 00000000..b8b6dd61 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,15 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "Rust-Bootcamp-2023" +version = "0.1.0" + +[[package]] +name = "basic-of-rust" +version = "0.1.0" + +[[package]] +name = "ownership-borrowing" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 00000000..4e9233ea --- /dev/null +++ b/Cargo.toml @@ -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] \ No newline at end of file diff --git a/exercises/basic-of-rust/Cargo.toml b/exercises/basic-of-rust/Cargo.toml new file mode 100644 index 00000000..e954d0a0 --- /dev/null +++ b/exercises/basic-of-rust/Cargo.toml @@ -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] diff --git a/exercises/basic-of-rust/README.md b/exercises/basic-of-rust/README.md new file mode 100644 index 00000000..34655b98 --- /dev/null +++ b/exercises/basic-of-rust/README.md @@ -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 +``` \ No newline at end of file diff --git a/exercises/basic-of-rust/src/conditions.rs b/exercises/basic-of-rust/src/conditions.rs new file mode 100644 index 00000000..8515151d --- /dev/null +++ b/exercises/basic-of-rust/src/conditions.rs @@ -0,0 +1,175 @@ +//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 { + 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: i32) -> &'static str { + if number>0 { + return "Positive"; + }else if number<0 { + return "Negative"; + } + return "Zero" +} + +// 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 if fizzish == "fuzz" { + "bar" + }else{ + "baz" + } +} + +// Exercise 4 +// Determine if a given year is a leap year +// Implement logic +fn is_leap_year(year: i32) -> bool { + if year%4==0 && year%100!=0 { + return true; + } + if year%400==0{ + return true; + } + return false; +} + +// Exercise 5 +// Calculate the factorial of a number +// Implement logic +fn factorial(n: u32) -> u32 { + if n==1 || n==0{ + return 1; + } + return n*factorial(n-1); +} + +// Exercise 6 +// Check if a number is prime +// Implement logic + +fn is_prime(n: u32) -> bool { + if n<2 { + return false; + } + for i in 2..n{ + if n%i==0{ + return false; + } + } + return true; +} + + +// 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); + } + + +} \ No newline at end of file diff --git a/exercises/basic-of-rust/src/functions.rs b/exercises/basic-of-rust/src/functions.rs new file mode 100644 index 00000000..cc6db7d4 --- /dev/null +++ b/exercises/basic-of-rust/src/functions.rs @@ -0,0 +1,123 @@ +// Exercise 1 +// Fix all errors +fn sum(x:i32, y: i32) -> i32 { + return 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`) + let mut sum1:u32=0; + for i in 1..n+1 { + sum1+=i; + } + + return sum1; + } + + // 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 { + let mut _len = 0.0; + let mut sum1: f64=0.0; + for i in numbers.into_iter(){ + _len=_len+1.0; + sum1=sum1+i; + } + if _len==0.0 { + return 0.0; + }else { + + return sum1/_len; + + } + } + + // Exercise 4 + // Calculate the sum of all even numbers in a list + fn sum_even_numbers(numbers: &[i32]) -> i32 { + let mut sum1:i32=0; + for i in numbers.into_iter() + { + if i%2==0 { + sum1=sum1+i; + } + } + + return sum1; + } + + + #[cfg(test)] + mod tests { + use super::*; + + // Test for exercise 1 + #[test] + fn sum_should_work() { + let (x, y) = (1, 2); + let s:i32 = 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); + } + } + \ No newline at end of file diff --git a/exercises/basic-of-rust/src/lib.rs b/exercises/basic-of-rust/src/lib.rs new file mode 100644 index 00000000..65ca46d1 --- /dev/null +++ b/exercises/basic-of-rust/src/lib.rs @@ -0,0 +1,4 @@ +mod conditions; +mod strings; +mod functions; + diff --git a/exercises/basic-of-rust/src/strings.rs b/exercises/basic-of-rust/src/strings.rs new file mode 100644 index 00000000..cc6db7d4 --- /dev/null +++ b/exercises/basic-of-rust/src/strings.rs @@ -0,0 +1,123 @@ +// Exercise 1 +// Fix all errors +fn sum(x:i32, y: i32) -> i32 { + return 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`) + let mut sum1:u32=0; + for i in 1..n+1 { + sum1+=i; + } + + return sum1; + } + + // 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 { + let mut _len = 0.0; + let mut sum1: f64=0.0; + for i in numbers.into_iter(){ + _len=_len+1.0; + sum1=sum1+i; + } + if _len==0.0 { + return 0.0; + }else { + + return sum1/_len; + + } + } + + // Exercise 4 + // Calculate the sum of all even numbers in a list + fn sum_even_numbers(numbers: &[i32]) -> i32 { + let mut sum1:i32=0; + for i in numbers.into_iter() + { + if i%2==0 { + sum1=sum1+i; + } + } + + return sum1; + } + + + #[cfg(test)] + mod tests { + use super::*; + + // Test for exercise 1 + #[test] + fn sum_should_work() { + let (x, y) = (1, 2); + let s:i32 = 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); + } + } + \ No newline at end of file diff --git a/exercises/ownership-borrowing/Cargo.toml b/exercises/ownership-borrowing/Cargo.toml new file mode 100644 index 00000000..55e140fa --- /dev/null +++ b/exercises/ownership-borrowing/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "ownership-borrowing" +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/exercises/ownership-borrowing/README.md b/exercises/ownership-borrowing/README.md new file mode 100644 index 00000000..3df30ce7 --- /dev/null +++ b/exercises/ownership-borrowing/README.md @@ -0,0 +1,4 @@ +## Complete Ownership and Borrowing exercises +### Ownership and Borrowing + ++ Make it compile `Ownership and Borrowing` exercises in `exercises/ownership-borrowing/src/lib.rs` diff --git a/exercises/ownership-borrowing/src/lib.rs b/exercises/ownership-borrowing/src/lib.rs new file mode 100644 index 00000000..b33ffc74 --- /dev/null +++ b/exercises/ownership-borrowing/src/lib.rs @@ -0,0 +1,103 @@ +// Exercise 1 +// Make it compile +fn exercise1() { + // Use as many approaches as you can to make it work + let x = String::from("hello, world"); + let y = x; + let _z = y; +} + +// Exercise 2 +// Make it compile +// Don't modify code in exercise2 function! +fn exercise2() { + let s1 = String::from("hello, world"); + let s2 = take_ownership(s1); + + println!("{}", s2); +} +// Only modify the code below! +fn take_ownership(s: String) -> String { + s +} + +// Exercise 3 +// Make it compile +// Dont care about logic +fn exercise3() { + let values: Vec = vec![ + 2817.42, 2162.17, 3756.57, 2817.42, -2817.42, 946.9, 2817.42, 964.42, 795.43, 3756.57, + 139.34, 903.58, -3756.57, 939.14, 828.04, 1120.04, 604.03, 3354.74, 2748.06, 1470.8, + 4695.71, 71.11, 2391.48, 331.29, 1214.69, 863.52, 7810.01, + ]; + + let values_number = values.len(); + + let additions: Vec = vec![0]; + println!("{:?}", values_number); + + while additions.len() > 0 { + let mut addition: f64 = 0.0; + + // Sumar valores en additions + for element_index in additions.clone() { + let addition_aux = values[element_index]; + addition = addition_aux + addition; + } + } +} + +// Exercise 4 +// Make it compile +fn exercise4(value: u32) -> String { + let str_value = value.to_string(); // Convert u32 to String + let str_ref: &str = &str_value; // Obtain a reference to the String + str_ref.to_string() // Return the reference to the String +} + +// Exercise 5 +// Make it compile +use std::collections::HashMap; +fn exercise5() { + let mut my_map = HashMap::from([(1, "1.0".to_string()), (2, "2.0".to_string())]); + + let key = 3; + + let res = match my_map.get(&key) { + Some(child) => child.clone(), + None => { + let value = "3.0".to_string(); + my_map.insert(key, value.clone()); + value // HERE IT FAILS + } + }; + + println!("{}", res); +} + +// Exercise 6 +// Make it compile + +use std::io; + +fn exercise6() { + let mut prev_key: String = String::new(); + + for line in io::stdin().lines() { + let s = line.unwrap(); + let data: Vec<&str> = s.split('\t').collect(); + if prev_key.len() == 0 { + prev_key = data[0].to_string(); + } + } +} + +// Exercise 7 +// Make it compile +fn exercise7() { + let mut v: Vec<&str> = Vec::new(); + let chars = [b'x', b'y', b'z']; + let s: &str = std::str::from_utf8(&chars).unwrap(); + v.push(&s); + println!("{:?}", v); +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 00000000..4f40d9b2 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, Rust Bootcamp by VBI Academy!"); +}