Avatar of ChrisNguyenAE

ChrisNguyenAE's solution

to Allergies in the C Track

Published at Jun 11 2019 · 0 comments
Instructions
Test suite
Solution

Note:

This exercise has changed since this solution was written.

Given a person's allergy score, determine whether or not they're allergic to a given item, and their full list of allergies.

An allergy test produces a single numeric score which contains the information about all the allergies the person has (that they were tested for).

The list of items (and their value) that were tested are:

  • eggs (1)
  • peanuts (2)
  • shellfish (4)
  • strawberries (8)
  • tomatoes (16)
  • chocolate (32)
  • pollen (64)
  • cats (128)

So if Tom is allergic to peanuts and chocolate, he gets a score of 34.

Now, given just that score of 34, your program should be able to say:

  • Whether Tom is allergic to any one of those allergens listed above.
  • All the allergens Tom is allergic to.

Note: a given score may include allergens not listed above (i.e. allergens that score 256, 512, 1024, etc.). Your program should ignore those components of the score. For example, if the allergy score is 257, your program should only report the eggs (1) allergy.

Getting Started

Make sure you have read the "Guides" section of the C track on the Exercism site. This covers the basic information on setting up the development environment expected by the exercises.

Passing the Tests

Get the first test compiling, linking and passing by following the three rules of test-driven development.

The included makefile can be used to create and run the tests using the test task.

make test

Create just the functions you need to satisfy any compiler errors and get the test to fail. Then write just enough code to get the test to pass. Once you've done that, move onto the next test.

As you progress through the tests, take the time to refactor your implementation for readability and expressiveness and then go on to the next test.

Try to use standard C99 facilities in preference to writing your own low-level algorithms or facilities by hand.

Source

Jumpstart Lab Warm-up http://jumpstartlab.com

Submitting Incomplete Solutions

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

test_allergies.c

#include "vendor/unity.h"
#include "../src/allergies.h"
#include <stdlib.h>

void setUp(void)
{
}

void tearDown(void)
{
}

void test_no_allergies_means_not_allergic(void)
{
   unsigned int score = 0;

   TEST_ASSERT_FALSE(is_allergic_to(ALLERGEN_PEANUTS, score));
   TEST_ASSERT_FALSE(is_allergic_to(ALLERGEN_CATS, score));
   TEST_ASSERT_FALSE(is_allergic_to(ALLERGEN_STRAWBERRIES, score));
}

void test_is_allergic_to_eggs(void)
{
   TEST_IGNORE();               // delete this line to run test
   unsigned int score = 1;

   TEST_ASSERT_TRUE(is_allergic_to(ALLERGEN_EGGS, score));
}

void test_is_allergic_to_eggs_in_addition_to_other_stuff(void)
{
   TEST_IGNORE();
   unsigned int score = 5;

   TEST_ASSERT_TRUE(is_allergic_to(ALLERGEN_EGGS, score));
   TEST_ASSERT_TRUE(is_allergic_to(ALLERGEN_SHELLFISH, score));
   TEST_ASSERT_FALSE(is_allergic_to(ALLERGEN_STRAWBERRIES, score));
}

void test_no_allergies_at_all(void)
{
   TEST_IGNORE();
   unsigned int score = 0;
   allergen_list_t list = get_allergens(score);

   TEST_ASSERT_EQUAL(0, list.count);
}

void test_allergic_to_just_eggs(void)
{
   TEST_IGNORE();
   allergen_list_t list = get_allergens(1);

   TEST_ASSERT_EQUAL(1, list.count);
   TEST_ASSERT_TRUE(list.allergens[ALLERGEN_EGGS]);
}

void test_allergic_to_just_peanuts(void)
{
   TEST_IGNORE();
   allergen_list_t list = get_allergens(2);

   TEST_ASSERT_EQUAL(1, list.count);
   TEST_ASSERT_TRUE(list.allergens[ALLERGEN_PEANUTS]);
}

void test_allergic_to_just_strawberries(void)
{
   TEST_IGNORE();
   allergen_list_t list = get_allergens(8);

   TEST_ASSERT_EQUAL(1, list.count);
   TEST_ASSERT_TRUE(list.allergens[ALLERGEN_STRAWBERRIES]);
}

void test_allergic_to_eggs_and_peanuts(void)
{
   TEST_IGNORE();
   allergen_list_t list = get_allergens(3);

   TEST_ASSERT_EQUAL(2, list.count);
   TEST_ASSERT_TRUE(list.allergens[ALLERGEN_EGGS]);
   TEST_ASSERT_TRUE(list.allergens[ALLERGEN_PEANUTS]);
}

void test_allergic_to_more_than_eggs_but_not_peanuts(void)
{
   TEST_IGNORE();
   allergen_list_t list = get_allergens(5);

   TEST_ASSERT_EQUAL(2, list.count);
   TEST_ASSERT_TRUE(list.allergens[ALLERGEN_EGGS]);
   TEST_ASSERT_TRUE(list.allergens[ALLERGEN_SHELLFISH]);
}

void test_allergic_to_lots_of_stuff(void)
{
   TEST_IGNORE();
   allergen_list_t list = get_allergens(248);

   TEST_ASSERT_EQUAL(5, list.count);
   TEST_ASSERT_TRUE(list.allergens[ALLERGEN_STRAWBERRIES]);
   TEST_ASSERT_TRUE(list.allergens[ALLERGEN_TOMATOES]);
   TEST_ASSERT_TRUE(list.allergens[ALLERGEN_CHOCOLATE]);
   TEST_ASSERT_TRUE(list.allergens[ALLERGEN_POLLEN]);
   TEST_ASSERT_TRUE(list.allergens[ALLERGEN_CATS]);
}

void test_allergic_to_everything(void)
{
   TEST_IGNORE();
   allergen_list_t list = get_allergens(255);

   TEST_ASSERT_EQUAL(8, list.count);
   TEST_ASSERT_TRUE(list.allergens[ALLERGEN_EGGS]);
   TEST_ASSERT_TRUE(list.allergens[ALLERGEN_PEANUTS]);
   TEST_ASSERT_TRUE(list.allergens[ALLERGEN_SHELLFISH]);
   TEST_ASSERT_TRUE(list.allergens[ALLERGEN_STRAWBERRIES]);
   TEST_ASSERT_TRUE(list.allergens[ALLERGEN_TOMATOES]);
   TEST_ASSERT_TRUE(list.allergens[ALLERGEN_CHOCOLATE]);
   TEST_ASSERT_TRUE(list.allergens[ALLERGEN_POLLEN]);
   TEST_ASSERT_TRUE(list.allergens[ALLERGEN_CATS]);
}

void test_ignore_non_allergen_score_parts(void)
{
   TEST_IGNORE();
   allergen_list_t list = get_allergens(509);

   TEST_ASSERT_EQUAL(7, list.count);
   TEST_ASSERT_TRUE(list.allergens[ALLERGEN_EGGS]);
   TEST_ASSERT_TRUE(list.allergens[ALLERGEN_SHELLFISH]);
   TEST_ASSERT_TRUE(list.allergens[ALLERGEN_STRAWBERRIES]);
   TEST_ASSERT_TRUE(list.allergens[ALLERGEN_TOMATOES]);
   TEST_ASSERT_TRUE(list.allergens[ALLERGEN_CHOCOLATE]);
   TEST_ASSERT_TRUE(list.allergens[ALLERGEN_POLLEN]);
   TEST_ASSERT_TRUE(list.allergens[ALLERGEN_CATS]);
}

int main(void)
{
   UnityBegin("test/test_allergies.c");

   RUN_TEST(test_no_allergies_means_not_allergic);
   RUN_TEST(test_is_allergic_to_eggs);
   RUN_TEST(test_is_allergic_to_eggs_in_addition_to_other_stuff);
   RUN_TEST(test_no_allergies_at_all);
   RUN_TEST(test_allergic_to_just_eggs);
   RUN_TEST(test_allergic_to_just_peanuts);
   RUN_TEST(test_allergic_to_just_strawberries);
   RUN_TEST(test_allergic_to_eggs_and_peanuts);
   RUN_TEST(test_allergic_to_more_than_eggs_but_not_peanuts);
   RUN_TEST(test_allergic_to_lots_of_stuff);
   RUN_TEST(test_allergic_to_everything);
   RUN_TEST(test_ignore_non_allergen_score_parts);

   return UnityEnd();
}

src/allergies.c

#include "allergies.h"
#include <stdlib.h>

bool is_allergic_to(allergen_t allergen, int score){
	return score & (1<<allergen);
}
allergen_list_t *get_allergens(int score){
	allergen_list_t *allergen_list = (allergen_list_t*)malloc(sizeof(allergen_list_t));
	for (int i = 0; i < ALLERGEN_COUNT; i++){
		if (is_allergic_to((allergen_t)i, score)) {
			allergen_list->count +=1;
			allergen_list->allergens[i] = true;
		}
		else
			allergen_list->allergens[i] = false;
	}

	return allergen_list;
}

src/allergies.h

#ifndef ALLERGIES_H
#define ALLERGIES_H

#include <stdbool.h>

typedef enum {
   ALLERGEN_EGGS = 0,
   ALLERGEN_PEANUTS,
   ALLERGEN_SHELLFISH,
   ALLERGEN_STRAWBERRIES,
   ALLERGEN_TOMATOES,
   ALLERGEN_CHOCOLATE,
   ALLERGEN_POLLEN,
   ALLERGEN_CATS,
   ALLERGEN_COUNT,
} allergen_t;

typedef struct {
   int count;
   bool allergens[ALLERGEN_COUNT];
} allergen_list_t;

bool is_allergic_to(allergen_t allergen, int score);
allergen_list_t *get_allergens(int score);
#endif

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?