please, can you help me with this function, many thanks (e) (ii) The following specifies a function, encode, to encode a given non-empty string, text, as a Morse code list, encoded. Each element of encoded will consist of a string of dots and dashes. Valid characters for text are upper-case letters A to Z; Any invalid characters in text are to be ignored. Function: encode Inputs: text, a string Preconditions: text is not empty Output: encoded, a list of strings. Postconditions: encoded is a list of 0 or more strings. Each element consists of a Morse code version of the upper-case letters specified in text. Write a Python function that satisfies this specification.
please, can you help me with this function, many thanks
(e) (ii)
The following specifies a function, encode, to encode a given non-empty string, text, as a Morse code list, encoded. Each element of encoded will consist of a string of dots and dashes. Valid characters for text are upper-case letters A to Z; Any invalid characters in text are to be ignored.
Function: encode
Inputs: text, a string
Preconditions: text is not empty
Output: encoded, a list of strings.
Postconditions: encoded is a list of 0 or more strings. Each element consists of a Morse code version of the upper-case letters specified in text.
Write a Python function that satisfies this specification.
I'm working with notebook phyton
The following code implements a class, Tree, and methods is_empty and join . You will need to run this code block to use in your solutions.
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)
Step by step
Solved in 4 steps