diff --git a/README.md b/README.md index 1a21906..071fc4f 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,172 @@ -# morse-code-decoder -Ruby implementation of Morse code decoding methods with reusable functions for decoding characters, words, and messages. + + +
+

Morse Code Decoder

+
+ +# 📗 Table of Contents + +- [📖 About the Project](#about-project) + - [🛠 Built With](#built-with) + - [Tech Stack](#tech-stack) + - [Key Features](#key-features) +- [💻 Getting Started](#getting-started) + - [Setup](#setup) + - [Prerequisites](#prerequisites) + - [Install](#install) + - [Usage](#usage) + - [Run tests](#run-tests) + - [Deployment](#triangular_flag_on_post-deployment) +- [👥 Authors](#authors) +- [🔭 Future Features](#future-features) +- [🤝 Contributing](#contributing) +- [⭐️ Show your support](#support) +- [🙏 Acknowledgements](#acknowledgements) +- [❓ FAQ (OPTIONAL)](#faq) +- [📝 License](#license) + +# 📖 Morse Code Decoder +This project is a Morse code decoder implemented in Ruby. It provides methods to decode Morse code characters, words, and entire messages. The decoded message can be printed or used in other applications. + +## 🛠 Built With + +### Tech Stack + +The project is built with the following technologies: + +- Ruby + +

(back to top)

+ +### Key Features + +The key features of this project include: + +- Decoding Morse code characters +- Decoding Morse code words +- Decoding entire messages in Morse code + +

(back to top)

+ +## 💻 Getting Started + +To get started with the project, follow the instructions below. + + +### Setup + +To set up the project locally, you need to have Ruby installed on your system. + +### Prerequisites + +- Ruby (version 2.5 or above) + +### Install + +1. Clone the repository: + +```sh + clone git@github.com:MussieTeka/morse-code-decoder.git +``` +2. Change into the project directory: + +``` +cd morse-code-decoder +``` +### Usage + +To use the Morse code decoder, follow these steps: + +1. Open the morse_decoder.rb file in a text editor. +2. Modify the message variable to contain the Morse code you want to decode. +3. Run the script using the command: + +``` +ruby morse_decoder.rb +``` +4. The decoded message will be printed on the console. +### Run tests + +There are no tests available for this project. +### Deployment + +There is no specific deployment process for this project as it is a code implementation. However, you can integrate the Morse code decoder into your own Ruby applications. + +

(back to top)

+ +## 👥 Author + +This project was developed by: + +👤 **Umair Hamza** + +- GitHub: [@emhamza](https://github.com/emhamza) +- Twitter: [@uhamzaz](https://twitter.com/uhamzaz) +- LinkedIn: [Umair Hamza](https://www.linkedin.com/in/umair-hamza-a8262b261/) + +👤 **Mussie Kahsay** + +- GitHub: [@MussieTeka](https://github.com/MussieTeka) +- Twitter: [@mussieteka](https://twitter.com/mussieteka) +- LinkedIn: [@mussieteka](https://linkedin.com/in/mussieteka) + +

(back to top)

+ +## 🔭 Future Features + +Possible future features for this project include: + +- Encoding text into Morse code +- Providing a command-line interface (CLI) for interactive decoding + +

(back to top)

+ +## 🤝 Contributing + +Contributions, issues, and feature requests are welcome! + +Feel free to check the [issues page](../../issues/). + +If you would like to contribute to this project, you can follow these steps: + +1. Fork the repository. +2. Create a new branch for your changes. +3. Make your changes and commit them. +4. Push your changes to your forked repository. +5. Create a pull request back to the original repository. + +

(back to top)

+ +## ⭐️ Show your support + +If you like this project, please give it a star on GitHub. + +

(back to top)

+ +## 🙏 Acknowledgments + +- [Ruby](https://www.ruby-lang.org/en/) +- [GitHub](https://github.com/) +- [Microverse](https://www.microverse.org/) + +

(back to top)

+ +## ❓ FAQ (OPTIONAL) + +- **Can I modify and redistribute this project?** + + - Yes, you can modify and redistribute this project as long as you follow the terms of the MIT license. + +- **How can I contribute to this project?** + + - Contributions, issues, and feature requests are welcome! You can check the issues page to see if there are any current issues or feature requests that you can work on. If not, feel free to submit a new issue or pull request. Before contributing, please read the CONTRIBUTING.md file for guidelines on how to contribute to this project. + +

(back to top)

+ + + +## 📝 License + +This project is [MIT](./LICENSE) licensed. + +

(back to top)

diff --git a/morse_decoder.rb b/morse_decoder.rb index 6f70eec..912b876 100644 --- a/morse_decoder.rb +++ b/morse_decoder.rb @@ -1,50 +1,41 @@ -MORSE_CODE_DICT = { - '.-' => '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' -}.freeze - def decode_char(morse_char) - MORSE_CODE_DICT[morse_char.upcase] -end + morse_code = { + '.-' => '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' + } -puts decode_char('.--') + morse_code[morse_char] +end def decode_word(morse_word) + decoded_word = '' morse_chars = morse_word.split - morse_chars.map { |char| decode_char(char) }.join + + morse_chars.each do |morse_char| + decoded_word += decode_char(morse_char) + end + + decoded_word end -puts decode_char('A') +def decode(message) + decoded_message = '' + morse_words = message.split(' ') + + morse_words.each do |morse_word| + decoded_word = decode_word(morse_word) + decoded_message += "#{decoded_word} " unless decoded_word.empty? + end -def decode_message(morse_message) - morse_words = morse_message.split - morse_words.map { |word| decode_word(word) }.join + decoded_message.rstrip end -morse_message = '.- -... --- -..- ..-. ..- .-.. .-.. --- ..-. .-. ..- -... .. . ...' -decoded_message = decode_message(morse_message) +# Decode the message from the old bottle +message = '.- -... --- -..- ..-. ..- .-.. .-.. --- ..-. .-. ..- -... .. . ...' +decoded_message = decode(message) puts decoded_message