morse code to english python3

Once you define the mapping in one direction, you can use a dict comprehension to map it the other way CODE = {‘A’: ‘.-‘, ‘B’: ‘-…’, ‘C’: ‘-.-.’, ‘D’: ‘-..’, ‘E’: ‘.’, ‘F’: ‘..-.’, ‘G’: ‘–.’, ‘H’: ‘….’, ‘I’: ‘..’, ‘J’: ‘.—‘, ‘K’: ‘-.-‘, ‘L’: ‘.-..’, ‘M’: ‘–‘, ‘N’: ‘-.’, ‘O’: ‘—‘, ‘P’: ‘.–.’, ‘Q’: … Read more

How to replace a char with a string

You can use a StringBuilder to do this. Given that you have one array with your chars, you can simply iterate over each char and append its translated variant to the StringBuilder object. Example: char[] chars = {‘h’, ‘e’, ‘l’, ‘l’, ‘o’}; StringBuilder sb = new StringBuilder(); for (char c : chars) { sb.append(getMorse(c)); } … Read more