-
Notifications
You must be signed in to change notification settings - Fork 396
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
172 additions
and
0 deletions.
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,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'); | ||
} | ||
|
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 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'); | ||
|
||
} |
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,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'); | ||
} |
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