Avatar of G-Rath

G-Rath's solution

to House in the TypeScript Track

Published at Mar 04 2019 · 0 comments
Instructions
Test suite
Solution

Note:

This exercise has changed since this solution was written.

Recite the nursery rhyme 'This is the House that Jack Built'.

[The] process of placing a phrase of clause within another phrase of clause is called embedding. It is through the processes of recursion and embedding that we are able to take a finite number of forms (words and phrases) and construct an infinite number of expressions. Furthermore, embedding also allows us to construct an infinitely long structure, in theory anyway.

The nursery rhyme reads as follows:

This is the house that Jack built.

This is the malt
that lay in the house that Jack built.

This is the rat
that ate the malt
that lay in the house that Jack built.

This is the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.

This is the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.

This is the cow with the crumpled horn
that tossed the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.

This is the maiden all forlorn
that milked the cow with the crumpled horn
that tossed the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.

This is the man all tattered and torn
that kissed the maiden all forlorn
that milked the cow with the crumpled horn
that tossed the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.

This is the priest all shaven and shorn
that married the man all tattered and torn
that kissed the maiden all forlorn
that milked the cow with the crumpled horn
that tossed the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.

This is the rooster that crowed in the morn
that woke the priest all shaven and shorn
that married the man all tattered and torn
that kissed the maiden all forlorn
that milked the cow with the crumpled horn
that tossed the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.

This is the farmer sowing his corn
that kept the rooster that crowed in the morn
that woke the priest all shaven and shorn
that married the man all tattered and torn
that kissed the maiden all forlorn
that milked the cow with the crumpled horn
that tossed the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.

This is the horse and the hound and the horn
that belonged to the farmer sowing his corn
that kept the rooster that crowed in the morn
that woke the priest all shaven and shorn
that married the man all tattered and torn
that kissed the maiden all forlorn
that milked the cow with the crumpled horn
that tossed the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.

Setup

Go through the setup instructions for TypeScript to install the necessary dependencies:

http://exercism.io/languages/typescript

Requirements

Install assignment dependencies:

$ yarn install

Making the test suite pass

Execute the tests with:

$ yarn test

Source

British nursery rhyme http://en.wikipedia.org/wiki/This_Is_The_House_That_Jack_Built

Submitting Incomplete Solutions

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

house.test.ts

import House from './house'

describe('House', () => {
    it('verse one - the house that jack built', () => {
        const lyrics = [
            'This is the house that Jack built.',
        ]
        expect(House.verse(1)).toEqual(lyrics)
    })

    xit('verse two - the malt that lay', () => {
        const lyrics = [
            'This is the malt',
            'that lay in the house that Jack built.',
        ]
        expect(House.verse(2)).toEqual(lyrics)
    })

    xit('verse three - the rat that ate', () => {
        const lyrics = [
            'This is the rat',
            'that ate the malt',
            'that lay in the house that Jack built.',
        ]
        expect(House.verse(3)).toEqual(lyrics)
    })

    xit('verse four - the cat that killed', () => {
        const lyrics = [
            'This is the cat',
            'that killed the rat',
            'that ate the malt',
            'that lay in the house that Jack built.',
        ]
        expect(House.verse(4)).toEqual(lyrics)
    })

    xit('verse five - the dog that worried', () => {
        const lyrics = [
            'This is the dog',
            'that worried the cat',
            'that killed the rat',
            'that ate the malt',
            'that lay in the house that Jack built.',
        ]
        expect(House.verse(5)).toEqual(lyrics)
    })

    xit('verse six - the cow with the crumpled horn', () => {
        const lyrics = [
            'This is the cow with the crumpled horn',
            'that tossed the dog',
            'that worried the cat',
            'that killed the rat',
            'that ate the malt',
            'that lay in the house that Jack built.',
        ]
        expect(House.verse(6)).toEqual(lyrics)
    })

    xit('verse seven - the maiden all forlorn', () => {
        const lyrics = [
            'This is the maiden all forlorn',
            'that milked the cow with the crumpled horn',
            'that tossed the dog',
            'that worried the cat',
            'that killed the rat',
            'that ate the malt',
            'that lay in the house that Jack built.',
        ]
        expect(House.verse(7)).toEqual(lyrics)
    })

    xit('verse eight - the man all tattered and torn', () => {
        const lyrics = [
            'This is the man all tattered and torn',
            'that kissed the maiden all forlorn',
            'that milked the cow with the crumpled horn',
            'that tossed the dog',
            'that worried the cat',
            'that killed the rat',
            'that ate the malt',
            'that lay in the house that Jack built.',
        ]
        expect(House.verse(8)).toEqual(lyrics)
    })

    xit('verse nine - the priest all shaven and shorn', () => {
        const lyrics = [
            'This is the priest all shaven and shorn',
            'that married the man all tattered and torn',
            'that kissed the maiden all forlorn',
            'that milked the cow with the crumpled horn',
            'that tossed the dog',
            'that worried the cat',
            'that killed the rat',
            'that ate the malt',
            'that lay in the house that Jack built.',
        ]
        expect(House.verse(9)).toEqual(lyrics)
    })

    xit('verse ten - the rooster that crowed in the morn', () => {
        const lyrics = [
            'This is the rooster that crowed in the morn',
            'that woke the priest all shaven and shorn',
            'that married the man all tattered and torn',
            'that kissed the maiden all forlorn',
            'that milked the cow with the crumpled horn',
            'that tossed the dog',
            'that worried the cat',
            'that killed the rat',
            'that ate the malt',
            'that lay in the house that Jack built.',
        ]
        expect(House.verse(10)).toEqual(lyrics)
    })

    xit('verse eleven - the farmer sowing his corn', () => {
        const lyrics = [
            'This is the farmer sowing his corn',
            'that kept the rooster that crowed in the morn',
            'that woke the priest all shaven and shorn',
            'that married the man all tattered and torn',
            'that kissed the maiden all forlorn',
            'that milked the cow with the crumpled horn',
            'that tossed the dog',
            'that worried the cat',
            'that killed the rat',
            'that ate the malt',
            'that lay in the house that Jack built.',
        ]
        expect(House.verse(11)).toEqual(lyrics)
    })

    xit('verse twelve - the horse and the hound and the horn', () => {
        const lyrics = [
            'This is the horse and the hound and the horn',
            'that belonged to the farmer sowing his corn',
            'that kept the rooster that crowed in the morn',
            'that woke the priest all shaven and shorn',
            'that married the man all tattered and torn',
            'that kissed the maiden all forlorn',
            'that milked the cow with the crumpled horn',
            'that tossed the dog',
            'that worried the cat',
            'that killed the rat',
            'that ate the malt',
            'that lay in the house that Jack built.',
        ]
        expect(House.verse(12)).toEqual(lyrics)
    })

    xit('multiple verses', () => {
        const startVerse = 4
        const endVerse = 8
        const lyrics = [
            'This is the cat',
            'that killed the rat',
            'that ate the malt',
            'that lay in the house that Jack built.',
            '',
            'This is the dog',
            'that worried the cat',
            'that killed the rat',
            'that ate the malt',
            'that lay in the house that Jack built.',
            '',
            'This is the cow with the crumpled horn',
            'that tossed the dog',
            'that worried the cat',
            'that killed the rat',
            'that ate the malt',
            'that lay in the house that Jack built.',
            '',
            'This is the maiden all forlorn',
            'that milked the cow with the crumpled horn',
            'that tossed the dog',
            'that worried the cat',
            'that killed the rat',
            'that ate the malt',
            'that lay in the house that Jack built.',
            '',
            'This is the man all tattered and torn',
            'that kissed the maiden all forlorn',
            'that milked the cow with the crumpled horn',
            'that tossed the dog',
            'that worried the cat',
            'that killed the rat',
            'that ate the malt',
            'that lay in the house that Jack built.',
        ]
        expect(House.verses(startVerse, endVerse)).toEqual(lyrics)
    })

    xit('full rhyme', () => {
        const startVerse = 1
        const endVerse = 12
        const lyrics = [
            'This is the house that Jack built.',
            '',
            'This is the malt',
            'that lay in the house that Jack built.',
            '',
            'This is the rat',
            'that ate the malt',
            'that lay in the house that Jack built.',
            '',
            'This is the cat',
            'that killed the rat',
            'that ate the malt',
            'that lay in the house that Jack built.',
            '',
            'This is the dog',
            'that worried the cat',
            'that killed the rat',
            'that ate the malt',
            'that lay in the house that Jack built.',
            '',
            'This is the cow with the crumpled horn',
            'that tossed the dog',
            'that worried the cat',
            'that killed the rat',
            'that ate the malt',
            'that lay in the house that Jack built.',
            '',
            'This is the maiden all forlorn',
            'that milked the cow with the crumpled horn',
            'that tossed the dog',
            'that worried the cat',
            'that killed the rat',
            'that ate the malt',
            'that lay in the house that Jack built.',
            '',
            'This is the man all tattered and torn',
            'that kissed the maiden all forlorn',
            'that milked the cow with the crumpled horn',
            'that tossed the dog',
            'that worried the cat',
            'that killed the rat',
            'that ate the malt',
            'that lay in the house that Jack built.',
            '',
            'This is the priest all shaven and shorn',
            'that married the man all tattered and torn',
            'that kissed the maiden all forlorn',
            'that milked the cow with the crumpled horn',
            'that tossed the dog',
            'that worried the cat',
            'that killed the rat',
            'that ate the malt',
            'that lay in the house that Jack built.',
            '',
            'This is the rooster that crowed in the morn',
            'that woke the priest all shaven and shorn',
            'that married the man all tattered and torn',
            'that kissed the maiden all forlorn',
            'that milked the cow with the crumpled horn',
            'that tossed the dog',
            'that worried the cat',
            'that killed the rat',
            'that ate the malt',
            'that lay in the house that Jack built.',
            '',
            'This is the farmer sowing his corn',
            'that kept the rooster that crowed in the morn',
            'that woke the priest all shaven and shorn',
            'that married the man all tattered and torn',
            'that kissed the maiden all forlorn',
            'that milked the cow with the crumpled horn',
            'that tossed the dog',
            'that worried the cat',
            'that killed the rat',
            'that ate the malt',
            'that lay in the house that Jack built.',
            '',
            'This is the horse and the hound and the horn',
            'that belonged to the farmer sowing his corn',
            'that kept the rooster that crowed in the morn',
            'that woke the priest all shaven and shorn',
            'that married the man all tattered and torn',
            'that kissed the maiden all forlorn',
            'that milked the cow with the crumpled horn',
            'that tossed the dog',
            'that worried the cat',
            'that killed the rat',
            'that ate the malt',
            'that lay in the house that Jack built.',
        ]
        expect(House.verses(startVerse, endVerse)).toEqual(lyrics)
    })
})
interface VerseDetails {
  /**
   * The 'item' that this `Verse` introduced.
   */
  item: string
  /**
   * The action that this Verses `item` 'thats' upon the next Verses `item`.
   */
  that: string
}

export default class House {
  /**
   * Gets the details for the given `verse` from the `This is the House that Jack Built` nursery rhyme.
   *
   * @param {number} verse
   *
   * @return {VerseDetails}
   * @private
   */
  private static _getVerseDetails(verse: number): VerseDetails {
    return [
      { item: 'house that Jack built.', that: '' },
      { item: 'malt', that: 'lay in' },
      { item: 'rat', that: 'ate' },
      { item: 'cat', that: 'killed' },
      { item: 'dog', that: 'worried' },
      { item: 'cow with the crumpled horn', that: 'tossed' },
      { item: 'maiden all forlorn', that: 'milked' },
      { item: 'man all tattered and torn', that: 'kissed' },
      { item: 'priest all shaven and shorn', that: 'married' },
      { item: 'rooster that crowed in the morn', that: 'woke' },
      { item: 'farmer sowing his corn', that: 'kept' },
      { item: 'horse and the hound and the horn', that: 'belonged to' }
    ][verse - 1]
  }

  /**
   * Generates a verse to the `This is the House that Jack Built` nursery rhyme.
   *
   * @param {number} verse
   *
   * @return {Array<string>}
   */
  public static verse(verse: number): string[] {
    const details = Array(verse).fill(undefined).map((_, index) => this._getVerseDetails(index + 1))

    return details.map((verseDetails, index) => {
      if ((index + 1) === verse) {
        return `This is the ${verseDetails.item}`
      }

      return `that ${details[index + 1].that} the ${verseDetails.item}`
    }).reverse()
  }

  /**
   * Generates all the verses to the `This is the House that Jack Built` nursery rhyme,
   * between the given `from` & `to` range.
   *
   * @param {number} from
   * @param {number} to
   *
   * @return {string}
   */
  public static verses(from: number, to: number): string[] {
    const verses = []

    for (let i = from; i <= to; i++) {
      verses.push(this.verse(i), [''])
    }

    verses.pop()

    return verses.reduce((carry, array) => carry.concat(array), [])
  }
}

Community comments

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

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?