Skip to content
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

Add 'allergies' exercise #83

Merged
merged 7 commits into from
Oct 29, 2016
Merged

Conversation

0xbarrett
Copy link
Contributor

@0xbarrett
Copy link
Contributor Author

Wow... I have no idea why every test is failing on the Travis build. They all pass on my machine.

@0xbarrett 0xbarrett changed the title Add 'allergies' exercise [WIP] Add 'allergies' exercise Oct 14, 2016
if (is_allergic_to(allergen, score)) {
score -= scores[allergen];
list->allergens[list->count] = allergen;
list->count++;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this guy is never initialized to 0

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep. I originally had get_allergens() mallocing the entire Allergen_List_t and then switched to having it passed in. Will fix.


void get_allergens(int score, Allergen_List_t * list)
{
list->allergens = calloc(0, sizeof(Allergen_t) * Allergen_Last);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Allergen_Last is 7, but there are actually 8 allergens.

I think it will make things a bit simpler if the allergen enum is rearranged like this:

enum {
  Allergen_Eggs,
  Allergen_Peanuts,
  ...
  Allergen_Count
}

Then you can rearrange your for to use allergen < Allergen_Count as the termination condition and use sizeof(Allergen_t) * Allergen_Count as your allocation size.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additionally, the signature for calloc is calloc(<number of items>, <item size>) so I think this should be calloc(Allergen_Last + 1, sizeof(Allergen_t)) (this is probably the reason that all of the tests fail on Travis).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call. I'm sure that's why the tests are failing.


typedef struct {
int count;
Allergen_t *allergens;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that we know exactly how many allergens there are prior to declaring this type, why wouldn't we simply create a properly sized array for allergens instead of requiring a memory allocation?

Copy link
Member

@ryanplusplus ryanplusplus Oct 15, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess leaving this as an allocation means that the implementation could potentially allocate only as many as are actually necessary for the given score.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could go either way. I do like having the option allocate only as many as necessary.

Allergen_t *allergens;
} Allergen_List_t;

bool is_allergic_to(Allergen_t allergen, int score);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't the score be an unsigned value? If you're treating this as a bitfield it will be safer and less confusing to have it be an unsigned.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep. Will fix.

@0xbarrett
Copy link
Contributor Author

Alright, tests are passing now.

} Allergen_List_t;

bool is_allergic_to(Allergen_t allergen, unsigned int score);
void get_allergens(int score, Allergen_List_t * list);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you update this score to be unsigned as well?

@ryanplusplus
Copy link
Member

Looks good once score is updated to be an unsigned

@0xbarrett 0xbarrett changed the title [WIP] Add 'allergies' exercise Add 'allergies' exercise Oct 19, 2016
@0xbarrett
Copy link
Contributor Author

Oops, missed that one... fixed

@ryanplusplus
Copy link
Member

LGTM

@ryanplusplus ryanplusplus merged commit 4d2b3d7 into exercism:master Oct 29, 2016
@0xbarrett 0xbarrett deleted the allergies branch October 31, 2016 12:42
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.

None yet

2 participants