Skip to content

Commit

Permalink
Fix funcs 'new' and 'difference'
Browse files Browse the repository at this point in the history
  • Loading branch information
Nenad committed Jun 24, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 1d9fc78 commit 2301b16
Showing 2 changed files with 10 additions and 28 deletions.
37 changes: 9 additions & 28 deletions exercises/practice/custom-set/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ use core::box::BoxTrait;

#[derive(Drop, Debug)]
pub struct CustomSet<T> {
collection: Array<T>,
pub collection: Array<T>,
}

pub impl CustomSetEq<
@@ -28,7 +28,7 @@ pub impl CustomSetImpl<
> of CustomSetTrait<T> {
fn new(inputs: @Array<T>) -> CustomSet<T> {
let mut set = CustomSet::<T> { collection: array![], };
let mut i = inputs.len();
let mut i = 0;
while let Option::Some(val) = inputs
.get(i) {
let unboxed = val.unbox();
@@ -147,39 +147,20 @@ pub impl CustomSetImpl<

#[must_use]
fn difference(self: @CustomSet<T>, other: @CustomSet<T>) -> CustomSet<T> {
let mut set = CustomSetImpl::<T>::new(@array![]);

let mut smaller_set = self;
let mut larger_set = other;
if smaller_set.collection.len() > larger_set.collection.len() {
smaller_set = other;
larger_set = self;
};
let mut diff = CustomSetImpl::<T>::new(@array![]);

let mut i = 0;
while i < smaller_set
.collection
.len() {
if !larger_set.contains(smaller_set.collection[i]) {
set.add(smaller_set.collection[i].clone());
}
if !smaller_set.contains(larger_set.collection[i]) {
set.add(larger_set.collection[i].clone());
}
i += 1;
};

// iterate the remaining items in the larger set
while i < larger_set
while let Option::Some(val) = self
.collection
.len() {
if !smaller_set.contains(larger_set.collection[i]) {
set.add(larger_set.collection[i].clone());
.get(i) {
let unboxed = val.unbox();
if !other.contains(unboxed) {
diff.add(unboxed.clone());
}
i += 1;
};

set
diff
}
}

1 change: 1 addition & 0 deletions exercises/practice/custom-set/src/tests.cairo
Original file line number Diff line number Diff line change
@@ -294,3 +294,4 @@ fn union_of_non_empty_sets_contains_all_unique_elements() {
let expected = U32Set::new(@array![3, 2, 1]);
assert_eq!(set_1.union(@set_2), expected);
}

0 comments on commit 2301b16

Please sign in to comment.