Skip to content

Add Concept Exercise: weather-report #371

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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
12 changes: 6 additions & 6 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,17 @@
"status": "wip"
},
{
"slug": "rpn-calculator",
"name": "RPN Calculator",
"slug": "weather-report",
"name": "Weather Report",
"uuid": "536d9f09-5910-4a26-93fd-2242667b0b87",
"concepts": [
"control-flow",
"enums"
"enums",
"match-basics"
],
"prerequisites": [
"arrays"
"functions"
],
"status": "wip"
"status": "beta"
},
{
"slug": "welcome-to-tech-palace",
Expand Down
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
15 changes: 15 additions & 0 deletions exercises/concept/weather-report/.docs/hints.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Hints

## General

- [The Cairo Book - Enums][tcb-enums]
- [The Cairo Book - Match][tcb-match]

## 1. Generate weather reports

- `match` comes in handy when working with enums.
In this case, see how you might use it when figuring out what kind of weather report to generate.
- Remember to convert the enum variants to uppercase strings in your reports.

[tcb-enums]: https://www.starknet.io/cairo-book/ch06-01-enums.html
[tcb-match]: https://www.starknet.io/cairo-book/ch06-02-the-match-control-flow-construct.html
34 changes: 34 additions & 0 deletions exercises/concept/weather-report/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Instructions

In this exercise you'll be generating weather reports for a local news station.

## Generate weather reports

You'll start with some stubbed functions and the following enum:

```rust
#[derive(Drop, Clone, PartialEq, Debug)]
pub enum WeatherCondition {
Sunny,
Rainy,
Cloudy,
Stormy
}
```

Your goal is to emit a weather report as follows: `"Today is <CONDITION> with a temperature of <TEMPERATURE> degrees Celsius."`.
You'll need to implement functions that correspond with weather conditions.

For example, the below snippet demonstrates an expected output for the `get_weather_report` function.

```rust
get_weather_report(WeatherCondition::Rainy, "25")
// Returns: "Today is RAINY with a temperature of 25 degrees Celsius."
```

And for `sunny_day`:

```rust
get_weather_report(WeatherCondition::Sunny, "30")
// Returns: "Today is SUNNY with a temperature of 30 degrees Celsius."
```
67 changes: 67 additions & 0 deletions exercises/concept/weather-report/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Introduction

Enums in Cairo allow you to define a type that can be one of several named variants.

## Basics

Enums, short for enumerations, limit all possible values of some data to a predefined set.
The possible values of an `enum` are called variants:

```rust
#[derive(Drop)]
enum Direction {
North,
South,
East,
West,
}
```

Create enum values by specifying the variant:

```rust
let my_direction = Direction::North;
let another_direction = Direction::West;
```

## Pattern Matching

Use match to handle different enum variants:

```rust
match my_direction {
Direction::North => println!("Going north!"),
Direction::South => println!("Going south!"),
Direction::East => println!("Going east!"),
Direction::West => println!("Going west!"),
}
```

Each arm of the match handles one variant of the enum.

## Enums with Data

Enum variants can hold additional data:

```rust
struct Location {
x: u32,
y: u32,
}

enum Message {
Text: ByteArray,
Number: u32,
Location: Location,
}
```

Pattern match to extract the data:

```rust
match msg {
Message::Text(content) => println!("Text: {}", content),
Message::Number(val) => println!("Number: {}", val),
Message::Location(Location { x, y }) => println!("Position: ({}, {})", x, y),
}
```
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"authors": [
"<your_gh_username>"
"0xNeshi"
],
"files": {
"solution": [
"src/lib.cairo"
],
"test": [
"tests/rpn_calculator.cairo"
"tests/weather_report.cairo"
],
"exemplar": [
".meta/exemplar.cairo"
Expand All @@ -16,8 +16,5 @@
"Scarb.toml"
]
},
"forked_from": [
"rust/rpn-calculator"
],
"blurb": "Use some of `Array`'s methods to evaluate Reverse Polish notation"
"blurb": "Learn enums while building a weather reporting system."
}
21 changes: 21 additions & 0 deletions exercises/concept/weather-report/.meta/design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Design

## Learning objectives

- Know how to use enums.
- Know how to use pattern matching.

## Out of scope

- Generic enums.
- `if let` and `while let` statements.
- The `Option` and `Result` enums.

## Concepts

- Enums
- Pattern matching basics

## Prerequisites

- Felts
25 changes: 25 additions & 0 deletions exercises/concept/weather-report/.meta/exemplar.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#[derive(Drop)]
pub enum WeatherCondition {
Sunny,
Rainy,
Cloudy,
Stormy,
}

pub fn get_weather_report(condition: WeatherCondition, temperature: ByteArray) -> ByteArray {
match condition {
WeatherCondition::Sunny => format!(
"Today is SUNNY with a temperature of {temperature} degrees Celsius.",
),
WeatherCondition::Rainy => format!(
"Today is RAINY with a temperature of {temperature} degrees Celsius.",
),
WeatherCondition::Cloudy => format!(
"Today is CLOUDY with a temperature of {temperature} degrees Celsius.",
),
WeatherCondition::Stormy => format!(
"Today is STORMY with a temperature of {temperature} degrees Celsius.",
),
}
}

7 changes: 7 additions & 0 deletions exercises/concept/weather-report/Scarb.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "weather_report"
version = "0.1.0"
edition = "2024_07"

[dev-dependencies]
cairo_test = "2.9.2"
15 changes: 15 additions & 0 deletions exercises/concept/weather-report/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/// various weather conditions
#[derive(Drop)]
pub enum WeatherCondition {
Sunny,
Rainy,
Cloudy,
Stormy,
}

/// primary function for generating weather reports
pub fn get_weather_report(condition: WeatherCondition, temperature: ByteArray) -> ByteArray {
// return a weather report for the given condition
panic!("implement `get_weather_report`")
}

63 changes: 63 additions & 0 deletions exercises/concept/weather-report/tests/weather_report.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use weather_report::{get_weather_report, WeatherCondition};

#[test]
fn reports_sunny_weather() {
assert_eq!(
get_weather_report(WeatherCondition::Sunny, "30"),
"Today is SUNNY with a temperature of 30 degrees Celsius.",
);
}

#[test]
#[ignore]
fn reports_rainy_weather() {
assert_eq!(
get_weather_report(WeatherCondition::Rainy, "25"),
"Today is RAINY with a temperature of 25 degrees Celsius.",
);
}

#[test]
#[ignore]
fn reports_cloudy_weather() {
assert_eq!(
get_weather_report(WeatherCondition::Cloudy, "27"),
"Today is CLOUDY with a temperature of 27 degrees Celsius.",
);
}

#[test]
#[ignore]
fn get_weather_report_sunny() {
assert_eq!(
get_weather_report(WeatherCondition::Sunny, "35"),
"Today is SUNNY with a temperature of 35 degrees Celsius.",
);
}

#[test]
#[ignore]
fn get_weather_report_rainy() {
assert_eq!(
get_weather_report(WeatherCondition::Rainy, "20"),
"Today is RAINY with a temperature of 20 degrees Celsius.",
);
}

#[test]
#[ignore]
fn get_weather_report_cloudy() {
assert_eq!(
get_weather_report(WeatherCondition::Cloudy, "22"),
"Today is CLOUDY with a temperature of 22 degrees Celsius.",
);
}

#[test]
#[ignore]
fn add_stormy_variant() {
assert_eq!(
get_weather_report(WeatherCondition::Stormy, "15"),
"Today is STORMY with a temperature of 15 degrees Celsius.",
);
}
Loading