Data Structure & Algorithm: Level-1 Problem #55. Morse Code

Morse code is a method used in telecommunication to encode text characters as standardized sequences of two different signal durations, called dots and dashes (or dits and dahs). Morse code is named after Samuel Morse, an inventor of the telegraph.

Following is the English alphabet letters in Morse Code:

Morese Code

Covert to . and -, the letters A-Z are as follows:

[“.-“,”-…”,”-.-.”,”-..”,”.”,”..-.”,”–.”,”….”,”..”,”.—“,”-.-“,”.-..”,”–“,”-.”,”—“,”.–.”,”–.-“,”.-.”,”…”,”-“,”..-“,”…-“,”.–“,”-..-“,”-.–“,”–..”][“.-“,”-…”,”-.-.”,”-..”,”.”,”..-.”,”–.”,”….”,”..”,”.—“,”-.-“,”.-..”,”–“,”-.”,”—“,”.–.”,”–.-“,”.-.”,”…”,”-“,”..-“,”…-“,”.–“,”-..-“,”-.–“,”–..”]

Given a list of words, convert them into Morse Code without adding the breaks between letters. Find out whether there are duplicated ones.

Sample Input:

words = [“we”, “tea”, “and”, “run”, “ate”]

Sample Output:

True

The Morse Code for the sample input are: [‘.–.’, ‘-..-‘, ‘.–.-..’, ‘.-…–.’, ‘.–.’]. The word “we” is the same as “ate”.


数据结构和算法:初级练习题 #55 – 摩斯密码


摩斯密码一般指摩尔斯电码,是一种时通时断的信号代码,通过不同的排列顺序来表达不同的英文字母、数字和标点符号。它发明于1837年,是一种早期的数字化通信形式。不同于现代化的数字通讯,摩尔斯电码只使用零和一两种状态的二进制代码,它的代码包括五种:短促的点信号“・”,读“滴”(Di)保持一定时间的长信号“—”,读“嗒”(Da)表示点和划之间的停顿、每个词之间中等的停顿,以及句子之间长的停顿。

下面是二十六个英文字母用摩斯密码来表示的数列:

[“.-“,”-…”,”-.-.”,”-..”,”.”,”..-.”,”–.”,”….”,”..”,”.—“,”-.-“,”.-..”,”–“,”-.”,”—“,”.–.”,”–.-“,”.-.”,”…”,”-“,”..-“,”…-“,”.–“,”-..-“,”-.–“,”–..”][“.-“,”-…”,”-.-.”,”-..”,”.”,”..-.”,”–.”,”….”,”..”,”.—“,”-.-“,”.-..”,”–“,”-.”,”—“,”.–.”,”–.-“,”.-.”,”…”,”-“,”..-“,”…-“,”.–“,”-..-“,”-.–“,”–..”]

给定一组单词,检查它们转换成摩斯密码后,如果不加停顿,是否有相同的结果。

输入样例:

words = [“we”, “tea”, “and”, “run”, “ate”]

输出样例:

True

输入样例的单词转换为摩斯密码后的结果是:[‘.–.’, ‘-..-‘, ‘.–.-..’, ‘.-…–.’, ‘.–.’]。单词ate 和 we 的摩斯密码是相同的。


Python Solution

Morse Code