Allergies

1. Readme

过敏

给出一个人的过敏分,确定他们是否对某一物品过敏,以及他们的过敏列表.

过敏测试产生单个数字分数,其中包含有关该人所有过敏的信息(他们进行了测试)。

测试的物品列表(及其值)为:

  • 鸡蛋(1)
  • 花生(2)
  • 贝类(4)
  • 草莓(8)
  • 西红柿(16)
  • 巧克力(32)
  • 花粉(64)
  • 猫(128)

因此,如果汤姆对花生和巧克力过敏,他会得到 34 分.

现在,只要得到 34 分,你的程序应该说:

  • 汤姆是否对上面列出的任何一种过敏原过敏.
  • 汤姆所有过敏原。

注意:给出的分数可能包括上面列出的过敏原(即分数为 256,512,1024 等的过敏原)。您的程序应忽略这些组成部分。例如,如果过敏分数是 257,您的程序应该只报告鸡蛋(1)过敏。

资源

Jumpstart 实验室热身http://jumpstartlab.com

2. 开始你的表演

pub struct Allergies;

#[derive(Debug, PartialEq)]
pub enum Allergen {
   Eggs,
   Peanuts,
   Shellfish,
   Strawberries,
   Tomatoes,
   Chocolate,
   Pollen,
   Cats,
}

impl Allergies {
   pub fn new(score: u32) -> Self {
       unimplemented!(
           "Given the '{}' score, construct a new Allergies struct.",
           score
       );
   }

   pub fn is_allergic_to(&self, allergen: &Allergen) -> bool {
       unimplemented!(
           "Determine if the patient is allergic to the '{:?}' allergen.",
           allergen
       );
   }

   pub fn allergies(&self) -> Vec<Allergen> {
       unimplemented!("Return the list of allergens contained within the score with which the Allergies struct was made.");
   }
}

3. 测试代码查看


# #![allow(unused_variables)]
#fn main() {
fn compare_allergy_vectors(expected: &[Allergen], actual: &[Allergen]) {
   for element in expected {
       if !actual.contains(element) {
           panic!(
               "Allergen missing\n  {:?} should be in {:?}",
               element, actual
           );
       }
   }

   if actual.len() != expected.len() {
       panic!(
           "Allergy vectors are of different lengths\n  expected {:?}\n  got {:?}",
           expected, actual
       );
   }
}

#[test]
fn is_not_allergic_to_anything() {
   let allergies = Allergies::new(0);
   assert!(!allergies.is_allergic_to(&Allergen::Peanuts));
   assert!(!allergies.is_allergic_to(&Allergen::Cats));
   assert!(!allergies.is_allergic_to(&Allergen::Strawberries));
}

#[test]
//#[ignore]
fn is_allergic_to_eggs() {
   assert!(Allergies::new(1).is_allergic_to(&Allergen::Eggs));
}

#[test]
//#[ignore]
fn is_allergic_to_egg_shellfish_and_strawberries() {
   let allergies = Allergies::new(5);
   assert!(allergies.is_allergic_to(&Allergen::Eggs));
   assert!(allergies.is_allergic_to(&Allergen::Shellfish));
   assert!(!allergies.is_allergic_to(&Allergen::Strawberries));
}

#[test]
//#[ignore]
fn no_allergies_at_all() {
   let expected = &[];
   let allergies = Allergies::new(0).allergies();

   compare_allergy_vectors(expected, &allergies);
}

#[test]
//#[ignore]
fn allergic_to_just_eggs() {
   let expected = &[Allergen::Eggs];
   let allergies = Allergies::new(1).allergies();

   compare_allergy_vectors(expected, &allergies);
}

#[test]
//#[ignore]
fn allergic_to_just_peanuts() {
   let expected = &[Allergen::Peanuts];
   let allergies = Allergies::new(2).allergies();

   compare_allergy_vectors(expected, &allergies);
}

#[test]
//#[ignore]
fn allergic_to_just_strawberries() {
   let expected = &[Allergen::Strawberries];
   let allergies = Allergies::new(8).allergies();

   compare_allergy_vectors(expected, &allergies);
}

#[test]
//#[ignore]
fn allergic_to_eggs_and_peanuts() {
   let expected = &[Allergen::Eggs, Allergen::Peanuts];
   let allergies = Allergies::new(3).allergies();

   compare_allergy_vectors(expected, &allergies);
}

#[test]
//#[ignore]
fn allergic_to_eggs_and_shellfish() {
   let expected = &[Allergen::Eggs, Allergen::Shellfish];
   let allergies = Allergies::new(5).allergies();

   compare_allergy_vectors(expected, &allergies);
}

#[test]
//#[ignore]
fn allergic_to_many_things() {
   let expected = &[
       Allergen::Strawberries,
       Allergen::Tomatoes,
       Allergen::Chocolate,
       Allergen::Pollen,
       Allergen::Cats,
   ];
   let allergies = Allergies::new(248).allergies();

   compare_allergy_vectors(expected, &allergies);
}

#[test]
//#[ignore]
fn allergic_to_everything() {
   let expected = &[
       Allergen::Eggs,
       Allergen::Peanuts,
       Allergen::Shellfish,
       Allergen::Strawberries,
       Allergen::Tomatoes,
       Allergen::Chocolate,
       Allergen::Pollen,
       Allergen::Cats,
   ];
   let allergies = Allergies::new(255).allergies();

   compare_allergy_vectors(expected, &allergies);
}

#[test]
//#[ignore]
fn scores_over_255_do_not_trigger_false_positives() {
   let expected = &[
       Allergen::Eggs,
       Allergen::Shellfish,
       Allergen::Strawberries,
       Allergen::Tomatoes,
       Allergen::Chocolate,
       Allergen::Pollen,
       Allergen::Cats,
   ];
   let allergies = Allergies::new(509).allergies();

   compare_allergy_vectors(expected, &allergies);
}

#}

4. 答案


# #![allow(unused_variables)]
#fn main() {
pub struct Allergies(pub usize);

#[derive(PartialEq, Debug)]
pub enum Allergen {
   Eggs,
   Peanuts,
   Shellfish,
   Strawberries,
   Tomatoes,
   Chocolate,
   Pollen,
   Cats,
}

impl Allergies {
   pub fn new(score: usize) -> Allergies {
       Allergies(score)
   }

   pub fn is_allergic_to(&self, allergen: &Allergen) -> bool {
       let allergens = Allergies::allergens();
       let index = allergens
           .iter()
           .position(|x: &Allergen| x == allergen)
           .unwrap();
       (self.0 & (1 << index)) != 0
   }

   pub fn allergies(&self) -> Vec<Allergen> {
       Allergies::allergens()
           .into_iter()
           .filter(|allergen| self.is_allergic_to(allergen))
           .collect()
   }

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

#}



填充/相关