You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
9.0 KiB
9.0 KiB
a morse code tree¶
In [ ]:
# https://www.101computing.net/morse-code-using-a-binary-tree/
In [62]:
# modified left and right pointers to dot and dash class Node: def __init__(self, value, dot=None, dash=None): self.value = value self.dot = dot self.dash = dash
In [63]:
def getMorseCode(node, character, code): if node==None: return False elif node.value==character: # return to end program return True # keep traversing when the pointer is not pointing to null else: if getMorseCode(node.dot,character,code)==True: code.insert(0,".") return True elif getMorseCode(node.dash,character,code)==True: code.insert(0,"-") return True
In [76]:
# printing tree utility # this segment is modified from Shubham Singh(SHUBHAMSINGH10)'s contribution # spacer COUNT = [10] # print a flat lying tree # speculation: this is a recursion that prints the right leaf until there is nothing left def print2DUtil_flat(root, space) : # Base case if (root == None) : return # Increase distance between levels space += COUNT[0] # Process right leaf/branch/child first print2DUtil_flat(root.dash, space) print() for i in range(COUNT[0], space): print(end = " ") print(root.value) #for i in range(COUNT[0], space): # print(end = " ") #print(root.letter) #print(root.value) # Process left child print2DUtil_flat(root.dot, space) # Wrapper over print2DUtil() def print2D(root) : #Pass initial space count as 0 print("here is a tree that's laying on the ground: ") print2DUtil_flat(root, 0)
In [77]:
#Let's initialise our binary tree: tree = Node("START") #The root node of our binary tree # 1st Level tree.dot = Node("E") tree.dash = Node("T") # 2nd Level tree.dot.dot = Node("I") tree.dot.dash = Node("A") tree.dash.dot = Node("N") tree.dash.dash = Node("M") # 3rd Level tree.dot.dot.dot = Node("S") tree.dot.dot.dash = Node("U") tree.dot.dash.dot = Node("R") tree.dot.dash.dash = Node("W") tree.dash.dot.dot = Node("D") tree.dash.dot.dash = Node("K") tree.dash.dash.dot = Node("G") tree.dash.dash.dash = Node("O") # 4th Level tree.dot.dot.dot.dot = Node("H") tree.dot.dot.dot.dash = Node("V") tree.dot.dot.dash.dot = Node("F") tree.dot.dot.dash.dash = Node("") tree.dot.dash.dot.dot = Node("L") tree.dot.dash.dot.dash = Node("") tree.dot.dash.dash.dot = Node("P") tree.dot.dash.dash.dash = Node("J") tree.dash.dot.dot.dot = Node("B") tree.dash.dot.dot.dash = Node("X") tree.dash.dot.dash.dot = Node("C") tree.dash.dot.dash.dash = Node("Y") tree.dash.dash.dot.dot = Node("Z") tree.dash.dash.dot.right = Node("Q") tree.dash.dash.dash.dot = Node("") tree.dash.dash.dash.dash = Node("") # how to insert letters without entering one by one? use what structure?
In [78]:
# print morse code tree configuration print2D(tree)
here is a tree that's laying on the ground: O M G Z T Y K C N X D B START J W P A R L E U F I V S H
In [79]:
#Message Input message = input("Enter a message to convert into Morse Code: (e.g. SOS)").upper() morseCode = "" #Convert the message, one character at a time! for character in message: dotsdashes = [] getMorseCode(tree,character,dotsdashes) code = "".join(dotsdashes) morseCode = morseCode + code + " " print(morseCode)
.... .-- .-.. .-.. ---
todo¶
modify the tree print out to show the dots and dashes take a user input in morse code, and use the binary tree to decode the message one character at a time translate the dot dash signals into other forms of pulsations: LED light, sound to be sent out via radio signal, ?FSK (written on the code memo) would be nice to emphasize the traversal aspect, can it be displayed / heard - traversing to the dot! traversing to the dash! make it procedural -> maybe console can be helpful
In [ ]:
# reference project # https://microbit-micropython.readthedocs.io/en/latest/tutorials/radio.html # https://new.pythonforengineers.com/blog/audio-and-digital-signal-processingdsp-in-python/