Skip to content
Snippets Groups Projects
Commit 17c88cc2 authored by s2-peng's avatar s2-peng
Browse files

Upload New File

parent c7c820b8
No related branches found
No related tags found
No related merge requests found
morse.py 0 → 100644
import numpy as np
class MorseNode:
def __init__(self, c,morseCode=''):
self.char = c
self.morseCode=morseCode
self.dot = None
self.dash = None
#←
def insertLeft(self,c):
self.dot = MorseNode(c)
return self.dot
#→
def insertRight(self,c):
self.dash = MorseNode(c)
return self.dash
root = MorseNode('*')
# 1st
root.dash = MorseNode('t')
root.dot = MorseNode('e')
# 2nd
root.dot.dot = MorseNode('i')
root.dot.dash = MorseNode('a')
root.dash.dot = MorseNode('n')
root.dash.dash = MorseNode('m')
# 3rd
root.dot.dot.dot = MorseNode('s')
root.dot.dot.dash = MorseNode('u')
root.dot.dash.dot = MorseNode('r')
root.dot.dash.dash = MorseNode('w')
root.dash.dot.dot = MorseNode('d')
root.dash.dot.dash = MorseNode('k')
root.dash.dash.dot = MorseNode('g')
root.dash.dash.dash = MorseNode('o')
# 4th
root.dot.dot.dot.dot = MorseNode('h')
root.dot.dot.dot.dash = MorseNode('v')
root.dot.dot.dash.dot = MorseNode('f')
root.dot.dot.dash.dash = MorseNode('')
root.dot.dash.dot.dot = MorseNode('l')
root.dot.dash.dot.dash = MorseNode('')
root.dot.dash.dash.dot = MorseNode('p')
root.dot.dash.dash.dash = MorseNode('j')
root.dash.dot.dot.dot = MorseNode("b")
root.dash.dot.dot.dash = MorseNode("x")
root.dash.dot.dash.dot = MorseNode("c")
root.dash.dot.dash.dash = MorseNode("y")
root.dash.dash.dot.dot = MorseNode("z")
root.dash.dash.dot.dash = MorseNode("q")
root.dash.dash.dash.dot = MorseNode("")
root.dash.dash.dash.dash = MorseNode("")
# 5
root.dot.dot.dot.dot.dot = MorseNode("5")
root.dot.dot.dot.dot.dash = MorseNode("4")
root.dot.dot.dot.dash.dot = MorseNode("")
root.dot.dot.dot.dash.dash = MorseNode("3")
root.dot.dot.dash.dot.dot = MorseNode("")
root.dot.dot.dash.dot.dash = MorseNode("¿")
root.dot.dot.dash.dash.dot = MorseNode("?")
root.dot.dot.dash.dash.dash = MorseNode("2")
root.dot.dash.dot.dot.dot = MorseNode("&")
root.dot.dash.dot.dot.dash = MorseNode("")
root.dot.dash.dot.dash.dot = MorseNode("+")
root.dot.dash.dot.dash.dash = MorseNode("")
root.dot.dash.dash.dot.dot = MorseNode("+")
root.dot.dash.dash.dot.dash = MorseNode("")
root.dot.dash.dash.dash.dot = MorseNode("")
root.dot.dash.dash.dash.dash = MorseNode("1")
root.dash.dot.dot.dot.dot = MorseNode("6")
root.dash.dot.dot.dot.dash = MorseNode("=")
root.dash.dot.dot.dash.dot = MorseNode("/")
root.dash.dot.dot.dash.dash = MorseNode("")
root.dash.dot.dash.dot.dot = MorseNode("")
root.dash.dot.dash.dot.dash = MorseNode("")
root.dash.dot.dash.dash.dot = MorseNode("(")
root.dash.dot.dash.dash.dash = MorseNode("")
root.dash.dash.dot.dot.dot = MorseNode("7")
root.dash.dash.dot.dot.dash = MorseNode("")
root.dash.dash.dot.dash.dot = MorseNode("")
root.dash.dash.dot.dash.dash = MorseNode("")
root.dash.dash.dash.dot.dot = MorseNode("8")
root.dash.dash.dash.dot.dash = MorseNode("")
root.dash.dash.dash.dash.dot = MorseNode("9")
root.dash.dash.dash.dash.dash = MorseNode("0")
#6
root.dot.dash.dot.dash.dot.dash = MorseNode(".")
root.dash.dash.dot.dot.dash.dash = MorseNode(",")
root.dash.dot.dash.dash.dot.dash = MorseNode(")")
root.dash.dot.dot.dot.dot.dash = MorseNode("-")
root.dash.dash.dot.dot.dot.dash = MorseNode("i")
root.dot.dot.dash.dash.dot.dash = MorseNode("_")
root.dot.dash.dash.dash.dash.dot = MorseNode("'")
root.dash.dash.dash.dot.dot.dot = MorseNode(":")
root.dot.dash.dot.dot.dash.dot = MorseNode("")
root.dash.dot.dash.dot.dash.dash = MorseNode("!")
root.dash.dot.dash.dot.dash.dot = MorseNode(";")
root.dot.dot.dot.dash.dot.dot = MorseNode("\n")
#7
root.dot.dot.dot.dash.dot.dot.dash = MorseNode("$")
class BinaryTree:
def __init__(self):
# 初始化结点,因为左右结点都可能为空,所以默认设置左右结点的值为空,如果没有传入左右结点就是空,有的话则是传入的结点
self.root = root
def encode(self,letter):
node= self.findMorseCode(self.root,'',letter)
return node.morseCode
def findMorseCode(self,node,morsecode,letter):
if node==None:
return None
node.morseCode=morsecode
if node.char==letter:
return node
next=self.findMorseCode(node.dot,morsecode+'.',letter)
if next!=None:
return next
else:
return self.findMorseCode(node.dash,morsecode+'-',letter)
def decode(self,morseCode):
letter = self.root
for c in morseCode:
if c == ' ':
return letter.char
if c == '.':
letter = letter.dot
elif c == '-':
letter = letter.dash
return letter.char
Tree=BinaryTree()
def encode(message):
message=message.upper()
cipher = ''
for letter in message:
if letter != ' ':
# 查字典并添加对应的摩斯密码 # 用空格分隔不同字符的摩斯密码
cipher += Tree.encode(letter) + ' '
else:
# 1个空格表示不同的字符 # 2表示不同的词
cipher += ' '
return cipher.strip()
# 将字符串从摩斯解密为英文的函数
def decode(message):
# 在末尾添加额外空间以访问最后一个摩斯密码
message += ' '
decipher = ''
citext = ''
i = 0
for letter in message:
# 检查空间
if letter != ' ':
# 计数器来跟踪空间
i = 0
# 在空格的情况下
citext += letter
# 在空间的情况下
else:
# 如果 i = 1 表示一个新字符
i += 1
# 如果 i = 2 表示一个新词
if i == 2:
# 添加空格来分隔单词
decipher += ' '
else:
# 使用它们的值访问密钥(加密的反向)
decipher += Tree.decode(citext)
citext = ''
return decipher
def testMorse():
result=encode("HELLO")
print(result)
result=decode(result)
print(result)
#testMorse()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment