Skip to content

Commit

Permalink
add food chain exercise
Browse files Browse the repository at this point in the history
  • Loading branch information
hucancode committed Jan 31, 2025
1 parent 2773422 commit c8d9c90
Show file tree
Hide file tree
Showing 2 changed files with 272 additions and 0 deletions.
93 changes: 93 additions & 0 deletions exercises/practice/food-chain/food_chain.odin
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package food_chain

import "core:strings"

Animal :: struct {
name: string,
name_long: Maybe(string),
phrase: string,
next: Maybe(^Animal),
}

fly := Animal {
name = "fly",
phrase = "I don't know why she swallowed the fly. Perhaps she'll die.",
}
spider := Animal {
name = "spider",
name_long = "spider that wriggled and jiggled and tickled inside her",
phrase = "It wriggled and jiggled and tickled inside her.",
next = &fly,
}
bird := Animal {
name = "bird",
phrase = "How absurd to swallow a bird!",
next = &spider,
}
cat := Animal {
name = "cat",
phrase = "Imagine that, to swallow a cat!",
next = &bird,
}
dog := Animal {
name = "dog",
phrase = "What a hog, to swallow a dog!",
next = &cat,
}
goat := Animal {
name = "goat",
phrase = "Just opened her throat and swallowed a goat!",
next = &dog,
}
cow := Animal {
name = "cow",
phrase = "I don't know how she swallowed a cow!",
next = &goat,
}
horse := Animal {
name = "horse",
phrase = "She's dead, of course!",
}

verses := [8]Animal{fly, spider, bird, cat, dog, goat, cow, horse}

generate_verse :: proc(builder: ^strings.Builder, i: int) {
animal: Maybe(^Animal) = &verses[i]
node, ok := animal.?
if !ok {
return
}
strings.write_string(builder, "I know an old lady who swallowed a ")
strings.write_string(builder, node.name)
strings.write_string(builder, ".\n")
if node.next != nil {
strings.write_string(builder, node.phrase)
strings.write_rune(builder, '\n')
}
for {
node := animal.? or_break
if node.next == nil {
strings.write_string(builder, node.phrase)
strings.write_rune(builder, '\n')
} else {
next := node.next.?
strings.write_string(builder, "She swallowed the ")
strings.write_string(builder, node.name)
strings.write_string(builder, " to catch the ")
name := next.name_long.? or_else next.name
strings.write_string(builder, name)
strings.write_string(builder, ".\n")
}
animal = node.next
}
}

recite :: proc(start, end: int) -> string {
builder := strings.builder_make()
defer strings.builder_destroy(&builder)
for i in start - 1 ..< end {
generate_verse(&builder, i)
strings.write_rune(&builder, '\n')
}
return strings.trim_right_space(strings.to_string(builder))
}
179 changes: 179 additions & 0 deletions exercises/practice/food-chain/food_chain_test.odin
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
package food_chain
import "core:testing"

@(test)
test_fly :: proc(t: ^testing.T) {
expected := `I know an old lady who swallowed a fly.
I don't know why she swallowed the fly. Perhaps she'll die.`


actual := recite(1, 1)
testing.expect_value(t, actual, expected)
}
@(test)
test_spider :: proc(t: ^testing.T) {
expected := `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.`


actual := recite(2, 2)
testing.expect_value(t, actual, expected)
}
@(test)
test_bird :: proc(t: ^testing.T) {
expected := `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.`


actual := recite(3, 3)
testing.expect_value(t, actual, expected)
}
@(test)
test_cat :: proc(t: ^testing.T) {
expected := `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.`


actual := recite(4, 4)
testing.expect_value(t, actual, expected)
}
@(test)
test_dog :: proc(t: ^testing.T) {
expected := `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.`


actual := recite(5, 5)
testing.expect_value(t, actual, expected)
}
@(test)
test_goat :: proc(t: ^testing.T) {
expected := `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.`


actual := recite(6, 6)
testing.expect_value(t, actual, expected)
}
@(test)
test_cow :: proc(t: ^testing.T) {
expected := `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.`


actual := recite(7, 7)
testing.expect_value(t, actual, expected)
}
@(test)
test_horse :: proc(t: ^testing.T) {
expected := `I know an old lady who swallowed a horse.
She's dead, of course!`


actual := recite(8, 8)
testing.expect_value(t, actual, expected)
}
@(test)
test_multiple_verse :: proc(t: ^testing.T) {
expected := `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.`


actual := recite(1, 3)
testing.expect_value(t, actual, expected)
}
@(test)
test_full_song :: proc(t: ^testing.T) {
expected := `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!`


actual := recite(1, 8)
testing.expect_value(t, actual, expected)
}

0 comments on commit c8d9c90

Please sign in to comment.