summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2016-08-28 02:17:28 +0200
committerDimitri Sokolyuk <demon@dim13.org>2016-08-28 02:17:28 +0200
commitc81284ad40511252401023ef238e3a6f47120aa2 (patch)
treee8ddcb10974ef98b90a9c8293e72764d97217091
parent8b2aa597999daf3efb5e842717fc2d373372ae4f (diff)
Solve allergies
-rw-r--r--go/allergies/README.md43
-rw-r--r--go/allergies/allergies.go29
-rw-r--r--go/allergies/allergies_test.go82
3 files changed, 154 insertions, 0 deletions
diff --git a/go/allergies/README.md b/go/allergies/README.md
new file mode 100644
index 0000000..b2af77a
--- /dev/null
+++ b/go/allergies/README.md
@@ -0,0 +1,43 @@
+# Allergies
+
+Write a program that, given a person's allergy score, can tell them 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.
+
+To run the tests simply run the command `go test` in the exercise directory.
+
+If the test suite contains benchmarks, you can run these with the `-bench`
+flag:
+
+ go test -bench .
+
+For more detailed info about the Go track see the [help
+page](http://exercism.io/languages/go).
+
+## Source
+
+Jumpstart Lab Warm-up [http://jumpstartlab.com](http://jumpstartlab.com)
+
+## Submitting Incomplete Problems
+It's possible to submit an incomplete solution so you can see how others have completed the exercise.
+
diff --git a/go/allergies/allergies.go b/go/allergies/allergies.go
new file mode 100644
index 0000000..1277b74
--- /dev/null
+++ b/go/allergies/allergies.go
@@ -0,0 +1,29 @@
+package allergies
+
+var index = map[string]int{
+ "eggs": 0x01,
+ "peanuts": 0x02,
+ "shellfish": 0x04,
+ "strawberries": 0x08,
+ "tomatoes": 0x10,
+ "chocolate": 0x20,
+ "pollen": 0x40,
+ "cats": 0x80,
+}
+
+func Allergies(n int) []string {
+ if n <= 0 {
+ return nil
+ }
+ var s []string
+ for k, v := range index {
+ if n&v != 0 {
+ s = append(s, k)
+ }
+ }
+ return s
+}
+
+func AllergicTo(n int, s string) bool {
+ return n > 0 && n&index[s] != 0
+}
diff --git a/go/allergies/allergies_test.go b/go/allergies/allergies_test.go
new file mode 100644
index 0000000..a8b2995
--- /dev/null
+++ b/go/allergies/allergies_test.go
@@ -0,0 +1,82 @@
+package allergies
+
+import (
+ "fmt"
+ "sort"
+ "testing"
+)
+
+var allergiesTests = []struct {
+ expected []string
+ input int
+}{
+ {[]string{}, 0},
+ {[]string{"eggs"}, 1},
+ {[]string{"peanuts"}, 2},
+ {[]string{"strawberries"}, 8},
+ {[]string{"eggs", "peanuts"}, 3},
+ {[]string{"eggs", "shellfish"}, 5},
+ {[]string{"strawberries", "tomatoes", "chocolate", "pollen", "cats"}, 248},
+ {[]string{"eggs", "peanuts", "shellfish", "strawberries", "tomatoes", "chocolate", "pollen", "cats"}, 255},
+ {[]string{"eggs", "shellfish", "strawberries", "tomatoes", "chocolate", "pollen", "cats"}, 509},
+}
+
+func TestAllergies(t *testing.T) {
+ for _, test := range allergiesTests {
+ actual := Allergies(test.input)
+ sort.Strings(actual)
+ sort.Strings(test.expected)
+ if fmt.Sprintf("%s", actual) != fmt.Sprintf("%s", test.expected) {
+ t.Fatalf("FAIL: Allergies(%d): expected %s, actual %s", test.input, test.expected, actual)
+ } else {
+ t.Logf("PASS: Allergic to %v", test.expected)
+ }
+ }
+}
+
+func BenchmarkAllergies(b *testing.B) {
+ b.StopTimer()
+ for _, test := range allergicToTests {
+ b.StartTimer()
+
+ for i := 0; i < b.N; i++ {
+ Allergies(test.i)
+ }
+ b.StopTimer()
+ }
+}
+
+var allergicToTests = []struct {
+ expected bool
+ i int
+ allergen string
+}{
+ {false, 0, "peanuts"},
+ {false, 0, "cats"},
+ {false, 0, "strawberries"},
+ {true, 1, "eggs"},
+ {true, 5, "eggs"},
+}
+
+func TestAllergicTo(t *testing.T) {
+ for _, test := range allergicToTests {
+ actual := AllergicTo(test.i, test.allergen)
+ if actual != test.expected {
+ t.Fatalf("FAIL: AllergicTo(%d, %s): expected %t, actual %t", test.i, test.allergen, test.expected, actual)
+ } else {
+ t.Logf("PASS: AllergicTo(%d, %s) %t", test.i, test.allergen, actual)
+ }
+ }
+}
+
+func BenchmarkAllergicTo(b *testing.B) {
+ b.StopTimer()
+ for _, test := range allergicToTests {
+ b.StartTimer()
+
+ for i := 0; i < b.N; i++ {
+ AllergicTo(test.i, test.allergen)
+ }
+ b.StopTimer()
+ }
+}