{"id":1877,"date":"2022-01-07T22:35:34","date_gmt":"2022-01-07T22:35:34","guid":{"rendered":"https:\/\/www.codingfriends.com\/?p=1877"},"modified":"2022-01-07T22:35:36","modified_gmt":"2022-01-07T22:35:36","slug":"morse-code","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2022\/01\/07\/morse-code\/","title":{"rendered":"Morse code"},"content":{"rendered":"\n<p>As a test, I was asked to write a morse code task where some of the signals weren&#8217;t able to be determined from the input.  So in this case instead of having either the dot (.) or dash (-) we have a question mark (?) where it could be either a dot \/ dash.<\/p>\n\n\n\n<p>Here is an image of the <a rel=\"noreferrer noopener\" href=\"https:\/\/en.wikipedia.org\/wiki\/Morse_code\" target=\"_blank\">morse code<\/a> alphabet of dots \/ dashes and here is a link to a view of the &#8220;<a rel=\"noreferrer noopener\" href=\"https:\/\/commons.wikimedia.org\/wiki\/File:Morse_code_tree3.png\" target=\"_blank\">tree<\/a>&#8221; of the morse code of the <a rel=\"noreferrer noopener\" href=\"https:\/\/en.wikipedia.org\/wiki\/Dichotomic_search\" data-type=\"URL\" data-id=\"https:\/\/en.wikipedia.org\/wiki\/Dichotomic_search\" target=\"_blank\">dichotomic search<\/a>, basically binary search of the morse code.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"329\" src=\"https:\/\/www.codingfriends.com\/wp-content\/uploads\/2022\/01\/Morse_code_tree3-1024x329.png\" alt=\"\" class=\"wp-image-1886\" srcset=\"https:\/\/www.codingfriends.com\/wp-content\/uploads\/2022\/01\/Morse_code_tree3-1024x329.png 1024w, https:\/\/www.codingfriends.com\/wp-content\/uploads\/2022\/01\/Morse_code_tree3-300x96.png 300w, https:\/\/www.codingfriends.com\/wp-content\/uploads\/2022\/01\/Morse_code_tree3-768x247.png 768w, https:\/\/www.codingfriends.com\/wp-content\/uploads\/2022\/01\/Morse_code_tree3-1536x494.png 1536w, https:\/\/www.codingfriends.com\/wp-content\/uploads\/2022\/01\/Morse_code_tree3-2048x658.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>So, we are only going down 3 levels, below are some tests to confirm that the process is working fine.<\/p>\n\n\n\n<p><\/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<ul class=\"wp-block-list\"><li>.- = A<\/li><li>. = E<\/li><li>&#8230; = S<\/li><li>-.- = K<\/li><li>? = ET<\/li><li>-? = NM<\/li><\/ul>\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>So, I approached this test by thinking that I need to have an &#8220;item&#8221; that stores the current value (e.g. character) and left \/ right item, and here is my item.h definition.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class item {\n    private:\n        char value;\n        item *left;\n        item *right;\n    public:\n        item(char value,item *left,item *right);\n        item(char value);\n        item(item *left,item *right);\n\n        item();\n\n        char getValue();\n\n        item* leftItem();\n        item* rightItem();\n};<\/code><\/pre>\n\n\n\n<p>And then to build the 3 levels of the search tree<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    item* head = new item(\n        new item('e',\n            new item('i', \n                new item('s'),\n                new item('u')\n            ),\n            new item('a',\n                new item('r'),\n                new item('w')\n            )\n        ),\n        new item('t',\n            new item('n', \n                new item('d'),\n                new item('k')\n            ),\n            new item('m',\n                new item('g'),\n                new item('o')\n            )\n        )\n    );<\/code><\/pre>\n<\/div><\/div>\n\n\n\n<p>and then the last part was literally reading in the signal to be processed and going down the tree either left \/ right or both for undermined signal.<\/p>\n<\/div><\/div>\n\n\n\n<pre class=\"wp-block-code\"><code>string output(string signal, item* codes) {\n    string ret=\"\";\n    if (signal.size() ==0 ) \n        return string(1,codes->getValue());\n    for (string::size_type i=0; i &lt; signal.size();i++) {\n        if ('?' == signal&#91;i]) {\n            ret += output(\".\"+signal.substr(i+1),codes);\n            ret += output(\"-\"+signal.substr(i+1),codes);\n            return ret;\n        } else if ('.' == signal&#91;i]) {\n            return output(signal.substr(i+1),codes->leftItem());\n        } else if ('-' == signal&#91;i]) {\n            return output(signal.substr(i+1),codes->rightItem());\n        } else {\n            throw invalid_argument(string(\"Invalid character at this point of the string \").append(signal));\n        }\n    }\n    return ret;\n}<\/code><\/pre>\n\n\n\n<p>If you want to view the whole code, here is the link to the zip file of the morse code.<\/p>\n\n\n\n<div class=\"wp-block-file\"><a href=\"https:\/\/www.codingfriends.com\/wp-content\/uploads\/2022\/01\/morse.zip\">morse<\/a><a href=\"https:\/\/www.codingfriends.com\/wp-content\/uploads\/2022\/01\/morse.zip\" class=\"wp-block-file__button\" download>Download<\/a><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>As a test, I was asked to write a morse code task where some of the signals weren&#8217;t able to be determined from the input. So in this case instead of having either the dot (.) or dash (-) we have a question mark (?) where it could be either a dot \/ dash. Here &hellip; <a href=\"https:\/\/www.codingfriends.com\/index.php\/2022\/01\/07\/morse-code\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Morse code<\/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":[7],"tags":[],"class_list":["post-1877","post","type-post","status-publish","format-standard","hentry","category-c_and_cpp"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/1877","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/comments?post=1877"}],"version-history":[{"count":2,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/1877\/revisions"}],"predecessor-version":[{"id":1888,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/1877\/revisions\/1888"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=1877"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=1877"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=1877"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}