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.

315 lines
9.0 KiB
Plaintext

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

{
"cells": [
{
"cell_type": "markdown",
"id": "29e43a9b-4fe9-4fa4-83fc-8bc7cc733914",
"metadata": {},
"source": [
"# a morse code tree"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "85da8440-0286-4fc5-8009-a1dbbbeaf195",
"metadata": {},
"outputs": [],
"source": [
"# https://www.101computing.net/morse-code-using-a-binary-tree/"
]
},
{
"cell_type": "code",
"execution_count": 62,
"id": "18c38e4e-67f6-4814-a9ee-014af7b231dd",
"metadata": {},
"outputs": [],
"source": [
"# modified left and right pointers to dot and dash\n",
"class Node:\n",
" def __init__(self, value, dot=None, dash=None):\n",
" self.value = value\n",
" self.dot = dot\n",
" self.dash = dash"
]
},
{
"cell_type": "code",
"execution_count": 63,
"id": "93595ce3-cbd6-4b58-ad8d-b25d89a14b11",
"metadata": {},
"outputs": [],
"source": [
"def getMorseCode(node, character, code):\n",
" if node==None:\n",
" return False\n",
" elif node.value==character:\n",
" # return to end program\n",
" return True\n",
" # keep traversing when the pointer is not pointing to null\n",
" else: \n",
" if getMorseCode(node.dot,character,code)==True:\n",
" code.insert(0,\".\")\n",
" return True\n",
" elif getMorseCode(node.dash,character,code)==True:\n",
" code.insert(0,\"-\")\n",
" return True"
]
},
{
"cell_type": "code",
"execution_count": 76,
"id": "1da7f6e4-114b-4087-a777-65a21525a51b",
"metadata": {},
"outputs": [],
"source": [
"# printing tree utility \n",
"# this segment is modified from Shubham Singh(SHUBHAMSINGH10)'s contribution \n",
"\n",
"# spacer\n",
"COUNT = [10]\n",
"\n",
"# print a flat lying tree\n",
"# speculation this is a recursion that prints the right leaf until there is nothing left\n",
"def print2DUtil_flat(root, space) :\n",
" # Base case\n",
" if (root == None) :\n",
" return\n",
" # Increase distance between levels\n",
" space += COUNT[0]\n",
" # Process right leaf/branch/child first\n",
" print2DUtil_flat(root.dash, space)\n",
" print()\n",
" for i in range(COUNT[0], space):\n",
" print(end = \" \")\n",
" print(root.value)\n",
" \n",
" #for i in range(COUNT[0], space):\n",
" # print(end = \" \")\n",
" #print(root.letter) \n",
" #print(root.value)\n",
" # Process left child\n",
" print2DUtil_flat(root.dot, space)\n",
"\n",
" # Wrapper over print2DUtil()\n",
"def print2D(root) :\n",
" #Pass initial space count as 0\n",
" print(\"here is a tree that's laying on the ground: \")\n",
" print2DUtil_flat(root, 0)\n"
]
},
{
"cell_type": "code",
"execution_count": 77,
"id": "c0e844a1-3d41-40e5-afde-9ff16de294d9",
"metadata": {},
"outputs": [],
"source": [
"#Let's initialise our binary tree:\n",
"tree = Node(\"START\") #The root node of our binary tree\n",
"\n",
"# 1st Level\n",
"tree.dot = Node(\"E\")\n",
"tree.dash = Node(\"T\")\n",
"\n",
"# 2nd Level\n",
"tree.dot.dot = Node(\"I\")\n",
"tree.dot.dash = Node(\"A\")\n",
"tree.dash.dot = Node(\"N\")\n",
"tree.dash.dash = Node(\"M\")\n",
"\n",
"# 3rd Level\n",
"tree.dot.dot.dot = Node(\"S\")\n",
"tree.dot.dot.dash = Node(\"U\")\n",
"tree.dot.dash.dot = Node(\"R\")\n",
"tree.dot.dash.dash = Node(\"W\")\n",
"\n",
"tree.dash.dot.dot = Node(\"D\")\n",
"tree.dash.dot.dash = Node(\"K\")\n",
"tree.dash.dash.dot = Node(\"G\")\n",
"tree.dash.dash.dash = Node(\"O\")\n",
"\n",
"# 4th Level\n",
"tree.dot.dot.dot.dot = Node(\"H\")\n",
"tree.dot.dot.dot.dash = Node(\"V\")\n",
"tree.dot.dot.dash.dot = Node(\"F\")\n",
"tree.dot.dot.dash.dash = Node(\"\")\n",
"tree.dot.dash.dot.dot = Node(\"L\")\n",
"tree.dot.dash.dot.dash = Node(\"\")\n",
"tree.dot.dash.dash.dot = Node(\"P\")\n",
"tree.dot.dash.dash.dash = Node(\"J\")\n",
"\n",
"tree.dash.dot.dot.dot = Node(\"B\")\n",
"tree.dash.dot.dot.dash = Node(\"X\")\n",
"tree.dash.dot.dash.dot = Node(\"C\")\n",
"tree.dash.dot.dash.dash = Node(\"Y\")\n",
"tree.dash.dash.dot.dot = Node(\"Z\")\n",
"tree.dash.dash.dot.right = Node(\"Q\")\n",
"tree.dash.dash.dash.dot = Node(\"\")\n",
"tree.dash.dash.dash.dash = Node(\"\")\n",
"\n",
"# how to insert letters without entering one by one? use what structure?\n"
]
},
{
"cell_type": "code",
"execution_count": 78,
"id": "be1252af-f941-47c1-a717-dd04f53e0c68",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"here is a tree that's laying on the ground: \n",
"\n",
" \n",
"\n",
" O\n",
"\n",
" \n",
"\n",
" M\n",
"\n",
" G\n",
"\n",
" Z\n",
"\n",
" T\n",
"\n",
" Y\n",
"\n",
" K\n",
"\n",
" C\n",
"\n",
" N\n",
"\n",
" X\n",
"\n",
" D\n",
"\n",
" B\n",
"\n",
"START\n",
"\n",
" J\n",
"\n",
" W\n",
"\n",
" P\n",
"\n",
" A\n",
"\n",
" \n",
"\n",
" R\n",
"\n",
" L\n",
"\n",
" E\n",
"\n",
" \n",
"\n",
" U\n",
"\n",
" F\n",
"\n",
" I\n",
"\n",
" V\n",
"\n",
" S\n",
"\n",
" H\n"
]
}
],
"source": [
"# print morse code tree configuration\n",
"print2D(tree)"
]
},
{
"cell_type": "code",
"execution_count": 79,
"id": "c9e6e242-c742-416e-b2cb-550d4e7a8ff2",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter a message to convert into Morse Code: (e.g. SOS) hwllo\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
".... .-- .-.. .-.. --- \n"
]
}
],
"source": [
"#Message Input\n",
"message = input(\"Enter a message to convert into Morse Code: (e.g. SOS)\").upper()\n",
"morseCode = \"\"\n",
"\n",
"#Convert the message, one character at a time!\n",
"for character in message:\n",
" dotsdashes = []\n",
" getMorseCode(tree,character,dotsdashes)\n",
" code = \"\".join(dotsdashes)\n",
" morseCode = morseCode + code + \" \"\n",
"print(morseCode)"
]
},
{
"cell_type": "markdown",
"id": "93252875-0f4f-40f8-af62-b027f916eb60",
"metadata": {},
"source": [
"# todo \n",
"modify the tree print out to show the dots and dashes \n",
"take a user input in morse code, and use the binary tree to decode the message one character at a time\n",
"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)\n",
"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 "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ddeba74b-f6bc-4e06-ab2b-3b176a485bc9",
"metadata": {},
"outputs": [],
"source": [
"# reference project\n",
"# https://microbit-micropython.readthedocs.io/en/latest/tutorials/radio.html\n",
"# https://new.pythonforengineers.com/blog/audio-and-digital-signal-processingdsp-in-python/"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}