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

Add Concept Exercise Metadata files: the-realm-of-echoes #326

Merged
merged 1 commit into from
Dec 25, 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
27 changes: 27 additions & 0 deletions exercises/concept/the-realm-of-echoes/.docs/hints.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Hints

## General

- [The Cairo Book: ByteArrays][book-bytearrays]
- [Starknet By Example: Strings & ByteArrays][sbe-strings]

### 1. Format a magical chant

- Use the `format!` macro to combine multiple `ByteArray` inputs into a single string.
- Separate the chants with a delimiter such as `"-"` to match the expected output.
- Ensure the output is returned as a `ByteArray`.

### 2. Implement a `Display` trait for `EchoStone`

- The `Display` trait allows you to define how the `EchoStone` should appear when printed with `{}`.
- Use the `write!` macro within the `fmt` function to format the output string.
- Include both `power` and `duration` fields in a clear and concise format, e.g., `EchoStone [power: X, duration: Y]`.

### 3. Implement a `Debug` trait for `EchoStone`

- The `Debug` trait is used for debugging purposes, allowing for more detailed or structured output when printed with `{:?}`.
- Follow a clear debugging format, such as `Debugging EchoStone: { power: X, duration: Y }`.
- Use the `write!` macro to create the debug output.

[book-bytearrays]: https://book.cairo-lang.org/ch02-02-data-types.html#string-types
[sbe-strings]: https://starknet-by-example.voyager.online/getting-started/basics/bytearrays-strings
46 changes: 46 additions & 0 deletions exercises/concept/the-realm-of-echoes/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Introduction

Printing in Cairo allows you to display messages or debug information during program execution.

## Basics

Cairo provides two macros for printing:

- `println!`: Outputs a message followed by a newline.
- `print!`: Outputs a message without a newline.

```rust
println!("Hello, Cairo!");
println!("x = {}, y = {}", 10, 20);
```

Placeholders `{}` are replaced with provided values.

## Formatting Strings

Use `format!` to create a `ByteArray` without immediately printing:

```rust
let result = format!("{}-{}-{}", "tic", "tac", "toe");
println!("{}", result); // Output: tic-tac-toe
```

## Custom Data Types

For custom types, implement `Display` or derive `Debug` for printing:

```rust
#[derive(Debug)]
struct Point { x: u8, y: u8 }

let p = Point { x: 3, y: 4 };
println!("{:?}", p); // Debug output: Point { x: 3, y: 4 }
```

## Hexadecimal Printing

Use `{:x}` to print integers as hexadecimal:

```rust
println!("{:x}", 255); // Output: ff
```
2 changes: 1 addition & 1 deletion exercises/concept/the-realm-of-echoes/.meta/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@
"Scarb.toml"
]
},
"blurb": "<blurb>"
"blurb": "Learn printing and formatting values with the art of EchoSpeak."
}
36 changes: 36 additions & 0 deletions exercises/concept/the-realm-of-echoes/.meta/design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Design

## Goal

Introduce students to the use of formatting traits like `Display` and `Debug` and how to concatenate strings effectively using the `format!` macro.

## Learning Objectives

- Understand and implement the `Display` trait for custom types.
- Understand and implement the `Debug` trait for custom types.
- Learn how to use the `format!` macro to concatenate strings and format output.
- Explore the differences between the `Display` and `Debug` traits in formatting.

## Out of Scope

- Advanced string manipulation techniques.
- Error handling mechanisms beyond basic usage.
- Interaction with non-primitive custom types within formatting.

## Concepts

- printing

## Prerequisites

- traits
- strings
- structs

## Resources to Refer To

- [Cairo Book - Printing][printing]
- [Cairo Book - Macros][macros]

[printing]: https://book.cairo-lang.org/ch11-08-printing.html
[macros]: https://book.cairo-lang.org/ch11-05-macros.html
Original file line number Diff line number Diff line change
@@ -1,21 +1,86 @@
use the_realm_of_echoes::{EchoStone, format_magical_chant};

const U32_MAX: u32 = 0xFFFFFFFF;

#[test]
fn format_magical_chant_basic() {
let chant1 = "abra";
let chant2 = "cadabra";
let chant3 = "alakazam";
assert_eq!(format_magical_chant(chant1, chant2, chant3), "abra-cadabra-alakazam");
}

#[test]
#[ignore]
fn format_magical_chant_empty_strings() {
let chant1 = "";
let chant2 = "";
let chant3 = "";
assert_eq!(format_magical_chant(chant1, chant2, chant3), "--");
}

#[test]
#[ignore]
fn format_magical_chant_mixed_empty_and_non_empty() {
let chant1 = "abra";
let chant2 = "";
let chant3 = "alakazam";
assert_eq!(format_magical_chant(chant1, chant2, chant3), "abra--alakazam");
}

#[test]
#[ignore]
fn format_magical_chant_with_special_characters() {
let chant1 = "ab@ra";
let chant2 = "ca!dabra";
let chant3 = "ala#kazam";
assert_eq!(format_magical_chant(chant1, chant2, chant3), "ab@ra-ca!dabra-ala#kazam");
}

#[test]
#[ignore]
fn echo_stone_display_basic() {
let stone = EchoStone { power: 100, duration: 50 };
assert_eq!(format!("{stone}"), "EchoStone [power: 100, duration: 50]");
}

#[test]
fn test_format_magical_chant() {
let result = format_magical_chant("Spark", "Shine", "Glow");
assert_eq!(result, "Spark-Shine-Glow");
#[ignore]
fn echo_stone_display_zero_values() {
let stone = EchoStone { power: 0, duration: 0 };
assert_eq!(format!("{stone}"), "EchoStone [power: 0, duration: 0]");
}


#[test]
#[ignore]
fn echo_stone_display_large_values() {
let stone = EchoStone { power: U32_MAX, duration: U32_MAX };
assert_eq!(
format!("{stone}"), format!("EchoStone [power: {}, duration: {}]", U32_MAX, U32_MAX),
);
}

#[test]
#[ignore]
fn echo_stone_debug_basic() {
let stone = EchoStone { power: 100, duration: 50 };
assert_eq!(format!("{stone:?}"), "Debugging EchoStone: { power: 100, duration: 50 }");
}

#[test]
#[ignore]
fn test_stringify_echo_stone() {
let stone = EchoStone { power: 100, duration: 300 };
assert_eq!(format!("{stone}"), "EchoStone [power: 100, duration: 300]");
fn echo_stone_debug_zero_values() {
let stone = EchoStone { power: 0, duration: 0 };
assert_eq!(format!("{stone:?}"), "Debugging EchoStone: { power: 0, duration: 0 }");
}

#[test]
#[ignore]
fn test_debug_echo_stone() {
let stone = EchoStone { power: 100, duration: 300 };
assert_eq!(format!("{:?}", stone), "Debugging EchoStone: { power: 100, duration: 300 }");
fn echo_stone_debug_large_values() {
let stone = EchoStone { power: U32_MAX, duration: U32_MAX };
assert_eq!(
format!("{stone:?}"),
format!("Debugging EchoStone: {{ power: {}, duration: {} }}", U32_MAX, U32_MAX),
);
}
Loading