PROG0198 - Morse code

no tags 

Morse code is an encrypted form of communication, consisting of signals which are transmitted at intervals. Fixed combinations of these signals represent the different letters, punctuation, and numbers. The Morse code was invented in 1835 and developed by Samuel Morsewith the aim of using it for sending telegraphic messages . With a telegraph (see figure) they could only choose between two states: the key down (= current) or key upwards (= no current) and duration (short or long). Telegraphy is widely regarded as a forerunner of the later digital communication. Note that at speed races between experienced Morse code operators and experts in sending text messages, Morse code always wins.

telegraaf

In the international Morse code short signals are noted down as one dot (.) and long signals with a dash (-). Between different characters a short pause is inserted each time which is denoted by a space (letter space), and between different words a longer pause is inserted each time (word space) which is denoted by a slash (/). For example, ... --- ... is commonle known as SOS in Morse code. The text MORSE CODE itself is written as

-- --- .-. ... .      /     -.-. --- -.. .
M  O   R   S   E  (space)  C    O   D   E

Assignment

Given is a text file morsecode.txt. This text file contains a list of letters (first column) and the corresponding representation in Morse code (second column). The columns are separated by a tab. For example, in this file you can see that the letter K is represented in Morse code as -.- and that a comma is represented as --..--. Asked:

  1. Write a function readMorseCodes that takes the location of a text file. The content of this file must be formatted as the text file morsecode.txt. The function must return a dictionary, in which the characters from the file are used as keys, and the Morse code notation of these characters and their corresponding values.
  2. Write a function textToMorse to which two parameters need to be passed. For the string that is passed as the first parameter, the function must return the corresponding representation in Morse code. Here the conversion of characters to their corresponding Morse code must be looked up in the dictionary that is passed to the function as a second parameter. This dictionary has the form that is returned by the function readMorseCodes. Characters that are not in the dictionary (and not in morsecode.txt) should be ignored in the conversion. For conversion into Morse code no distinction should be made between large and small letters in the string that is passed to the function textToMorse as the first parameter.
  3. Write a function morseToText that converts morse code into readable text. The Morse code must be passed to the function as first parameter. Analogous to the textToMorse function the function morseToText must also call on the dictionary, which is passed to the function as a second parameter, for the conversion of Morse code to the corresponding character. This dictionary has the form that is returned by the function readMorseCodes.

Example

In the following interactive Python session we assume that the text file morsecode.txt is located in the current directory.

>>> characterToCode = readMorseCodes('morsecode.txt')
>>> characterToCode['S']
'...'
>>> characterToCode['O']
'---'

>>> characterToCode = readMorseCodes('morsecode.txt')
>>> morse = textToMorse('SOS Piet', characterToCode)
>>> morse
'... --- .../.--. .. . -'
>>> morseToText(morse, characterToCode)
'SOS PIET'

Epilogue

The blinking light atop the Capitol Records Tower in Hollywood, Los Angeles spells out the word HOLLYWOOD in Morse code. It's done so ever since the building opened in 1956.

Morse is een gecodeerde communicatievorm bestaande uit signalen die met tussenpozen worden uitgezonden. Vastgelegde combinaties van deze signalen stellen dan de verschillende letters, leestekens en cijfers voor. Deze morsecode werd in 1835 uitgevonden en ontwikkeld door Samuel Morse, met als doel ze te gebruiken voor het verzenden van telegrafische berichten. Met een telegraaf (zie figuur) kon men namelijk enkel kiezen tussen twee toestanden: sleutel naar beneden (= stroom) of sleutel naar boven (= geen stroom) en tijdsduur (kort of lang). Telegrafie wordt algemeen beschouwd als een voorloper van de latere digitale communicatie. Merk op dat in snelheidswedstrijden tussen ervaren morsecodeoperatoren en experts in het verzenden van SMS berichten, morsecode steevast als overwinnaar uit de bus komt.

telegraaf

In de internationale morsecode worden korte signalen genoteerd met een punt (.) en lange signalen met een koppelteken (-). Tussen verschillende lettertekens wordt telkens een korte pauze ingelast (letterspatie) die wordt genoteerd met een spatie, en tussen verschillende woorden wordt telkens een langere pauze ingelast (woordspatie) die wordt genoteerd met een slash (/). Zo is ... --- ... vrij algemeen bekend als de voorstelling van het SOS signaal in morsecode. De tekst MORSE CODE zelf wordt in morsecode genoteerd als

-- --- .-. ... .      /     -.-. --- -.. .
M  O   R   S   E  (spatie)  C    O   D   E

Opgave

Gegeven is een tekstbestand morsecode.txt. Dit tekstbestand bevat een lijst van lettertekens (eerste kolom) en hun corresponderende voorstelling in morsecode (tweede kolom). De kolommen worden gescheiden door een tab. In dit bestand zie je bijvoorbeeld dat de letter K in morsecode wordt voorgesteld als -.- en dat een komma wordt voorgesteld als --..--. Gevraagd wordt:

  1. Schrijf een functie leesMorseCodes waaraan de locatie van een tekstbestand moet doorgegeven worden. De inhoud van dit tekstbestand moet opgemaakt zijn zoals bestand morsecode.txt. De functie moet als resultaat een dictionary teruggeven, waarin de karakters uit het bestand als sleutels gebruikt worden, en de morsecode notatie van deze karakters als hun corresponderende waarden.
  2. Schrijf een functie tekstNaarMorse waaraan twee parameters moeten doorgegeven worden. De functie moet voor de string die als eerste parameter wordt doorgegeven, de corresponderende voorstelling in morsecode teruggeven. Hierbij moet de omzetting van karakters naar hun corresponderende morsecode opgezocht worden in de dictionary die als tweede parameter aan de functie wordt doorgegeven. Deze dictionary heeft de vorm zoals die door de functie leesMorseCodes wordt teruggegeven. Karakters die niet in de dictionary (en dus niet in morsecode.txt) teruggevonden worden, moeten bij deze omzetting genegeerd worden. Bij de omzetting naar morsecode moet ook geen onderscheid gemaakt worden tussen grote en kleine letters in de string die als eerste parameter aan de functie tekstNaarMorse wordt doorgegeven.
  3. Schrijf een functie morseNaarTekst die morsecode omzet naar leesbare tekst. De morsecode moet als eerste parameter aan de functie doorgegeven worden. Analoog aan de functie tekstNaarMorse, moet ook de functie morseNaarTekst voor de omzetting van morsecode naar hun corresponderende karakter een beroep doen op de dictionary die als tweede parameter aan de functie wordt doorgegeven. Deze dictionary heeft de vorm zoals die door de functie leesMorseCodes wordt teruggegeven.

Voorbeeld

In onderstaande interactieve Python sessie gaan we ervan uit dat het tekstbestand morsecode.txt zich in de huidige directory bevindt.

>>> karakterNaarCode = leesMorseCodes('morsecode.txt')
>>> karakterNaarCode['S']
'...'
>>> karakterNaarCode['O']
'---'

>>> karakterNaarCode = leesMorseCodes('morsecode.txt')
>>> morse = tekstNaarMorse('SOS Piet', karakterNaarCode)
>>> morse
'... --- .../.--. .. . -'
>>> morseNaarTekst(morse, karakterNaarCode)
'SOS PIET'

Epiloog

Het knipperlicht bovenop de Capital Records Tower in Hollywood, Los Angeles spelt het woord HOLLYWOOD in morsecode. Het doet dit al onafgebroken sedert de opening van het gebouw in 1956.

Capital Records Tower
afbeelding: Wikimedia Commons


Added by:Peter Dawyndt
Date:2011-12-13
Time limit:5s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:PY_NBC
Resource:None