{"id":278,"date":"2020-08-18T18:52:39","date_gmt":"2020-08-18T23:52:39","guid":{"rendered":"https:\/\/www.brezeale.com\/?p=278"},"modified":"2020-08-20T20:26:10","modified_gmt":"2020-08-21T01:26:10","slug":"python-application-converting-numbers-to-words","status":"publish","type":"post","link":"https:\/\/www.brezeale.com\/?p=278","title":{"rendered":"Python Application: Converting Numbers to Words"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">People learning to program often struggle with how to decompose a problem into the steps necessary to write a program to solve that problem. This is one of a series of posts in which I take a problem and go through my decision-making process that leads to a program.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Problem: Given an integer in the range of 1 to 999, print out the number using word (e.g., 123 = one hundred twenty-three). I assume that you understand statements, conditionals, loops, lists, strings, and functions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can find a video with more details at&nbsp;<a href=\"https:\/\/www.youtube.com\/watch?v=1gAiDtRj4qw\">https:\/\/www.youtube.com\/watch?v=1gAiDtRj4qw<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">One way that comes to mind and is easy to understand is this:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: false; title: ; notranslate\" title=\"\">\ndef f(num) :\n    if num == 1 :\n        print(&quot;one&quot;)\n    elif num == 2 :\n        print(&quot;two&quot;)\n    elif num == 3 :\n        print(&quot;three&quot;)\n    .\n    .\n    .\n    elif num == 999 :\n        print(&quot;nine hundred ninety-nine&quot;)\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">What&#8217;s the problem with this approach?  For the interval 1 to 999 I have to have 999 conditionals.  For 1 to 1 million I would have to have 1 million conditionals.  Surely there is an easy way, right?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Design:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In order to produce a scalable solution, assuming it exists, I start by writing some of the numbers in the range of 1 to 999 as words, looking for patterns. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; gutter: false; title: ; notranslate\" title=\"\">\n1 = one\n2 = two\n3 = three\n    .\n    .\n    .\n10 = ten\n11 = eleven\n12 = twelve\n    .\n    .\n    .\n20 = twenty\n21 = twenty-one\n    .\n    .\n    .\n99 = ninety-nine\n100 = one hundred\n101 = one hundred one\n    .\n    .\n    .\n110 = one hundred ten\n111 = one hundred eleven\n    .\n    .\n    .\n120 = one hundred twenty\n121 = one hundred twenty-one\n    .\n    .\n    .\n999 = nine hundred ninety-nine\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Is there any kind of pattern here and, if so, are there exceptions to it.  The most obvious exception are the teens&#8211;11 to 19.  Each of these has its own distinct word that I will have to deal with.  Except for things containing teens, including numbers like 814, I can use the words &#8220;one&#8221;, &#8220;two&#8221;, and so forth if the last digit is a 1, 2, and so forth.  This means that I can re-use these words.  The three digit numbers also begin with one of these words followed by the word &#8220;hundred&#8221;.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I can see now that there are three main word classes<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>ones: &#8220;one&#8221;, &#8220;two&#8221;, &#8230;<\/li><li>teens: &#8220;eleven&#8221;, &#8220;twelve&#8221;, &#8230;<\/li><li>tens: &#8220;ten&#8221;, &#8220;twenty&#8221;, &#8230;<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">I will need some logic to decide which word classes to use and also whether or not their are compound words requiring a hyphen.  By writing out a variety of numbers and their corresponding word representations, I can see that<\/p>\n\n\n\n<ul class=\"wp-block-list\" id=\"block-b5b0cf3a-389a-4c47-8ca9-7804148644e4\"><li>3-digit numbers are in the hundreds<\/li><li>except for the teens, a number without a zero in both the second and third digits will need to be hyphenated<\/li><li>if the second digit is a 1 and the third digit is not a 0, then the number is a teen.<\/li><\/ul>\n\n\n\n<br \/>\n\n\n\n<strong>Final Program<\/strong>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: false; title: ; notranslate\" title=\"\">\n###    functions    ###\ndef convert( num ) :\n    ones = &#x5B;&quot;&quot;, &quot;one&quot;, &quot;two&quot;, &quot;three&quot;, &quot;four&quot;, \n            &quot;five&quot;, &quot;six&quot;, &quot;seven&quot;, &quot;eight&quot;, &quot;nine&quot; ]\n    teens = &#x5B;&quot;&quot;, &quot;eleven&quot;, &quot;twelve&quot;, &quot;thirteen&quot;, &quot;fourteen&quot;, \n             &quot;fifteen&quot;, &quot;sixteen&quot;, &quot;seventeen&quot;, &quot;eighteen&quot;, \n             &quot;nineteen&quot; ]\n    tens = &#x5B;&quot;&quot;, &quot;ten&quot;, &quot;twenty&quot;, &quot;thirty&quot;, &quot;forty&quot;, &quot;fifty&quot;, \n            &quot;sixty&quot;, &quot;seventy&quot;, &quot;eighty&quot;, &quot;ninety&quot; ]\n    # hundreds uses ones class plus &quot;hundred&quot;\n\n    snum = &quot;%03d&quot; % num\n    d0 = int(snum&#x5B;0])\n    d1 = int(snum&#x5B;1])\n    d2 = int(snum&#x5B;2])\n\n    if d0 != 0 :\n        word = &quot;%s hundred &quot; % ones&#x5B; d0 ]\n    else :\n        word = &quot;&quot;\n\n    if d1 != 0 and d2 != 0 :\n        hyphen = &quot;-&quot;\n    else :\n        hyphen = &quot;&quot;\n\n    if d1 == 1 and d2 != 0 :\n        word = word + teens&#x5B; d2 ]   # last two digits \n                                    # are teens\n    else :\n        word = word + tens&#x5B; d1 ] + hyphen + ones&#x5B; d2 ]\n\n    return word\n\n####  main  ####\n\nnum = int(input(&quot;Enter a positive integer with up to three digits:  &quot;))\nwhile 1 &lt;= num and num &lt;= 999 :\n    word = convert( num )\n    print(word)\n\n    num = int(input(&quot;Enter a positive integer with up to three digits:  &quot;))\n\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>People learning to program often struggle with how to decompose a problem into the steps necessary to write a program to solve that problem. This is one of a series of posts in which I take a problem and go through my decision-making process that leads to a program. Problem: Given an integer in the range of 1 to 999, print out the number using word (e.g., 123 = one hundred twenty-three). I assume that you understand statements, conditionals, loops,&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/www.brezeale.com\/?p=278\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"footnotes":""},"categories":[9,5],"tags":[],"class_list":["post-278","post","type-post","status-publish","format-standard","hentry","category-programming","category-python-programming"],"_links":{"self":[{"href":"https:\/\/www.brezeale.com\/index.php?rest_route=\/wp\/v2\/posts\/278","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.brezeale.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.brezeale.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.brezeale.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.brezeale.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=278"}],"version-history":[{"count":10,"href":"https:\/\/www.brezeale.com\/index.php?rest_route=\/wp\/v2\/posts\/278\/revisions"}],"predecessor-version":[{"id":400,"href":"https:\/\/www.brezeale.com\/index.php?rest_route=\/wp\/v2\/posts\/278\/revisions\/400"}],"wp:attachment":[{"href":"https:\/\/www.brezeale.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=278"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.brezeale.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=278"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.brezeale.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=278"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}