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

Upgrade all exercises to Cairo 2.7.1 #139

Merged
merged 4 commits into from
Aug 31, 2024
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
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ jobs:
- name: Getting scarb
uses: software-mansion/setup-scarb@v1
with:
scarb-version: "2.6.3"
scarb-version: "2.7.1"

- name: Verify all exercises
env:
Expand All @@ -56,7 +56,7 @@ jobs:
- name: Getting scarb
uses: software-mansion/setup-scarb@v1
with:
scarb-version: "2.6.3"
scarb-version: "2.7.1"

- name: Format
run: ./bin/format_exercises.sh
Expand Down
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
scarb 2.7.1
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ _Thoughtful suggestions will likely result faster & more enthusiastic responses

## Testing

Tests currently use Cairo v2.7.1 (included with Scarb v2.7.1).

To test all exercises, run `./bin/verify-exercises`.
This command will iterate over all exercises and check to see if their exemplar/example implementation passes all the tests.

Expand Down
9 changes: 6 additions & 3 deletions bin/verify-exercises
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ repo=$(git rev-parse --show-toplevel)
slug="${1:-*}"

if [ "$slug" != "*" ]; then
exercises="exercises/*/$slug"
if [ ! -f "$exercises" ]; then
echo "No such exercises: $slug"
if [ -d "$repo/exercises/concept/$slug" ]; then
exercises="exercises/concept/$slug"
elif [ -d "$repo/exercises/practice/$slug" ]; then
exercises="exercises/practice/$slug"
else
echo "No such exercise: $slug"
exit 1
fi
elif [ "$GITHUB_EVENT_NAME" = "pull_request" ]; then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub impl PositionImpl of PositionTrait {
impl AbsImpl of AbsTrait {
fn abs(self: @i16) -> u16 {
let val = if *self < 0 {
*self * -1
-*self
} else {
*self
};
Expand Down
5 changes: 4 additions & 1 deletion exercises/concept/low-power-embedded-game/Scarb.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
[package]
name = "low_power_embedded_game"
version = "0.1.0"
edition = "2023_11"
edition = "2024_07"

[dev-dependencies]
cairo_test = "2.7.1"
Original file line number Diff line number Diff line change
Expand Up @@ -72,27 +72,7 @@ mod evens {
}

mod manhattan {
use core::byte_array::ByteArrayTrait;
use low_power_embedded_game::{Position, PositionTrait};
use core::fmt::{Debug, Formatter, Error};

// Enables printing i16 values in tests.
// Note that this will soon be added to the core library.
impl IDebug of Debug<i16> {
fn fmt(self: @i16, ref f: Formatter) -> Result<(), Error> {
if *self < 0 {
f.buffer.append(@"-");
}
let abs_value = if *self < 0 {
*self * -1
} else {
*self
};
let abs_value: u16 = abs_value.try_into().unwrap();
f.buffer.append(@format!("{}", abs_value));
Result::Ok(())
}
}

#[test]
#[ignore]
Expand Down
60 changes: 30 additions & 30 deletions exercises/practice/allergies/.meta/example.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ struct Allergies {
score: u32,
}

#[derive(Drop, Debug, PartialEq)]
#[derive(Drop, Copy, Debug, PartialEq)]
pub enum Allergen {
Eggs,
Peanuts,
Expand All @@ -15,49 +15,49 @@ pub enum Allergen {
Cats,
}

const ALLERGENS: [
Allergen
; 8] = [
Allergen::Eggs,
Allergen::Peanuts,
Allergen::Shellfish,
Allergen::Strawberries,
Allergen::Tomatoes,
Allergen::Chocolate,
Allergen::Pollen,
Allergen::Cats,
];

#[generate_trait]
pub impl AllergiesImpl of AllergiesTrait {
fn new(score: u32) -> Allergies {
Allergies { score }
}

fn is_allergic_to(self: @Allergies, allergen: @Allergen) -> bool {
let allergens = AllergiesImpl::allergens().span();
let allergens = ALLERGENS.span();
let mut found = false;
let mut index = 0;
while let Option::Some(next_allergen) = allergens
.get(index) {
if next_allergen.unbox() == allergen {
break;
}
index += 1;
};
index != allergens.len() && (*self.score & pow(2, index)) != 0
while let Option::Some(next_allergen) = allergens.get(index) {
if next_allergen.unbox() == allergen {
found = true;
break;
}
index += 1;
};
found && (*self.score & pow(2, index)) != 0
}

fn allergies(self: @Allergies) -> Array<Allergen> {
let mut result: Array<Allergen> = array![];
let mut allergens = AllergiesImpl::allergens();
while let Option::Some(allergen) = allergens
.pop_front() {
if self.is_allergic_to(@allergen) {
result.append(allergen);
}
};
let allergens = ALLERGENS.span();
for allergen in allergens {
if self.is_allergic_to(allergen) {
result.append(*allergen);
}
};
result
}

fn allergens() -> Array<Allergen> {
array![
Allergen::Eggs,
Allergen::Peanuts,
Allergen::Shellfish,
Allergen::Strawberries,
Allergen::Tomatoes,
Allergen::Chocolate,
Allergen::Pollen,
Allergen::Cats,
]
}
}

fn pow(base: u32, mut power: u32) -> u32 {
Expand Down
5 changes: 4 additions & 1 deletion exercises/practice/allergies/Scarb.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
[package]
name = "allergies"
version = "0.1.0"
edition = "2023_11"
edition = "2024_07"

[dev-dependencies]
cairo_test = "2.7.1"
25 changes: 11 additions & 14 deletions exercises/practice/allergies/tests/allergies.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -286,22 +286,19 @@ fn compare_allergy_vectors(expected: @Array<Allergen>, actual: @Array<Allergen>)
);
}

let mut i = 0;
while let Option::Some(expected_elem) = expected
.get(i) {
let mut j = 0;
while let Option::Some(actual_elem) = actual
.get(j) {
if actual_elem.unbox() == expected_elem.unbox() {
break;
}
j += 1;
};
if j == actual.len() {
panic!("Allergen missing\n {expected_elem:?} should be in {actual:?}");
let expected = expected.span();
for expected_elem in expected {
let mut j = 0;
while let Option::Some(actual_elem) = actual.get(j) {
if actual_elem.unbox() == expected_elem {
break;
}
i += 1;
j += 1;
};
if j == actual.len() {
panic!("Allergen missing\n {expected_elem:?} should be in {actual:?}");
}
};
}

#[test]
Expand Down
17 changes: 5 additions & 12 deletions exercises/practice/anagram/.meta/example.cairo
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use core::dict::Felt252Dict;

#[derive(Drop, Debug)]
struct Set {
values: Array<ByteArray>
Expand Down Expand Up @@ -35,10 +37,6 @@ impl SetEq of PartialEq<Set> {
i += 1;
}
}

fn ne(lhs: @Set, rhs: @Set) -> bool {
!(lhs == rhs)
}
}

pub fn anagrams_for(word: @ByteArray, candidates: @Set) -> Set {
Expand Down Expand Up @@ -79,10 +77,6 @@ impl IgnoreCase of PartialEq<ByteArray> {
i += 1;
}
}

fn ne(lhs: @ByteArray, rhs: @ByteArray) -> bool {
!IgnoreCase::eq(lhs, rhs)
}
}

fn sort_ignore_case(word: @ByteArray) -> ByteArray {
Expand All @@ -98,22 +92,21 @@ fn sort_ignore_case(word: @ByteArray) -> ByteArray {
let mut sorted_word: ByteArray = "";

// append each appearing alphabet ASCII character
let mut alphabet_char: u8 = 65; // char 'A'
while alphabet_char <= 90 { // char 'Z'
let mut alphabet_char: u8 = 'A';
while alphabet_char <= 'Z' {
// process uppercase char
let mut count = ascii_chars.get(alphabet_char.into());
while count != 0 {
sorted_word.append_byte(alphabet_char);
count -= 1;
};
// process lowercase char
let lowercase_char = alphabet_char + 32;
let lowercase_char = lowercase(@alphabet_char);
let mut count = ascii_chars.get(lowercase_char.into());
while count != 0 {
sorted_word.append_byte(lowercase_char);
count -= 1;
};

alphabet_char += 1;
};

Expand Down
5 changes: 4 additions & 1 deletion exercises/practice/anagram/Scarb.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
[package]
name = "anagram"
version = "0.1.0"
edition = "2023_11"
edition = "2024_07"

[dev-dependencies]
cairo_test = "2.7.1"
5 changes: 0 additions & 5 deletions exercises/practice/anagram/src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ impl SetEq of PartialEq<Set> {
// determine whether the two Sets are equal
panic!("implement `eq`")
}

fn ne(lhs: @Set, rhs: @Set) -> bool {
// determine whether the two Sets are NOT equal
panic!("implement `ne`")
}
}

pub fn anagrams_for(word: @ByteArray, candidates: @Set) -> Set {
Expand Down
5 changes: 4 additions & 1 deletion exercises/practice/armstrong-numbers/Scarb.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
[package]
name = "armstrong_numbers"
version = "0.1.0"
edition = "2023_11"
edition = "2024_07"

[dev-dependencies]
cairo_test = "2.7.1"
5 changes: 4 additions & 1 deletion exercises/practice/beer-song/Scarb.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
[package]
name = "beer_song"
version = "0.1.0"
edition = "2023_11"
edition = "2024_07"

[dev-dependencies]
cairo_test = "2.7.1"
5 changes: 4 additions & 1 deletion exercises/practice/binary-search/Scarb.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
[package]
name = "binary_search"
version = "0.1.0"
edition = "2023_11"
edition = "2024_07"

[dev-dependencies]
cairo_test = "2.7.1"
2 changes: 1 addition & 1 deletion exercises/practice/binary-search/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub fn find(search_array: @Array<usize>, value: usize) -> Option<usize> {
// use the binary search algorithm to find the element '{value}' in the array '{search_array:?}'
// and return its index, otherwise return Option::None
// and return its index, otherwise return Option::None
panic!("implement `find`")
}
4 changes: 2 additions & 2 deletions exercises/practice/clock/.meta/example.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn double_digit(n: u32) -> ByteArray {
#[generate_trait]
pub impl ClockImpl of ClockTrait {
fn new(hour: i32, minute: i32) -> Clock {
ClockImpl::build(hour * 60 + minute)
Self::build(hour * 60 + minute)
}

fn build(minutes: i32) -> Clock {
Expand All @@ -37,7 +37,7 @@ pub impl ClockImpl of ClockTrait {
}

fn add_minutes(ref self: Clock, minutes: i32) -> Clock {
ClockImpl::build(self.minutes.try_into().unwrap() + minutes)
Self::build(self.minutes.try_into().unwrap() + minutes)
}

fn to_string(self: @Clock) -> ByteArray {
Expand Down
5 changes: 4 additions & 1 deletion exercises/practice/clock/Scarb.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
[package]
name = "clock"
version = "0.1.0"
edition = "2023_11"
edition = "2024_07"

[dev-dependencies]
cairo_test = "2.7.1"
5 changes: 4 additions & 1 deletion exercises/practice/collatz-conjecture/Scarb.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
[package]
name = "collatz_conjecture"
version = "0.1.0"
edition = "2023_11"
edition = "2024_07"

[dev-dependencies]
cairo_test = "2.7.1"
Loading
Loading