Initial Commit
This commit is contained in:
121
go/house/README.md
Normal file
121
go/house/README.md
Normal file
@@ -0,0 +1,121 @@
|
||||
# House
|
||||
|
||||
Write a program that outputs 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.
|
||||
|
||||
- [papyr.com](http://papyr.com/hypertextbooks/grammar/ph_noun.htm)
|
||||
|
||||
|
||||
The nursery rhyme reads as follows:
|
||||
|
||||
```plain
|
||||
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.
|
||||
```
|
||||
|
||||
To run the tests simply run the command `go test` in the exercise directory.
|
||||
|
||||
If the test suite contains benchmarks, you can run these with the `-bench`
|
||||
flag:
|
||||
|
||||
go test -bench .
|
||||
|
||||
For more detailed info about the Go track see the [help
|
||||
page](http://help.exercism.io/getting-started-with-go.html).
|
||||
|
||||
## Source
|
||||
|
||||
British nursery rhyme [view source](http://en.wikipedia.org/wiki/This_Is_The_House_That_Jack_Built)
|
50
go/house/house.go
Normal file
50
go/house/house.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package house
|
||||
|
||||
// ASong holds the data we need to generate a song
|
||||
type ASong struct {
|
||||
start string
|
||||
clauses []string
|
||||
end string
|
||||
}
|
||||
|
||||
// Embed takes two strings and returns a string with the embedded
|
||||
func Embed(l, p string) string {
|
||||
return l + " " + p
|
||||
}
|
||||
|
||||
// Verse takes three things and returns a verse
|
||||
func Verse(start string, clause []string, end string) string {
|
||||
ret := start + " "
|
||||
for i := 0; i < len(clause); i++ {
|
||||
ret += clause[i] + " "
|
||||
}
|
||||
ret += end
|
||||
return ret
|
||||
}
|
||||
|
||||
// Song generates the whole song
|
||||
func Song() string {
|
||||
jackSong := ASong{
|
||||
start: "This is",
|
||||
clauses: []string{
|
||||
"the horse and the hound and the horn\nthat belonged to",
|
||||
"the farmer sowing his corn\nthat kept",
|
||||
"the rooster that crowed in the morn\nthat woke",
|
||||
"the priest all shaven and shorn\nthat married",
|
||||
"the man all tattered and torn\nthat kissed",
|
||||
"the maiden all forlorn\nthat milked",
|
||||
"the cow with the crumpled horn\nthat tossed",
|
||||
"the dog\nthat worried",
|
||||
"the cat\nthat killed",
|
||||
"the rat\nthat ate",
|
||||
"the malt\nthat lay in",
|
||||
},
|
||||
end: "the house that Jack built.",
|
||||
}
|
||||
var ret string
|
||||
for i := len(jackSong.clauses); i >= 0; i-- {
|
||||
ret += Verse(jackSong.start, jackSong.clauses[i:], jackSong.end) + "\n\n"
|
||||
}
|
||||
// Trim the trailing "\n\n"
|
||||
return ret[:len(ret)-2]
|
||||
}
|
171
go/house/house_test.go
Normal file
171
go/house/house_test.go
Normal file
@@ -0,0 +1,171 @@
|
||||
// Embed embeds a noun phrase as the object of relative clause with a
|
||||
// transitive verb.
|
||||
//
|
||||
// Argument relPhrase is a phrase with a relative clause, minus the object
|
||||
// of the clause. That is, relPhrase consists of a subject, a relative
|
||||
// pronoun, a transitive verb, possibly a preposition, but then no object.
|
||||
//
|
||||
// func Embed(relPhrase, nounPhrase string) string
|
||||
|
||||
// Verse generates a verse of a song with relative clauses that have
|
||||
// a recursive structure.
|
||||
//
|
||||
// func Verse(subject string, relPhrases []string, nounPhrase string) string
|
||||
//
|
||||
// There are different ways to do this of course, but try using Embed as a
|
||||
// subroutine and using programmatic recursion that reflects the grammatical
|
||||
// recursion.
|
||||
|
||||
// Song generates the full text of "The House That Jack Built". Oh yes, you
|
||||
// could just return a string literal, but humor us; use Verse as a subroutine.
|
||||
//
|
||||
// func Song() string
|
||||
|
||||
package house
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var (
|
||||
s = "This is"
|
||||
r = []string{
|
||||
"the cat that broke",
|
||||
"the vase that was on",
|
||||
}
|
||||
p = "the shelf."
|
||||
last = len(r) - 1
|
||||
// song copied from readme
|
||||
song = `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.`
|
||||
)
|
||||
|
||||
func TestEmbed(t *testing.T) {
|
||||
l := r[last]
|
||||
want := l + " " + p
|
||||
if e := Embed(l, p); e != want {
|
||||
t.Fatalf("Embed(%q, %q) = %q, want %q.", l, p, e, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestVerse(t *testing.T) {
|
||||
for i := len(r); i >= 0; i-- {
|
||||
ri := r[i:]
|
||||
want := s + " " + strings.Join(append(ri, p), " ")
|
||||
if v := Verse(s, ri, p); v != want {
|
||||
t.Fatalf("Verse(%q, %q, %q) = %q, want %q.", s, ri, p, v, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSong(t *testing.T) {
|
||||
s := Song()
|
||||
if s == song {
|
||||
return
|
||||
}
|
||||
fmt.Print(s)
|
||||
// a little help in locating an error
|
||||
got := strings.Split(s, "\n")
|
||||
want := strings.Split(song, "\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 = %q, want %q", i+1, g, w)
|
||||
}
|
Reference in New Issue
Block a user