diff --git a/morse/decoder.py b/morse/decoder.py index f43fdc4..9d5355f 100644 --- a/morse/decoder.py +++ b/morse/decoder.py @@ -1,9 +1,34 @@ -pass # YOUR CODE HERE +from morse.mapping import MORSE +# Sözlüğü tersine çeviriyoruz: {".-": "A", "-...": "B", ...} +DECODE_DICT = {value: key for key, value in MORSE.items()} + +def decode_word(morse_word): + """ + Tek bir Morse kelimesini metne çevirir. + """ + # Morse harflerini boşluktan ayırıp karşılıklarını buluyoruz + letters = [DECODE_DICT[m_char] for m_char in morse_word.split() if m_char in DECODE_DICT] + return "".join(letters) + +def decode(morse_text): + """ + Tüm Morse cümlesini metne çevirir. Kelimeler '|' ile ayrılmıştır. + """ + # Önce kelimeleri ayırıyoruz + morse_words = morse_text.split('|') + # Her kelimeyi decode edip boşlukla birleştiriyoruz + decoded_words = [decode_word(word) for word in morse_words] + return " ".join(decoded_words) if __name__ == "__main__": - # Example usage for one word - pass # YOUR CODE HERE + # Test: Önce encode et, sonra geri decode et (Round-trip) + from morse.encoder import encode + + ORIGINAL = "HELLO WORLD" + ENCODED = encode(ORIGINAL) + DECODED = decode(ENCODED) - # Example usage for one sentence - pass # YOUR CODE HERE + print(f"Original: {ORIGINAL}") + print(f"Morse: {ENCODED}") + print(f"Decoded: {DECODED}") diff --git a/morse/encoder.py b/morse/encoder.py index e7ce48e..f050fa3 100644 --- a/morse/encoder.py +++ b/morse/encoder.py @@ -1,37 +1,29 @@ -""" -This module provides functions to encode text into Morse code. - -Functions: -- encode(text): Encodes a given text into Morse code, separating words with a pipe (|) and - letters with a space. -- encode_word(word): Encodes a single word into Morse code, separating letters with a space. -""" - from morse.mapping import MORSE -def encode(text): +def encode_word(word): """ - Encodes the given text into Morse code. - Words are separated by a pipe (|) and letters by a space. + Bir kelimeyi Morse koduna çevirir. Harfler arası boşluk bırakır. """ - pass # YOUR CODE HERE - + # Harfleri büyük harfe çevirip karşılığını alıyoruz, geçersiz karakterleri atlıyoruz. + morse_letters = [MORSE[char.upper()] for char in word if char.upper() in MORSE] + return " ".join(morse_letters) -def encode_word(word): +def encode(text): """ - Encodes a single word into Morse code. - Letters are separated by a space. + Tüm metni Morse koduna çevirir. Kelimeler arası '|' bırakır. """ - pass # YOUR CODE HERE - + # Metni kelimelere bölüyoruz + words = text.split() + # Her kelimeyi encode_word ile çevirip aralarına '|' ekliyoruz + encoded_words = [encode_word(word) for word in words] + return "|".join(encoded_words) if __name__ == "__main__": - # Example usage for one word - EXAMPLE_TEXT = "abc" - ENCODED_TEXT = encode_word(EXAMPLE_TEXT) - print(f"Encoded word '{EXAMPLE_TEXT}' to Morse code: '{ENCODED_TEXT}'") - - # Example usage for a sentence - EXAMPLE_TEXT = "abc ABC" - ENCODED_TEXT = encode(EXAMPLE_TEXT) - print(f"Encoded '{EXAMPLE_TEXT}' to Morse code: '{ENCODED_TEXT}'") + # Örnek kullanım + EXAMPLE_WORD = "abc" + ENCODED_WORD = encode_word(EXAMPLE_WORD) + print(f"Encoded word '{EXAMPLE_WORD}' to Morse code: '{ENCODED_WORD}'") + + EXAMPLE_SENTENCE = "Hi Guys" + ENCODED_SENTENCE = encode(EXAMPLE_SENTENCE) + print(f"Encoded '{EXAMPLE_SENTENCE}' to Morse code: '{ENCODED_SENTENCE}'")