Skip to content

Exercise allergies test case #1969

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 28, 2022
Merged

Conversation

karstenj
Copy link
Contributor

Added test case. As mentioned in instructions(https://github.com/karstenj/problem-specifications/blob/main/exercises/allergies/description.md) the program shall only report the eggs (1) allergy for a given score of 257. This case is currently not fully covered by the existing test cases.

Added test case
As mentioned in instructions the program shall only report the eggs (1) allergy for a given score of 257.
@wolf99
Copy link
Contributor

wolf99 commented Feb 24, 2022

Nice!!

I'm not sure about the description. I understand the teat data but I think my brain just isn't parsing the description correctly. Can you comment with an explanation?

@karstenj
Copy link
Contributor Author

Hi @wolf99 ,

yes, sure. The score is build out of a bit vector. Each bit represents an allergy. This is the list of possible values, where the bits are already converted to an integer:

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

In the new test case the score is generated out of: Bit 8 = 256 and Bit 0 = 1. This results in the given input score of 257. Due to the fact, that the Bit8(256) is not assigned to an allergy it shall be ignored and only "egg" shall be returned in the list of allergies. Compared to the existing test "no allergen score parts", the highest valid bit Bit7 is not set in the input score.

The main rational for adding this test was also, that this input was mentioned in the instructions(https://github.com/karstenj/problem-specifications/blob/main/exercises/allergies/description.md) , but not considered in the test.

@angelikatyborska
Copy link
Member

@karstenj

Compared to the existing test "no allergen score parts", the highest valid bit Bit7 is not set in the input score.

Does this really make a difference? Are there implementations that would pass the existing test for 509 (0001 1111 1101) and all the other tests, but wouldn't pass for 257 (0001 0000 0001)?

@karstenj
Copy link
Contributor Author

@angelikatyborska, not a big, but it makes a differnce. I had one student who provided a solution where the 509 was succesfully executed, but the 257 would fail. The failure was not observed, because the highest valid bit(128) is included in the 509. The 257 does not include this bit.

@angelikatyborska
Copy link
Member

Alright, in that case it's a useful change. I'm approving.

I had one student who provided a solution where the 509 was succesfully executed, but the 257 would fail.

Could you show us that solution? I'm really curious 😅

@karstenj
Copy link
Contributor Author

@angelikatyborska
Copy link
Member

@karstenj I am unable to access this link because mentoring discussions are private, you would need to copy-paste the code.

@karstenj
Copy link
Contributor Author

karstenj commented Feb 25, 2022

#include "allergies.h"
#include <string.h>
#include <stdio.h>  

/*
subtract numbers until you exhaust the score.
flag the subtracted number along with increasing count.
*/

static int find_less_or_equal(int *arr, int len, int num) {
    // find a index in the array which is less that or eual to number
    for (int i = len-1; i >= 0 ; --i)
        if (arr[i] <= num)
            return i;
    return -1;
}

allergen_list_t get_allergens(int score) {
    int scores[] = {1, 2, 4, 8, 16, 32, 64, 128};
    allergen_list_t my_list;
    memset(my_list.allergens, 0, sizeof(my_list.allergens));
    my_list.count = 0;

    int temp = score;
    while (temp > 0) {
        int idx = find_less_or_equal(scores, ALLERGEN_COUNT, temp); // find the one to subtract
        if (!my_list.allergens[idx]) {
            my_list.allergens[idx] = 1;
            my_list.count++;
        }
        temp -= scores[idx];
    }
    return my_list;
}

bool is_allergic_to(allergen_t a, int score) {
    allergen_list_t result = get_allergens(score);
    if (result.allergens[a])
        return true;
    return false;
}





#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;

allergen_list_t get_allergens(int score);
bool is_allergic_to(allergen_t a, int score);

#endif

Edit: Place code in fence for highlighting.

@wolf99
Copy link
Contributor

wolf99 commented Feb 28, 2022

ping @exercism/reviewers 🙂

@SaschaMann SaschaMann merged commit 3bed97a into exercism:main Feb 28, 2022
petertseng added a commit to petertseng/exercism-problem-specifications that referenced this pull request Mar 4, 2022
race condition between
"Exercise allergies test case"
exercism#1969
and
"allow prettier to format more files"
exercism#1966

Since this file is now being formatted by prettier, the test case added
in the latter PR must be formatted according to prettier.

There are alternate approaches, such as directing format-array to always
format `expected` with one item on each line, but it doesn't seem like
this is particular important for the `allergies` exercise.
petertseng added a commit to petertseng/exercism-problem-specifications that referenced this pull request Mar 4, 2022
race condition between
"Exercise allergies test case"
exercism#1969
and
"allow prettier to format more files"
exercism#1966

Since this file is now being formatted by prettier, the test case added
in the latter PR must be formatted according to prettier.

Otherwise, as will be observable from any PR after
exercism#1966, CI fails
on main.

There are alternate approaches, such as directing format-array to always
format `expected` with one item on each line, but it doesn't seem like
this is particular important for the `allergies` exercise.
@petertseng petertseng mentioned this pull request Mar 4, 2022
SaschaMann pushed a commit that referenced this pull request Mar 4, 2022
race condition between
"Exercise allergies test case"
#1969
and
"allow prettier to format more files"
#1966

Since this file is now being formatted by prettier, the test case added
in the latter PR must be formatted according to prettier.

Otherwise, as will be observable from any PR after
#1966, CI fails
on main.

There are alternate approaches, such as directing format-array to always
format `expected` with one item on each line, but it doesn't seem like
this is particular important for the `allergies` exercise.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants