summaryrefslogtreecommitdiff
path: root/go/food-chain
diff options
context:
space:
mode:
Diffstat (limited to 'go/food-chain')
-rw-r--r--go/food-chain/README.md84
-rw-r--r--go/food-chain/food_chain.go15
-rw-r--r--go/food-chain/food_chain_test.go95
3 files changed, 194 insertions, 0 deletions
diff --git a/go/food-chain/README.md b/go/food-chain/README.md
new file mode 100644
index 0000000..effb1e9
--- /dev/null
+++ b/go/food-chain/README.md
@@ -0,0 +1,84 @@
+# Food Chain
+
+Write a program that generates the lyrics of the song 'I Know an Old Lady Who Swallowed a Fly'
+
+Write a program that generates the lyrics to the song
+"I know an old lady who swallowed a fly". While you could
+copy/paste the lyrics, or read them from a file, this
+problem is much more interesting if you approach it
+algorithmically.
+
+This is a [cumulative song](http://en.wikipedia.org/wiki/Cumulative_song) of unknown origin.
+
+This is one of many common variants.
+
+```plain
+I know an old lady who swallowed a fly.
+I don't know why she swallowed the fly. Perhaps she'll die.
+
+I know an old lady who swallowed a spider.
+It wriggled and jiggled and tickled inside her.
+She swallowed the spider to catch the fly.
+I don't know why she swallowed the fly. Perhaps she'll die.
+
+I know an old lady who swallowed a bird.
+How absurd to swallow a bird!
+She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
+She swallowed the spider to catch the fly.
+I don't know why she swallowed the fly. Perhaps she'll die.
+
+I know an old lady who swallowed a cat.
+Imagine that, to swallow a cat!
+She swallowed the cat to catch the bird.
+She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
+She swallowed the spider to catch the fly.
+I don't know why she swallowed the fly. Perhaps she'll die.
+
+I know an old lady who swallowed a dog.
+What a hog, to swallow a dog!
+She swallowed the dog to catch the cat.
+She swallowed the cat to catch the bird.
+She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
+She swallowed the spider to catch the fly.
+I don't know why she swallowed the fly. Perhaps she'll die.
+
+I know an old lady who swallowed a goat.
+Just opened her throat and swallowed a goat!
+She swallowed the goat to catch the dog.
+She swallowed the dog to catch the cat.
+She swallowed the cat to catch the bird.
+She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
+She swallowed the spider to catch the fly.
+I don't know why she swallowed the fly. Perhaps she'll die.
+
+I know an old lady who swallowed a cow.
+I don't know how she swallowed a cow!
+She swallowed the cow to catch the goat.
+She swallowed the goat to catch the dog.
+She swallowed the dog to catch the cat.
+She swallowed the cat to catch the bird.
+She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
+She swallowed the spider to catch the fly.
+I don't know why she swallowed the fly. Perhaps she'll die.
+
+I know an old lady who swallowed a horse.
+She's dead, of course!
+```
+
+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
+
+Wikipedia [http://en.wikipedia.org/wiki/There_Was_an_Old_Lady_Who_Swallowed_a_Fly](http://en.wikipedia.org/wiki/There_Was_an_Old_Lady_Who_Swallowed_a_Fly)
+
+## 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/food-chain/food_chain.go b/go/food-chain/food_chain.go
new file mode 100644
index 0000000..ce651a8
--- /dev/null
+++ b/go/food-chain/food_chain.go
@@ -0,0 +1,15 @@
+package foodchain
+
+const testVersion = 2
+
+func Verse(n int) string {
+ return ""
+}
+
+func Verses(a, b int) string {
+ return ""
+}
+
+func Song() string {
+ return ""
+}
diff --git a/go/food-chain/food_chain_test.go b/go/food-chain/food_chain_test.go
new file mode 100644
index 0000000..179e9f5
--- /dev/null
+++ b/go/food-chain/food_chain_test.go
@@ -0,0 +1,95 @@
+package foodchain
+
+import (
+ "strings"
+ "testing"
+)
+
+const targetTestVersion = 2
+
+func TestTestVersion(t *testing.T) {
+ if testVersion != targetTestVersion {
+ t.Errorf("Found testVersion = %v, want %v.", testVersion, targetTestVersion)
+ }
+}
+
+var ref = []string{``,
+
+ `I know an old lady who swallowed a fly.
+I don't know why she swallowed the fly. Perhaps she'll die.`,
+
+ `I know an old lady who swallowed a spider.
+It wriggled and jiggled and tickled inside her.
+She swallowed the spider to catch the fly.
+I don't know why she swallowed the fly. Perhaps she'll die.`,
+
+ `I know an old lady who swallowed a bird.
+How absurd to swallow a bird!
+She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
+She swallowed the spider to catch the fly.
+I don't know why she swallowed the fly. Perhaps she'll die.`,
+
+ `I know an old lady who swallowed a cat.
+Imagine that, to swallow a cat!
+She swallowed the cat to catch the bird.
+She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
+She swallowed the spider to catch the fly.
+I don't know why she swallowed the fly. Perhaps she'll die.`,
+
+ `I know an old lady who swallowed a dog.
+What a hog, to swallow a dog!
+She swallowed the dog to catch the cat.
+She swallowed the cat to catch the bird.
+She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
+She swallowed the spider to catch the fly.
+I don't know why she swallowed the fly. Perhaps she'll die.`,
+
+ `I know an old lady who swallowed a goat.
+Just opened her throat and swallowed a goat!
+She swallowed the goat to catch the dog.
+She swallowed the dog to catch the cat.
+She swallowed the cat to catch the bird.
+She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
+She swallowed the spider to catch the fly.
+I don't know why she swallowed the fly. Perhaps she'll die.`,
+
+ `I know an old lady who swallowed a cow.
+I don't know how she swallowed a cow!
+She swallowed the cow to catch the goat.
+She swallowed the goat to catch the dog.
+She swallowed the dog to catch the cat.
+She swallowed the cat to catch the bird.
+She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.
+She swallowed the spider to catch the fly.
+I don't know why she swallowed the fly. Perhaps she'll die.`,
+
+ `I know an old lady who swallowed a horse.
+She's dead, of course!`,
+}
+
+func TestVerse(t *testing.T) {
+ for v := 1; v <= 8; v++ {
+ if ret := Verse(v); ret != ref[v] {
+ t.Fatalf("Verse(%d) =\n%s\n want:\n%s", v, ret, ref[v])
+ }
+ }
+}
+
+func TestVerses(t *testing.T) {
+ if ret, want := Verses(1, 2), ref[1]+"\n\n"+ref[2]; ret != want {
+ t.Fatalf("Verses(1, 2) =\n%s\n want:\n%s", ret, want)
+ }
+
+}
+
+func TestSong(t *testing.T) {
+ if ret, want := Song(), strings.Join(ref[1:], "\n\n"); ret != want {
+ t.Fatalf("Song() =\n%s\n want:\n%s", ret, want)
+ }
+}
+
+func BenchmarkSong(b *testing.B) {
+ for i := 0; i < b.N; i++ {
+ Song()
+ }
+}