From 901500732b100b20170d59a9eb52d5e3ce1ad1cc Mon Sep 17 00:00:00 2001 From: Charlotte <49371958+chachaleo@users.noreply.github.com> Date: Thu, 19 Oct 2023 16:21:13 +0700 Subject: [PATCH] Exercises on dicts (#161) * new exercises on dict * Apply suggestions from code review Co-authored-by: Mathieu <60658558+enitrat@users.noreply.github.com> * modifications from review * minor: Readme and positioned after traits --------- Co-authored-by: Mathieu <60658558+enitrat@users.noreply.github.com> Co-authored-by: Shramee Srivastav --- exercises/dict/README.md | 6 ++++ exercises/dict/dict1.cairo | 30 +++++++++++++++++ exercises/dict/dict2.cairo | 47 ++++++++++++++++++++++++++ exercises/dict/dict3.cairo | 67 ++++++++++++++++++++++++++++++++++++++ info.toml | 32 +++++++++++++++++- 5 files changed, 181 insertions(+), 1 deletion(-) create mode 100644 exercises/dict/README.md create mode 100644 exercises/dict/dict1.cairo create mode 100644 exercises/dict/dict2.cairo create mode 100644 exercises/dict/dict3.cairo diff --git a/exercises/dict/README.md b/exercises/dict/README.md new file mode 100644 index 000000000..1ee208623 --- /dev/null +++ b/exercises/dict/README.md @@ -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) diff --git a/exercises/dict/dict1.cairo b/exercises/dict/dict1.cairo new file mode 100644 index 000000000..83fbe35e3 --- /dev/null +++ b/exercises/dict/dict1.cairo @@ -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 { + let mut dict: Felt252Dict = Default::default(); + //TODO + +} + + +// Don't change anything in the test +#[test] +#[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'); +} + diff --git a/exercises/dict/dict2.cairo b/exercises/dict/dict2.cairo new file mode 100644 index 000000000..1643004df --- /dev/null +++ b/exercises/dict/dict2.cairo @@ -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, 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 = 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 = 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'); + +} \ No newline at end of file diff --git a/exercises/dict/dict3.cairo b/exercises/dict/dict3.cairo new file mode 100644 index 000000000..aef34eb29 --- /dev/null +++ b/exercises/dict/dict3.cairo @@ -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, + 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] +#[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] +#[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'); +} diff --git a/info.toml b/info.toml index a2f85698c..4bcac8b3d 100644 --- a/info.toml +++ b/info.toml @@ -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]] @@ -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]]