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

Exercises on dicts #161

Merged
merged 4 commits into from
Oct 19, 2023
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
6 changes: 6 additions & 0 deletions exercises/dict/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Dicts
The Felt252Dict maps a felt252 to a value of the specified type.

## Further information

- [Dictionaries](https://cairo-book.github.io/ch03-02-dictionaries.html)
30 changes: 30 additions & 0 deletions exercises/dict/dict1.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// dict1.cairo
// The Felt252Dict maps a felt252 to a value of the specified type.
// In this exercise, you will map a `felt252` key to a value of type `u32`.

// Your task is to create a `Felt252Dict` containing three elements of type `u32`.
// The first element shoud map the key 'A' to the value 1, the second key 'B' to the value 2
// and the third shoud map 'bob' to the value 3.
// Make me compile and pass the test!
// Execute `starklings hint dict1` or use the `hint` watch subcommand for a hint.

// I AM NOT DONE


fn create_dictionary() -> Felt252Dict<u32> {
let mut dict: Felt252Dict<u32> = Default::default();
//TODO

}


// Don't change anything in the test
#[test]
shramee marked this conversation as resolved.
Show resolved Hide resolved
#[available_gas(200000)]
fn test_dict() {
let mut dict = create_dictionary();
assert(dict.get('A') == 1, 'First element is not 1');
assert(dict.get('B') == 2, 'Second element is not 2');
assert(dict.get('bob') == 3, 'Third element is not 3');
}

47 changes: 47 additions & 0 deletions exercises/dict/dict2.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// dict2.cairo
// Dictionaries can be used to simulate dynamic array : the value they store can be accessed and modified.
// Your task is to create a function that mutliplies the elements stored at the indexes 0 to n of a dictionary by 10
// Make me compile and pass the test!
// Execute `starklings hint dict2` or use the `hint` watch subcommand for a hint.

// I AM NOT DONE



fn multiply_element_by_10(ref dict: Felt252Dict<u32>, n: usize) {
//TODO : make a function that mutliplies the elements stored at the indexes 0 to n of a dictionary by 10


}

// Don't change anything in the test
#[test]
#[available_gas(2000000000)]
fn test_3() {
let mut dict: Felt252Dict<u32> = Default::default();
dict.insert(0, 1);
dict.insert(1, 2);
dict.insert(2, 3);

multiply_element_by_10(ref dict, 3);

assert(dict.get(0) == 10, 'First element is not 10');
assert(dict.get(1) == 20, 'Second element is not 20');
assert(dict.get(2) == 30, 'Third element is not 30');
}

#[test]
#[available_gas(200000000)]
fn test_4() {
let mut dict: Felt252Dict<u32> = Default::default();
dict.insert(0, 1);
dict.insert(1, 2);
dict.insert(2, 5);
dict.insert(3, 10);

multiply_element_by_10(ref dict, 4);

assert(dict.get(2) == 50, 'First element is not 50');
assert(dict.get(3) == 100, 'First element is not 100');

}
67 changes: 67 additions & 0 deletions exercises/dict/dict3.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// dict3.cairo
// Custom data structure using dicts
// Using Felt252Dict in structs allow us to simulate mutable data structures
// In this exercise we have a struct Team where a Felt252Dict maps the name of a player to its level and keeps track of
// the number of player.
// Using the methods set and get from the Felt252DictTrait, implement the required functions to interact with the team
// Make me compile and pass the test!
// Execute `starklings hint dict3` or use the `hint` watch subcommand for a hint.


// I AM NOT DONE


#[derive(Destruct)]
struct Team {
level: Felt252Dict<usize>,
players_count: usize
}

#[generate_trait]
impl TeamImpl of TeamTrait {
fn new() -> Team {
//TODO : initialize empty team with 0 player
}

fn get_level(ref self: Team, name: felt252) -> usize {
//TODO
}

fn add_player(ref self: Team, name: felt252, level: usize) -> () {
//TODO
}

fn level_up(ref self: Team, name: felt252) {
//TODO
}

fn players_count(self: @Team) -> usize {
//TODO
}
}



#[test]
shramee marked this conversation as resolved.
Show resolved Hide resolved
#[available_gas(200000)]
fn test_add_player() {

let mut team = TeamTrait::new();
team.add_player('bob', 10);
team.add_player('alice', 20);

assert(team.players_count == 2, 'Wrong number of player');
assert(team.get_level('bob') == 10, 'Wrong level');
assert(team.get_level('alice') == 20, 'Wrong level');
}

#[test]
shramee marked this conversation as resolved.
Show resolved Hide resolved
#[available_gas(200000)]
fn test_level_up() {

let mut team = TeamTrait::new();
team.add_player('bobby',10);
team.level_up('bobby');

assert(team.level.get('bobby') == 11, 'Wrong level');
}
32 changes: 31 additions & 1 deletion info.toml
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,6 @@ Looking at the test functions will also help you understand more about the synta
This section will help you understanding more about methods https://cairo-book.github.io/ch05-03-method-syntax.html
"""


# MOVE SEMANTICS

[[exercises]]
Expand Down Expand Up @@ -506,6 +505,37 @@ If you're having trouble updating the distance value in the `Fish` and `Dog` imp
3. Reconstruct `self` with the updated variables (`self = MyStruct { ... }`)
"""

# DICT

[[exercises]]
name = "dict1"
path = "exercises/dict/dict1.cairo"
mode = "test"
hint = """
More info about the Felt252Dict type can be found in the following chapter :
https://cairo-book.github.io/ch03-02-dictionaries.html
"""


[[exercises]]
name = "dict2"
path = "exercises/dict/dict2.cairo"
mode = "test"
hint = """
More info about the Felt252Dict type can be found in the following chapter :
https://cairo-book.github.io/ch03-02-dictionaries.html
"""


[[exercises]]
name = "dict3"
path = "exercises/dict/dict3.cairo"
mode = "test"
hint = """
Example of custom data structures using dicts can be found in this chapter :
https://cairo-book.github.io/ch03-03-custom-data-structures.html
"""

# MODULES

[[exercises]]
Expand Down