🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of dvdvgt

dvdvgt's solution

to Custom Set in the Rust Track

Published at Mar 25 2021 · 0 comments
Instructions
Test suite
Solution

Create a custom set type.

Sometimes it is necessary to define a custom data structure of some type, like a set. In this exercise you will define your own set. How it works internally doesn't matter, as long as it behaves like a set of unique elements.

Rust Installation

Refer to the exercism help page for Rust installation and learning resources.

Writing the Code

Execute the tests with:

$ cargo test

All but the first test have been ignored. After you get the first test to pass, open the tests source file which is located in the tests directory and remove the #[ignore] flag from the next test and get the tests to pass again. Each separate test is a function with #[test] flag above it. Continue, until you pass every test.

If you wish to run all ignored tests without editing the tests source file, use:

$ cargo test -- --ignored

To run a specific test, for example some_test, you can use:

$ cargo test some_test

If the specific test is ignored use:

$ cargo test some_test -- --ignored

To learn more about Rust tests refer to the online test documentation

Make sure to read the Modules chapter if you haven't already, it will help you with organizing your files.

Further improvements

After you have solved the exercise, please consider using the additional utilities, described in the installation guide, to further refine your final solution.

To format your solution, inside the solution directory use

cargo fmt

To see, if your solution contains some common ineffective use cases, inside the solution directory use

cargo clippy --all-targets

Submitting the solution

Generally you should submit all files in which you implemented your solution (src/lib.rs in most cases). If you are using any external crates, please consider submitting the Cargo.toml file. This will make the review process faster and clearer.

Feedback, Issues, Pull Requests

The exercism/rust repository on GitHub is the home for all of the Rust exercises. If you have feedback about an exercise, or want to help implement new exercises, head over there and create an issue. Members of the rust track team are happy to help!

If you want to know more about Exercism, take a look at the contribution guide.

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

custom-set.rs

use custom_set::*;

#[test]
fn sets_with_no_elements_are_empty() {
    let set: CustomSet<()> = CustomSet::new(&[]);
    assert!(set.is_empty());
}

#[test]
#[ignore]
fn sets_with_elements_are_not_empty() {
    let set = CustomSet::new(&[1]);
    assert!(!set.is_empty());
}

#[test]
#[ignore]
fn nothing_is_contained_in_an_empty_set() {
    let set = CustomSet::new(&[]);
    assert!(!set.contains(&1));
}

#[test]
#[ignore]
fn true_when_the_element_is_in_the_set() {
    let set = CustomSet::new(&[1, 2, 3]);
    assert!(set.contains(&1));
}

#[test]
#[ignore]
fn false_when_the_element_is_not_in_the_set() {
    let set = CustomSet::new(&[1, 2, 3]);
    assert!(!set.contains(&4));
}

#[test]
#[ignore]
fn empty_sets_are_subsets_of_each_other() {
    let set1: CustomSet<()> = CustomSet::new(&[]);
    let set2: CustomSet<()> = CustomSet::new(&[]);
    assert!(set1.is_subset(&set2));
    assert!(set2.is_subset(&set1));
}

#[test]
#[ignore]
fn empty_set_is_subset_of_non_empty_set() {
    let set1 = CustomSet::new(&[]);
    let set2 = CustomSet::new(&[1]);
    assert!(set1.is_subset(&set2));
}

#[test]
#[ignore]
fn non_empty_set_is_not_subset_of_empty_set() {
    let set1 = CustomSet::new(&[1]);
    let set2 = CustomSet::new(&[]);
    assert!(!set1.is_subset(&set2));
}

#[test]
#[ignore]
fn sets_with_same_elements_are_subsets() {
    let set1 = CustomSet::new(&[1, 2, 3]);
    let set2 = CustomSet::new(&[1, 2, 3]);
    assert!(set1.is_subset(&set2));
    assert!(set2.is_subset(&set1));
}

#[test]
#[ignore]
fn set_contained_in_other_set_is_a_subset() {
    let set1 = CustomSet::new(&[1, 2, 3]);
    let set2 = CustomSet::new(&[4, 1, 2, 3]);
    assert!(set1.is_subset(&set2));
}

#[test]
#[ignore]
fn set_not_contained_in_other_set_is_not_a_subset_one() {
    let set1 = CustomSet::new(&[1, 2, 3]);
    let set2 = CustomSet::new(&[4, 1, 3]);
    assert!(!set1.is_subset(&set2));
}

#[test]
#[ignore]
fn empty_sets_are_disjoint_with_each_other() {
    let set1: CustomSet<()> = CustomSet::new(&[]);
    let set2: CustomSet<()> = CustomSet::new(&[]);
    assert!(set1.is_disjoint(&set2));
    assert!(set2.is_disjoint(&set1));
}

#[test]
#[ignore]
fn empty_set_disjoint_with_non_empty_set() {
    let set1 = CustomSet::new(&[]);
    let set2 = CustomSet::new(&[1]);
    assert!(set1.is_disjoint(&set2));
}

#[test]
#[ignore]
fn non_empty_set_disjoint_with_empty_set() {
    let set1 = CustomSet::new(&[1]);
    let set2 = CustomSet::new(&[]);
    assert!(set1.is_disjoint(&set2));
}

#[test]
#[ignore]
fn sets_with_one_element_in_common_are_not_disjoint() {
    let set1 = CustomSet::new(&[1, 2]);
    let set2 = CustomSet::new(&[2, 3]);
    assert!(!set1.is_disjoint(&set2));
    assert!(!set2.is_disjoint(&set1));
}

#[test]
#[ignore]
fn sets_with_no_elements_in_common_are_disjoint() {
    let set1 = CustomSet::new(&[1, 2]);
    let set2 = CustomSet::new(&[3, 4]);
    assert!(set1.is_disjoint(&set2));
    assert!(set2.is_disjoint(&set1));
}

#[test]
#[ignore]
fn empty_sets_are_equal() {
    let set1: CustomSet<()> = CustomSet::new(&[]);
    let set2: CustomSet<()> = CustomSet::new(&[]);
    assert_eq!(set1, set2);
}

#[test]
#[ignore]
fn empty_set_is_not_equal_to_a_non_empty_set() {
    let set1 = CustomSet::new(&[]);
    let set2 = CustomSet::new(&[1, 2, 3]);
    assert_ne!(set1, set2);
}

#[test]
#[ignore]
fn non_empty_set_is_not_equal_to_an_empty_set() {
    let set1 = CustomSet::new(&[1, 2, 3]);
    let set2 = CustomSet::new(&[]);
    assert_ne!(set1, set2);
}

#[test]
#[ignore]
fn sets_with_the_same_elements_are_equal() {
    let set1 = CustomSet::new(&[1, 2]);
    let set2 = CustomSet::new(&[2, 1]);
    assert_eq!(set1, set2);
}

#[test]
#[ignore]
fn sets_with_different_elements_are_not_equal() {
    let set1 = CustomSet::new(&[1, 2, 3]);
    let set2 = CustomSet::new(&[2, 1, 4]);
    assert_ne!(set1, set2);
}

#[test]
#[ignore]
fn add_to_empty_set() {
    let mut set = CustomSet::new(&[]);
    set.add(3);
    assert_eq!(set, CustomSet::new(&[3]));
}

#[test]
#[ignore]
fn add_to_non_empty_set() {
    let mut set = CustomSet::new(&[1, 2, 4]);
    set.add(3);
    assert_eq!(set, CustomSet::new(&[1, 2, 3, 4]));
}

#[test]
#[ignore]
fn add_existing_element() {
    let mut set = CustomSet::new(&[1, 2, 3]);
    set.add(3);
    assert_eq!(set, CustomSet::new(&[1, 2, 3]));
}

#[test]
#[ignore]
fn intersecting_empty_sets_return_empty_set() {
    let set1: CustomSet<()> = CustomSet::new(&[]);
    let set2: CustomSet<()> = CustomSet::new(&[]);
    assert_eq!(set1.intersection(&set2), CustomSet::new(&[]));
}

#[test]
#[ignore]
fn intersecting_empty_set_with_non_empty_returns_empty_set() {
    let set1 = CustomSet::new(&[]);
    let set2 = CustomSet::new(&[3, 2, 5]);
    assert_eq!(set1.intersection(&set2), CustomSet::new(&[]));
}

#[test]
#[ignore]
fn intersecting_non_empty_set_with_empty_returns_empty_set() {
    let set1 = CustomSet::new(&[1, 2, 3, 4]);
    let set2 = CustomSet::new(&[]);
    assert_eq!(set1.intersection(&set2), CustomSet::new(&[]));
}

#[test]
#[ignore]
fn intersection_of_two_sets_with_no_shared_elements_is_an_empty_set() {
    let set1 = CustomSet::new(&[1, 2, 3]);
    let set2 = CustomSet::new(&[4, 5, 6]);
    assert_eq!(set1.intersection(&set2), CustomSet::new(&[]));
    assert_eq!(set2.intersection(&set1), CustomSet::new(&[]));
}

#[test]
#[ignore]
fn intersection_of_two_sets_with_shared_elements_is_a_set_of_the_shared_elements() {
    let set1 = CustomSet::new(&[1, 2, 3, 4]);
    let set2 = CustomSet::new(&[3, 2, 5]);
    assert_eq!(set1.intersection(&set2), CustomSet::new(&[2, 3]));
    assert_eq!(set2.intersection(&set1), CustomSet::new(&[2, 3]));
}

#[test]
#[ignore]
fn difference_of_two_empty_sets_is_empty_set() {
    let set1: CustomSet<()> = CustomSet::new(&[]);
    let set2: CustomSet<()> = CustomSet::new(&[]);
    assert_eq!(set1.difference(&set2), CustomSet::new(&[]));
}

#[test]
#[ignore]
fn difference_of_an_empty_and_non_empty_set_is_an_empty_set() {
    let set1 = CustomSet::new(&[]);
    let set2 = CustomSet::new(&[3, 2, 5]);
    assert_eq!(set1.difference(&set2), CustomSet::new(&[]));
}

#[test]
#[ignore]
fn difference_of_a_non_empty_set_and_empty_set_is_the_non_empty_set() {
    let set1 = CustomSet::new(&[1, 2, 3, 4]);
    let set2 = CustomSet::new(&[]);
    assert_eq!(set1.difference(&set2), CustomSet::new(&[1, 2, 3, 4]));
}

#[test]
#[ignore]
fn difference_of_two_non_empty_sets_is_elements_only_in_first_set_one() {
    let set1 = CustomSet::new(&[3, 2, 1]);
    let set2 = CustomSet::new(&[2, 4]);
    assert_eq!(set1.difference(&set2), CustomSet::new(&[1, 3]));
}

#[test]
#[ignore]
fn union_of_two_empty_sets_is_empty_set() {
    let set1: CustomSet<()> = CustomSet::new(&[]);
    let set2: CustomSet<()> = CustomSet::new(&[]);
    assert_eq!(set1.union(&set2), CustomSet::new(&[]));
}

#[test]
#[ignore]
fn union_of_empty_set_and_non_empty_set_is_all_elements() {
    let set1 = CustomSet::new(&[]);
    let set2 = CustomSet::new(&[2]);
    assert_eq!(set1.union(&set2), CustomSet::new(&[2]));
}

#[test]
#[ignore]
fn union_of_non_empty_set_and_empty_set_is_the_non_empty_set() {
    let set1 = CustomSet::new(&[1, 3]);
    let set2 = CustomSet::new(&[]);
    assert_eq!(set1.union(&set2), CustomSet::new(&[1, 3]));
}

#[test]
#[ignore]
fn union_of_non_empty_sets_contains_all_unique_elements() {
    let set1 = CustomSet::new(&[1, 3]);
    let set2 = CustomSet::new(&[2, 3]);
    assert_eq!(set1.union(&set2), CustomSet::new(&[3, 2, 1]));
}
#[derive(Debug, PartialEq)]
pub struct CustomSet<T> {
    data: Vec<T>
}

impl<T> CustomSet<T> 
where 
    T: PartialEq + Clone + Ord
{
    pub fn new(_input: &[T]) -> Self {
        let mut v = _input.to_vec();
        v.sort_unstable();
        v.dedup();
        Self {
            data: v
        }
    }

    fn uniqueify(&mut self) {
        self.data.sort_unstable();
        self.data.dedup();
    }

    pub fn contains(&self, _element: &T) -> bool {
        self.data.contains(_element)
    }

    pub fn add(&mut self, _element: T) {
        self.data.push(_element);
        self.uniqueify();
    }

    pub fn is_subset(&self, _other: &Self) -> bool {
        self.data.iter()
            .all(|x| _other.contains(x))
    }

    pub fn is_empty(&self) -> bool {
        self.data.is_empty()
    }

    pub fn is_disjoint(&self, _other: &Self) -> bool {
        !self.data.iter().any(|x| _other.contains(x))
    }

    pub fn intersection(&self, _other: &Self) -> Self {
        Self::new(
            &self.data.iter()
                .cloned()
                .filter(|x| _other.contains(x))
                .collect::<Vec<T>>()
        )
    }

    pub fn difference(&self, _other: &Self) -> Self {
        Self::new(
            &self.data.iter()
                .cloned()
                .filter(|x| !_other.contains(x))
                .collect::<Vec<T>>()
        )
    }

    pub fn union(&self, _other: &Self) -> Self {
        Self::new(
            &self.data.iter()
                .chain(_other.data.iter())
                .cloned()
                .collect::<Vec<T>>()
        )
    }
}

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?