QUESTION: A decode algorithm will be given a non-empty string, code, consisting of dots and dashes (. and -) and will use the tree MORSE to return the uppercase letter, result, associated with the sequence. In the case of an invalid input, the algorithm should return an empty string. Define the decode function. Write your answer here Function: decode Inputs: \character, morseCode Preconditions: \non-empty string, code, consisting of dog and dashes(. and -) Output: \return uppercase letter, result, associated with the sequence Postconditions:if invalid input return an empty string\
please, can you help me, I'm working with notebooks python.
class Tree:
"""A rooted binary tree"""
def __init__(self):
self.root = None
self.left = None
self.right = None
def is_empty(tree: Tree) -> bool:
"""Return True if and only if tree is empty."""
return tree.root == tree.left == tree.right == None
def join(item: object, left: Tree, right: Tree) -> Tree:
"""Return a tree with the given root and subtrees."""
tree = Tree()
tree.root = item
tree.left = left
tree.right = right
return tree
The following code builds a binary tree, MORSE, which implements the upper-case Morse code, as per the image below. More information on Morse code can be found on Wikipedia. You will need to run this code block to use in your solutions.
EMPTY = Tree()
H = join('H',EMPTY,EMPTY)
V = join('V',EMPTY,EMPTY)
F = join('F',EMPTY,EMPTY)
L = join('L',EMPTY,EMPTY)
P = join('P',EMPTY,EMPTY)
J = join('J',EMPTY,EMPTY)
B = join('B',EMPTY,EMPTY)
X = join('X',EMPTY,EMPTY)
C = join('C',EMPTY,EMPTY)
Y = join('Y',EMPTY,EMPTY)
Z = join('Z',EMPTY,EMPTY)
Q = join('Q',EMPTY,EMPTY)
S = join('S',H,V)
U = join('U',F,EMPTY)
R = join('R',L,EMPTY)
W = join('W',P,J)
D = join('D',B,X)
K = join('K',C,Y)
G = join('G',Z,Q)
O = join('O',EMPTY,EMPTY)
I = join('I',S,U)
A = join('A',R,W)
N = join('N',D,K)
M = join('M',G,O)
E = join('E',I,A)
T = join('T',N,M)
MORSE = join('START',E,T)
QUESTION:
A decode
Write your answer here
Function: decode
Inputs: \character, morseCode
Preconditions: \non-empty string, code, consisting of dog and dashes(. and -)
Output: \return uppercase letter, result, associated with the sequence
Postconditions:if invalid input return an empty string\
Step by step
Solved in 2 steps