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

New challenges #22

Merged
merged 4 commits into from
Jun 12, 2024
Merged
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
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.

52 changes: 44 additions & 8 deletions challenges/challenges.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
6 changes: 6 additions & 0 deletions challenges/sum-of-array/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "sum-of-array"
version = "0.1.0"
edition = "2021"

[dependencies]
25 changes: 25 additions & 0 deletions challenges/sum-of-array/description.md
Original file line number Diff line number Diff line change
@@ -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.
3 changes: 3 additions & 0 deletions challenges/sum-of-array/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub fn sum_array(arr: &[i32]) -> i32 {
arr.iter().sum()
}
3 changes: 3 additions & 0 deletions challenges/sum-of-array/src/starter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub fn sum_array(arr: &[i32]) -> i32 {
// TODO: Implement the function here
}
34 changes: 34 additions & 0 deletions challenges/sum-of-array/tests/tests.rs
Original file line number Diff line number Diff line change
@@ -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);
}
}
7 changes: 7 additions & 0 deletions challenges/the-unit-type/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "the-unit-type"
version = "0.1.0"
edition = "2021"

[dev-dependencies]
syntest = { path = "../../crates/syntest" }
19 changes: 19 additions & 0 deletions challenges/the-unit-type/description.md
Original file line number Diff line number Diff line change
@@ -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, ());
```
3 changes: 3 additions & 0 deletions challenges/the-unit-type/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub fn print_message() -> () {
println!("Hello, Rust!");
}
3 changes: 3 additions & 0 deletions challenges/the-unit-type/src/starter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub fn print_message() -> () {
// TODO: Implement the function here
}
34 changes: 34 additions & 0 deletions challenges/the-unit-type/tests/tests.rs
Original file line number Diff line number Diff line change
@@ -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 = &macros[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!`"
)
}
}
}
6 changes: 6 additions & 0 deletions challenges/tuples/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "tuples"
version = "0.1.0"
edition = "2021"

[dependencies]
24 changes: 24 additions & 0 deletions challenges/tuples/description.md
Original file line number Diff line number Diff line change
@@ -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.
3 changes: 3 additions & 0 deletions challenges/tuples/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub fn create_tuple(a: i32, b: f64, c: &str) -> (i32, f64, String) {
(a, b, c.to_string())
}
3 changes: 3 additions & 0 deletions challenges/tuples/src/starter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub fn create_tuple(a: i32, b: f64, c: &str) -> (i32, f64, String) {
// TODO: Implement the function here
}
22 changes: 22 additions & 0 deletions challenges/tuples/tests/tests.rs
Original file line number Diff line number Diff line change
@@ -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")));
}
}
Loading