Reputation: 312
I've been struggling with how to complete this assignment for quite a while now and decided I need some help. I have to convert morse code to english and output all possible translations to a listbox (even gibberish). Searching has turned up a lot of various threads but nothing useful as they're either not in this language or they're outdated (VB6) code. The assignment states:
Using the typical dot (.) and dash (-) for a written representation of the code, the word ...---..-....- in Morse code could be an encoding of the names Sofia or Eugenia depending on where you break up the letters: ...|---|..-.|..|.- Sofia .|..-|--.|.|-.|..|.- Eugenia
where the pipe is simply to show how the string is broken up.
I have tried "For Each" loops, "For" loops, writing to text files and reading back, splitting strings by 1 to 4 characters, a Select Case method and all combinations of the above I could think of with no luck. My For loops result in sets of 26 of everything, but no words, just series of blank spaces and then a single letter 26 times, followed by that letter twice 26 times, and so on and so on. Text files and splitting the strings by one or two characters result in either E, A or T for output, each on its own line times the number of iterations in the string, and splitting the string by three or four characters results in the same or an IndexOutOfRange error if the string doesn't evenly divide. I've come back to the closest thing that I've coded to actually working, but still the output results in just the English Alphabet being output.
I'm using a Linq query this time around and would appreciate any help in figuring out how to iterate through the strings properly to do what the assignment is asking.
Here is my code thus far:
Imports System.IO
Imports System.Text
Public Class Form1
Dim MorseCode() = File.ReadAllLines("MorseCode2.txt")
Private Sub input_TextChanged(sender As Object, e As EventArgs) Handles input.TextChanged
End Sub
Private Sub output_SelectedIndexChanged(sender As Object, e As EventArgs) Handles output.SelectedIndexChanged
End Sub
Private Sub help_Click(sender As Object, e As EventArgs) Handles help.Click
'Displays the Morse Code and English letter equivalents
Process.Start("MorseCode.txt")
End Sub
Private Sub translate_Click(sender As Object, e As EventArgs) Handles translate.Click
Dim userText As String = input.Text
Dim temp As String
Dim word As String
'Search the users entry for possible translations
Dim query = From line In MorseCode
Let data = line.split(","c)
Let engLtr = data(0)
Let code = data(1)
Select engLtr, code
For Each code In query
If userText.Contains(code.code) Then
temp = code.engLtr
word = String.Concat(temp + temp)
output.Items.Add(word)
End If
Next
End Sub
End Class
I'm pretty sure my error is in the .Concat call but nothing I've tried has resolved the issue.
Thank you for any pointers you can give me!
Edit to add Pseudo Code:
1 - Read input string
2 - break string into individual elements 1 symbol each, match symbol to letter, send to temp, concat strings into word, output each word to listbox
3 - re-read string breaking into two symbols each "..."
4 - "..." three symbols each "..."
5 - "..." four symbols each "..."
Upvotes: 2
Views: 711
Reputation: 1
Lang = {}
Concealer = {}
Reveal = {}
bottle = []
Mess1 = []
Mess2 = []
with open('69_Morse.txt', 'r') as translate:
for line in translate:
L, M = line.split()
Lang[M] = L
secret = open('SECRET.txt', 'r')
glasses = secret.read()
bottle = (glasses.replace("\n", " \n ")).split(" ")
secret.close()
for i in range(0, len(bottle)):
if bottle[i] in Lang:
Mess1.append(Lang[bottle[i]])
if bottle[i] == "\n":
Mess1.append(bottle[i])
for i in range(0, len(Mess1)):
if Mess1[i] in Reveal:
Mess2.append(Reveal[Mess1[i]])
else:
Mess2.append(Mess1[i])
Final = ("".join(Mess2))
with open("CodeBook.txt", 'r') as rosetta:
for line in rosetta:
a, b = line.split()
Concealer[a] = b
c, d = line.split()
Reveal[d] = c
f = open('69_MSG.txt', 'w')
f.write(Final)
f.close()
#Second file of code
import numpy as np
np.random.seed(20201123)
RandNumb = []
Code = []
RandLet = []
AlphaNum = {1: }
while len(RandNumb) != 26:
val = np.random.randint(1,27)
if val not in RandNumb:
RandNumb.append(val)
for i in range(0, len(RandNumb)):
RandLet.append(AlphaNum[RandNumb[i]])
with open("MorseCode.txt", 'r') as Morse:
for line in Morse:
line = line.strip('\n')
Code.append(line[2:])
f = open('69_Morse.txt', 'w')
for i in range(0, len(RandLet)):
f.write(RandLet[i] + " " + Code[i] + "\n")
f.close()
Upvotes: 0
Reputation: 312
I was given an answer [here] and was able to implement the idea into my own program. Thank you to everyone that helped me out!1
Upvotes: 0
Reputation: 172280
I have tried "For Each" loops, "For" loops, writing to text files and reading back, splitting strings by 1 to 4 characters, a Select Case method and all combinations of the above I could think of with no luck.
That's like saying "I want to prepare a delicious meal, and I have tried sugar, meat, milk, salt, a gas stove, an electric stove, cleaning the kitchen, a frying pan, a fork and all combinations of the above I could think of with no luck."
What you are obviously missing is a recipe. In computer science terms, that's an algorithm. I suggets that you do the following:
Step away from the computer. Take a piece of paper and a pencil.
Try to solve the problem by hand. Take the example ...---..-....-
and try to find all the possible meanings.
When you are done, think about how you solved the problem. What did you do to ensure that you do not miss any of the possible meanings? Try to write down the steps that you took in plain English. That's your algorithm.
Now you can start translating your algorithm into your programming language of choice.
If you have trouble with any of these steps, I suggest that you start a new question and ask about this point specifically. Don't forget to mention what approaches you have tried and where you are stuck.
Upvotes: 5