diff --git a/morse.py b/morse.py new file mode 100644 index 0000000000000000000000000000000000000000..27d4c43e3d2a1e80c64f086cc55f54a9cb2d014b --- /dev/null +++ b/morse.py @@ -0,0 +1,145 @@ +class MorseNode: + def __init__(self, c): + + self.char = c + 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") + +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.dash = MorseNode("$") + + +def encode(tree, morse): + current_node = tree + for morse_symbol in morse: + print(morse_symbol) + if (morse_symbol == '.'): + current_node = current_node.dot + elif (morse_symbol == '-'): + current_node = current_node.dash + return current_node.char + + + +class BinaryTree: + def __init__(self): + self.root = root + + def encode(self,letter): + node = self.findMorseCode(self.root,'',letter) + + def findMorseCode(self,node,letter): + if node==None: + return None + if node ==root: + node.morseCode='' + if node.char==letter: + return node.char + next = self.findMorseCode(node.dot,'.',letter) + if next!=None: + return next + else: + return self.findMorseCode(node.dash,'-',letter) + + def decode(self,moreCode): + letter = self.root + for c in morseCode: + if c == '': + \ No newline at end of file