-
-
Notifications
You must be signed in to change notification settings - Fork 554
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
Conversation
Added test case As mentioned in instructions the program shall only report the eggs (1) allergy for a given score of 257.
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? |
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:
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. |
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)? |
@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. |
Alright, in that case it's a useful change. I'm approving.
Could you show us that solution? I'm really curious 😅 |
Yes, you can have a look https://exercism.org/mentoring/discussions/b93c6b3f7e9d44d7809d54db691208d0 |
@karstenj I am unable to access this link because mentoring discussions are private, you would need to copy-paste the code. |
#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. |
ping @exercism/reviewers 🙂 |
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.
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.
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.
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.