Skip to content

Commit

Permalink
new exercises on dict
Browse files Browse the repository at this point in the history
  • Loading branch information
chachaleo committed Oct 18, 2023
1 parent cf37881 commit b961396
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 0 deletions.
29 changes: 29 additions & 0 deletions exercises/dict/dict1.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// dict1.cairo
// The Felt252Dict maps a felt252 to a value of the specified type.
// In this exercise, you will map a `u32` to a `felt252`.

// Your task is to create an `Felt252Dict` which holds 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 a: Felt252Dict<u32> = Default::default();
//TODO

}


// Don't change anything in the test
#[test]
fn test_dict() {
let mut a = create_dictionary();
assert(a.get('A') == 1, 'First element is not 1');
assert(a.get('B') == 2, 'Second element is not 2');
assert(a.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 as 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 a: 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 a: Felt252Dict<u32> = Default::default();
a.insert(0, 1);
a.insert(1, 2);
a.insert(2, 3);

multiply_element_by_10(ref a, 3);

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

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

multiply_element_by_10(ref a, 4);

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

}
65 changes: 65 additions & 0 deletions exercises/dict/dict3.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// dict3.cairo
// Custom data structure using dicts
// Using Felt252Dict in structures allow us to modify values stored in memory
// In this exercise we have a struct Team where a Felt252Dict maps the name of the player to its level and keeps track of
// the number of player.
// Using the methods set and get from the Felt252DictTrait, implement to functions to modify the level of your 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>,
number_of_player: 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 number_of_player(self: @Team) -> usize {
//TODO
}
}



#[test]
fn test_add_player() {

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

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

#[test]
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');
}
31 changes: 31 additions & 0 deletions info.toml
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,37 @@ 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
"""

# DICT

[[exercises]]
name = "dict1"
path = "exercises/dict/dict1.cairo"
mode = "test"
hint = """
Build-in methods for Felt252Dict can be found in the corresponding chapter :
https://cairo-book.github.io/ch03-02-dictionaries.html
"""


[[exercises]]
name = "dict2"
path = "exercises/dict/dict2.cairo"
mode = "test"
hint = """
Build-in methods for Felt252Dict can be found in the corresponding 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 dict can be found on the chapter :
https://cairo-book.github.io/ch03-03-custom-data-structures.html
"""


# MOVE SEMANTICS

Expand Down

0 comments on commit b961396

Please sign in to comment.