{"id":46,"date":"2007-09-19T22:16:44","date_gmt":"2007-09-19T20:16:44","guid":{"rendered":"http:\/\/ken.friislarsen.net\/blog\/2007\/09\/19\/morse-code-decoding-with-python-list-comprehensions\/"},"modified":"2007-09-19T22:16:44","modified_gmt":"2007-09-19T20:16:44","slug":"morse-code-decoding-with-python-list-comprehensions","status":"publish","type":"post","link":"http:\/\/ken.friislarsen.net\/blog\/2007\/09\/19\/morse-code-decoding-with-python-list-comprehensions\/","title":{"rendered":"Morse Code Decoding With Python List Comprehensions"},"content":{"rendered":"<p>As a small exercise for getting up to speed with <a href=\"http:\/\/python.org\">Python<\/a> I decided to solve <a href=\"http:\/\/www.rubyquiz.com\/quiz121.html\">ruby quiz #121<\/a>, which is to to write a function that finds all possible decodings of a string of <a href=\"http:\/\/en.wikipedia.org\/wiki\/Morse_code\">Morse codes<\/a> without letter- and word-separators.  Given the nature of the problem I decided to use python&#8217;s list comprehensions for the solution.<\/p>\n<p>Without further ado here is the code I ended up with:<\/p>\n<pre>#!\/usr\/bin\/env python\n\nimport string\n\nletters = [('A',\".-\"),   ('B',\"-...\"), ('C',\"-.-.\"), ('D',\"-..\"), ('E',\".\"),\n           ('F',\"..-.\"), ('G',\"--.\"),  ('H',\"....\"), ('I',\"..\"),  ('J',\".---\"),\n           ('K',\"-.-\"),  ('L',\".-..\"), ('M',\"--\"),   ('N',\"-.\"),  ('O',\"---\"),\n           ('P',\".--.\"), ('Q',\"--.-\"), ('R',\".-.\"),  ('S',\"...\"), ('T',\"-\"),\n           ('U',\"..-\"),  ('V',\"...-\"), ('W',\".--\"),  ('X',\"-..-\"),('Y',\"-.--\"),\n           ('Z',\"--..\")]\n\ndef decode(input):\n    if input == \"\" :\n        return [\"\"]\n    else:\n        return [ letter + remaining\n                 for letter, code in letters if input.startswith(code)\n                 for remaining in decode(input[len(code):]) ]\n\n# Some Testing code\ndef test(s, code):\n    if s in decode(code):\n        print code + \" can be decoded as \" + s\n    else:\n        print code + \" can NOT be decoded as \" + s\n\ntest(\"SOFIA\", \"...---..-....-\")\ntest(\"SOPHIA\", \"...---..-....-\")\ntest(\"EUGENIA\", \"...---..-....-\")<\/pre>\n<p>Interesting, my solution is rather similar to <a href=\"http:\/\/patricklogan.blogspot.com\/2007\/09\/list-comprehensions.html\">Patrick Logan&#8217;s Erlang solution<\/a>. And I find it simpler to understand than the <a href=\"http:\/\/www.haskell.org\/haskellwiki\/Haskell_Quiz\/Morse_Code\">Haskell solutions at the HaskellWiki.<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>As a small exercise for getting up to speed with Python I decided to solve ruby quiz #121, which is to to write a function that finds all possible decodings of a string of Morse codes without letter- and word-separators. Given the nature of the problem I decided to use python&#8217;s list comprehensions for the <a class=\"read-more\" href=\"http:\/\/ken.friislarsen.net\/blog\/2007\/09\/19\/morse-code-decoding-with-python-list-comprehensions\/\">[&hellip;]<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,3,14],"tags":[],"class_list":["post-46","post","type-post","status-publish","format-standard","hentry","category-coding","category-general","category-python"],"_links":{"self":[{"href":"http:\/\/ken.friislarsen.net\/blog\/wp-json\/wp\/v2\/posts\/46","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/ken.friislarsen.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/ken.friislarsen.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/ken.friislarsen.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/ken.friislarsen.net\/blog\/wp-json\/wp\/v2\/comments?post=46"}],"version-history":[{"count":0,"href":"http:\/\/ken.friislarsen.net\/blog\/wp-json\/wp\/v2\/posts\/46\/revisions"}],"wp:attachment":[{"href":"http:\/\/ken.friislarsen.net\/blog\/wp-json\/wp\/v2\/media?parent=46"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/ken.friislarsen.net\/blog\/wp-json\/wp\/v2\/categories?post=46"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/ken.friislarsen.net\/blog\/wp-json\/wp\/v2\/tags?post=46"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}