Avatar of JaeHyoLee

JaeHyoLee's solution

to Food Chain in the Lua Track

Published at Jul 13 2018 · 2 comments
Instructions
Test suite
Solution

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

Generate the lyrics of 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 of unknown origin.

This is one of many common variants.

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!

Running the tests

To run the tests, run the command busted from within the exercise directory.

Further information

For more detailed information about the Lua track, including how to get help if you're having trouble, please visit the exercism.io Lua language page.

Source

Wikipedia http://en.wikipedia.org/wiki/There_Was_an_Old_Lady_Who_Swallowed_a_Fly

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

food-chain_spec.lua

local song = require('food-chain')

describe('food-hain', function ()
  it('fly', function ()
    local expected = "I know an old lady who swallowed a fly.\nI don't know why she swallowed the fly. Perhaps she'll die.\n"

    assert.are.equal(expected, song.verse(1))
  end)

  it('spider', function ()
    local expected = "I know an old lady who swallowed a spider.\nIt wriggled and jiggled and tickled inside her.\n" ..
      "She swallowed the spider to catch the fly.\n" ..
      "I don't know why she swallowed the fly. Perhaps she'll die.\n"

    assert.are.equal(expected, song.verse(2))
  end)

  it('bird', function ()
    local expected = "I know an old lady who swallowed a bird.\n" ..
      "How absurd to swallow a bird!\n" ..
      "She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.\n" ..
      "She swallowed the spider to catch the fly.\n" ..
      "I don't know why she swallowed the fly. Perhaps she'll die.\n"

    assert.are.equal(expected, song.verse(3))
  end)

  it('cat', function ()
    local expected = "I know an old lady who swallowed a cat.\n" ..
      "Imagine that, to swallow a cat!\n" ..
      "She swallowed the cat to catch the bird.\n" ..
      "She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.\n" ..
      "She swallowed the spider to catch the fly.\n" ..
      "I don't know why she swallowed the fly. " ..
      "Perhaps she'll die.\n"

    assert.are.equal(expected, song.verse(4))
  end)

  it('dog', function ()
    local expected = "I know an old lady who swallowed a dog.\n" ..
      "What a hog, to swallow a dog!\n" ..
      "She swallowed the dog to catch the cat.\n" ..
      "She swallowed the cat to catch the bird.\n" ..
      "She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.\n" ..
      "She swallowed the spider to catch the fly.\n" ..
      "I don't know why she swallowed the fly. " ..
      "Perhaps she'll die.\n"

    assert.are.equal(expected, song.verse(5))
  end)

  it('goat', function ()
    local expected = "I know an old lady who swallowed a goat.\n" ..
      "Just opened her throat and swallowed a goat!\n" ..
      "She swallowed the goat to catch the dog.\n" ..
      "She swallowed the dog to catch the cat.\n" ..
      "She swallowed the cat to catch the bird.\n" ..
      "She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.\n" ..
      "She swallowed the spider to catch the fly.\n" ..
      "I don't know why she swallowed the fly. " ..
      "Perhaps she'll die.\n"

    assert.are.equal(expected, song.verse(6))
  end)

  it('cow', function ()
    local expected = "I know an old lady who swallowed a cow.\n" ..
      "I don't know how she swallowed a cow!\n" ..
      "She swallowed the cow to catch the goat.\n" ..
      "She swallowed the goat to catch the dog.\n" ..
      "She swallowed the dog to catch the cat.\n" ..
      "She swallowed the cat to catch the bird.\n" ..
      "She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.\n" ..
      "She swallowed the spider to catch the fly.\n" ..
      "I don't know why she swallowed the fly. " ..
      "Perhaps she'll die.\n"

    assert.are.equal(expected, song.verse(7))
  end)

  it('horse', function ()
    local expected = "I know an old lady who swallowed a horse.\n" ..
      "She's dead, of course!\n"

    assert.are.equal(expected, song.verse(8))
  end)

  it('multiple verses', function ()
    local expected = ""

    expected = "I know an old lady who swallowed a fly.\nI don't know why she swallowed the fly. Perhaps she'll die.\n\n" ..
      "I know an old lady who swallowed a spider.\nIt wriggled and jiggled and tickled inside her.\n" ..
      "She swallowed the spider to catch the fly.\n" ..
      "I don't know why she swallowed the fly. Perhaps she'll die.\n\n"

    assert.are.equal(expected, song.verses(1, 2))
  end)

  it('the whole song', function ()
    assert.are.equal(song.verses(1, 8), song.sing())
  end)
end)
local lyrics_table = {
  ['fly'] = { response = "I don't know why she swallowed the ", name = true, suffix = ". Perhaps she'll die."},
  ['spider'] = { response = "It wriggled and jiggled and tickled inside her.", name = false, supplement = " that wriggled and jiggled and tickled inside her"},
  ['bird'] = { response = "How absurd to swallow a ", name = true, suffix = "!" },
  ['cat'] = { response  = "Imagine that, to swallow a ", name = true,  suffix = "!"},
  ['dog'] = { response  = "What a hog, to swallow a ", name = true,  suffix = "!"},
  ['goat'] = { response  = "Just opened her throat and swallowed a ", name = true,  suffix = "!"},
  ['cow'] = { response  = "I don't know how she swallowed a ", name = true,  suffix = "!"},
  ['horse'] = { response = "She's dead, of course!",  name = false }
}

local foods = { 'fly', 'spider', 'bird', 'cat', 'dog', 'goat', 'cow', 'horse' }

setmetatable(foods, {__index = lyrics_table})

local function make_cumulative_verse(index)
  local name = foods[index]
  local cumulated_verse = 'I know an old lady who swallowed a '.. name .. '.\n'
  cumulated_verse = cumulated_verse .. foods[name].response

  if foods[name].name == true then
    cumulated_verse = cumulated_verse .. name
  end

  cumulated_verse = cumulated_verse .. (foods[name].suffix or '') .. '\n'

  if foods[name].suffix == nil and foods[name].supplement == nil or index == 1 then return cumulated_verse end

  for i = index, 1, -1 do
    local name = foods[i]
    if foods[i -1] == nil then
      cumulated_verse = cumulated_verse .. foods[name].response .. name .. foods[name].suffix .. '\n'
    else
      cumulated_verse = cumulated_verse .. 'She swallowed the '.. name..' to catch the '.. foods[i -1].. (foods[foods[i-1]].supplement or '').. '.\n'
    end
  end
  return cumulated_verse
end

local function verse(from, to)
  local lyrics
  for index = from, (to or from) do
     lyrics = lyrics and lyrics .. '\n'
     lyrics = (lyrics or '') .. make_cumulative_verse(index)
  end
  return lyrics
end

local function verses(from, to)
  return verse(from, to) .. '\n'
end

local function sing()
  return verses(1, 8)
end

return {
  verse = verse,
  verses = verses,
  sing = sing
}

Community comments

Find this solution interesting? Ask the author a question to learn more.
Avatar of ryanplusplus

You can combine the foods and lyrics_table tables together like this: local foods = { 'fly', 'spider', 'bird', 'cat', 'dog', 'goat', 'cow', 'horse', ['fly'] = { response = "I don't know why she swallowed the ", name = true, suffix = ". Perhaps she'll die."}, ['spider'] = { response = "It wriggled and jiggled and tickled inside her.", name = false, supplement = " that wriggled and jiggled and tickled inside her"}, ['bird'] = { response = "How absurd to swallow a ", name = true, suffix = "!" }, ['cat'] = { response = "Imagine that, to swallow a ", name = true, suffix = "!"}, ['dog'] = { response = "What a hog, to swallow a ", name = true, suffix = "!"}, ['goat'] = { response = "Just opened her throat and swallowed a ", name = true, suffix = "!"}, ['cow'] = { response = "I don't know how she swallowed a ", name = true, suffix = "!"}, }

The first values will be interpreted as array values (automatically indexed starting from 1) and then the rest will be inserted as hash/map values. It will behave identically to your current solution with a metatable.

Avatar of ryanplusplus

You have a magic number on line 54! It's really easy to fix: return verses(1, #foods)

Even if you combine foods with lyrics_table it will still correctly count the array part of the table with # and give you 8.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?