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.

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

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)

 

Expert Solution
steps

Step by step

Solved in 4 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY