From 4baf3b10db77695f7ea3e3ed170b50e741eefa20 Mon Sep 17 00:00:00 2001 From: dcodesdev <101001810+dcodesdev@users.noreply.github.com> Date: Mon, 10 Jun 2024 11:37:58 +0300 Subject: [PATCH 1/4] Sum of array challenge --- Cargo.lock | 4 +++ challenges/challenges.json | 12 +++++++++ challenges/sum-of-array/Cargo.toml | 6 +++++ challenges/sum-of-array/description.md | 25 +++++++++++++++++++ challenges/sum-of-array/src/lib.rs | 3 +++ challenges/sum-of-array/src/starter.rs | 3 +++ challenges/sum-of-array/tests/tests.rs | 34 ++++++++++++++++++++++++++ 7 files changed, 87 insertions(+) create mode 100644 challenges/sum-of-array/Cargo.toml create mode 100644 challenges/sum-of-array/description.md create mode 100644 challenges/sum-of-array/src/lib.rs create mode 100644 challenges/sum-of-array/src/starter.rs create mode 100644 challenges/sum-of-array/tests/tests.rs diff --git a/Cargo.lock b/Cargo.lock index 5eb90e8..2ca10ac 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1190,6 +1190,10 @@ version = "0.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" +[[package]] +name = "sum-of-array" +version = "0.1.0" + [[package]] name = "sum-of-even-numbers" version = "0.1.0" diff --git a/challenges/challenges.json b/challenges/challenges.json index 107481d..74304e5 100644 --- a/challenges/challenges.json +++ b/challenges/challenges.json @@ -83,6 +83,18 @@ "created_at": "2024-06-10T00:00:00Z", "updated_at": "2024-06-10T00:00:00Z" }, + { + "id": 28, + "title": "Sum of Array", + "slug": "sum-of-array", + "short_description": "Calculate the sum of all elements in an array.", + "language": "RUST", + "difficulty": "BEGINNER", + "track": "RUST_BASICS", + "tags": ["arrays", "basic operations"], + "created_at": "2024-06-10T00:00:00Z", + "updated_at": "2024-06-10T00:00:00Z" + }, { "id": 2, "title": "Character counting string", diff --git a/challenges/sum-of-array/Cargo.toml b/challenges/sum-of-array/Cargo.toml new file mode 100644 index 0000000..47b3562 --- /dev/null +++ b/challenges/sum-of-array/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "sum-of-array" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/challenges/sum-of-array/description.md b/challenges/sum-of-array/description.md new file mode 100644 index 0000000..8c90508 --- /dev/null +++ b/challenges/sum-of-array/description.md @@ -0,0 +1,25 @@ +Arrays are a fundamental data structure in Rust that allow you to store a **fixed-size** collection of elements of the same type. A common operation is to calculate the sum of all elements in an array. + +In this challenge, you will implement a function to calculate the **sum of elements in an array of integers `i32`**. + +## Your task + +You need to implement the function `sum_array(arr: &[i32]) -> i32` that takes a slice of integers and returns the sum of all elements. + +## Requirements + +- The `sum_array` function should return the sum of all elements in the array. + +## Example + +```rust +let arr = [1, 2, 3, 4, 5]; + +let sum = sum_array(&arr); +assert_eq!(sum, 15); // 1 + 2 + 3 + 4 + 5 = 15 +``` + +## Hints + +- Use the `.iter()` method to iterate over the elements of the array. +- Use the `.sum()` method to calculate the sum of the elements. diff --git a/challenges/sum-of-array/src/lib.rs b/challenges/sum-of-array/src/lib.rs new file mode 100644 index 0000000..6f10539 --- /dev/null +++ b/challenges/sum-of-array/src/lib.rs @@ -0,0 +1,3 @@ +pub fn sum_array(arr: &[i32]) -> i32 { + arr.iter().sum() +} diff --git a/challenges/sum-of-array/src/starter.rs b/challenges/sum-of-array/src/starter.rs new file mode 100644 index 0000000..571a8d1 --- /dev/null +++ b/challenges/sum-of-array/src/starter.rs @@ -0,0 +1,3 @@ +pub fn sum_array(arr: &[i32]) -> i32 { + // TODO: Implement the function here +} diff --git a/challenges/sum-of-array/tests/tests.rs b/challenges/sum-of-array/tests/tests.rs new file mode 100644 index 0000000..e700407 --- /dev/null +++ b/challenges/sum-of-array/tests/tests.rs @@ -0,0 +1,34 @@ +#[cfg(test)] +mod tests { + use sum_of_array::*; + + #[test] + fn test_sum_array() { + let arr = [1, 2, 3, 4, 5]; + assert_eq!(sum_array(&arr), 15); + } + + #[test] + fn test_sum_array_with_negatives() { + let arr = [-1, -2, -3, -4, -5]; + assert_eq!(sum_array(&arr), -15); + } + + #[test] + fn test_sum_array_with_mixed() { + let arr = [1, -2, 3, -4, 5]; + assert_eq!(sum_array(&arr), 3); + } + + #[test] + fn test_sum_array_empty() { + let arr: [i32; 0] = []; + assert_eq!(sum_array(&arr), 0); + } + + #[test] + fn test_large_array() { + let arr = [2; 1000]; + assert_eq!(sum_array(&arr), 2000); + } +} From 51249176d796838ddf9f6279b6a5a2b72fdbd4c2 Mon Sep 17 00:00:00 2001 From: dcodesdev <101001810+dcodesdev@users.noreply.github.com> Date: Mon, 10 Jun 2024 11:45:56 +0300 Subject: [PATCH 2/4] tuples --- Cargo.lock | 4 ++++ challenges/challenges.json | 12 ++++++++++++ challenges/tuples/Cargo.toml | 6 ++++++ challenges/tuples/description.md | 24 ++++++++++++++++++++++++ challenges/tuples/src/lib.rs | 3 +++ challenges/tuples/src/starter.rs | 3 +++ challenges/tuples/tests/tests.rs | 22 ++++++++++++++++++++++ 7 files changed, 74 insertions(+) create mode 100644 challenges/tuples/Cargo.toml create mode 100644 challenges/tuples/description.md create mode 100644 challenges/tuples/src/lib.rs create mode 100644 challenges/tuples/src/starter.rs create mode 100644 challenges/tuples/tests/tests.rs diff --git a/Cargo.lock b/Cargo.lock index 2ca10ac..7b5a1df 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1422,6 +1422,10 @@ version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" +[[package]] +name = "tuples" +version = "0.1.0" + [[package]] name = "unicode-bidi" version = "0.3.15" diff --git a/challenges/challenges.json b/challenges/challenges.json index 74304e5..c458ae6 100644 --- a/challenges/challenges.json +++ b/challenges/challenges.json @@ -95,6 +95,18 @@ "created_at": "2024-06-10T00:00:00Z", "updated_at": "2024-06-10T00:00:00Z" }, + { + "id": 29, + "title": "Tuples", + "slug": "tuples", + "short_description": "Create a function that returns a tuple of values.", + "language": "RUST", + "difficulty": "BEGINNER", + "track": "RUST_BASICS", + "tags": ["tuples", "basic operations"], + "created_at": "2024-06-10T00:00:00Z", + "updated_at": "2024-06-10T00:00:00Z" + }, { "id": 2, "title": "Character counting string", diff --git a/challenges/tuples/Cargo.toml b/challenges/tuples/Cargo.toml new file mode 100644 index 0000000..9b0879c --- /dev/null +++ b/challenges/tuples/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "tuples" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/challenges/tuples/description.md b/challenges/tuples/description.md new file mode 100644 index 0000000..1f8db72 --- /dev/null +++ b/challenges/tuples/description.md @@ -0,0 +1,24 @@ +Tuples are a simple and versatile data structure in Rust that allow you to **group multiple values of different types into a single compound value.** They are particularly useful for returning multiple values from a function. + +**Tuples** can return multiple values of different types, which is not possible with arrays or slices. For example a tuple could be `(i32, f64, String)` which contains an integer, a float, and a string. + +In this challenge, you will implement a function that takes three arguments of different types and returns them as a tuple. + +## Your task + +You need to implement the function `create_tuple(a: i32, b: f64, c: &str) -> (i32, f64, String)` that takes an **integer `i32`**, a **float `f64`**, and a **string slice `&str`** as input and returns them as a tuple. The string slice should be converted into a `String` type. + +- The `create_tuple` function should return a tuple containing the three input values **in order**. +- The string slice input should be converted into a `String` before returning. + +## Example + +```rust +let result = create_tuple(42, 3.14, "hello"); +assert_eq!(result, (42, 3.14, String::from("hello"))); +``` + +## Hints + +- Use parentheses `()` to define and return the tuple. +- Remember to convert the **string slice `&str`** to String using `String::from()` or the `.to_string()` method. diff --git a/challenges/tuples/src/lib.rs b/challenges/tuples/src/lib.rs new file mode 100644 index 0000000..f8a7d4a --- /dev/null +++ b/challenges/tuples/src/lib.rs @@ -0,0 +1,3 @@ +pub fn create_tuple(a: i32, b: f64, c: &str) -> (i32, f64, String) { + (a, b, c.to_string()) +} diff --git a/challenges/tuples/src/starter.rs b/challenges/tuples/src/starter.rs new file mode 100644 index 0000000..e3e5b83 --- /dev/null +++ b/challenges/tuples/src/starter.rs @@ -0,0 +1,3 @@ +pub fn create_tuple(a: i32, b: f64, c: &str) -> (i32, f64, String) { + // TODO: Implement the function here +} diff --git a/challenges/tuples/tests/tests.rs b/challenges/tuples/tests/tests.rs new file mode 100644 index 0000000..512514c --- /dev/null +++ b/challenges/tuples/tests/tests.rs @@ -0,0 +1,22 @@ +#[cfg(test)] +mod tests { + use tuples::*; + + #[test] + fn test_create_tuple() { + let result = create_tuple(42, 3.14, "hello"); + assert_eq!(result, (42, 3.14, String::from("hello"))); + } + + #[test] + fn test_create_tuple_empty_string() { + let result = create_tuple(0, 0.0, ""); + assert_eq!(result, (0, 0.0, String::from(""))); + } + + #[test] + fn test_create_tuple_negative_values() { + let result = create_tuple(-42, -3.14, "negative"); + assert_eq!(result, (-42, -3.14, String::from("negative"))); + } +} From 89997568c823d845f41b807be236858981f2ff3e Mon Sep 17 00:00:00 2001 From: dcodesdev <101001810+dcodesdev@users.noreply.github.com> Date: Mon, 10 Jun 2024 11:56:20 +0300 Subject: [PATCH 3/4] the unit type --- Cargo.lock | 7 +++++ challenges/challenges.json | 12 +++++++++ challenges/the-unit-type/Cargo.toml | 7 +++++ challenges/the-unit-type/description.md | 19 ++++++++++++++ challenges/the-unit-type/src/lib.rs | 3 +++ challenges/the-unit-type/src/starter.rs | 3 +++ challenges/the-unit-type/tests/tests.rs | 34 +++++++++++++++++++++++++ 7 files changed, 85 insertions(+) create mode 100644 challenges/the-unit-type/Cargo.toml create mode 100644 challenges/the-unit-type/description.md create mode 100644 challenges/the-unit-type/src/lib.rs create mode 100644 challenges/the-unit-type/src/starter.rs create mode 100644 challenges/the-unit-type/tests/tests.rs diff --git a/Cargo.lock b/Cargo.lock index 7b5a1df..0d24c61 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1265,6 +1265,13 @@ dependencies = [ name = "the-from-trait" version = "0.1.0" +[[package]] +name = "the-unit-type" +version = "0.1.0" +dependencies = [ + "syntest", +] + [[package]] name = "tinyvec" version = "1.6.0" diff --git a/challenges/challenges.json b/challenges/challenges.json index c458ae6..ceef464 100644 --- a/challenges/challenges.json +++ b/challenges/challenges.json @@ -107,6 +107,18 @@ "created_at": "2024-06-10T00:00:00Z", "updated_at": "2024-06-10T00:00:00Z" }, + { + "id": 30, + "title": "The Unit Type", + "slug": "the-unit-type", + "short_description": "Understand and use the unit type `()` in Rust.", + "language": "RUST", + "difficulty": "BEGINNER", + "track": "RUST_BASICS", + "tags": ["unit type", "basic operations"], + "created_at": "2024-06-10T00:00:00Z", + "updated_at": "2024-06-10T00:00:00Z" + }, { "id": 2, "title": "Character counting string", diff --git a/challenges/the-unit-type/Cargo.toml b/challenges/the-unit-type/Cargo.toml new file mode 100644 index 0000000..e28dbde --- /dev/null +++ b/challenges/the-unit-type/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "the-unit-type" +version = "0.1.0" +edition = "2021" + +[dev-dependencies] +syntest = { path = "../../crates/syntest" } diff --git a/challenges/the-unit-type/description.md b/challenges/the-unit-type/description.md new file mode 100644 index 0000000..e0ee7d8 --- /dev/null +++ b/challenges/the-unit-type/description.md @@ -0,0 +1,19 @@ +In Rust, the unit type `()` is a type that has exactly one value, also written as `()`. It is used to indicate the absence of a meaningful value and is often seen in functions that do not return a value. + +In this challenge, you will implement a function that prints a message and returns the unit type. + +## Your task + +You need to implement the function `print_message() -> ()` that prints `"Hello, Rust!"` to the console and returns the unit type. + +## Requirements + +- The `print_message` function should print `"Hello, Rust!"` to the console. +- The function should return the unit type `()`. + +## Example + +```rust +let result = print_message(); +assert_eq!(result, ()); +``` diff --git a/challenges/the-unit-type/src/lib.rs b/challenges/the-unit-type/src/lib.rs new file mode 100644 index 0000000..34acbfb --- /dev/null +++ b/challenges/the-unit-type/src/lib.rs @@ -0,0 +1,3 @@ +pub fn print_message() -> () { + println!("Hello, Rust!"); +} diff --git a/challenges/the-unit-type/src/starter.rs b/challenges/the-unit-type/src/starter.rs new file mode 100644 index 0000000..e0d8459 --- /dev/null +++ b/challenges/the-unit-type/src/starter.rs @@ -0,0 +1,3 @@ +pub fn print_message() -> () { + // TODO: Implement the function here +} diff --git a/challenges/the-unit-type/tests/tests.rs b/challenges/the-unit-type/tests/tests.rs new file mode 100644 index 0000000..99cc0e0 --- /dev/null +++ b/challenges/the-unit-type/tests/tests.rs @@ -0,0 +1,34 @@ +#[cfg(test)] +mod tests { + use syntest::Syntest; + use the_unit_type::*; + + #[test] + fn test_compiles() { + print_message(); + } + + #[test] + fn test_prints() { + let syntest = Syntest::new("print_message", "src/lib.rs"); + + let macros = syntest.mac.macros(); + + assert!(!macros.is_empty(), "You should use the `println!` macro"); + assert_eq!(macros.len(), 1, "You should only print one message"); + + let macro_name = ¯os[0].name; + + assert_eq!(macro_name, "println", "You should use the `println!` macro"); + + for token in macros[0].tokens.iter() { + let token = token.to_lowercase(); + + assert_eq!( + (token.contains("hello"), token.contains("rust")), + (true, true), + "You should print `Hello, Rust!`" + ) + } + } +} From 8e78505cfa090cc23dc0b083bd34687a86115d5b Mon Sep 17 00:00:00 2001 From: dcodesdev <101001810+dcodesdev@users.noreply.github.com> Date: Wed, 12 Jun 2024 10:18:37 +0300 Subject: [PATCH 4/4] challenges fix --- challenges/challenges.json | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/challenges/challenges.json b/challenges/challenges.json index ceef464..1843d2d 100644 --- a/challenges/challenges.json +++ b/challenges/challenges.json @@ -83,6 +83,18 @@ "created_at": "2024-06-10T00:00:00Z", "updated_at": "2024-06-10T00:00:00Z" }, + { + "id": 3, + "title": "Mathematical operations", + "slug": "mathematical-operations", + "short_description": "Practice mathematical operations in Rust, including addition, subtraction, multiplication, and division.", + "language": "RUST", + "difficulty": "BEGINNER", + "track": "RUST_BASICS", + "tags": [], + "created_at": "2024-04-20T00:00:00Z", + "updated_at": "2024-04-20T00:00:00Z" + }, { "id": 28, "title": "Sum of Array", @@ -92,8 +104,8 @@ "difficulty": "BEGINNER", "track": "RUST_BASICS", "tags": ["arrays", "basic operations"], - "created_at": "2024-06-10T00:00:00Z", - "updated_at": "2024-06-10T00:00:00Z" + "created_at": "2024-06-12T00:00:00Z", + "updated_at": "2024-06-12T00:00:00Z" }, { "id": 29, @@ -104,8 +116,8 @@ "difficulty": "BEGINNER", "track": "RUST_BASICS", "tags": ["tuples", "basic operations"], - "created_at": "2024-06-10T00:00:00Z", - "updated_at": "2024-06-10T00:00:00Z" + "created_at": "2024-06-12T00:00:00Z", + "updated_at": "2024-06-12T00:00:00Z" }, { "id": 30, @@ -116,8 +128,8 @@ "difficulty": "BEGINNER", "track": "RUST_BASICS", "tags": ["unit type", "basic operations"], - "created_at": "2024-06-10T00:00:00Z", - "updated_at": "2024-06-10T00:00:00Z" + "created_at": "2024-06-12T00:00:00Z", + "updated_at": "2024-06-12T00:00:00Z" }, { "id": 2, @@ -131,18 +143,6 @@ "created_at": "2024-04-20T00:00:00Z", "updated_at": "2024-04-20T00:00:00Z" }, - { - "id": 3, - "title": "Mathematical operations", - "slug": "mathematical-operations", - "short_description": "Practice mathematical operations in Rust, including addition, subtraction, multiplication, and division.", - "language": "RUST", - "difficulty": "BEGINNER", - "track": "RUST_BASICS", - "tags": [], - "created_at": "2024-04-20T00:00:00Z", - "updated_at": "2024-04-20T00:00:00Z" - }, { "id": 4, "title": "Fizz Buzz",