Skip to content

Commit

Permalink
solve food chain
Browse files Browse the repository at this point in the history
  • Loading branch information
rootulp committed Nov 29, 2023
1 parent eb388e7 commit 61183de
Showing 1 changed file with 37 additions and 57 deletions.
94 changes: 37 additions & 57 deletions go/food-chain/food_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,46 +16,6 @@ var verseNumberToStartingAnimal = map[int]string{
8: "horse",
}

func Verse(verseNumber int) string {
lines := []string{firstLine(startingAnimal(verseNumber))}
lines = append(lines, middleLines(verseNumber)...)
lines = append(lines, lastLine(verseNumber))
return strings.Join(lines, "\n")
}

func middleLines(verseNumber int) []string {
if verseNumber <= 1 {
return []string{}
}
if verseNumber == 2 {
return verseNumberToMiddleLines[verseNumber]
}
if verseNumber == 3 {
lines := verseNumberToMiddleLines[verseNumber]
return append(lines, middleLines(verseNumber - 1)[1:]...)
}
if verseNumber == 4 {
lines := verseNumberToMiddleLines[verseNumber]
return append(lines, middleLines(verseNumber - 1)[1:]...)
}
if verseNumber == 5 {
lines := verseNumberToMiddleLines[verseNumber]
return append(lines, middleLines(verseNumber - 1)[1:]...)
}
if verseNumber == 6 {
lines := verseNumberToMiddleLines[verseNumber]
return append(lines, middleLines(verseNumber - 1)[1:]...)
}
if verseNumber == 7 {
lines := verseNumberToMiddleLines[verseNumber]
return append(lines, middleLines(verseNumber - 1)[1:]...)
}
if verseNumber == 8 {
return []string{}
}
panic(fmt.Sprintf("unsupported verseNumber %v", verseNumber))
}

var verseNumberToMiddleLines = map[int][]string{
2: {
"It wriggled and jiggled and tickled inside her.",
Expand All @@ -65,37 +25,30 @@ var verseNumberToMiddleLines = map[int][]string{
"How absurd to swallow a bird!",
"She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.",
},
4: []string{
4: {
"Imagine that, to swallow a cat!",
"She swallowed the cat to catch the bird.",
},
5: []string{
5: {
"What a hog, to swallow a dog!",
"She swallowed the dog to catch the cat.",
},
6: []string{
6: {
"Just opened her throat and swallowed a goat!",
"She swallowed the goat to catch the dog.",
},
7: []string{
7: {
"I don't know how she swallowed a cow!",
"She swallowed the cow to catch the goat.",
},
}

func startingAnimal(verseNumber int) string {
return verseNumberToStartingAnimal[verseNumber]
}

func firstLine(animal string) string {
return fmt.Sprintf("I know an old lady who swallowed a %v.", animal)
}

func lastLine(verseNumber int) string {
if verseNumber == 8 {
return "She's dead, of course!"
}
return "I don't know why she swallowed the fly. Perhaps she'll die."
func Verse(verseNumber int) string {
lines := []string{}
lines = append(lines, firstLine(verseNumber))
lines = append(lines, middleLines(verseNumber)...)
lines = append(lines, lastLine(verseNumber))
return strings.Join(lines, "\n")
}

func Verses(start, end int) string {
Expand All @@ -109,3 +62,30 @@ func Verses(start, end int) string {
func Song() string {
return Verses(1, 8)
}

func middleLines(verseNumber int) []string {
if verseNumber <= 1 || verseNumber == 8 {
return []string{}
}
if verseNumber == 2 {
return verseNumberToMiddleLines[verseNumber]
}
lines := verseNumberToMiddleLines[verseNumber]
return append(lines, middleLines(verseNumber - 1)[1:]...)
}

func firstLine(verseNumber int) string {
animal := startingAnimal(verseNumber)
return fmt.Sprintf("I know an old lady who swallowed a %v.", animal)
}

func startingAnimal(verseNumber int) string {
return verseNumberToStartingAnimal[verseNumber]
}

func lastLine(verseNumber int) string {
if verseNumber == 8 {
return "She's dead, of course!"
}
return "I don't know why she swallowed the fly. Perhaps she'll die."
}

0 comments on commit 61183de

Please sign in to comment.