{"id":6219,"date":"2018-12-28T04:41:07","date_gmt":"2018-12-28T04:41:07","guid":{"rendered":"https:\/\/www.appservgrid.com\/paw92\/?p=6219"},"modified":"2018-12-28T23:10:33","modified_gmt":"2018-12-28T23:10:33","slug":"more-roman-numerals-and-bash","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2018\/12\/28\/more-roman-numerals-and-bash\/","title":{"rendered":"More Roman Numerals and Bash"},"content":{"rendered":"<p><em>When in Rome: finishing the Roman numeral converter script.<\/em><\/p>\n<p><a href=\"https:\/\/www.linuxjournal.com\/content\/roman-numerals-and-bash\">In my last article<\/a>, I started digging in to a classic computer science puzzle: converting Roman numerals to<br \/>\nArabic numerals. First off, it more accurately should be called Hindu-Arabic, and it&#8217;s worth<br \/>\nmentioning that it&#8217;s believed to have been invented somewhere between the first and fourth<br \/>\ncentury\u2014a counting system based on 0..9 values.<\/p>\n<p>The script I ended up with last time offered the basics of parsing a specified Roman numeral and<br \/>\nconverted each value into its decimal equivalent with this simple function:<\/p>\n<p>mapit() {<br \/>\ncase $1 in<br \/>\nI|i) value=1 ;;<br \/>\nV|v) value=5 ;;<br \/>\nX|x) value=10 ;;<br \/>\nL|l) value=50 ;;<br \/>\nC|c) value=100 ;;<br \/>\nD|d) value=500 ;;<br \/>\nM|m) value=1000 ;;<br \/>\n* ) echo &#8220;Error: Value $1 unknown&#8221; &gt;&amp;2 ; exit 2 ;;<br \/>\nesac<br \/>\n}<\/p>\n<p>Then I demonstrated a slick way to use the underutilized seq command to parse a string character by<br \/>\ncharacter, but the sad news is that you won&#8217;t be able to use it for the final Roman numeral to<br \/>\nArabic numeral converter. Why? Because depending on the situation, the script sometimes<br \/>\nwill need to jump two ahead, and not just go left to right linearly, one character at a time.<\/p>\n<p>Instead, you can build the main loop as a while loop:<\/p>\n<p>while [ $index -lt $length ] ; do<\/p>\n<p>our code<\/p>\n<p>index=$(( $index + 1 ))<br \/>\ndone<\/p>\n<p>There are two basic cases to think about in terms of solving this algorithmic puzzle: the subsequent<br \/>\nvalue is greater than the current value, or it isn&#8217;t\u2014for example, IX versus II. The first is 9<br \/>\n(literally 1 subtracted from 10), and the second is 2. That&#8217;s no surprise; you&#8217;ll need to know both the<br \/>\ncurrent and next values within the script.<\/p>\n<p>Sharp readers already will recognize that the last character in a sequence is a special case,<br \/>\nbecause there won&#8217;t be a next value available. I&#8217;m going to ignore the special case to start with,<br \/>\nand I&#8217;ll address it later in the code development. Stay tuned, sharpies!<\/p>\n<p>Because Bash shell scripts don&#8217;t have elegant in-line functions, the code to get the current and<br \/>\nnext values won&#8217;t be value=mapit(romanchar), but it&#8217;ll be a smidge clumsy with its use of the global<br \/>\nvariable value:<\/p>\n<p>mapit $<br \/>\ncurrentval=$value<\/p>\n<p>mapit $<br \/>\nnextval=$value<\/p>\n<p>It&#8217;s key to realize that in the situation where the next value isn&#8217;t greater than the current value<br \/>\n(for example, MC), you can&#8217;t automatically conclude that the next value isn&#8217;t going to be part of a<br \/>\ncomplex two-value sequence anyway. Like this: MCM. You can&#8217;t just say M=1000 and C=500, so let&#8217;s<br \/>\njust convert it to 1500 and process the second M when we get to it. MCM=1900, not 2500!<\/p>\n<p>The basic logic turns out to be pretty straightforward:<\/p>\n<p>if [ $nextval -gt $currentval ] ; then<br \/>\nsum=$(( $sum + $nextval &#8211; $currentval ))<br \/>\nelse<br \/>\nsum=$(( $sum + currentval ))<br \/>\nfi<\/p>\n<p>Done!<\/p>\n<p>Or, um, not. The problem with the conditional code above is that in the situation where you&#8217;ve<br \/>\nreferenced both the current and next value, you need to ensure that the next value isn&#8217;t again<br \/>\nprocessed the next time through the loop.<\/p>\n<p>In other words, when the sequence &#8220;CM&#8221; is converted, the M shouldn&#8217;t be converted yet<br \/>\nagain the second time through the loop.<\/p>\n<p>This is precisely why I stepped away from the for loop, so you can have some passes through the loop<br \/>\nbe a +1 iteration but others be a +2 iteration as appropriate.<\/p>\n<p>With that in mind, let&#8217;s add the necessary line to the conditional statement:<\/p>\n<p>if [ $nextval -gt $currentval ] ; then<br \/>\nsum=$(( $sum + $nextval &#8211; $currentval ))<br \/>\nindex=$(( $index + 1 ))<br \/>\nelse<br \/>\nsum=$(( $sum + currentval ))<br \/>\nfi<\/p>\n<p>Remember that the very bottom of the while loop still has the index value increment +1. The above<br \/>\naddition to the conditional statement is basically that when the situation of next &gt; current is<br \/>\nencountered, the script will process both values and jump ahead an extra character.<\/p>\n<p>This means that for any given Roman numeral, the number of times through the loop will be equal to or<br \/>\nless than the total number of characters in the sequence.<\/p>\n<p>Which means the problem is now solved except for the very last value in the sequence. What happens if<br \/>\nit isn&#8217;t part of a next-current pair? At its most simple, how do you parse &#8220;X&#8221;?<\/p>\n<p>That turns out to require a bunch of code to sidestep both the conversion of nextval from the string<br \/>\n(which will fail as it&#8217;s reaching beyond the length of the string) and any test reference to<br \/>\nnextval.<\/p>\n<p>That suggests a simple solution: wrap the entire if-then-else code block in a conditional that tests<br \/>\nfor the last character:<\/p>\n<p>if [ $index -lt $length ] ; then<br \/>\nif-then-else code block<br \/>\nelse<br \/>\nsum=$(( $sum + $currentval ))<br \/>\nfi<\/p>\n<p>That&#8217;s it. By George, I think you&#8217;ve got it! Here&#8217;s the full while statement, so you can<br \/>\nsee how this fits into the overall program logic:<\/p>\n<p>while [ $index -le $length ] ; do<\/p>\n<p>mapit $<br \/>\ncurrentval=$value<\/p>\n<p>if [ $index -lt $length ] ; then<br \/>\nmapit $<br \/>\nnextval=$value<\/p>\n<p>if [ $nextval -gt $currentval ] ; then<br \/>\nsum=$(( $sum + $nextval &#8211; $currentval ))<br \/>\nindex=$(( $index + 1 ))<br \/>\nelse<br \/>\nsum=$(( $sum + $currentval ))<br \/>\nfi<br \/>\nelse<br \/>\nsum=$(( $sum + $currentval ))<br \/>\nfi<\/p>\n<p>index=$(( $index + 1 ))<\/p>\n<p>done<\/p>\n<p>It turns out not to be particularly complex after all. The key is to recognize that you need to parse the<br \/>\nRoman number in a rather clumped manner, not letter by letter.<\/p>\n<p>Let&#8217;s give this script a few quick tests:<\/p>\n<p>$ sh roman.sh DXXV<br \/>\nRoman number DXXV converts to Arabic value 525<br \/>\n$ sh roman.sh CMXCIX<br \/>\nRoman number CMXCIX converts to Arabic value 999<br \/>\n$ sh roman.sh MCMLXXII<br \/>\nRoman number MCMLXXII converts to Arabic value 1972<\/p>\n<p>Mission accomplished.<\/p>\n<p>In my next article, I plan to look at the obverse of this coding challenge, converting Arabic numerals to<br \/>\nRoman numeral sequences. In other words, you enter 99, and it returns XCIX. Why not take a stab at<br \/>\ncoding it yourself while you&#8217;re waiting?<\/p>\n<p><a href=\"https:\/\/www.linuxjournal.com\/content\/more-roman-numerals-and-bash\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>When in Rome: finishing the Roman numeral converter script. In my last article, I started digging in to a classic computer science puzzle: converting Roman numerals to Arabic numerals. First off, it more accurately should be called Hindu-Arabic, and it&#8217;s worth mentioning that it&#8217;s believed to have been invented somewhere between the first and fourth &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2018\/12\/28\/more-roman-numerals-and-bash\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;More Roman Numerals and Bash&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-6219","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/6219","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=6219"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/6219\/revisions"}],"predecessor-version":[{"id":6845,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/6219\/revisions\/6845"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=6219"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=6219"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=6219"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}