From da68129ecd6b1363e2de54df4c4ebabf13483b47 Mon Sep 17 00:00:00 2001 From: Nenad Date: Mon, 24 Jun 2024 14:32:26 +0200 Subject: [PATCH] Add missing lib placeholder --- .tool-versions | 1 + concepts/operator-overload/.meta/config.json | 7 + concepts/operator-overload/about.md | 1 + concepts/operator-overload/introduction.md | 1 + concepts/operator-overload/links.json | 1 + concepts/structs/.meta/config.json | 7 + concepts/structs/about.md | 1 + concepts/structs/introduction.md | 1 + concepts/structs/links.json | 1 + config.json | 15 +- .../practice/custom-set/.meta/example.cairo | 175 ++++++++++++++++++ exercises/practice/custom-set/src/lib.cairo | 152 ++------------- 12 files changed, 227 insertions(+), 136 deletions(-) create mode 100644 .tool-versions create mode 100644 concepts/operator-overload/.meta/config.json create mode 100644 concepts/operator-overload/about.md create mode 100644 concepts/operator-overload/introduction.md create mode 100644 concepts/operator-overload/links.json create mode 100644 concepts/structs/.meta/config.json create mode 100644 concepts/structs/about.md create mode 100644 concepts/structs/introduction.md create mode 100644 concepts/structs/links.json diff --git a/.tool-versions b/.tool-versions new file mode 100644 index 00000000..179f2a8c --- /dev/null +++ b/.tool-versions @@ -0,0 +1 @@ +scarb 2.6.5 diff --git a/concepts/operator-overload/.meta/config.json b/concepts/operator-overload/.meta/config.json new file mode 100644 index 00000000..52914445 --- /dev/null +++ b/concepts/operator-overload/.meta/config.json @@ -0,0 +1,7 @@ +{ + "blurb": "", + "authors": [ + "misicnenad" + ], + "contributors": [] +} diff --git a/concepts/operator-overload/about.md b/concepts/operator-overload/about.md new file mode 100644 index 00000000..19c307b1 --- /dev/null +++ b/concepts/operator-overload/about.md @@ -0,0 +1 @@ +# Operator Overload diff --git a/concepts/operator-overload/introduction.md b/concepts/operator-overload/introduction.md new file mode 100644 index 00000000..e10b99d0 --- /dev/null +++ b/concepts/operator-overload/introduction.md @@ -0,0 +1 @@ +# Introduction diff --git a/concepts/operator-overload/links.json b/concepts/operator-overload/links.json new file mode 100644 index 00000000..fe51488c --- /dev/null +++ b/concepts/operator-overload/links.json @@ -0,0 +1 @@ +[] diff --git a/concepts/structs/.meta/config.json b/concepts/structs/.meta/config.json new file mode 100644 index 00000000..52914445 --- /dev/null +++ b/concepts/structs/.meta/config.json @@ -0,0 +1,7 @@ +{ + "blurb": "", + "authors": [ + "misicnenad" + ], + "contributors": [] +} diff --git a/concepts/structs/about.md b/concepts/structs/about.md new file mode 100644 index 00000000..c88ba870 --- /dev/null +++ b/concepts/structs/about.md @@ -0,0 +1 @@ +# Structs diff --git a/concepts/structs/introduction.md b/concepts/structs/introduction.md new file mode 100644 index 00000000..e10b99d0 --- /dev/null +++ b/concepts/structs/introduction.md @@ -0,0 +1 @@ +# Introduction diff --git a/concepts/structs/links.json b/concepts/structs/links.json new file mode 100644 index 00000000..fe51488c --- /dev/null +++ b/concepts/structs/links.json @@ -0,0 +1 @@ +[] diff --git a/config.json b/config.json index 5447615b..256b635f 100644 --- a/config.json +++ b/config.json @@ -105,8 +105,7 @@ "uuid": "f3b7ce44-1667-42b4-b792-401d36aee2f1", "practices": [ "tuples", - "traits", - "control-flow" + "traits" ], "prerequisites": [], "difficulty": 2 @@ -118,7 +117,7 @@ "practices": [ "structs", "traits", - "control-flow" + "operator-overload" ], "prerequisites": [], "difficulty": 4 @@ -183,6 +182,16 @@ "uuid": "fe83c9a6-cf50-47b1-9993-21ddd1e895ee", "slug": "traits", "name": "Traits" + }, + { + "uuid": "4e1f6433-3125-4428-837d-47b0c10fddea", + "slug": "structs", + "name": "Structs" + }, + { + "uuid": "0b069f59-f8b4-4979-935a-c3172538f4c9", + "slug": "operator-overload", + "name": "Operator Overload" } ], "key_features": [ diff --git a/exercises/practice/custom-set/.meta/example.cairo b/exercises/practice/custom-set/.meta/example.cairo index e69de29b..3c3f5bf7 100644 --- a/exercises/practice/custom-set/.meta/example.cairo +++ b/exercises/practice/custom-set/.meta/example.cairo @@ -0,0 +1,175 @@ +use core::clone::Clone; +use core::array::ArrayTrait; +use core::box::BoxTrait; + +#[derive(Drop, Debug)] +pub struct CustomSet { + pub collection: Array, +} + +pub impl CustomSetEq< + T, +Copy, +Drop, +PartialEq, +core::fmt::Display +> of PartialEq> { + fn eq(lhs: @CustomSet, rhs: @CustomSet) -> bool { + if lhs.collection.len() != rhs.collection.len() { + return false; + } + lhs.is_subset(rhs) && rhs.is_subset(lhs) + } + + fn ne(lhs: @CustomSet, rhs: @CustomSet) -> bool { + !(lhs == rhs) + } +} + +#[generate_trait] +pub impl CustomSetImpl< + T, +Copy, +Drop, +core::fmt::Display, +PartialEq +> of CustomSetTrait { + fn new(inputs: @Array) -> CustomSet { + let mut set = CustomSet:: { collection: array![], }; + let mut i = 0; + while let Option::Some(val) = inputs + .get(i) { + let unboxed = val.unbox(); + set.add(unboxed.clone()); + i += 1; + }; + set + } + + fn add(ref self: CustomSet, element: T) { + if !self.contains(@element) { + self.collection.append(element) + } + } + + fn contains(self: @CustomSet, other: @T) -> bool { + let mut is_contained = false; + let mut i = 0; + while let Option::Some(boxed) = self + .collection + .get(i) { + let val = boxed.unbox(); + if val == other { + is_contained = true; + break; + } + i += 1; + }; + is_contained + } + + fn is_empty(self: @CustomSet) -> bool { + self.collection.is_empty() + } + + fn is_subset(self: @CustomSet, other: @CustomSet) -> bool { + if self.collection.len() > other.collection.len() { + return false; + } + let mut result = true; + let mut i = 0; + while let Option::Some(val) = self + .collection + .get(i) { + if !other.contains(val.unbox()) { + result = false; + break; + } + i += 1; + }; + result + } + + fn is_disjoint(self: @CustomSet, other: @CustomSet) -> bool { + let mut are_disjoint = true; + + // a more efficient way is to iterate the smaller set + let mut to_iterate = self; + let mut to_compare = other; + if to_iterate.collection.len() > to_compare.collection.len() { + to_iterate = other; + to_compare = self; + }; + + let mut i = 0; + while let Option::Some(val) = to_iterate + .collection + .get(i) { + if to_compare.contains(val.unbox()) { + are_disjoint = false; + break; + } + i += 1; + }; + + are_disjoint + } + + #[must_use] + fn intersection(self: @CustomSet, other: @CustomSet) -> CustomSet { + let mut collection: Array = array![]; + + // a more efficient way is to iterate the smaller set + let mut to_iterate = self; + let mut to_compare = other; + if to_iterate.collection.len() > to_compare.collection.len() { + to_iterate = other; + to_compare = self; + }; + + let mut i = 0; + while let Option::Some(val) = to_iterate + .collection + .get(i) { + let unboxed = val.unbox(); + if to_compare.contains(unboxed) { + collection.append(*unboxed); + } + i += 1; + }; + + CustomSetImpl::::new(@collection) + } + + #[must_use] + fn union(self: @CustomSet, other: @CustomSet) -> CustomSet { + let mut collection: Array = array![]; + let mut i = 0; + while let Option::Some(val) = self + .collection + .get(i) { + collection.append(*val.unbox()); + i += 1; + }; + i = 0; + while let Option::Some(val) = other + .collection + .get(i) { + collection.append(*val.unbox()); + i += 1; + }; + CustomSetImpl::::new(@collection) + } + + #[must_use] + fn difference(self: @CustomSet, other: @CustomSet) -> CustomSet { + let mut collection: Array = array![]; + let mut i = 0; + while let Option::Some(val) = self + .collection + .get(i) { + let unboxed = val.unbox(); + if !other.contains(unboxed) { + collection.append(unboxed.clone()); + } + i += 1; + }; + CustomSetImpl::::new(@collection) + } +} + + +#[cfg(test)] +mod tests; diff --git a/exercises/practice/custom-set/src/lib.cairo b/exercises/practice/custom-set/src/lib.cairo index 3c3f5bf7..70907bc7 100644 --- a/exercises/practice/custom-set/src/lib.cairo +++ b/exercises/practice/custom-set/src/lib.cairo @@ -1,24 +1,15 @@ -use core::clone::Clone; -use core::array::ArrayTrait; -use core::box::BoxTrait; - #[derive(Drop, Debug)] -pub struct CustomSet { - pub collection: Array, -} +pub struct CustomSet {} pub impl CustomSetEq< T, +Copy, +Drop, +PartialEq, +core::fmt::Display > of PartialEq> { fn eq(lhs: @CustomSet, rhs: @CustomSet) -> bool { - if lhs.collection.len() != rhs.collection.len() { - return false; - } - lhs.is_subset(rhs) && rhs.is_subset(lhs) + panic!() } fn ne(lhs: @CustomSet, rhs: @CustomSet) -> bool { - !(lhs == rhs) + panic!() } } @@ -26,150 +17,45 @@ pub impl CustomSetEq< pub impl CustomSetImpl< T, +Copy, +Drop, +core::fmt::Display, +PartialEq > of CustomSetTrait { - fn new(inputs: @Array) -> CustomSet { - let mut set = CustomSet:: { collection: array![], }; - let mut i = 0; - while let Option::Some(val) = inputs - .get(i) { - let unboxed = val.unbox(); - set.add(unboxed.clone()); - i += 1; - }; - set + fn new(_input: @Array) -> CustomSet { + panic!() } - fn add(ref self: CustomSet, element: T) { - if !self.contains(@element) { - self.collection.append(element) - } + fn contains(self: @CustomSet, element: @T) -> bool { + panic!() } - fn contains(self: @CustomSet, other: @T) -> bool { - let mut is_contained = false; - let mut i = 0; - while let Option::Some(boxed) = self - .collection - .get(i) { - let val = boxed.unbox(); - if val == other { - is_contained = true; - break; - } - i += 1; - }; - is_contained + fn add(ref self: CustomSet, element: T) { + panic!(); } - fn is_empty(self: @CustomSet) -> bool { - self.collection.is_empty() + fn is_subset(self: @CustomSet, other: @CustomSet) -> bool { + panic!() } - fn is_subset(self: @CustomSet, other: @CustomSet) -> bool { - if self.collection.len() > other.collection.len() { - return false; - } - let mut result = true; - let mut i = 0; - while let Option::Some(val) = self - .collection - .get(i) { - if !other.contains(val.unbox()) { - result = false; - break; - } - i += 1; - }; - result + fn is_empty(self: @CustomSet) -> bool { + panic!() } fn is_disjoint(self: @CustomSet, other: @CustomSet) -> bool { - let mut are_disjoint = true; - - // a more efficient way is to iterate the smaller set - let mut to_iterate = self; - let mut to_compare = other; - if to_iterate.collection.len() > to_compare.collection.len() { - to_iterate = other; - to_compare = self; - }; - - let mut i = 0; - while let Option::Some(val) = to_iterate - .collection - .get(i) { - if to_compare.contains(val.unbox()) { - are_disjoint = false; - break; - } - i += 1; - }; - - are_disjoint + panic!() } #[must_use] fn intersection(self: @CustomSet, other: @CustomSet) -> CustomSet { - let mut collection: Array = array![]; - - // a more efficient way is to iterate the smaller set - let mut to_iterate = self; - let mut to_compare = other; - if to_iterate.collection.len() > to_compare.collection.len() { - to_iterate = other; - to_compare = self; - }; - - let mut i = 0; - while let Option::Some(val) = to_iterate - .collection - .get(i) { - let unboxed = val.unbox(); - if to_compare.contains(unboxed) { - collection.append(*unboxed); - } - i += 1; - }; - - CustomSetImpl::::new(@collection) + panic!() } #[must_use] - fn union(self: @CustomSet, other: @CustomSet) -> CustomSet { - let mut collection: Array = array![]; - let mut i = 0; - while let Option::Some(val) = self - .collection - .get(i) { - collection.append(*val.unbox()); - i += 1; - }; - i = 0; - while let Option::Some(val) = other - .collection - .get(i) { - collection.append(*val.unbox()); - i += 1; - }; - CustomSetImpl::::new(@collection) + fn difference(self: @CustomSet, other: @CustomSet) -> CustomSet { + panic!() } #[must_use] - fn difference(self: @CustomSet, other: @CustomSet) -> CustomSet { - let mut collection: Array = array![]; - let mut i = 0; - while let Option::Some(val) = self - .collection - .get(i) { - let unboxed = val.unbox(); - if !other.contains(unboxed) { - collection.append(unboxed.clone()); - } - i += 1; - }; - CustomSetImpl::::new(@collection) + fn union(self: @CustomSet, other: @CustomSet) -> CustomSet { + panic!() } } - #[cfg(test)] mod tests;