diff --git a/Cargo.lock b/Cargo.lock index 5eb90e8..0d24c61 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" @@ -1261,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" @@ -1418,6 +1429,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 107481d..1843d2d 100644 --- a/challenges/challenges.json +++ b/challenges/challenges.json @@ -84,10 +84,10 @@ "updated_at": "2024-06-10T00:00:00Z" }, { - "id": 2, - "title": "Character counting string", - "slug": "character-counting-string", - "short_description": "Write a program that takes a string as input and counts the number of characters in the string.", + "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", @@ -96,10 +96,46 @@ "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.", + "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-12T00:00:00Z", + "updated_at": "2024-06-12T00: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-12T00:00:00Z", + "updated_at": "2024-06-12T00: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-12T00:00:00Z", + "updated_at": "2024-06-12T00:00:00Z" + }, + { + "id": 2, + "title": "Character counting string", + "slug": "character-counting-string", + "short_description": "Write a program that takes a string as input and counts the number of characters in the string.", "language": "RUST", "difficulty": "BEGINNER", "track": "RUST_BASICS", 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); + } +} 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!`" + ) + } + } +} 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"))); + } +}