{"id":287,"date":"2020-01-17T11:24:57","date_gmt":"2020-01-17T11:24:57","guid":{"rendered":"https:\/\/www.samarthya.me\/wps\/?p=287"},"modified":"2020-01-17T11:33:47","modified_gmt":"2020-01-17T11:33:47","slug":"house-that-jack-built","status":"publish","type":"post","link":"https:\/\/blog.samarthya.me\/wps\/2020\/01\/17\/house-that-jack-built\/","title":{"rendered":"House that Jack built."},"content":{"rendered":"<p>Funny thing about simple programs is that there are too many optimization options available that never lets you feel content.<\/p>\n<p>So here I am trying to solve another problem from <a href=\"https:\/\/exercism.io\/my\/solutions\/19fb3977b3be4ff4bedfee14c711af1a\">exercism.io<\/a> which is totally a must visit for anyone who wants to learn and practice.<\/p>\n<blockquote>\n<p>Nursery rhyme<\/p>\n<\/blockquote>\n\n\n<p>I wrote a couple of version before trying to improvise in a different way what soothes the undying thirst of  improving that something, which is still not right. <\/p>\n\n\n\n<blockquote class=\"wp-block-quote has-text-align-right is-layout-flow wp-block-quote-is-layout-flow\"><p>Beauty lies in the eyes of beholder!<\/p><cite>Funny context though<\/cite><\/blockquote>\n\n\n\n<p>It is still not the best <code>version<\/code>, but has brought some peace to my vacillating mind.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">The Rhyme<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">This is the house that Jack built.\n\nThis is the malt\nthat lay in the house that Jack built.\n\nThis is the rat\nthat ate the malt\nthat lay in the house that Jack built.\n\nThis is the cat\nthat killed the rat\nthat ate the malt\nthat lay in the house that Jack built.\n\nThis is the dog\nthat worried the cat\nthat killed the rat\nthat ate the malt\nthat lay in the house that Jack built.\n\nThis is 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 the house that Jack built.\n\nThis is 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 the house that Jack built.\n\nThis is 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 the house that Jack built.\n\nThis is 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 the house that Jack built.\n\nThis is 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 the house that Jack built.\n\nThis is 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 the house that Jack built.\n\nThis is 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 the house that Jack built.<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Logic building<\/h2>\n\n\n\n<p>The obvious question I had in mind was, what can help me break it in a way that it has a meaningful structure.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Grammar to the rescue<\/h3>\n\n\n\n<p>The subject is the &#8220;who&#8221; or &#8220;what&#8221; of the sentence, the predicate is the verb, and the object is any noun or concept that is part of the action of the subject.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>Subject, predicate, and objects are the three different components when breaking down a sentence. <\/p><\/blockquote>\n\n\n\n<p>This helped me define a structure that I though will help me solve the poem into smaller chunks for each para.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">type Predicate struct {\n    character string\n    predicate string\n}\n\ntype Character struct {\n    adjective string\n    pred      Predicate\n}<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Example<\/strong>: <code>This is the house that Jack built.<\/code><\/pre>\n\n\n\n<p>In this I was able to Identify <code>house<\/code> as the <code>Character<\/code> (Subject) and <code>built<\/code> is the <code>action<\/code> or <code>predicate<\/code> and Jack is the <code>character<\/code> embedded in predicate who took the action.<\/p>\n\n\n\n<p>Using this as a building block I initialized two maps.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">var <strong>verses<\/strong> <strong>map<\/strong>[int]string = map[int]string{\n    1: \"house\",\n    2: \"malt\",\n    3: \"rat\",\n    4: \"cat\",\n    5: \"dog\",\n    6: \"cow\", \n    7: \"maiden\",\n    8: \"man\", \n    9: \"priest\",\n    10: \"rooster\", \n    11: \"farmer\",\n    12: \"horse\",\n}\n\nvar <strong>characters<\/strong> <strong>map<\/strong>[string]Character = map[string]Character{\n    \"house\": {adjective: \"\", pred: Predicate{\n        predicate: \"built.\", character: \"Jack\",\n    },\n    },\n    \"malt\": {adjective: \"\", pred: Predicate{\n        predicate: \"lay in the\", character: \"house\",\n    },\n    },\n    \"rat\": {adjective: \"\", pred: Predicate{\n        predicate: \"ate the\", character: \"malt\",\n    },\n    },\n    \"cat\": {adjective: \"\", pred: Predicate{\n        predicate: \"killed the\", character: \"rat\",\n    },\n    },\n    \"dog\": {adjective: \"\", pred: Predicate{\n        predicate: \"worried the\", character: \"cat\",\n    },\n    },\n    \"cow\": {adjective: \"with the crumpled horn\", pred: Predicate{\n        predicate: \"tossed the\", character: \"dog\",\n    },\n    },\n    \"maiden\": {adjective: \"all forlorn\", pred: Predicate{\n        predicate: \"milked the\", character: \"cow\",\n    },\n    },\n    \"man\": {adjective: \"all tattered and torn\", pred: Predicate{\n        predicate: \"kissed the\", character: \"maiden\",\n    },\n    },\n    \"priest\": {adjective: \"all shaven and shorn\", pred: Predicate{\n        predicate: \"married the\", character: \"man\",\n    },\n    },\n    \"rooster\": {adjective: \"that crowed in the morn\", pred: Predicate{\n        predicate: \"woke the\", character: \"priest\",\n    },\n    },\n    \"farmer\": {adjective: \"sowing his corn\", pred: Predicate{\n        predicate: \"kept the\", character: \"rooster\",\n    },\n    },\n    \"horse\": {adjective: \"and the hound and the horn\", pred: Predicate{\n        predicate: \"belonged to the\", character: \"farmer\",\n    },\n    },\n}<\/pre>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<p>The problem that was still wasn&#8217;t that obvious to me was some subjects had adjectives which were unassumingly borrowed when used in the predicate.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Example<\/strong>: <code><em>This is the maiden all forlorn that milked the cow with the crumpled horn<\/em><\/code><\/pre>\n<\/div><\/div>\n\n\n\n<p>Now to write the function to return the verses numerically from 1 to 12, a simple stitcher which uses <code>fmt<\/code>&#8216;s <code>Fprintf<\/code> method and <code>StringBuilder<\/code> from <code>strings<\/code><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">func Verse(stanza int) (verse string) {\n    var v strings.Builder\n    var character string\n    character = verses[stanza]\n    if stanza == 1 {\n        fmt.Fprintf(&amp;v, \"%s %s that %s %s\", This, character, characters[character].pred.character, characters[character].pred.predicate)\n    } else {\n        for j := stanza; j > 0; j-- {\n            character = verses[j]\n            predChar := characters[character].pred.character\n            if j == stanza {\n                if characters[character].adjective == \"\" {\n                    fmt.Fprintf(&amp;v, \"%s %s\\nthat %s %s\", This, character, characters[character].pred.predicate, characters[character].pred.character)\n                } else {\n                    fmt.Fprintf(&amp;v, \"%s %s %s\\nthat %s %s\", This, character, characters[character].adjective, characters[character].pred.predicate, characters[character].pred.character)\n                }\n            } else if j == 1 {\n                fmt.Fprintf(&amp;v, \" that %s %s\", characters[character].pred.character, characters[character].pred.predicate)\n            } else {\n                fmt.Fprintf(&amp;v, \"\\nthat %s %s\", characters[character].pred.predicate, characters[character].pred.character)\n            }\n            if predChar != \"\" &amp;&amp; characters[predChar].adjective != \"\" {\n                fmt.Fprintf(&amp;v, \" %s\", characters[predChar].adjective)\n            }\n        }\n    }\n\n    verse = strings.TrimSpace(v.String())\n    return verse\n}<\/pre>\n\n\n\n<h2 class=\"has-text-align-center wp-block-heading\">&#8212; THE &#8211; END &#8212;<\/h2>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Funny thing about simple programs is that there are too many optimization options available that never lets you feel content. So here I am trying to solve another problem from exercism.io which is totally a must visit for anyone who wants to learn and practice. Nursery rhyme I wrote a couple of version before trying [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":288,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[1,34],"tags":[36,23,35,37,38],"class_list":["post-287","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-others","category-technical","tag-fprintf","tag-golang","tag-strings","tag-struct","tag-types"],"_links":{"self":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/287","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/comments?post=287"}],"version-history":[{"count":0,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/287\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media\/288"}],"wp:attachment":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media?parent=287"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/categories?post=287"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/tags?post=287"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}