diff options
| author | Conor Hoekstra <codereport@outlook.com> | 2021-12-28 17:13:22 -0500 |
|---|---|---|
| committer | Conor Hoekstra <codereport@outlook.com> | 2021-12-28 17:13:22 -0500 |
| commit | 7d13afd3d3b2ba151d99a1d694d0906711ce9ead (patch) | |
| tree | b2faf3227040bd3967011050d611e4f720540123 /challenge-035/paulo-custodio/python/ch-2.py | |
| parent | 6e08a3189797e123107f7e6944f43ff5d9cab9bd (diff) | |
| parent | c524a3754a9f0a685a84c2dfdeea58b36e85f512 (diff) | |
| download | perlweeklychallenge-club-7d13afd3d3b2ba151d99a1d694d0906711ce9ead.tar.gz perlweeklychallenge-club-7d13afd3d3b2ba151d99a1d694d0906711ce9ead.tar.bz2 perlweeklychallenge-club-7d13afd3d3b2ba151d99a1d694d0906711ce9ead.zip | |
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
Diffstat (limited to 'challenge-035/paulo-custodio/python/ch-2.py')
| -rw-r--r-- | challenge-035/paulo-custodio/python/ch-2.py | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/challenge-035/paulo-custodio/python/ch-2.py b/challenge-035/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..f38b35a5bb --- /dev/null +++ b/challenge-035/paulo-custodio/python/ch-2.py @@ -0,0 +1,47 @@ +#!/usr/bin/python3 + +# Challenge 035 +# +# Challenge 035 +# +# TASK #2 +# Contributed by Paul Johnson +# Write a program to decode binary morse code. +# Consider how it might be possible to recover from badly formed morse code. +# +# a) by splitting the morse code on gaps +# b) without looking further than one digit ahead + +import sys +import re + +# morse code chart +MORSE = { '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':'--..', '1':'.----', '2':'..---', '3':'...--', '4':'....-', + '5':'.....', '6':'-....', '7':'--...', '8':'---..', '9':'----.', + '0':'-----'} + +MORSE_INV = {v:k for k, v in MORSE.items()} + +def decode_bin_morse(encoded): + encoded = re.sub(r"0+$", "", encoded) # remove trailing 0s + encoded = re.sub(r"0000000", "\n", encoded) # word gap + encoded = re.sub(r"000", " ", encoded) # character gap + encoded = re.sub(r"1110?", "-", encoded) # - + encoded = re.sub(r"10?", ".", encoded) # . + + text = "" + words = encoded.split("\n") + for word in words: + chars = word.split() + for c in chars: + text += MORSE_INV[c] + text += " " + text = re.sub(r"\s+$", "", text) + return text + +print(decode_bin_morse(" ".join(sys.argv[1:]))) |
