Add 'allergies' exercise (#171)

This commit is contained in:
Erik Schierboom
2024-05-05 19:08:42 +02:00
committed by GitHub
parent 90f4ad5bf7
commit 029c27f3a4
8 changed files with 622 additions and 0 deletions

View File

@@ -428,6 +428,14 @@
"practices": [],
"prerequisites": [],
"difficulty": 6
},
{
"slug": "allergies",
"name": "Allergies",
"uuid": "9900a26d-52dd-404a-a7f0-d1d260c4f279",
"practices": [],
"prerequisites": [],
"difficulty": 4
}
]
},

View File

@@ -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.

View File

@@ -0,0 +1,19 @@
{
"authors": [
"erikschierboom"
],
"files": {
"solution": [
"allergies.8th"
],
"test": [
"test.8th"
],
"example": [
".meta/example.8th"
]
},
"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"
}

View File

@@ -0,0 +1,9 @@
["eggs", "peanuts", "shellfish", "strawberries", "tomatoes", "chocolate", "pollen", "cats"] constant allergens
: allergic-to? \ n s -- T
allergens swap ' s:= a:indexof nip 1 swap n:shl n:band >bool
;
: list \ n -- a
>r allergens ( r@ swap allergic-to? ) a:filter rdrop
;

View File

@@ -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"

View File

@@ -0,0 +1,7 @@
: allergic-to? \ n s -- T
;
: list \ n -- a
;

View File

@@ -0,0 +1,173 @@
needs console/loaded
\ -----------------------------------------------------------------
ns: test
-1 var, test-count
var tests-passed
var tests-failed
var tests-skipped
true var, run-test
\ Some utility words
: test-passed \ s x x -- \\ test name, expected value, actual value
2drop
1 tests-passed n:+!
con:green con:onBlack . space " ... OK" . con:white con:onBlack cr
;
: test-skipped \ s --
1 tests-skipped n:+!
con:cyan con:onBlack . space " ... SKIPPED" . con:white con:onBlack cr
;
: test-failed \ s x x -- \\ test name, expected value, actual value
1 tests-failed n:+!
rot
con:red con:onBlack . space " ... FAIL" . con:white con:onBlack cr
" Actual: «" . . "»" . cr
" Expected: «" . . "»" . cr cr
;
: isword? \ x -- x f
dup >kind ns:w n:=
;
: run-test? \ -- T
run-test @ if true else "RUN_ALL_TESTS" getenv n:>bool then
;
\ Num passed + num skipped + num failed should == num tests
: all-tests-run? \ -- T
tests-passed @ tests-skipped @ tests-failed @ n:+ n:+
test-count @ n:=
;
\ returns true if x is a date, false otherwise
: date? \ x -- x T
dup >kind ns:d n:=
;
\ adapted from 8th forum -- https://8th-dev.com/forum/index.php/topic,2745.0.html
: eq? \ x x -- T
\ are the items the same kind?
2dup >kind swap >kind n:=
!if 2drop false ;then
\ same kind: try different comparators
number? if n:= ;then
string? if s:= ;then
array? if ' eq? a:= 2nip ;then
map? if ' eq? m:= 2nip ;then
date? if d:= ;then
\ otherwise fall back to 'lazy evaluation'
l: =
;
: eps_eq? \ n x x -- T
\ are the items the same kind?
2dup >kind swap >kind n:=
!if 2drop false ;then
number? !if 2drop false ;then
rot n:~=
;
: check-depth \ ... n -- ...
dup>r
n:1+ depth n:=
!if
con:red con:onBlack
"PANIC: expected stack depth to be " . r> . cr
"Stack is:" . cr
.s cr
255 die
then
rdrop
;
\ -----------------------------------------------------------------
\ status report at end of run
( all-tests-run?
!if con:red con:onBlack "... FAIL - not all tests completed" . con:white con:onBlack cr then
) onexit
\ Print a summary of the tests run
( con:white con:onBlack
test-count @ . space "tests planned - " .
tests-passed @ . space "passed - " .
tests-skipped @ . space "skipped - " .
tests-failed @ . space "failed" . cr
) onexit
\ -----------------------------------------------------------------
\ The public-facing words
\ -----------------------------------------------------------------
: equal? \ s x w -- | s w x --
run-test? !if 2drop test-skipped ;; then
isword? !if swap then
w:exec
3 check-depth
2dup \ so test-failed can show actual and expected
eq? if test-passed else test-failed then
;
: approx_equal? \ s x w n -- | s w x n --
run-test? !if 3drop test-skipped ;; then
-rot isword? !if swap then
w:exec
4 check-depth
3dup \ so test-failed can show actual and expected
eps_eq?
if rot drop test-passed else rot drop test-failed then
;
: true? \ s w --
run-test? !if drop test-skipped ;; then
w:exec
2 check-depth
true swap dup \ so test-failed can show actual and expected
if test-passed else test-failed then
;
: false? \ s w --
run-test? !if drop test-skipped ;; then
w:exec
2 check-depth
false swap dup \ so test-failed can show actual and expected
!if test-passed else test-failed then
;
: null? \ s w --
run-test? !if drop test-skipped ;; then
w:exec
2 check-depth
null swap dup \ so test-failed can show actual and expected
G:null? nip if test-passed else test-failed then
;
: SKIP-REST-OF-TESTS false run-test ! ;
: tests \ n --
test-count !
;
\ Set the exit status:
\ 0 = all OK
\ 1 = not all tests were run (some error occurred)
\ 2 = some tests failed
: end-of-tests \ --
all-tests-run?
if
tests-failed @ 0 n:= if 0 else 2 then
else
1
then
die
;

View File

@@ -0,0 +1,219 @@
"allergies.8th" f:include
needs exercism/test
with: test
50 tests
"not allergic to anything, including eggs"
( 0 "eggs" allergic-to? )
false?
SKIP-REST-OF-TESTS
"allergic only to eggs"
( 1 "eggs" allergic-to? )
true?
"allergic to eggs and something else"
( 3 "eggs" allergic-to? )
true?
"allergic to something, but not eggs"
( 2 "eggs" allergic-to? )
false?
"allergic to everything, including eggs"
( 255 "eggs" allergic-to? )
true?
"not allergic to anything, including peanuts"
( 0 "peanuts" allergic-to? )
false?
"allergic only to peanuts"
( 2 "peanuts" allergic-to? )
true?
"allergic to peanuts and something else"
( 7 "peanuts" allergic-to? )
true?
"allergic to something, but not peanuts"
( 5 "peanuts" allergic-to? )
false?
"allergic to everything, including peanuts"
( 255 "peanuts" allergic-to? )
true?
"not allergic to anything, including shellfish"
( 0 "shellfish" allergic-to? )
false?
"allergic only to shellfish"
( 4 "shellfish" allergic-to? )
true?
"allergic to shellfish and something else"
( 14 "shellfish" allergic-to? )
true?
"allergic to something, but not shellfish"
( 10 "shellfish" allergic-to? )
false?
"allergic to everything, including shellfish"
( 255 "shellfish" allergic-to? )
true?
"not allergic to anything, including strawberries"
( 0 "strawberries" allergic-to? )
false?
"allergic only to strawberries"
( 8 "strawberries" allergic-to? )
true?
"allergic to strawberries and something else"
( 28 "strawberries" allergic-to? )
true?
"allergic to something, but not strawberries"
( 20 "strawberries" allergic-to? )
false?
"allergic to everything, including strawberries"
( 255 "strawberries" allergic-to? )
true?
"not allergic to anything, including tomatoes"
( 0 "tomatoes" allergic-to? )
false?
"allergic only to tomatoes"
( 16 "tomatoes" allergic-to? )
true?
"allergic to tomatoes and something else"
( 56 "tomatoes" allergic-to? )
true?
"allergic to something, but not tomatoes"
( 40 "tomatoes" allergic-to? )
false?
"allergic to everything, including tomatoes"
( 255 "tomatoes" allergic-to? )
true?
"not allergic to anything, including chocolate"
( 0 "chocolate" allergic-to? )
false?
"allergic only to chocolate"
( 32 "chocolate" allergic-to? )
true?
"allergic to chocolate and something else"
( 112 "chocolate" allergic-to? )
true?
"allergic to something, but not chocolate"
( 80 "chocolate" allergic-to? )
false?
"allergic to everything, including chocolate"
( 255 "chocolate" allergic-to? )
true?
"not allergic to anything, including pollen"
( 0 "pollen" allergic-to? )
false?
"allergic only to pollen"
( 64 "pollen" allergic-to? )
true?
"allergic to pollen and something else"
( 224 "pollen" allergic-to? )
true?
"allergic to something, but not pollen"
( 160 "pollen" allergic-to? )
false?
"allergic to everything, including pollen"
( 255 "pollen" allergic-to? )
true?
"not allergic to anything, including cats"
( 0 "cats" allergic-to? )
false?
"allergic only to cats"
( 128 "cats" allergic-to? )
true?
"allergic to cats and something else"
( 192 "cats" allergic-to? )
true?
"allergic to something, but not cats"
( 64 "cats" allergic-to? )
false?
"allergic to everything, including cats"
( 255 "cats" allergic-to? )
true?
"no allergies"
( 0 list )
[]
equal?
"just eggs"
( 1 list )
["eggs"]
equal?
"just peanuts"
( 2 list )
["peanuts"]
equal?
"just strawberries"
( 8 list )
["strawberries"]
equal?
"eggs and peanuts"
( 3 list )
["eggs", "peanuts"]
equal?
"more than eggs but not peanuts"
( 5 list )
["eggs", "shellfish"]
equal?
"lots of stuff"
( 248 list )
["strawberries", "tomatoes", "chocolate", "pollen", "cats"]
equal?
"everything"
( 255 list )
["eggs", "peanuts", "shellfish", "strawberries", "tomatoes", "chocolate", "pollen", "cats"]
equal?
"no allergen score parts"
( 509 list )
["eggs", "shellfish", "strawberries", "tomatoes", "chocolate", "pollen", "cats"]
equal?
"no allergen score parts without highest valid score"
( 257 list )
["eggs"]
equal?
end-of-tests
;with