aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2021-12-27 16:41:55 +0000
committerPaulo Custodio <pauloscustodio@gmail.com>2021-12-27 16:41:55 +0000
commit7191fb81bf1851e1a3804bf7975b6902432e889e (patch)
tree7ad44d58fb4c309a7dc5d6afb64fe2fcea7dd235
parent334029e5f043365b08eefda94c620376d58ae611 (diff)
downloadperlweeklychallenge-club-7191fb81bf1851e1a3804bf7975b6902432e889e.tar.gz
perlweeklychallenge-club-7191fb81bf1851e1a3804bf7975b6902432e889e.tar.bz2
perlweeklychallenge-club-7191fb81bf1851e1a3804bf7975b6902432e889e.zip
Add Python solution to challenge 035
-rw-r--r--challenge-035/paulo-custodio/python/ch-1.py47
-rw-r--r--challenge-035/paulo-custodio/python/ch-2.py47
2 files changed, 94 insertions, 0 deletions
diff --git a/challenge-035/paulo-custodio/python/ch-1.py b/challenge-035/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..f0ce1e091d
--- /dev/null
+++ b/challenge-035/paulo-custodio/python/ch-1.py
@@ -0,0 +1,47 @@
+#!/usr/bin/python3
+
+# Challenge 035
+#
+# TASK #1
+# Contributed by Paul Johnson
+# Write a program to encode text into binary encoded morse code.
+# Pay attention to any changes which might need to be made to the text to
+# make it valid morse code.
+#
+# Morse code consists of dots, dashes and gaps. It can be encoded in binary
+# in the following fashion:
+#
+# dot: 1
+# dash: 111
+# intra-character gap: 0
+# character gap: 000
+# word gap: 0000000
+# An intra-character gap is inserted between the dots and dashes in a character.
+
+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':'-----'}
+
+def encode_bin_morse(text):
+ encoded = ""
+ for c in text.upper():
+ if c.isalnum():
+ morse = MORSE[c]
+ morse = re.sub(r"\.", "10", morse)
+ morse = re.sub(r"\-", "1110", morse)
+ encoded += morse+"00"
+ else:
+ encoded += "0000"
+ encoded = re.sub(r"0+$", "", encoded)
+ return encoded
+
+print(encode_bin_morse(" ".join(sys.argv[1:])))
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:])))