Custom Set

1. Readme

自定义集合

创建自定义 set 类型。

有时需要定义某种类型的自定义数据结构,例如一个集合(set)。在本练习中,您将定义自己的集合。它的内部工作原理无关紧要,只要它的行为就像一组唯一元素的集合。

2. 开始你的表演

use std::fmt::Debug;
use std::marker::PhantomData;

#[derive(Debug, PartialEq)]
pub struct CustomSet<T> {
   // This field is here to make the template compile and not to
   // complain about unused type parameter 'T'. Once you start
   // solving the exercise, delete this field and the 'std::marker::PhantomData'
   // import.
   phantom: PhantomData<T>,
}

impl<T: Debug> CustomSet<T> {
   pub fn new(input: &[T]) -> Self {
       unimplemented!(
           "From the given input '{:?}' construct a new CustomSet struct.",
           input
       );
   }

   pub fn contains(&self, element: &T) -> bool {
       unimplemented!(
           "Determine if the '{:?}' element is present in the CustomSet struct.",
           element
       );
   }

   pub fn add(&mut self, element: T) {
       unimplemented!("Add the '{:?}' element to the CustomSet struct.", element);
   }

   pub fn is_subset(&self, other: &Self) -> bool {
       unimplemented!(
           "Determine if the CustomSet struct is a subset of the other '{:?}' struct.",
           other
       );
   }

   pub fn is_empty(&self) -> bool {
       unimplemented!("Determine if the CustomSet struct is empty.");
   }

   pub fn is_disjoint(&self, other: &Self) -> bool {
       unimplemented!(
           "Determine if the CustomSet struct and the other struct '{:?}' are disjoint.",
           other
       );
   }

   pub fn intersection(&self, other: &Self) -> Self {
       unimplemented!("Construct a new CustomSet struct that is an intersection between current struct and the other struct '{:?}'.", other);
   }

   pub fn difference(&self, other: &Self) -> Self {
       unimplemented!("Construct a new CustomSet struct that is a difference between current struct and the other struct '{:?}'.", other);
   }

   pub fn union(&self, other: &Self) -> Self {
       unimplemented!("Construct a new CustomSet struct that is an union between current struct and the other struct '{:?}'.", other);
   }
}

3. 测试代码查看


# #![allow(unused_variables)]
#fn main() {
#[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]));
}

#}

4. 答案


# #![allow(unused_variables)]
#fn main() {
#[derive(Debug)]
pub struct CustomSet<T> {
   collection: Vec<T>,
}

impl<T: Ord + Clone> PartialEq for CustomSet<T> {
   fn eq(&self, other: &Self) -> bool {
       self.collection.iter().all(|x| other.contains(&x))
           && other.collection.iter().all(|x| self.contains(&x))
   }
}

impl<T: Ord + Clone> CustomSet<T> {
   pub fn new(inputs: &[T]) -> CustomSet<T> {
       let mut s = CustomSet {
           collection: Vec::new(),
       };
       for input in inputs {
           s.add(input.clone());
       }
       s
   }

   pub fn add(&mut self, element: T) {
       if !self.contains(&element) {
           self.collection.push(element)
       }
   }

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

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

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

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

   pub fn intersection(&self, other: &Self) -> CustomSet<T> {
       CustomSet::new(&self.collection
           .iter()
           .cloned()
           .filter(|c| other.contains(c))
           .collect::<Vec<_>>())
   }

   pub fn union(&self, other: &Self) -> CustomSet<T> {
       CustomSet::new(&self.collection
           .iter()
           .cloned()
           .chain(other.collection.iter().cloned())
           .collect::<Vec<_>>())
   }

   pub fn difference(&self, other: &Self) -> CustomSet<T> {
       CustomSet::new(&self.collection
           .iter()
           .cloned()
           .filter(|c| !other.contains(c))
           .collect::<Vec<_>>())
   }
}

#}



填充/相关