{"id":487,"date":"2020-03-29T10:12:32","date_gmt":"2020-03-29T10:12:32","guid":{"rendered":"https:\/\/www.samarthya.me\/wps\/?p=487"},"modified":"2020-03-29T10:12:32","modified_gmt":"2020-03-29T10:12:32","slug":"say-a-number","status":"publish","type":"post","link":"https:\/\/blog.samarthya.me\/wps\/2020\/03\/29\/say-a-number\/","title":{"rendered":"Say a Number"},"content":{"rendered":"\n<p>With the Corona-Lockdown in place &#8220;literally&#8221;, it was time to squeeze out some me-time to continue the quest for GO-Proficiency.<\/p>\n\n\n\n<p>Sometimes you re-do optimization in your head so many times that it takes a whole day to see through the mistake you are making.<\/p>\n\n\n\n<p>Trying to solve problem &#8211; <a href=\"https:\/\/exercism.io\/my\/solutions\/de6f71f14d0d4ce996a13bd259944756\">Say<\/a>.       <\/p>\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>input:       123,        <\/p>\n\n\n\n<p>expected:    &#8220;one hundred twenty-three&#8221;,<\/p>\n<\/div><\/div>\n\n\n\n<p>For every such input number you need to translate and provide the English output.<\/p>\n\n\n\n<p>The max limit goes till out a 100 billion.<\/p>\n\n\n\n<p>I started it with a standard map definition which has few constants already in place.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">var myMap = map[int]string{\n    0:          \"zero\", \/\/ 0 - Starts with zero.\n    1:          \"one\",\n    2:          \"two\",\n    3:          \"three\",\n    4:          \"four\",\n    5:          \"five\",\n    6:          \"six\",\n    7:          \"seven\",\n    8:          \"eight\",\n    9:          \"nine\",\n    10:         \"ten\",\n    11:         \"eleven\",\n    12:         \"twelve\",\n    13:         \"thirteen\",\n    20:         \"twenty\",\n    30:         \"thirty\",\n    40:         \"fourty\",\n    50:         \"fifty\",\n    60:         \"sixty\",\n    70:         \"seventy\",\n    80:         \"eighty\",\n    90:         \"ninety\",\n    100:        \"hundred\",\n    1000:       \"thousand\",\n    1000000:    \"million\",\n    1000000000: \"billion\",\n}<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Step &#8211; 1<\/h2>\n\n\n\n<p>Addressing all numbers less than 100.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/tens It is a function that will convert &lt; 100\nfunc <strong>tens<\/strong>(i int) (string, bool) {\n\n    \/\/ Valid for numbers less than hundred only.\n    switch {\n    case i > 13 &amp;&amp; i &lt; 20:\n        return fmt.Sprintf(\"%s%s\", myMap[i-10], \"teen\"), true\n    case i > 20 &amp;&amp; i &lt; 29:\n        return fmt.Sprintf(\"%s%s\", \"twenty-\", myMap[i-20]), true\n    case i > 30 &amp;&amp; i &lt; 40:\n        return fmt.Sprintf(\"%s%s\", \"thirty-\", myMap[i-30]), true\n    case i > 40 &amp;&amp; i &lt; 50:\n        return fmt.Sprintf(\"%s%s\", \"forty-\", myMap[i-40]), true\n    case i > 50 &amp;&amp; i &lt; 60:\n        return fmt.Sprintf(\"%s%s\", \"fifty-\", myMap[i-50]), true\n    case i > 60 &amp;&amp; i &lt; 70:\n        return fmt.Sprintf(\"%s%s\", \"sixty-\", myMap[i-60]), true\n    case i > 70 &amp;&amp; i &lt; 80:\n        return fmt.Sprintf(\"%s%s\", \"seventy-\", myMap[i-70]), true\n    case i > 80 &amp;&amp; i &lt; 90:\n        return fmt.Sprintf(\"%s%s\", \"eighty-\", myMap[i-80]), true\n    case i > 90 &amp;&amp; i &lt; 100:\n        return fmt.Sprintf(\"%s%s\", \"ninety-\", myMap[i-90]), true\n    default:\n        \/\/ If there is a numeric equivalent available in map.\n        if val, ok := myMap[int(i)]; ok {\n            return val, true\n        }\n    }\n\n    \/\/No match found in map or can be calculated\n    return \"\", false\n}<\/pre>\n\n\n\n<p>Now time to build the logic upwards<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/NumberConversion Converts the number\nfunc <strong>NumberConversion<\/strong>(i int64) (string, bool) {\n    var s strings.Builder\n\n    for i >= 0 {\n        switch {\n        case i == 0:\n            if len(s.String()) == 0 {\n                if val, ok := tens(int(i)); ok {\n                    fmt.Fprintf(&amp;s, \" %s\", val)\n                }\n            }\n            return strings.TrimSpace(s.String()), true\n        case i &lt; 100:\n            if val, ok := tens(int(i)); ok {\n                fmt.Fprintf(&amp;s, \" %s\", val)\n            }\n            return strings.TrimSpace(s.String()), true\n         }\n    }\n    return \"\", false\n}<\/pre>\n\n\n\n<p>This function allows all number conversion for elements less than 100 by calling the <code>NumberConversion<\/code> function.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step &#8211; 2 Number > 100 &lt; 1000<\/h2>\n\n\n\n<p>Added a hundreds function<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ hundreds Allows to compute the numeric equivalent of the 100-999\nfunc <strong>hundreds<\/strong>(r int) (string, bool) {\n    switch {\n    case r == 0:\n        return fmt.Sprintf(\"one %s\", myMap[100]), true\n    default:\n        return fmt.Sprintf(\"%s %s\", myMap[r], myMap[100]), true\n    }\n}<\/pre>\n\n\n\n<p>and adding a new case to the function <code>NumberConversion<\/code><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">     case i >= 100 &amp;&amp; i &lt; 1000:\n            r := int(i \/ 100)\n            if val, ok := hundreds(r); ok {\n                fmt.Fprintf(&amp;s, \" %s\", val)\n            }\n            i %= 100<\/pre>\n\n\n\n<p>Similarly for elements greater than 1000<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">     case i >= 1000 &amp;&amp; i &lt; 1000000:\n            r := i \/ 1000\n            if val, ok := NumberConversion(r); ok {\n                fmt.Fprintf(&amp;s, \" %s %s\", val, myMap[1000])\n            }\n            i %= 1000<\/pre>\n\n\n\n<p>For elements greater than million<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">      case i >= 1000000 &amp;&amp; i &lt; 1000000000:\n            r := i \/ 1000000\n            if val, ok := NumberConversion(r); ok {\n                fmt.Fprintf(&amp;s, \" %s %s\", val, myMap[1000000])\n            }\n            i %= 1000000<\/pre>\n\n\n\n<p>And the last billion and less than 100 billion.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">      case i >= 1000000000 &amp;&amp; i &lt; 1000000000000:\n            r := i \/ 1000000000\n            if val, ok := NumberConversion(r); ok {\n                fmt.Fprintf(&amp;s, \"%s %s\", val, myMap[1000000000])\n            }\n            i %= 1000000000<\/pre>\n\n\n\n<p>Finally the function looks like below<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/NumberConversion Converts the number\nfunc <strong>NumberConversion<\/strong>(i int64) (string, bool) {\n    var s strings.Builder\n\n    for i >= 0 {\n        switch {\n        case i == 0:\n            if len(s.String()) == 0 {\n                if val, ok := tens(int(i)); ok {\n                    fmt.Fprintf(&amp;s, \" %s\", val)\n                }\n            }\n            return strings.TrimSpace(s.String()), true\n        case i &lt; 100:\n            if val, ok := tens(int(i)); ok {\n                fmt.Fprintf(&amp;s, \" %s\", val)\n            }\n            return strings.TrimSpace(s.String()), true\n        case i >= 100 &amp;&amp; i &lt; 1000:\n            r := int(i \/ 100)\n            if val, ok := hundreds(r); ok {\n                fmt.Fprintf(&amp;s, \" %s\", val)\n            }\n            i %= 100\n        case i >= 1000 &amp;&amp; i &lt; 1000000:\n            r := i \/ 1000\n            if val, ok := NumberConversion(r); ok {\n                fmt.Fprintf(&amp;s, \" %s %s\", val, myMap[1000])\n            }\n            i %= 1000\n        case i >= 1000000 &amp;&amp; i &lt; 1000000000:\n            r := i \/ 1000000\n            if val, ok := NumberConversion(r); ok {\n                fmt.Fprintf(&amp;s, \" %s %s\", val, myMap[1000000])\n            }\n            i %= 1000000\n        case i >= 1000000000 &amp;&amp; i &lt; 1000000000000:\n            r := i \/ 1000000000\n            if val, ok := NumberConversion(r); ok {\n                fmt.Fprintf(&amp;s, \"%s %s\", val, myMap[1000000000])\n            }\n            i %= 1000000000\n        }\n    }\n    return \"\", false\n}\n<\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>All code available <a href=\"https:\/\/github.com\/samarthya\/goexercism\/tree\/master\/say\">here<\/a> <\/li><\/ul>\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>With the Corona-Lockdown in place &#8220;literally&#8221;, it was time to squeeze out some me-time to continue the quest for GO-Proficiency. Sometimes you re-do optimization in your head so many times that it takes a whole day to see through the mistake you are making. Trying to solve problem &#8211; Say. input: 123, expected: &#8220;one hundred [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":488,"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":[34],"tags":[23],"class_list":["post-487","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technical","tag-golang"],"_links":{"self":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/487","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=487"}],"version-history":[{"count":0,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/487\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media\/488"}],"wp:attachment":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media?parent=487"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/categories?post=487"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/tags?post=487"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}