Skip to content

Commit

Permalink
basic control flow
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodesdev committed Jun 12, 2024
1 parent f27e307 commit 6451a22
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

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

12 changes: 12 additions & 0 deletions challenges/challenges.json
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,18 @@
"created_at": "2024-06-12T00:00:00Z",
"updated_at": "2024-06-12T00:00:00Z"
},
{
"id": 33,
"title": "Control Flow",
"slug": "control-flow",
"short_description": "Implement basic control flow in Rust using if-else statements.",
"language": "RUST",
"difficulty": "BEGINNER",
"track": "RUST_BASICS",
"tags": ["control flow", "if-else"],
"created_at": "2024-06-12T00:00:00Z",
"updated_at": "2024-06-12T00:00:00Z"
},
{
"id": 2,
"title": "Character counting string",
Expand Down
6 changes: 6 additions & 0 deletions challenges/control-flow/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "control-flow"
version = "0.1.0"
edition = "2021"

[dependencies]
31 changes: 31 additions & 0 deletions challenges/control-flow/description.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
Control flow is a fundamental concept in programming that allows you to dictate how your code executes based on certain conditions. In Rust, one of the ways to control the flow of your program is by using `if-else` statements.

In this challenge, you will implement a function that checks whether a number is **positive**, **negative**, or **zero**. Depending on the value, your function should return a corresponding string message.

## Your task

Your task is to complete the function `check_number_sign` that takes an **integer `i32`** as input and returns a `String` indicating whether the number is `"positive"`, `"negative"`, or `"zero"`.

## Requirements

- If the number is greater than zero, return `"positive"`.
- If the number is less than zero, return `"negative"`.
- If the number is equal to zero, return `"zero"`.

## Example

```rust
let result = check_number_sign(10);
assert_eq!(result, "positive");

let result = check_number_sign(-5);
assert_eq!(result, "negative");

let result = check_number_sign(0);
assert_eq!(result, "zero");
```

## Hints

- You can use the `if`, `else-if`, and `else` keywords to implement the control flow.
- Remember to return the result as a `String`.
9 changes: 9 additions & 0 deletions challenges/control-flow/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
pub fn check_number_sign(number: i32) -> String {
if number > 0 {
"positive".to_string()
} else if number < 0 {
"negative".to_string()
} else {
"zero".to_string()
}
}
14 changes: 14 additions & 0 deletions challenges/control-flow/src/starter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
pub fn check_number_sign(number: i32) -> String {
// Return `"positive"` if the number is positive.
// Return `"negative"` if the number is negative.
// Return `"zero"` if the number is zero.

// Step 1:
// Check if the number is positive.

// Step 2:
// Check if the number is negative.

// Step 3:
// Handle the case where it's neither positive nor negative.
}
29 changes: 29 additions & 0 deletions challenges/control-flow/tests/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#[cfg(test)]
mod tests {
use control_flow::*;

#[test]
fn test_positive_number() {
assert_eq!(check_number_sign(10), "positive");
}

#[test]
fn test_negative_number() {
assert_eq!(check_number_sign(-5), "negative");
}

#[test]
fn test_zero() {
assert_eq!(check_number_sign(0), "zero");
}

#[test]
fn test_large_positive_number() {
assert_eq!(check_number_sign(1000000), "positive");
}

#[test]
fn test_large_negative_number() {
assert_eq!(check_number_sign(-1000000), "negative");
}
}

0 comments on commit 6451a22

Please sign in to comment.