diff --git a/binaryHeap.py b/binaryHeap.py new file mode 100644 index 0000000000000000000000000000000000000000..b9343e5233ecc72f3ebe167b68b9ee4e8c19c1f5 --- /dev/null +++ b/binaryHeap.py @@ -0,0 +1,27 @@ +tree = "-ETIANMSURWDKGOHVF*L*PJBXCYZQ**54*3*¿?2&*+****16=/***(*7***8*90*************_****\"**.********'**-********;!*)***¡*,****:****************$***********************************************************************************************************************" + + +def decode(string): + + output = "" + parent = 1 + for i in range(len(string)): + if i < len(string) - 1 : + if string[i] == " ": + output += tree[parent - 1] + parent = 1 + if string[i] == ".": + parent = 2 * parent + elif string[i] == "-": + parent = 2 * parent + 1 + else: + if string[i] == ".": + parent = 2 * parent + if string[i] == "-": + parent = 2 * parent + 1 + output += tree[parent - 1] + parent = 1 + return output + + +print(decode(".-. .---- -.. . ... .---- -...- .... .. -...- -.--.")) \ No newline at end of file diff --git a/binaryHeap2.py b/binaryHeap2.py new file mode 100644 index 0000000000000000000000000000000000000000..56710c52ec40058833b31aa7ffbb942b8fe93f02 --- /dev/null +++ b/binaryHeap2.py @@ -0,0 +1,24 @@ +# encode_ham(sender: str, receiver: str, msg:str) -> str +import morse +import server +import asyncio + +def encode_ham(sender, receiver, msg): + output = receiver + "de" + sender + "=" + msg + "=(" + # print(output) + return morse.encode(output) + +def decode_ham(str2): + print("hi") + ret = morse.decode(str2) + sender = ret.split("DE") + receiver = sender[1].split("=") + msg = receiver[1].split("=(") + return (sender[0],receiver[0],msg[0]) + +# print(encode_ham("jack","echo","its me")) +print(encode_ham("s","echo","msg")) +asyncio.run(server.main(encode_ham("jack","time","hello world"))) +print(decode_ham(asyncio.run(server.main(encode_ham("jack","echo","hello how are you"))))) + +# print(decode_ham(".-. .---- -.. . ... .---- -...- .... .. -...- -.--.")) \ No newline at end of file diff --git a/convert.py b/convert.py new file mode 100644 index 0000000000000000000000000000000000000000..6542bc0432fe961fb2925b1c2e1d70f0a1b79755 --- /dev/null +++ b/convert.py @@ -0,0 +1,6 @@ +tree = "-ETIANMSURWDKGOHVF*L*PJBXCYZQ**54*3*¿?2&*+****16=/***(*7***8*90*************_****\"**.********'**-********;!*)***¡*,****:****************$***********************************************************************************************************************" +output = "" +for item in tree: + output += "\"" + item + "\"" + "," + +print(output) \ No newline at end of file diff --git a/inserter.py b/inserter.py new file mode 100644 index 0000000000000000000000000000000000000000..aa05641ea8f0b4fa8660aeeb274c675021f3c4ff --- /dev/null +++ b/inserter.py @@ -0,0 +1,76 @@ +tree = "-ETIANMSURWDKGOHVF*L*PJBXCYZQ**54*3***2**+****16=/*****7***8*90" + + +def decode(string): + global tree + output = "" + parent = 1 + for i in range(len(string)): + if i < len(string) - 1 : + if string[i] == " ": + output += tree[parent - 1] + parent = 1 + if string[i] == ".": + parent = 2 * parent + elif string[i] == "-": + parent = 2 * parent + 1 + else: + if string[i] == ".": + parent = 2 * parent + if string[i] == "-": + parent = 2 * parent + 1 + output += tree[parent - 1] + parent = 1 + return output + + + +def add(string,addString): + global tree + string = string.replace(" ", "") + print(pow(2,len(string))*2) + print(len(tree)) + if len(tree) <= (pow(2,len(string)*2)): + for i in range(pow(2,len(string))*2 - len(tree)): + tree = tree + "*" + output = "" + parent = 1 + for i in range(len(string)): + if i < len(string) - 1 : + if string[i] == ".": + parent = 2 * parent + elif string[i] == "-": + parent = 2 * parent + 1 + else: + if string[i] == ".": + parent = 2 * parent + if string[i] == "-": + parent = 2 * parent + 1 + output = tree[:parent-1] + addString + tree[parent:] + tree = output + print(output) + parent = 1 + + print(decode(string)) + if decode(string) != addString: + print("error, insert fail") + return output + +# print(decode(".-. .---- -.. . ... .---- -...- .... .. -...- -.--.")) +print(add(".-.-.-",".")) +print(add("-.--.","(")) +print(add(".-.-.","+")) +print(add("..-.- ","¿")) +print(add("--..-- ",",")) +print(add("-.--.-",")")) +print(add("-....-","-")) +print(add("--...-","¡")) +print(add("..--.","?")) +print(add(".-...","&")) +print(add("..--.- ","_")) +print(add(".----.","'")) +print(add("---...",":")) +print(add(".-..-. ",'"')) +print(add("-.-.--","!")) +print(add("-.-.-.",";")) +print(add("...-..-","$")) diff --git a/morse.py b/morse.py index 125e84c3119f61c461f87ed691f39a5bf2a61f3d..8e138aab664609abcbe058d258e6d55e15af5116 100644 --- a/morse.py +++ b/morse.py @@ -86,7 +86,8 @@ class node: return "" def initTree(): - alphabet = ["E","T","I","A","N","M","S","U","R","W","D","K","G","O","H","V","F","","L","","P","","J","B","X","C","Y","Z","Q","",""] + # alphabet = ["E","T","I","A","N","M","S","U","R","W","D","K","G","O","H","V","F","","L","","P","","J","B","X","C","Y","Z","Q","",""] + alphabet = ["E","T","I","A","N","M","S","U","R","W","D","K","G","O","H","V","F","*","L","*","P","J","B","X","C","Y","Z","Q","*","*","5","4","*","3","*","¿","?","2","&","*","+","*","*","*","*","1","6","=","/","*","*","*","(","*","7","*","*","*","8","*","9","0","*","*","*","*","*","*","*","*","*","*","*","*","*","_","*","*","*","*","\"","*","*",".","*","*","*","*","*","*","*","*","'","*","*","-","*","*","*","*","*","*","*","*",";","!","*",")","*","*","*","¡","*",",","*","*","*","*",":","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","$","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*","*"] queue = [] start = node("start") diff --git a/server.py b/server.py new file mode 100644 index 0000000000000000000000000000000000000000..cc508982dc1563b8dab34a82c982a458b86cafef --- /dev/null +++ b/server.py @@ -0,0 +1,50 @@ +import asyncio +import websockets +import json +import time + + +async def main(str): + uri = "ws://localhost:10102" + async with websockets.connect(uri) as websocket: + # After joining server will send client unique id. + message = json.loads(await websocket.recv()) + print(message) + # Get the client_id from the join message + if message['type'] == 'join_evt': + client_id = message['client_id'] + # Send a ping to the server + await send_message(websocket, str, client_id) + # Wait for the 'ping' response from the server + response = await recv_message(websocket) + print("The Server Sent Back:") + print(response) + + else: + # If first message is not the join message exit + print("Did not receive a correct join message") + return 0 + return response + +async def send_message(websocket, message, client_id): + outward_message = { + 'type' : 'morse_evt', + 'client_id': client_id, + 'payload': message + } + await websocket.send(json.dumps(outward_message)) + +async def recv_message(websocket): + message = json.loads(await websocket.recv()) + return message['payload'] + + + + + +if __name__ == "__main__": + print("Echo client") + + while 1: + time.sleep(1) + asyncio.run(main())