Exercism v3 launches on Sept 1st 2021. Learn more! ๐Ÿš€๐Ÿš€๐Ÿš€
Avatar of katrinleinweber

katrinleinweber's solution

to Allergies in the R Track

Published at Jul 13 2018 · 3 comments
Test suite

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.


See this guide for instructions on how to setup your local R environment.

How to implement your solution

In each problem folder, there is a file named <exercise_name>.R containing a function that returns a NULL value. Place your implementation inside the body of the function.

How to run tests

Inside of RStudio, simply execute the test_<exercise_name>.R script. This can be conveniently done with testthat's auto_test function. Because exercism code and tests are in the same folder, use this same path for both code_path and test_path parameters. On the command-line, you can also run Rscript test_<exercise_name>.R.


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_that("no allergies means not allergic", {
  x <- allergy(0)
  expect_false(allergic_to(x, "peanuts"))
  expect_false(allergic_to(x, "cats"))
  expect_false(allergic_to(x, "strawberries"))

test_that("is allergic to eggs", {
  x <- allergy(1)
  expect_true(allergic_to(x, "eggs"))

test_that("allergic to eggs in addition to other stuff", {
  x <- allergy(5)
  expect_true(allergic_to(x, "eggs"))
  expect_true(allergic_to(x, "shellfish"))
  expect_false(allergic_to(x, "strawberries"))

test_that("no allergies at all", {
  x <- allergy(0)
  expect_equal(list_allergies(x), character())

test_that("allergic to just eggs", {
  x <- allergy(1)
  expect_equal(list_allergies(x), c("eggs"))

test_that("allergic to just peanuts", {
  x <- allergy(2)
  expect_equal(list_allergies(x), c("peanuts"))

test_that("allergic to just strawberries", {
  x <- allergy(8)
  expect_equal(list_allergies(x), c("strawberries"))

test_that("allergic to eggs and peanuts", {
  x <- allergy(3)
    c("eggs", "peanuts"))

test_that("allergic to more than eggs but not peanuts", {
  x <- allergy(5)
    c("eggs", "shellfish"))

test_that("allergic to lots of stuff", {
  x <- allergy(248)
    c("strawberries", "tomatoes", "chocolate", "pollen", "cats"))

test_that("allergic to everything", {
  x <- allergy(255)
    c("eggs", "peanuts", "shellfish", "strawberries", "tomatoes", 
      "chocolate", "pollen", "cats")))

test_that("ignore non allergen score parts", {
  x <- allergy(509)
    c("eggs", "shellfish", "strawberries", "tomatoes", 
      "chocolate", "pollen", "cats"))

message("All tests passed for exercise: allergies")
allergy <- function(num) {
  comparison_mask <- as.logical(intToBits(num))
  # construct list
  allergy_list <- c(
    "eggs",          # 2
    "peanuts",       # 4
    "shellfish",     # 8
    "strawberries",  # 16
    "tomatoes",      # 32
    "chocolate",     # 64
    "pollen",        # 128
    "cats"           # 256
  # match places with 1
  allergies <- allergy_list[comparison_mask]

allergic_to <- function(allergy_object, allergy) {
  # check element in list
  allergy %in% allergy_object

list_allergies <- function(allergy_object) {
  # pass list through

Community comments

Find this solution interesting? Ask the author a question to learn more.
Avatar of mstn

Thanks for sharing your solution. Helped me a lot. Could you tell me what the rawToChar step is for?

Avatar of katrinleinweber

Thanks or the feedback :-) rawToChar is only "needed", because I use binary[i] == "" to compare which allergy to append.

It's not particularly elegant, TBH. Will push another solution version... Done

For posterity's sake, the weirder, previous version was:

3   binary <- rawToChar(intToBits(num), multiple = TRUE)
19  # match places with 1
20  for (i in seq(length(allergy_list), 1))
21    if (binary[i] == "")
22      allergy_list <- allergy_list[-i]
24   return(allergies)
(edited over 1 year ago)
Avatar of mstn

Thanks for the explanation!

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?