Initial Commit
This commit is contained in:
39
go/allergies/README.md
Normal file
39
go/allergies/README.md
Normal file
@@ -0,0 +1,39 @@
|
||||
# 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 [view source](http://jumpstartlab.com)
|
44
go/allergies/allergies.go
Normal file
44
go/allergies/allergies.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package allergies
|
||||
|
||||
// Allergies takes a score and returns all of the
|
||||
// things that the score is allergic to.
|
||||
func Allergies(score int) []string {
|
||||
var ret []string
|
||||
|
||||
for _, v := range getAllAllergens() {
|
||||
if AllergicTo(score, v) {
|
||||
ret = append(ret, v)
|
||||
}
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
// AllergicTo takes a score and an allergen and returns if
|
||||
// That score is allergic to that thing.
|
||||
func AllergicTo(score int, tst string) bool {
|
||||
return score&getScoreForAllergen(tst) == getScoreForAllergen(tst)
|
||||
}
|
||||
|
||||
func getAllAllergens() []string {
|
||||
return []string{
|
||||
"eggs",
|
||||
"peanuts",
|
||||
"shellfish",
|
||||
"strawberries",
|
||||
"tomatoes",
|
||||
"chocolate",
|
||||
"pollen",
|
||||
"cats",
|
||||
}
|
||||
}
|
||||
|
||||
func getScoreForAllergen(tst string) int {
|
||||
ret := 1
|
||||
for _, v := range getAllAllergens() {
|
||||
if tst == v {
|
||||
return ret
|
||||
}
|
||||
ret *= 2
|
||||
}
|
||||
return -1
|
||||
}
|
79
go/allergies/allergies_test.go
Normal file
79
go/allergies/allergies_test.go
Normal file
@@ -0,0 +1,79 @@
|
||||
package allergies
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"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)
|
||||
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()
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user