From 31d6cb4823a712a0c62d17ca30d0131b99ca5faa Mon Sep 17 00:00:00 2001 From: Kah Goh Date: Tue, 19 Mar 2024 22:34:21 +0800 Subject: [PATCH 1/3] Add Allergies exercise --- config.json | 8 + .../practice/allergies/.docs/instructions.md | 27 +++ .../practice/allergies/.meta/config.json | 19 ++ exercises/practice/allergies/.meta/example.v | 23 ++ exercises/practice/allergies/.meta/tests.toml | 160 ++++++++++++++ exercises/practice/allergies/allergen.v | 12 ++ exercises/practice/allergies/allergies.v | 10 + exercises/practice/allergies/run_test.v | 203 ++++++++++++++++++ 8 files changed, 462 insertions(+) create mode 100644 exercises/practice/allergies/.docs/instructions.md create mode 100644 exercises/practice/allergies/.meta/config.json create mode 100644 exercises/practice/allergies/.meta/example.v create mode 100644 exercises/practice/allergies/.meta/tests.toml create mode 100644 exercises/practice/allergies/allergen.v create mode 100644 exercises/practice/allergies/allergies.v create mode 100644 exercises/practice/allergies/run_test.v diff --git a/config.json b/config.json index 734d82d..796b245 100644 --- a/config.json +++ b/config.json @@ -702,6 +702,14 @@ "practices": [], "prerequisites": [], "difficulty": 5 + }, + { + "slug": "allergies", + "name": "Allergies", + "uuid": "3a8383d1-b504-470e-b035-f6ceecfbbb28", + "practices": [], + "prerequisites": [], + "difficulty": 2 } ] }, diff --git a/exercises/practice/allergies/.docs/instructions.md b/exercises/practice/allergies/.docs/instructions.md new file mode 100644 index 0000000..daf8cfd --- /dev/null +++ b/exercises/practice/allergies/.docs/instructions.md @@ -0,0 +1,27 @@ +# Instructions + +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. diff --git a/exercises/practice/allergies/.meta/config.json b/exercises/practice/allergies/.meta/config.json new file mode 100644 index 0000000..c21ba37 --- /dev/null +++ b/exercises/practice/allergies/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "kahgoh" + ], + "files": { + "solution": [ + "allergies.v" + ], + "test": [ + "run_test.v" + ], + "example": [ + ".meta/example.v" + ] + }, + "blurb": "Given a person's allergy score, determine whether or not they're allergic to a given item, and their full list of allergies.", + "source": "Exercise by the JumpstartLab team for students at The Turing School of Software and Design.", + "source_url": "https://turing.edu" +} diff --git a/exercises/practice/allergies/.meta/example.v b/exercises/practice/allergies/.meta/example.v new file mode 100644 index 0000000..3690fc5 --- /dev/null +++ b/exercises/practice/allergies/.meta/example.v @@ -0,0 +1,23 @@ +module main + +fn allergic_to(allergen Allergen, score u8) bool { + return mask(allergen) & score > 0 +} + +fn list(score u8) []Allergen { + return [Allergen.eggs, Allergen.peanuts, Allergen.shellfish, Allergen.strawberries, + Allergen.tomatoes, Allergen.chocolate, Allergen.pollen, Allergen.cats].filter(score & mask(it) > 0) +} + +fn mask(allergen Allergen) u8 { + return match allergen { + .eggs { 1 } + .peanuts { 2 } + .shellfish { 4 } + .strawberries { 8 } + .tomatoes { 16 } + .chocolate { 32 } + .pollen { 64 } + .cats { 128 } + } +} diff --git a/exercises/practice/allergies/.meta/tests.toml b/exercises/practice/allergies/.meta/tests.toml new file mode 100644 index 0000000..799ab85 --- /dev/null +++ b/exercises/practice/allergies/.meta/tests.toml @@ -0,0 +1,160 @@ +# This is an auto-generated file. +# +# Regenerating this file via `configlet sync` will: +# - Recreate every `description` key/value pair +# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications +# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) +# - Preserve any other key/value pair +# +# As user-added comments (using the # character) will be removed when this file +# is regenerated, comments can be added via a `comment` key. + +[17fc7296-2440-4ac4-ad7b-d07c321bc5a0] +description = "testing for eggs allergy -> not allergic to anything" + +[07ced27b-1da5-4c2e-8ae2-cb2791437546] +description = "testing for eggs allergy -> allergic only to eggs" + +[5035b954-b6fa-4b9b-a487-dae69d8c5f96] +description = "testing for eggs allergy -> allergic to eggs and something else" + +[64a6a83a-5723-4b5b-a896-663307403310] +description = "testing for eggs allergy -> allergic to something, but not eggs" + +[90c8f484-456b-41c4-82ba-2d08d93231c6] +description = "testing for eggs allergy -> allergic to everything" + +[d266a59a-fccc-413b-ac53-d57cb1f0db9d] +description = "testing for peanuts allergy -> not allergic to anything" + +[ea210a98-860d-46b2-a5bf-50d8995b3f2a] +description = "testing for peanuts allergy -> allergic only to peanuts" + +[eac69ae9-8d14-4291-ac4b-7fd2c73d3a5b] +description = "testing for peanuts allergy -> allergic to peanuts and something else" + +[9152058c-ce39-4b16-9b1d-283ec6d25085] +description = "testing for peanuts allergy -> allergic to something, but not peanuts" + +[d2d71fd8-63d5-40f9-a627-fbdaf88caeab] +description = "testing for peanuts allergy -> allergic to everything" + +[b948b0a1-cbf7-4b28-a244-73ff56687c80] +description = "testing for shellfish allergy -> not allergic to anything" + +[9ce9a6f3-53e9-4923-85e0-73019047c567] +description = "testing for shellfish allergy -> allergic only to shellfish" + +[b272fca5-57ba-4b00-bd0c-43a737ab2131] +description = "testing for shellfish allergy -> allergic to shellfish and something else" + +[21ef8e17-c227-494e-8e78-470a1c59c3d8] +description = "testing for shellfish allergy -> allergic to something, but not shellfish" + +[cc789c19-2b5e-4c67-b146-625dc8cfa34e] +description = "testing for shellfish allergy -> allergic to everything" + +[651bde0a-2a74-46c4-ab55-02a0906ca2f5] +description = "testing for strawberries allergy -> not allergic to anything" + +[b649a750-9703-4f5f-b7f7-91da2c160ece] +description = "testing for strawberries allergy -> allergic only to strawberries" + +[50f5f8f3-3bac-47e6-8dba-2d94470a4bc6] +description = "testing for strawberries allergy -> allergic to strawberries and something else" + +[23dd6952-88c9-48d7-a7d5-5d0343deb18d] +description = "testing for strawberries allergy -> allergic to something, but not strawberries" + +[74afaae2-13b6-43a2-837a-286cd42e7d7e] +description = "testing for strawberries allergy -> allergic to everything" + +[c49a91ef-6252-415e-907e-a9d26ef61723] +description = "testing for tomatoes allergy -> not allergic to anything" + +[b69c5131-b7d0-41ad-a32c-e1b2cc632df8] +description = "testing for tomatoes allergy -> allergic only to tomatoes" + +[1ca50eb1-f042-4ccf-9050-341521b929ec] +description = "testing for tomatoes allergy -> allergic to tomatoes and something else" + +[e9846baa-456b-4eff-8025-034b9f77bd8e] +description = "testing for tomatoes allergy -> allergic to something, but not tomatoes" + +[b2414f01-f3ad-4965-8391-e65f54dad35f] +description = "testing for tomatoes allergy -> allergic to everything" + +[978467ab-bda4-49f7-b004-1d011ead947c] +description = "testing for chocolate allergy -> not allergic to anything" + +[59cf4e49-06ea-4139-a2c1-d7aad28f8cbc] +description = "testing for chocolate allergy -> allergic only to chocolate" + +[b0a7c07b-2db7-4f73-a180-565e07040ef1] +description = "testing for chocolate allergy -> allergic to chocolate and something else" + +[f5506893-f1ae-482a-b516-7532ba5ca9d2] +description = "testing for chocolate allergy -> allergic to something, but not chocolate" + +[02debb3d-d7e2-4376-a26b-3c974b6595c6] +description = "testing for chocolate allergy -> allergic to everything" + +[17f4a42b-c91e-41b8-8a76-4797886c2d96] +description = "testing for pollen allergy -> not allergic to anything" + +[7696eba7-1837-4488-882a-14b7b4e3e399] +description = "testing for pollen allergy -> allergic only to pollen" + +[9a49aec5-fa1f-405d-889e-4dfc420db2b6] +description = "testing for pollen allergy -> allergic to pollen and something else" + +[3cb8e79f-d108-4712-b620-aa146b1954a9] +description = "testing for pollen allergy -> allergic to something, but not pollen" + +[1dc3fe57-7c68-4043-9d51-5457128744b2] +description = "testing for pollen allergy -> allergic to everything" + +[d3f523d6-3d50-419b-a222-d4dfd62ce314] +description = "testing for cats allergy -> not allergic to anything" + +[eba541c3-c886-42d3-baef-c048cb7fcd8f] +description = "testing for cats allergy -> allergic only to cats" + +[ba718376-26e0-40b7-bbbe-060287637ea5] +description = "testing for cats allergy -> allergic to cats and something else" + +[3c6dbf4a-5277-436f-8b88-15a206f2d6c4] +description = "testing for cats allergy -> allergic to something, but not cats" + +[1faabb05-2b98-4995-9046-d83e4a48a7c1] +description = "testing for cats allergy -> allergic to everything" + +[f9c1b8e7-7dc5-4887-aa93-cebdcc29dd8f] +description = "list when: -> no allergies" + +[9e1a4364-09a6-4d94-990f-541a94a4c1e8] +description = "list when: -> just eggs" + +[8851c973-805e-4283-9e01-d0c0da0e4695] +description = "list when: -> just peanuts" + +[2c8943cb-005e-435f-ae11-3e8fb558ea98] +description = "list when: -> just strawberries" + +[6fa95d26-044c-48a9-8a7b-9ee46ec32c5c] +description = "list when: -> eggs and peanuts" + +[19890e22-f63f-4c5c-a9fb-fb6eacddfe8e] +description = "list when: -> more than eggs but not peanuts" + +[4b68f470-067c-44e4-889f-c9fe28917d2f] +description = "list when: -> lots of stuff" + +[0881b7c5-9efa-4530-91bd-68370d054bc7] +description = "list when: -> everything" + +[12ce86de-b347-42a0-ab7c-2e0570f0c65b] +description = "list when: -> no allergen score parts" + +[93c2df3e-4f55-4fed-8116-7513092819cd] +description = "list when: -> no allergen score parts without highest valid score" diff --git a/exercises/practice/allergies/allergen.v b/exercises/practice/allergies/allergen.v new file mode 100644 index 0000000..9093a11 --- /dev/null +++ b/exercises/practice/allergies/allergen.v @@ -0,0 +1,12 @@ +module main + +enum Allergen as u8 { + eggs + peanuts + shellfish + strawberries + tomatoes + chocolate + pollen + cats +} diff --git a/exercises/practice/allergies/allergies.v b/exercises/practice/allergies/allergies.v new file mode 100644 index 0000000..bdc3b0e --- /dev/null +++ b/exercises/practice/allergies/allergies.v @@ -0,0 +1,10 @@ +module main + +fn allergic_to(allergen Allergen, score u8) bool { + // Please implement the `allergic_to` function +} + +fn list(score u8) []Allergen { + // Please implement the `list` function +} + diff --git a/exercises/practice/allergies/run_test.v b/exercises/practice/allergies/run_test.v new file mode 100644 index 0000000..a94f5c8 --- /dev/null +++ b/exercises/practice/allergies/run_test.v @@ -0,0 +1,203 @@ +module main + +fn test_eggs_allergy_not_allergic_to_anything() { + assert !allergic_to(Allergen.eggs, 0) +} + +fn test_eggs_allergy_allergic_to_only_eggs() { + assert allergic_to(Allergen.eggs, 1) +} + +fn test_eggs_allergy_allergic_to_eggs_and_something_else() { + assert allergic_to(Allergen.eggs, 3) +} + +fn test_eggs_allergy_allergic_to_something_but_not_eggs() { + assert !allergic_to(Allergen.eggs, 2) +} + +fn test_eggs_allergy_allergic_to_everything() { + assert allergic_to(Allergen.eggs, 255) +} + +fn test_peanuts_allergy_not_allergic_to_anything() { + assert !allergic_to(Allergen.peanuts, 0) +} + +fn test_peanuts_allergy_allergic_only_to_peanuts() { + assert allergic_to(Allergen.peanuts, 2) +} + +fn test_peanuts_allergy_allergic_to_peanuts_and_something_else() { + assert allergic_to(Allergen.peanuts, 7) +} + +fn test_peanuts_allergy_allergic_to_something_but_not_peanuts() { + assert !allergic_to(Allergen.peanuts, 5) +} + +fn test_peanuts_allergy_allergic_to_everything() { + assert allergic_to(Allergen.peanuts, 255) +} + +fn test_shellfish_allergy_not_allergic_to_anything() { + assert !allergic_to(Allergen.shellfish, 0) +} + +fn test_shellfish_allergy_allergic_only_to_shellfish() { + assert allergic_to(Allergen.shellfish, 4) +} + +fn test_shellfish_allergy_allergic_to_shellfish_and_something_else() { + assert allergic_to(Allergen.shellfish, 14) +} + +fn test_shellfish_allergy_allergic_to_something_but_not_shellfish() { + assert !allergic_to(Allergen.shellfish, 10) +} + +fn test_shellfish_allergy_allergic_to_everything() { + assert allergic_to(Allergen.shellfish, 255) +} + +fn test_strawberries_allergy_not_allergic_to_anything() { + assert !allergic_to(Allergen.strawberries, 0) +} + +fn test_strawberries_allergy_allergic_only_to_strawberries() { + assert allergic_to(Allergen.strawberries, 8) +} + +fn test_strawberries_allergy_allergic_to_strawberries_and_something_else() { + assert allergic_to(Allergen.strawberries, 28) +} + +fn test_strawberries_allergic_to_something_but_not_strawberries() { + assert !allergic_to(Allergen.strawberries, 20) +} + +fn test_strawberries_allergic_to_everything() { + assert allergic_to(Allergen.strawberries, 255) +} + +fn test_tomatoes_allergy_not_allergic_to_anything() { + assert !allergic_to(Allergen.tomatoes, 0) +} + +fn test_tomatoes_allergy_allergic_only_to_tomatoes() { + assert allergic_to(Allergen.tomatoes, 16) +} + +fn test_tomatoes_allergy_allergic_to_tomatoes_and_something_else() { + assert allergic_to(Allergen.tomatoes, 56) +} + +fn test_tomatoes_allergy_allergic_to_something_but_not_tomatoes() { + assert !allergic_to(Allergen.tomatoes, 40) +} + +fn test_tomatoes_allergy_allergic_to_everything() { + assert allergic_to(Allergen.tomatoes, 255) +} + +fn test_chocolate_allergy_not_allergic_to_anything() { + assert !allergic_to(Allergen.chocolate, 0) +} + +fn test_chocolate_allergy_allergic_only_to_chocolate() { + assert allergic_to(Allergen.chocolate, 32) +} + +fn test_chocolate_allergy_allergic_to_chocolate_and_something_else() { + assert allergic_to(Allergen.chocolate, 112) +} + +fn test_chocolate_allergy_allergic_to_something_but_not_chocolate() { + assert !allergic_to(Allergen.chocolate, 80) +} + +fn test_chocolate_allergy_allergic_to_everything() { + assert allergic_to(Allergen.chocolate, 255) +} + +fn test_pollen_allergy_not_allergic_to_anything() { + assert !allergic_to(Allergen.pollen, 0) +} + +fn test_pollen_allergy_allergic_only_to_pollen() { + assert allergic_to(Allergen.pollen, 64) +} + +fn test_pollen_allergy_allergic_to_pollen_and_something_else() { + assert allergic_to(Allergen.pollen, 224) +} + +fn test_pollen_allergy_allergic_to_something_but_not_pollen() { + assert !allergic_to(Allergen.pollen, 160) +} + +fn test_pollen_allergy_allergic_to_everything() { + assert allergic_to(Allergen.pollen, 255) +} + +fn test_cats_allergy_not_allergic_to_anything() { + assert !allergic_to(Allergen.cats, 0) +} + +fn test_cats_allergy_allergic_only_to_cats() { + assert allergic_to(Allergen.cats, 128) +} + +fn test_cats_allergy_allergy_to_cats_and_something_else() { + assert allergic_to(Allergen.cats, 192) +} + +fn test_cats_allergy_allergic_to_something_but_not_cats() { + assert !allergic_to(Allergen.cats, 64) +} + +fn test_cats_allergy_alergic_to_everything() { + assert allergic_to(Allergen.cats, 255) +} + +fn test_list_no_allergies() { + assert list(0) == [] +} + +fn test_list_just_eggs() { + assert list(1) == [Allergen.eggs] +} + +fn test_list_just_peanuts() { + assert list(2) == [Allergen.peanuts] +} + +fn test_list_just_strawberries() { + assert list(8) == [Allergen.strawberries] +} + +fn test_list_eggs_and_peanuts() { + assert same_in_any_order(list(3), [Allergen.eggs, Allergen.peanuts]) +} + +fn test_list_more_than_eggs_but_not_peanuts() { + assert same_in_any_order(list(5), [Allergen.eggs, Allergen.shellfish]) +} + +fn test_list_lots_of_stuff() { + assert same_in_any_order(list(248), [Allergen.strawberries, Allergen.tomatoes, Allergen.chocolate, + Allergen.pollen, Allergen.cats]) +} + +fn test_list_everything() { + assert same_in_any_order(list(255), [Allergen.eggs, Allergen.peanuts, Allergen.shellfish, + Allergen.strawberries, Allergen.tomatoes, Allergen.chocolate, Allergen.pollen, Allergen.cats]) +} + +fn compare(left voidptr, right voidptr) int { + return int(left) - int(right) +} + +fn same_in_any_order(left []Allergen, right []Allergen) bool { + return left.sorted_with_compare(compare) == right.sorted_with_compare(compare) +} From bde6dffc209b971a874e1d8bb3601110ddbeca46 Mon Sep 17 00:00:00 2001 From: Kah Goh Date: Tue, 19 Mar 2024 22:53:47 +0800 Subject: [PATCH 2/3] Move allergies enum into allergies.v This is to put the solution into one file. --- exercises/practice/allergies/.meta/example.v | 28 +++++++++----------- exercises/practice/allergies/allergen.v | 11 -------- exercises/practice/allergies/allergies.v | 16 ++++++++--- 3 files changed, 26 insertions(+), 29 deletions(-) diff --git a/exercises/practice/allergies/.meta/example.v b/exercises/practice/allergies/.meta/example.v index 3690fc5..f561ec0 100644 --- a/exercises/practice/allergies/.meta/example.v +++ b/exercises/practice/allergies/.meta/example.v @@ -1,23 +1,21 @@ module main +enum Allergen as u8 { + eggs = 1 + peanuts = 2 + shellfish = 4 + strawberries = 8 + tomatoes = 16 + chocolate = 32 + pollen = 64 + cats = 128 +} + fn allergic_to(allergen Allergen, score u8) bool { - return mask(allergen) & score > 0 + return u8(allergen) & score > 0 } fn list(score u8) []Allergen { return [Allergen.eggs, Allergen.peanuts, Allergen.shellfish, Allergen.strawberries, - Allergen.tomatoes, Allergen.chocolate, Allergen.pollen, Allergen.cats].filter(score & mask(it) > 0) -} - -fn mask(allergen Allergen) u8 { - return match allergen { - .eggs { 1 } - .peanuts { 2 } - .shellfish { 4 } - .strawberries { 8 } - .tomatoes { 16 } - .chocolate { 32 } - .pollen { 64 } - .cats { 128 } - } + Allergen.tomatoes, Allergen.chocolate, Allergen.pollen, Allergen.cats].filter(score & int(it) > 0) } diff --git a/exercises/practice/allergies/allergen.v b/exercises/practice/allergies/allergen.v index 9093a11..6bd0de5 100644 --- a/exercises/practice/allergies/allergen.v +++ b/exercises/practice/allergies/allergen.v @@ -1,12 +1 @@ module main - -enum Allergen as u8 { - eggs - peanuts - shellfish - strawberries - tomatoes - chocolate - pollen - cats -} diff --git a/exercises/practice/allergies/allergies.v b/exercises/practice/allergies/allergies.v index bdc3b0e..89a36e9 100644 --- a/exercises/practice/allergies/allergies.v +++ b/exercises/practice/allergies/allergies.v @@ -1,10 +1,20 @@ module main +enum Allergen as u8 { + eggs + peanuts + shellfish + strawberries + tomatoes + chocolate + pollen + cats +} + fn allergic_to(allergen Allergen, score u8) bool { - // Please implement the `allergic_to` function + // Please implement the `allergic_to` function } fn list(score u8) []Allergen { - // Please implement the `list` function + // Please implement the `list` function } - From a93409037658833f3bb568f2e2956e2c8e34e0a7 Mon Sep 17 00:00:00 2001 From: Kah Goh Date: Wed, 20 Mar 2024 07:37:58 +0800 Subject: [PATCH 3/3] Remove allergen.v File is no longer needed. --- exercises/practice/allergies/allergen.v | 1 - 1 file changed, 1 deletion(-) delete mode 100644 exercises/practice/allergies/allergen.v diff --git a/exercises/practice/allergies/allergen.v b/exercises/practice/allergies/allergen.v deleted file mode 100644 index 6bd0de5..0000000 --- a/exercises/practice/allergies/allergen.v +++ /dev/null @@ -1 +0,0 @@ -module main