Skip to content

Commit 14ea5e5

Browse files
authored
add allergies (#58)
1 parent a41948e commit 14ea5e5

File tree

15 files changed

+757
-5
lines changed

15 files changed

+757
-5
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,14 @@
185185
"prerequisites": [],
186186
"difficulty": 2
187187
},
188+
{
189+
"slug": "allergies",
190+
"name": "Allergies",
191+
"uuid": "a5c2feb2-392c-411c-8c57-206a912ad0f2",
192+
"practices": [],
193+
"prerequisites": [],
194+
"difficulty": 3
195+
},
188196
{
189197
"slug": "anagram",
190198
"name": "Anagram",
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Instructions
2+
3+
Given a person's allergy score, determine whether or not they're allergic to a given item, and their full list of allergies.
4+
5+
An allergy test produces a single numeric score which contains the information about all the allergies the person has (that they were tested for).
6+
7+
The list of items (and their value) that were tested are:
8+
9+
- eggs (1)
10+
- peanuts (2)
11+
- shellfish (4)
12+
- strawberries (8)
13+
- tomatoes (16)
14+
- chocolate (32)
15+
- pollen (64)
16+
- cats (128)
17+
18+
So if Tom is allergic to peanuts and chocolate, he gets a score of 34.
19+
20+
Now, given just that score of 34, your program should be able to say:
21+
22+
- Whether Tom is allergic to any one of those allergens listed above.
23+
- All the allergens Tom is allergic to.
24+
25+
Note: a given score may include allergens **not** listed above (i.e. allergens that score 256, 512, 1024, etc.).
26+
Your program should ignore those components of the score.
27+
For example, if the allergy score is 257, your program should only report the eggs (1) allergy.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
namespace Allergies
2+
3+
inductive Allergen where
4+
| eggs : Allergen
5+
| peanuts : Allergen
6+
| shellfish : Allergen
7+
| strawberries : Allergen
8+
| tomatoes : Allergen
9+
| chocolate : Allergen
10+
| pollen : Allergen
11+
| cats : Allergen
12+
deriving BEq, Repr
13+
14+
def Allergen.toNat : Allergen -> Nat
15+
| .eggs => 1
16+
| .peanuts => 2
17+
| .shellfish => 4
18+
| .strawberries => 8
19+
| .tomatoes => 16
20+
| .chocolate => 32
21+
| .pollen => 64
22+
| .cats => 128
23+
24+
def allergicTo (allergen : Allergen) (score : Nat) : Bool :=
25+
score &&& allergen.toNat != 0
26+
27+
def list (score : Nat) : List Allergen :=
28+
[.eggs, .peanuts, .shellfish, .strawberries, .tomatoes, .chocolate, .pollen, .cats].filter (allergicTo · score)
29+
30+
end Allergies
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"oxe-i"
4+
],
5+
"files": {
6+
"solution": [
7+
"Allergies.lean"
8+
],
9+
"test": [
10+
"AllergiesTest.lean"
11+
],
12+
"example": [
13+
".meta/Example.lean"
14+
]
15+
},
16+
"blurb": "Given a person's allergy score, determine whether or not they're allergic to a given item, and their full list of allergies.",
17+
"source": "Exercise by the JumpstartLab team for students at The Turing School of Software and Design.",
18+
"source_url": "https://turing.edu"
19+
}
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[17fc7296-2440-4ac4-ad7b-d07c321bc5a0]
13+
description = "testing for eggs allergy -> not allergic to anything"
14+
15+
[07ced27b-1da5-4c2e-8ae2-cb2791437546]
16+
description = "testing for eggs allergy -> allergic only to eggs"
17+
18+
[5035b954-b6fa-4b9b-a487-dae69d8c5f96]
19+
description = "testing for eggs allergy -> allergic to eggs and something else"
20+
21+
[64a6a83a-5723-4b5b-a896-663307403310]
22+
description = "testing for eggs allergy -> allergic to something, but not eggs"
23+
24+
[90c8f484-456b-41c4-82ba-2d08d93231c6]
25+
description = "testing for eggs allergy -> allergic to everything"
26+
27+
[d266a59a-fccc-413b-ac53-d57cb1f0db9d]
28+
description = "testing for peanuts allergy -> not allergic to anything"
29+
30+
[ea210a98-860d-46b2-a5bf-50d8995b3f2a]
31+
description = "testing for peanuts allergy -> allergic only to peanuts"
32+
33+
[eac69ae9-8d14-4291-ac4b-7fd2c73d3a5b]
34+
description = "testing for peanuts allergy -> allergic to peanuts and something else"
35+
36+
[9152058c-ce39-4b16-9b1d-283ec6d25085]
37+
description = "testing for peanuts allergy -> allergic to something, but not peanuts"
38+
39+
[d2d71fd8-63d5-40f9-a627-fbdaf88caeab]
40+
description = "testing for peanuts allergy -> allergic to everything"
41+
42+
[b948b0a1-cbf7-4b28-a244-73ff56687c80]
43+
description = "testing for shellfish allergy -> not allergic to anything"
44+
45+
[9ce9a6f3-53e9-4923-85e0-73019047c567]
46+
description = "testing for shellfish allergy -> allergic only to shellfish"
47+
48+
[b272fca5-57ba-4b00-bd0c-43a737ab2131]
49+
description = "testing for shellfish allergy -> allergic to shellfish and something else"
50+
51+
[21ef8e17-c227-494e-8e78-470a1c59c3d8]
52+
description = "testing for shellfish allergy -> allergic to something, but not shellfish"
53+
54+
[cc789c19-2b5e-4c67-b146-625dc8cfa34e]
55+
description = "testing for shellfish allergy -> allergic to everything"
56+
57+
[651bde0a-2a74-46c4-ab55-02a0906ca2f5]
58+
description = "testing for strawberries allergy -> not allergic to anything"
59+
60+
[b649a750-9703-4f5f-b7f7-91da2c160ece]
61+
description = "testing for strawberries allergy -> allergic only to strawberries"
62+
63+
[50f5f8f3-3bac-47e6-8dba-2d94470a4bc6]
64+
description = "testing for strawberries allergy -> allergic to strawberries and something else"
65+
66+
[23dd6952-88c9-48d7-a7d5-5d0343deb18d]
67+
description = "testing for strawberries allergy -> allergic to something, but not strawberries"
68+
69+
[74afaae2-13b6-43a2-837a-286cd42e7d7e]
70+
description = "testing for strawberries allergy -> allergic to everything"
71+
72+
[c49a91ef-6252-415e-907e-a9d26ef61723]
73+
description = "testing for tomatoes allergy -> not allergic to anything"
74+
75+
[b69c5131-b7d0-41ad-a32c-e1b2cc632df8]
76+
description = "testing for tomatoes allergy -> allergic only to tomatoes"
77+
78+
[1ca50eb1-f042-4ccf-9050-341521b929ec]
79+
description = "testing for tomatoes allergy -> allergic to tomatoes and something else"
80+
81+
[e9846baa-456b-4eff-8025-034b9f77bd8e]
82+
description = "testing for tomatoes allergy -> allergic to something, but not tomatoes"
83+
84+
[b2414f01-f3ad-4965-8391-e65f54dad35f]
85+
description = "testing for tomatoes allergy -> allergic to everything"
86+
87+
[978467ab-bda4-49f7-b004-1d011ead947c]
88+
description = "testing for chocolate allergy -> not allergic to anything"
89+
90+
[59cf4e49-06ea-4139-a2c1-d7aad28f8cbc]
91+
description = "testing for chocolate allergy -> allergic only to chocolate"
92+
93+
[b0a7c07b-2db7-4f73-a180-565e07040ef1]
94+
description = "testing for chocolate allergy -> allergic to chocolate and something else"
95+
96+
[f5506893-f1ae-482a-b516-7532ba5ca9d2]
97+
description = "testing for chocolate allergy -> allergic to something, but not chocolate"
98+
99+
[02debb3d-d7e2-4376-a26b-3c974b6595c6]
100+
description = "testing for chocolate allergy -> allergic to everything"
101+
102+
[17f4a42b-c91e-41b8-8a76-4797886c2d96]
103+
description = "testing for pollen allergy -> not allergic to anything"
104+
105+
[7696eba7-1837-4488-882a-14b7b4e3e399]
106+
description = "testing for pollen allergy -> allergic only to pollen"
107+
108+
[9a49aec5-fa1f-405d-889e-4dfc420db2b6]
109+
description = "testing for pollen allergy -> allergic to pollen and something else"
110+
111+
[3cb8e79f-d108-4712-b620-aa146b1954a9]
112+
description = "testing for pollen allergy -> allergic to something, but not pollen"
113+
114+
[1dc3fe57-7c68-4043-9d51-5457128744b2]
115+
description = "testing for pollen allergy -> allergic to everything"
116+
117+
[d3f523d6-3d50-419b-a222-d4dfd62ce314]
118+
description = "testing for cats allergy -> not allergic to anything"
119+
120+
[eba541c3-c886-42d3-baef-c048cb7fcd8f]
121+
description = "testing for cats allergy -> allergic only to cats"
122+
123+
[ba718376-26e0-40b7-bbbe-060287637ea5]
124+
description = "testing for cats allergy -> allergic to cats and something else"
125+
126+
[3c6dbf4a-5277-436f-8b88-15a206f2d6c4]
127+
description = "testing for cats allergy -> allergic to something, but not cats"
128+
129+
[1faabb05-2b98-4995-9046-d83e4a48a7c1]
130+
description = "testing for cats allergy -> allergic to everything"
131+
132+
[f9c1b8e7-7dc5-4887-aa93-cebdcc29dd8f]
133+
description = "list when: -> no allergies"
134+
135+
[9e1a4364-09a6-4d94-990f-541a94a4c1e8]
136+
description = "list when: -> just eggs"
137+
138+
[8851c973-805e-4283-9e01-d0c0da0e4695]
139+
description = "list when: -> just peanuts"
140+
141+
[2c8943cb-005e-435f-ae11-3e8fb558ea98]
142+
description = "list when: -> just strawberries"
143+
144+
[6fa95d26-044c-48a9-8a7b-9ee46ec32c5c]
145+
description = "list when: -> eggs and peanuts"
146+
147+
[19890e22-f63f-4c5c-a9fb-fb6eacddfe8e]
148+
description = "list when: -> more than eggs but not peanuts"
149+
150+
[4b68f470-067c-44e4-889f-c9fe28917d2f]
151+
description = "list when: -> lots of stuff"
152+
153+
[0881b7c5-9efa-4530-91bd-68370d054bc7]
154+
description = "list when: -> everything"
155+
156+
[12ce86de-b347-42a0-ab7c-2e0570f0c65b]
157+
description = "list when: -> no allergen score parts"
158+
159+
[93c2df3e-4f55-4fed-8116-7513092819cd]
160+
description = "list when: -> no allergen score parts without highest valid score"
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace Allergies
2+
3+
inductive Allergen where
4+
| eggs : Allergen
5+
| peanuts : Allergen
6+
| shellfish : Allergen
7+
| strawberries : Allergen
8+
| tomatoes : Allergen
9+
| chocolate : Allergen
10+
| pollen : Allergen
11+
| cats : Allergen
12+
deriving BEq, Repr
13+
14+
def allergicTo (allergen : Allergen) (score : Nat) : Bool :=
15+
sorry
16+
17+
def list (score : Nat) : List Allergen :=
18+
sorry
19+
20+
end Allergies
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import LeanTest
2+
import Allergies
3+
4+
open LeanTest
5+
6+
def allergiesTests : TestSuite :=
7+
(TestSuite.empty "Allergies")
8+
|>.addTest "testing for eggs allergy -> not allergic to anything" (do
9+
return assertEqual false (Allergies.allergicTo .eggs 0))
10+
|>.addTest "testing for eggs allergy -> allergic only to eggs" (do
11+
return assertEqual true (Allergies.allergicTo .eggs 1))
12+
|>.addTest "testing for eggs allergy -> allergic to eggs and something else" (do
13+
return assertEqual true (Allergies.allergicTo .eggs 3))
14+
|>.addTest "testing for eggs allergy -> allergic to something, but not eggs" (do
15+
return assertEqual false (Allergies.allergicTo .eggs 2))
16+
|>.addTest "testing for eggs allergy -> allergic to everything" (do
17+
return assertEqual true (Allergies.allergicTo .eggs 255))
18+
|>.addTest "testing for peanuts allergy -> not allergic to anything" (do
19+
return assertEqual false (Allergies.allergicTo .peanuts 0))
20+
|>.addTest "testing for peanuts allergy -> allergic only to peanuts" (do
21+
return assertEqual true (Allergies.allergicTo .peanuts 2))
22+
|>.addTest "testing for peanuts allergy -> allergic to peanuts and something else" (do
23+
return assertEqual true (Allergies.allergicTo .peanuts 7))
24+
|>.addTest "testing for peanuts allergy -> allergic to something, but not peanuts" (do
25+
return assertEqual false (Allergies.allergicTo .peanuts 5))
26+
|>.addTest "testing for peanuts allergy -> allergic to everything" (do
27+
return assertEqual true (Allergies.allergicTo .peanuts 255))
28+
|>.addTest "testing for shellfish allergy -> not allergic to anything" (do
29+
return assertEqual false (Allergies.allergicTo .shellfish 0))
30+
|>.addTest "testing for shellfish allergy -> allergic only to shellfish" (do
31+
return assertEqual true (Allergies.allergicTo .shellfish 4))
32+
|>.addTest "testing for shellfish allergy -> allergic to shellfish and something else" (do
33+
return assertEqual true (Allergies.allergicTo .shellfish 14))
34+
|>.addTest "testing for shellfish allergy -> allergic to something, but not shellfish" (do
35+
return assertEqual false (Allergies.allergicTo .shellfish 10))
36+
|>.addTest "testing for shellfish allergy -> allergic to everything" (do
37+
return assertEqual true (Allergies.allergicTo .shellfish 255))
38+
|>.addTest "testing for strawberries allergy -> not allergic to anything" (do
39+
return assertEqual false (Allergies.allergicTo .strawberries 0))
40+
|>.addTest "testing for strawberries allergy -> allergic only to strawberries" (do
41+
return assertEqual true (Allergies.allergicTo .strawberries 8))
42+
|>.addTest "testing for strawberries allergy -> allergic to strawberries and something else" (do
43+
return assertEqual true (Allergies.allergicTo .strawberries 28))
44+
|>.addTest "testing for strawberries allergy -> allergic to something, but not strawberries" (do
45+
return assertEqual false (Allergies.allergicTo .strawberries 20))
46+
|>.addTest "testing for strawberries allergy -> allergic to everything" (do
47+
return assertEqual true (Allergies.allergicTo .strawberries 255))
48+
|>.addTest "testing for tomatoes allergy -> not allergic to anything" (do
49+
return assertEqual false (Allergies.allergicTo .tomatoes 0))
50+
|>.addTest "testing for tomatoes allergy -> allergic only to tomatoes" (do
51+
return assertEqual true (Allergies.allergicTo .tomatoes 16))
52+
|>.addTest "testing for tomatoes allergy -> allergic to tomatoes and something else" (do
53+
return assertEqual true (Allergies.allergicTo .tomatoes 56))
54+
|>.addTest "testing for tomatoes allergy -> allergic to something, but not tomatoes" (do
55+
return assertEqual false (Allergies.allergicTo .tomatoes 40))
56+
|>.addTest "testing for tomatoes allergy -> allergic to everything" (do
57+
return assertEqual true (Allergies.allergicTo .tomatoes 255))
58+
|>.addTest "testing for chocolate allergy -> not allergic to anything" (do
59+
return assertEqual false (Allergies.allergicTo .chocolate 0))
60+
|>.addTest "testing for chocolate allergy -> allergic only to chocolate" (do
61+
return assertEqual true (Allergies.allergicTo .chocolate 32))
62+
|>.addTest "testing for chocolate allergy -> allergic to chocolate and something else" (do
63+
return assertEqual true (Allergies.allergicTo .chocolate 112))
64+
|>.addTest "testing for chocolate allergy -> allergic to something, but not chocolate" (do
65+
return assertEqual false (Allergies.allergicTo .chocolate 80))
66+
|>.addTest "testing for chocolate allergy -> allergic to everything" (do
67+
return assertEqual true (Allergies.allergicTo .chocolate 255))
68+
|>.addTest "testing for pollen allergy -> not allergic to anything" (do
69+
return assertEqual false (Allergies.allergicTo .pollen 0))
70+
|>.addTest "testing for pollen allergy -> allergic only to pollen" (do
71+
return assertEqual true (Allergies.allergicTo .pollen 64))
72+
|>.addTest "testing for pollen allergy -> allergic to pollen and something else" (do
73+
return assertEqual true (Allergies.allergicTo .pollen 224))
74+
|>.addTest "testing for pollen allergy -> allergic to something, but not pollen" (do
75+
return assertEqual false (Allergies.allergicTo .pollen 160))
76+
|>.addTest "testing for pollen allergy -> allergic to everything" (do
77+
return assertEqual true (Allergies.allergicTo .pollen 255))
78+
|>.addTest "testing for cats allergy -> not allergic to anything" (do
79+
return assertEqual false (Allergies.allergicTo .cats 0))
80+
|>.addTest "testing for cats allergy -> allergic only to cats" (do
81+
return assertEqual true (Allergies.allergicTo .cats 128))
82+
|>.addTest "testing for cats allergy -> allergic to cats and something else" (do
83+
return assertEqual true (Allergies.allergicTo .cats 192))
84+
|>.addTest "testing for cats allergy -> allergic to something, but not cats" (do
85+
return assertEqual false (Allergies.allergicTo .cats 64))
86+
|>.addTest "testing for cats allergy -> allergic to everything" (do
87+
return assertEqual true (Allergies.allergicTo .cats 255))
88+
|>.addTest "list when: -> no allergies" (do
89+
return assertEqual [] (Allergies.list 0))
90+
|>.addTest "list when: -> just eggs" (do
91+
return assertEqual [.eggs] (Allergies.list 1))
92+
|>.addTest "list when: -> just peanuts" (do
93+
return assertEqual [.peanuts] (Allergies.list 2))
94+
|>.addTest "list when: -> just strawberries" (do
95+
return assertEqual [.strawberries] (Allergies.list 8))
96+
|>.addTest "list when: -> eggs and peanuts" (do
97+
return assertEqual [.eggs, .peanuts] (Allergies.list 3))
98+
|>.addTest "list when: -> more than eggs but not peanuts" (do
99+
return assertEqual [.eggs, .shellfish] (Allergies.list 5))
100+
|>.addTest "list when: -> lots of stuff" (do
101+
return assertEqual [.strawberries, .tomatoes, .chocolate, .pollen, .cats] (Allergies.list 248))
102+
|>.addTest "list when: -> everything" (do
103+
return assertEqual [.eggs, .peanuts, .shellfish, .strawberries, .tomatoes, .chocolate, .pollen, .cats] (Allergies.list 255))
104+
|>.addTest "list when: -> no allergen score parts" (do
105+
return assertEqual [.eggs, .shellfish, .strawberries, .tomatoes, .chocolate, .pollen, .cats] (Allergies.list 509))
106+
|>.addTest "list when: -> no allergen score parts without highest valid score" (do
107+
return assertEqual [.eggs] (Allergies.list 257))
108+
109+
def main : IO UInt32 := do
110+
runTestSuitesWithExitCode [allergiesTests]

0 commit comments

Comments
 (0)