摩尔斯电码
The other night I was watching some videos by 2 Youtube channels I really like. The first one was Tom Scott’s video on why the Prime Meridian isn’t exactly at 0 degrees. I noticed that he happened to be filming from the same exact place that my profile photo for all my social accounts (including medium) are taken. It was a fun coincidence and it was also nice to know I had visited the Prime Meridian without knowing it.
前一天晚上,我观看了我真正喜欢的2个Youtube频道的一些视频。 第一个是汤姆·斯科特(Tom Scott)的视频,其中介绍了为何本初子午线并非精确地位于0度。 我注意到他恰巧是在拍摄我所有社交帐户(包括媒体)的个人资料照片的同一地点拍摄的。 这是一个有趣的巧合,很高兴知道我在不知道的情况下拜访了本初子午线。
The next video I watched was by the other Youtube channel I enjoy, Vsauce. It was on Morse Code. There was another fun coincidence in this video as well. In the very beginning, he defines what a palindrome is, and uses ‘racecar’ as an example. his explanation was almost word for word how I described it in my previous blog post on the 2 parts of solving an algorithm. It was weird seeing something I had just studied and written about, pop up again soon after.
我观看的下一个视频是我喜欢的另一个YouTube频道Vsauce。 这是在摩尔斯电码上。 该视频中还有另一个有趣的巧合。 从一开始,他就定义了回文是什么,并以“赛车”为例。 他的解释几乎是逐字逐句的,我在上篇博客中关于解决算法的两个部分是如何描述的。 看到我刚刚学习和写过的东西很奇怪,然后又突然出现。
I continued watching his video and he explained some interesting history and facts about morse code. In Morse Code, every character is made up of a combination of dots ‘.’ and dashes ‘-’. He explained that it was developed so the most common characters had the simplest or shortest make-up in morse code.
我继续观看他的视频,他解释了一些有关莫尔斯电码的有趣历史和事实。 在摩尔斯电码中,每个字符都由点“。”的组合组成。 和破折号“-”。 他解释说,它是经过开发的,因此最常见的字符在摩尔斯电码中的构成最简单或最短。
Later on, Michael from Vsauce introduced some nemonic devices that can help you learn or understand Morse Code. One of these devices was, as he described it ‘a flow chart looking thing’. Here’s what it looked like.
后来,来自Vsauce的Michael推出了一些nemonic设备,可帮助您学习或理解摩尔斯电码。 正如他所描述的,这些设备之一就是“流程图看的东西”。 这是它的样子。
This wasn’t just ‘a flow chart looking thing’, this was a binary tree. It’s a connection of nodes with each node having one or two children, and storing a value. What a coincidence, I have just been learning about this data structure for my technical challenges. It turns out there is a term for this coincidence. It is called the Baader Meinhof Phenomenon, and it describes a situation in which you come across something for the first time, and then see it again shortly after in an unrelated way.
这不仅仅是一个“看似流程图的东西”,而是一棵二叉树。 它是节点的连接,每个节点有一个或两个子节点,并存储一个值。 碰巧的是,我刚开始学习有关此数据结构的技术挑战。 事实证明,这个巧合有一个名词。 它被称为Baader Meinhof现象,它描述了一种情况,在这种情况下,您第一次碰到某些东西,然后不久就以不相关的方式再次看到它。
What was so interesting about this particular binary tree is that every value to the left of a given node, adds one more dot in morse code, in relation to its parent, and every value to the right adds one more dash. This means that we can write a function that takes in any valid Morse Code string, decodes it, and returns its decoded message.
这个特定的二叉树的有趣之处在于,给定节点左侧的每个值都相对于其父级在摩尔斯电码中增加了一个点,右侧的每个值又增加了一个破折号。 这意味着我们可以编写一个函数,该函数接受任何有效的摩尔斯电码字符串,对其进行解码,然后返回其解码后的消息。
decode(morse){
morse = morse.split(' / ') //['... .- --', '... .. .']
let message = ''
for(let word of morse){
let chars = word.split(' ') //['...', '.-', '--']
let decoded = ''
for(let char in chars){ // '...'
let node = this.root
for(let symb in chars[char]){ // '.' '.' '.'
if(chars[char][symb] === "-") node = node.right
else if(chars[char][symb] === '.')
node = node.left
else console.log('error')
}
decoded = decoded + node.data
}
message = message + decoded + " "
}
return message
}
With this as a function within our Binary Tree class, we can call tree.decode on a valid Morse Code string and return its english counterpart. When we call decode, we split the string apart by words and we define a variable to store the returned decoded message. Then we iterate through our array of words. We then split each word by a space, which allows us to have an array of characters for every word in the message. Following that, we define a variable to hold our word we are iterating with. We then grab the root of our morse tree, and iterate through every morse symbol of our letter. If the symbol is a dash, we move to the node to our right, if it is a dot, we move to the left. After we finish our iteration, we know that the value of our node is the character that our symbols resembled. We add every node data to our word variable. After we finish our letter iteration, we add our completed word and a space to our message variable. Finally, we return our decoded message.
将此作为Binary Tree类中的函数,我们可以在有效的摩尔斯电码字符串上调用tree.decode并返回其英文对应物。 调用解码时,我们将字符串按单词分开,然后定义一个变量来存储返回的解码消息。 然后,我们遍历单词数组。 然后,我们用空格分隔每个单词,这使我们可以为消息中的每个单词使用字符数组。 之后,我们定义一个变量来保存要迭代的单词。 然后,我们抓住莫尔斯树的根,遍历字母的每个摩尔斯符号。 如果符号是一个破折号,我们将移至右侧的节点,如果它是一个点,则将移至左侧。 完成迭代后,我们知道节点的值就是我们的符号类似的字符。 我们将每个节点数据添加到我们的word变量中。 完成字母迭代后,将完成的单词和空格添加到消息变量中。 最后,我们返回解码后的消息。
By implementing this function, we can see that Vsauce was right, and this tree decodes morse code, and we can decode it with our understanding of trees and their traversal. Thank you for reading, I hope you found this as interesting as I did!
通过实现此功能,我们可以看到Vsauce是正确的,并且该树可以解码摩尔斯电码,并且可以通过对树及其遍历的理解来对其进行解码。 感谢您的阅读,希望您能像我一样有趣!
翻译自: https://medium.com/dev-genius/morse-code-binary-trees-and-the-baader-meinhof-phenomenon-c0aa27876369
摩尔斯电码




被折叠的 条评论
为什么被折叠?



