mchd
mchd

Reputation: 3163

''.join() method omits the space between words

I'm building a simple morse code converter for a challenge. Basically I'm given a sentence and I have to convert that to its morse equivalent using a dictionary. I'm using a basic for loop for this but once I try to convert my list into a string, I see that ''.join() method completely omits the space between the morse translation.

This is my code:

def encode_morse(message):
    char_to_dots = {
  'A': '.-', 'B': '-...', 'C': '-.-.', 'D': '-..', 'E': '.', 'F': '..-.',
  'G': '--.', 'H': '....', 'I': '..', 'J': '.---', 'K': '-.-', 'L': '.-..',
  'M': '--', 'N': '-.', 'O': '---', 'P': '.--.', 'Q': '--.-', 'R': '.-.',
  'S': '...', 'T': '-', 'U': '..-', 'V': '...-', 'W': '.--', 'X': '-..-',
  'Y': '-.--', 'Z': '--..', ' ': ' ', '0': '-----',
  '1': '.----', '2': '..---', '3': '...--', '4': '....-', '5': '.....',
  '6': '-....', '7': '--...', '8': '---..', '9': '----.',
  '&': '.-...', "'": '.----.', '@': '.--.-.', ')': '-.--.-', '(': '-.--.',
  ':': '---...', ',': '--..--', '=': '-...-', '!': '-.-.--', '.': '.-.-.-',
  '-': '-....-', '+': '.-.-.', '"': '.-..-.', '?': '..--..', '/': '-..-.'
}
    print(message)
    message_list = list(message)
    morse_output = []

    for i in message_list:
        if i != ' ':
            morse_output.append(char_to_dots.get(str(i)))
        else:
            morse_output.append(' ')

    return ''.join(morse_output)

And this is the output:

EDABBIT CHALLENGE
FAILED: Test 1: '.-...--...-.....- -.-......-.-...-...-.--..' should equal '. -.. .- -... -... .. -   -.-. .... .- .-.. .-.. . -. --. .'
ERROR: Traceback:
   in <module>
  File "./frameworks/python/cw-2.py", line 28, in assert_equals
    expect(actual == expected, message, allow_raise)
  File "./frameworks/python/cw-2.py", line 18, in expect
    raise AssertException(message)
cw-2.AssertException: Test 1: '.-...--...-.....- -.-......-.-...-...-.--..' should equal '. -.. .- -... -... .. -   -.-. .... .- .-.. .-.. . -. --. .'

Upvotes: 0

Views: 208

Answers (3)

LevB
LevB

Reputation: 953

You can insert ' ':' ' into the dictionary to take care of the space (so you don't need the "if i != ' '" condition).

char_to_dots = {
'A': '.-', 'B': '-...', 'C': '-.-.', 'D': '-..', 'E': '.', 'F': '..-.',
'G': '--.', 'H': '....', 'I': '..', 'J': '.---', 'K': '-.-', 'L': '.-..',
'M': '--', 'N': '-.', 'O': '---', 'P': '.--.', 'Q': '--.-', 'R': '.-.',
'S': '...', 'T': '-', 'U': '..-', 'V': '...-', 'W': '.--', 'X': '-..-',
'Y': '-.--', 'Z': '--..', ' ': ' ', '0': '-----',
'1': '.----', '2': '..---', '3': '...--', '4': '....-', '5': '.....',
'6': '-....', '7': '--...', '8': '---..', '9': '----.',
'&': '.-...', "'": '.----.', '@': '.--.-.', ')': '-.--.-', '(': '-.--.',
':': '---...', ',': '--..--', '=': '-...-', '!': '-.-.--', '.': '.-.-.-',
'-': '-....-', '+': '.-.-.', '"': '.-..-.', '?': '..--..', '/': '-..-.',
' ': ' '
}

message ="HI THERE"
a = [char_to_dots[c] for c in message]
print(' '.join(a))

Output:

.... ..   - .... . .-. .

Upvotes: 0

MarianD
MarianD

Reputation: 14121

Your code doesn't produce your output, as you claimed.

Replace the part

message_list = list(message)
morse_output = []

for i in message_list:
    if i != ' ':
        morse_output.append(char_to_dots.get(str(i)))
    else:
        morse_output.append(' ')

return ''.join(morse_output)

with

morse_output = []

for i in message:
    if i != ' ':
        morse_output.append(char_to_dots[i.upper()])
    else:
        morse_output.append('  ')

return ' '.join(morse_output)

The explanation:

  1. No need for making a list from a string (message_list = list(message)), if you want to iterate over its chars.

  2. You need to convert letters to the capital ones (i.upper()), because your dictionary contains only capital letter keys.

  3. Using dictionary to assign value to a given key is as simple as add that key in square brackets after the dictionary name (char_to_dots[i.upper()]).

  4. You need to separate both individual letters and individual words in the converted output — I chose 1 space for letter delimiter (' '.join(morse_output)) and another 2 for word delimiter (morse_output.append(' ')).

Upvotes: 2

Thomas
Thomas

Reputation: 899

You should specify that you want to join by a space (' '). Have you’re program say ' '.join(morse_output), not ''.join(morse_output).

Upvotes: 2

Related Questions