Skip to content

Commit 8dc7cdb

Browse files
cclaussgithub-actions
and
github-actions
authored
Add tests to morse_code.py (TheAlgorithms#5337)
* Add tests to morse_code.py @dhruvmanila @poyea Your reviews, please. * updating DIRECTORY.md Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent 433b804 commit 8dc7cdb

File tree

3 files changed

+64
-98
lines changed

3 files changed

+64
-98
lines changed

DIRECTORY.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
* [A1Z26](https://github.com/TheAlgorithms/Python/blob/master/ciphers/a1z26.py)
5454
* [Affine Cipher](https://github.com/TheAlgorithms/Python/blob/master/ciphers/affine_cipher.py)
5555
* [Atbash](https://github.com/TheAlgorithms/Python/blob/master/ciphers/atbash.py)
56+
* [Baconian Cipher](https://github.com/TheAlgorithms/Python/blob/master/ciphers/baconian_cipher.py)
5657
* [Base16](https://github.com/TheAlgorithms/Python/blob/master/ciphers/base16.py)
5758
* [Base32](https://github.com/TheAlgorithms/Python/blob/master/ciphers/base32.py)
5859
* [Base64 Encoding](https://github.com/TheAlgorithms/Python/blob/master/ciphers/base64_encoding.py)
@@ -70,7 +71,7 @@
7071
* [Hill Cipher](https://github.com/TheAlgorithms/Python/blob/master/ciphers/hill_cipher.py)
7172
* [Mixed Keyword Cypher](https://github.com/TheAlgorithms/Python/blob/master/ciphers/mixed_keyword_cypher.py)
7273
* [Mono Alphabetic Ciphers](https://github.com/TheAlgorithms/Python/blob/master/ciphers/mono_alphabetic_ciphers.py)
73-
* [Morse Code Implementation](https://github.com/TheAlgorithms/Python/blob/master/ciphers/morse_code_implementation.py)
74+
* [Morse Code](https://github.com/TheAlgorithms/Python/blob/master/ciphers/morse_code.py)
7475
* [Onepad Cipher](https://github.com/TheAlgorithms/Python/blob/master/ciphers/onepad_cipher.py)
7576
* [Playfair Cipher](https://github.com/TheAlgorithms/Python/blob/master/ciphers/playfair_cipher.py)
7677
* [Porta Cipher](https://github.com/TheAlgorithms/Python/blob/master/ciphers/porta_cipher.py)
@@ -328,10 +329,12 @@
328329
* [Graph Matrix](https://github.com/TheAlgorithms/Python/blob/master/graphs/graph_matrix.py)
329330
* [Graphs Floyd Warshall](https://github.com/TheAlgorithms/Python/blob/master/graphs/graphs_floyd_warshall.py)
330331
* [Greedy Best First](https://github.com/TheAlgorithms/Python/blob/master/graphs/greedy_best_first.py)
332+
* [Greedy Min Vertex Cover](https://github.com/TheAlgorithms/Python/blob/master/graphs/greedy_min_vertex_cover.py)
331333
* [Kahns Algorithm Long](https://github.com/TheAlgorithms/Python/blob/master/graphs/kahns_algorithm_long.py)
332334
* [Kahns Algorithm Topo](https://github.com/TheAlgorithms/Python/blob/master/graphs/kahns_algorithm_topo.py)
333335
* [Karger](https://github.com/TheAlgorithms/Python/blob/master/graphs/karger.py)
334336
* [Markov Chain](https://github.com/TheAlgorithms/Python/blob/master/graphs/markov_chain.py)
337+
* [Matching Min Vertex Cover](https://github.com/TheAlgorithms/Python/blob/master/graphs/matching_min_vertex_cover.py)
335338
* [Minimum Spanning Tree Boruvka](https://github.com/TheAlgorithms/Python/blob/master/graphs/minimum_spanning_tree_boruvka.py)
336339
* [Minimum Spanning Tree Kruskal](https://github.com/TheAlgorithms/Python/blob/master/graphs/minimum_spanning_tree_kruskal.py)
337340
* [Minimum Spanning Tree Kruskal2](https://github.com/TheAlgorithms/Python/blob/master/graphs/minimum_spanning_tree_kruskal2.py)
@@ -497,6 +500,7 @@
497500
* [Prime Numbers](https://github.com/TheAlgorithms/Python/blob/master/maths/prime_numbers.py)
498501
* [Prime Sieve Eratosthenes](https://github.com/TheAlgorithms/Python/blob/master/maths/prime_sieve_eratosthenes.py)
499502
* [Primelib](https://github.com/TheAlgorithms/Python/blob/master/maths/primelib.py)
503+
* [Proth Number](https://github.com/TheAlgorithms/Python/blob/master/maths/proth_number.py)
500504
* [Pythagoras](https://github.com/TheAlgorithms/Python/blob/master/maths/pythagoras.py)
501505
* [Qr Decomposition](https://github.com/TheAlgorithms/Python/blob/master/maths/qr_decomposition.py)
502506
* [Quadratic Equations Complex Numbers](https://github.com/TheAlgorithms/Python/blob/master/maths/quadratic_equations_complex_numbers.py)
@@ -951,6 +955,7 @@
951955
* [Current Stock Price](https://github.com/TheAlgorithms/Python/blob/master/web_programming/current_stock_price.py)
952956
* [Current Weather](https://github.com/TheAlgorithms/Python/blob/master/web_programming/current_weather.py)
953957
* [Daily Horoscope](https://github.com/TheAlgorithms/Python/blob/master/web_programming/daily_horoscope.py)
958+
* [Download Images From Google Query](https://github.com/TheAlgorithms/Python/blob/master/web_programming/download_images_from_google_query.py)
954959
* [Emails From Url](https://github.com/TheAlgorithms/Python/blob/master/web_programming/emails_from_url.py)
955960
* [Fetch Bbc News](https://github.com/TheAlgorithms/Python/blob/master/web_programming/fetch_bbc_news.py)
956961
* [Fetch Github Info](https://github.com/TheAlgorithms/Python/blob/master/web_programming/fetch_github_info.py)

ciphers/morse_code.py

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/usr/bin/env python3
2+
3+
"""
4+
Python program to translate to and from Morse code.
5+
6+
https://en.wikipedia.org/wiki/Morse_code
7+
"""
8+
9+
# fmt: off
10+
MORSE_CODE_DICT = {
11+
"A": ".-", "B": "-...", "C": "-.-.", "D": "-..", "E": ".", "F": "..-.", "G": "--.",
12+
"H": "....", "I": "..", "J": ".---", "K": "-.-", "L": ".-..", "M": "--", "N": "-.",
13+
"O": "---", "P": ".--.", "Q": "--.-", "R": ".-.", "S": "...", "T": "-", "U": "..-",
14+
"V": "...-", "W": ".--", "X": "-..-", "Y": "-.--", "Z": "--..", "1": ".----",
15+
"2": "..---", "3": "...--", "4": "....-", "5": ".....", "6": "-....", "7": "--...",
16+
"8": "---..", "9": "----.", "0": "-----", "&": ".-...", "@": ".--.-.",
17+
":": "---...", ",": "--..--", ".": ".-.-.-", "'": ".----.", '"': ".-..-.",
18+
"?": "..--..", "/": "-..-.", "=": "-...-", "+": ".-.-.", "-": "-....-",
19+
"(": "-.--.", ")": "-.--.-", "!": "-.-.--", " ": "/"
20+
} # Exclamation mark is not in ITU-R recommendation
21+
# fmt: on
22+
REVERSE_DICT = {value: key for key, value in MORSE_CODE_DICT.items()}
23+
24+
25+
def encrypt(message: str) -> str:
26+
"""
27+
>>> encrypt("Sos!")
28+
'... --- ... -.-.--'
29+
>>> encrypt("SOS!") == encrypt("sos!")
30+
True
31+
"""
32+
return " ".join(MORSE_CODE_DICT[char] for char in message.upper())
33+
34+
35+
def decrypt(message: str) -> str:
36+
"""
37+
>>> decrypt('... --- ... -.-.--')
38+
'SOS!'
39+
"""
40+
return "".join(REVERSE_DICT[char] for char in message.split())
41+
42+
43+
def main() -> None:
44+
"""
45+
>>> s = "".join(MORSE_CODE_DICT)
46+
>>> decrypt(encrypt(s)) == s
47+
True
48+
"""
49+
message = "Morse code here!"
50+
print(message)
51+
message = encrypt(message)
52+
print(message)
53+
message = decrypt(message)
54+
print(message)
55+
56+
57+
if __name__ == "__main__":
58+
main()

ciphers/morse_code_implementation.py

-97
This file was deleted.

0 commit comments

Comments
 (0)