Morse

Rok za oddajo: sreda, 17. november 2021, 15.00

Morsejeva abeceda omogoča pošiljanje besedila s pomočjo različnih dolžin piskov, svetlobnih signalov ali, morda tudi z dimnimi signali. Črko V, na primer, sporočimo s tremi kratkimi in enim dolgim signalom, kar bi lahko zapisali tako "- - - ---"; dolgi signal je namreč približno trikrat daljši od kratkega. Med posameznimi signali je presledek. Da ločimo črke med sabo, naredimo med posameznimi črkami trikrat daljši presledek. Da ločimo dve besedi, naredimo še daljši premor, recimo šest presledkov. Tole bi lahko bil primer sporočila:

- - - ---   - - -   --- --- --- - ---      - - --- -   - --- -   - -      - - ---   - --- - -

Takšnega zapisa v resnici ne uporabljamo; običajno bi kratki znak opisali s piko in daljšega s črtico. Črko V bi torej opisali z "...-". Presledkov znotraj črke ne pišemo. Pač pa bomo med dvema črkama naredili en presledek, med dvema besedama pa po tri. Gornji signal bi torej zapisali tako:

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

Prva črka gornjega zapisa je, vemo, V. Naprej gre tako:

VSŠ FRI UL

V tem zapisu seveda ni presledkov med črkami, pač pa imamo en presledek med besedami.

Obvezni del

Napiši naslednje funkcije.

  1. v_zaporedje(signal) prejme signal v prvi obliki in vrne zaporedje v drugi obliki.

  2. vrni_znak(zaporedje, abeceda, morse) prejme niz z nekim zaporedjem v drugi obliki, na primer, "...-", niz, ki vsebuje vse črke abecede, na primer "ABCČDEFGHIJKLMNOPRSŠTUVZŽ" in seznam z Morsejevo abecedo: n-ti element seznama predstavlja zapis n-te črke abecede. Funkcija mora vrniti znak, ki ga predstavlja zaporedje - v gornjem primeru "V".

  3. v_besedo(zaporedje, abeceda, morse) prejme posamično besedo v drugi obliki in vrne besedo, zapisano z običajnimi črkami. Če funkcijo pokličemo z "...- ... ---.-" (in ustrezno definiranama abecedama), vrne niz "VSŠ".

    Funkcija prejme le eno besedo, brez trojnih presledkov.

  4. v_znake(zaporedje, abeceda, morse) prejme niz z zaporedjem v drugi obliki, vendar lahko vsebuje več besed. Če dobi kot argument celotno vrstico

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

    vrne celotno tretjo, "VSŠ FRI UL".

  5. preberi(signal, abeceda, morse) prejme signal, kot ga opisuje prva vrstica in vrne besedilo, kot je v tretji.

Dodatna naloga

Tole je težje samo zato, ker se morate zorganizirati sami: pišite si poljubne dodatne funkcije, ki vam utegnejo pomagati.

Napiši funkcijo zapisi(besedilo, abeceda, morse), ki prejme besedilo in vrne signal. Se pravi, dobi niz, kot ga imamo v tretji vrstici, in vrne takšnega, kot je v prvi.


Morse code allow us to send texts using signals (like sounds, light bursts...) of different lengths. The letter V, for instance, is represented by three short and one long burst, which could be written like - - - ---; the long burst is approximatelly three times longer than the short one, thus we us a single - character for the short signal and three - characters for a long one. Bursts are separated with a signle space. Two letters are separated by three spaces. Two words are separated by six spaces.

This could be an example of a message.

- - - ---   - - -   --- --- --- - ---      - - --- -   - --- -   - -      - - ---   - --- - -

We usually do not use such representation. Instead, we represent a short burst with . and a long one with -. The letter V is thus `"...-"

Takšnega zapisa v resnici ne uporabljamo; običajno bi kratki znak opisali s piko in daljšega s črtico. Črko V bi torej opisali z ...-. We don't use spaces between bursts; letters can be separated by a single space, and words can be separated by three spaces. The above signal would thus be written as:

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

The first letter of the above message is, we know, V. The rest is:

VSŠ FRI UL

In this representation we, of course, do not separate letters, and we put a single space between words.

Mandatory part

Write the following functions..

  1. v_zaporedje(signal) gets a string in the first form and returns the sequence in the second.

  2. vrni_znak(zaporedje, abeceda, morse) gets a string with a sequence in the second form, e.g. "...-", a string that contains all letters of some alphabet, e.g. "ABCČDEFGHIJKLMNOPRSŠTUVZŽ" and a list of Morse code sequences; the n-th element of the list represents the n-th letter of the alphabet. The function must return the character represented by the first argument - for the above case, it's "V".

  3. v_besedo(zaporedje, abeceda, morse) gets a single word in the second form and return the word in normal alphabet. If we call the function with "...- ... ---.-" (and properly defined alphabets), it returns the string "VSŠ".

    The function gets a single word, without triple spaces.

  4. v_znake(zaporedje, abeceda, morse) gets a string in the second form and returns its textual representation. The difference between this and the previous function is that this can receive multiple words. For instance, if the argument is

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

    the function returns, "VSŠ FRI UL".

  5. preberi(signal, abeceda, morse) gets a signal as described in the first line and returns the text as described in the last line. In other words, it ties everything together.

Extra task

This is more difficult just because the task does not lead you through a series of small steps. You are allowed and encouraged to organize the work into smaller functions, similar to those above.

Write a function zapisi(besedilo, abeceda, morse), which receive a text and returns a signal (as in the first form, with minuses and single, triple and 6-uple spaces). In other words: the opposite of preberi.


Test