Avatar of Ric0chet

Ric0chet's solution

to House in the Go Track

Published at May 20 2019 · 0 comments
Instructions
Test suite
Solution

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.

Running the tests

To run the tests run the command go test from within the exercise directory.

If the test suite contains benchmarks, you can run these with the --bench and --benchmem flags:

go test -v --bench . --benchmem

Keep in mind that each reviewer will run benchmarks on a different machine, with different specs, so the results from these benchmark tests may vary.

Further information

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

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.go

// As ever, there are different ways to complete this exercise.
// Try using using programmatic recursion to generate the verses of the song,
// thus reflecting the song's grammatical recursion.

// While recursion isn't always the simplest or most efficient solution to a problem,
// it's a powerful programming technique nonetheless.
//
// New to recursion? Here's a quick introduction:
// https://www.golang-book.com/books/intro/7#section5

package house

import (
	"strings"
	"testing"
)

var (
	// song copied from README
	expectedSong = `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.`

	expectedVerses = strings.Split(expectedSong, "\n\n")
)

func TestVerse(t *testing.T) {
	for v := 0; v < len(expectedVerses); v++ {
		if ret := Verse(v + 1); ret != expectedVerses[v] {
			t.Fatalf("Verse(%d) =\n%q\n  want:\n%q", v+1, ret, expectedVerses[v])
		}
	}
}

func TestSong(t *testing.T) {
	s := Song()
	if s == expectedSong {
		return
	}
	// a little help in locating an error
	gotStanzas := len(strings.Split(s, "\n\n"))
	wantStanzas := len(expectedVerses)
	if wantStanzas != gotStanzas {
		t.Fatalf("Song() has %d verse(s), want %d verses", gotStanzas, wantStanzas)
	}
	got := strings.Split(s, "\n")
	want := strings.Split(expectedSong, "\n")
	var g, w string
	var i int
	for i, w = range want {
		if len(got) <= i {
			g = ""
			break
		}
		if g = got[i]; g != w {
			break
		}
	}
	t.Fatalf("Song() line %d =\n%q\n want \n%q", i+1, g, w)
}
package house
// 04-07-2017

const testVersion = 1

var house = []string{
	"the house that Jack built.", // verse 1
	"the malt\nthat lay in ",
	"the rat\nthat ate ",
	"the cat\nthat killed ",
	"the dog\nthat worried ",
	"the cow with the crumpled horn\nthat tossed ",
	"the maiden all forlorn\nthat milked ",
	"the man all tattered and torn\nthat kissed ",
	"the priest all shaven and shorn\nthat married ",
	"the rooster that crowed in the morn\nthat woke ",
	"the farmer sowing his corn\nthat kept ",
	"the horse and the hound and the horn\nthat belonged to ", // verse 12
}

// Song returns entire song
func Song() (acc string) {
	for i := 1; i <= 12; i++ {
		acc += Verse(i)
		if i < 12 {
			acc += "\n\n"
		}
	}
	return
}

//	RECURSIVE METHOD (slightly slower)

// Verse creates numbered verse
func Verse(num int) string {
	return recurse(house[:num], "This is ")
}

func recurse(house []string, cur string) string {
	len := len(house)
	if len == 0 {
		return cur
	}
	return recurse(house[:len-1], cur+house[len-1])
}

/*
//	ITERATIVE METHOD (slightly faster)

// Verse creates numbered verse
func Verse(num int) (ver string) {
	ver = "This is "
	for i := num; i > 0; i-- {
		ver += house[i-1]
	}
	return
}
*/

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?