-
Notifications
You must be signed in to change notification settings - Fork 395
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* new exercises on dict * Apply suggestions from code review Co-authored-by: Mathieu <[email protected]> * modifications from review * minor: Readme and positioned after traits --------- Co-authored-by: Mathieu <[email protected]> Co-authored-by: Shramee Srivastav <[email protected]>
- Loading branch information
1 parent
00dfd01
commit 9015007
Showing
5 changed files
with
181 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
#[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'); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
#[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'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters