aboutsummaryrefslogtreecommitdiff
path: root/challenge-045/paulo-custodio/python/ch-1.py
diff options
context:
space:
mode:
author冯昶 <fengchang@novel-supertv.com>2024-09-02 15:40:05 +0800
committer冯昶 <fengchang@novel-supertv.com>2024-09-02 15:40:05 +0800
commit0c04a0825fd1dc67c5295fffdfbf3cb90ba075b8 (patch)
treed963a7fe57782d1d25580ea05c96f969768e8067 /challenge-045/paulo-custodio/python/ch-1.py
parent30a42138e1d8bdd2f174e06fb18515ced0b2b241 (diff)
parent0512711fccf91c731495f1dd901349cc900ec299 (diff)
downloadperlweeklychallenge-club-0c04a0825fd1dc67c5295fffdfbf3cb90ba075b8.tar.gz
perlweeklychallenge-club-0c04a0825fd1dc67c5295fffdfbf3cb90ba075b8.tar.bz2
perlweeklychallenge-club-0c04a0825fd1dc67c5295fffdfbf3cb90ba075b8.zip
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-045/paulo-custodio/python/ch-1.py')
-rw-r--r--challenge-045/paulo-custodio/python/ch-1.py51
1 files changed, 51 insertions, 0 deletions
diff --git a/challenge-045/paulo-custodio/python/ch-1.py b/challenge-045/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..a6417710be
--- /dev/null
+++ b/challenge-045/paulo-custodio/python/ch-1.py
@@ -0,0 +1,51 @@
+#!/usr/bin/env python3
+
+# Challenge 045
+#
+# TASK #1
+# Square Secret Code
+#
+# The square secret code mechanism first removes any space from the original
+# message. Then it lays down the message in a row of 8 columns. The coded
+# message is then obtained by reading down the columns going left to right.
+#
+# For example, the message is "The quick brown fox jumps over the lazy dog".
+#
+# Then the message would be laid out as below:
+#
+# thequick
+# brownfox
+# jumpsove
+# rthelazy
+# dog
+#
+# The code message would be as below:
+#
+# tbjrd hruto eomhg qwpe unsl ifoa covz kxey
+#
+# Write a script that accepts a message from command line and prints the
+# equivalent coded message.
+
+import re
+import sys
+
+def encode(text):
+ text, dummy = re.subn(r'\W+', '', text)
+ box = []
+ while text != '':
+ box.append(text[0:8])
+ text = text[8:]
+ encoded = []
+ while True:
+ box = [x for x in filter(lambda x: x != '', box)]
+ if len(box) == 0:
+ break;
+ word = ""
+ for i in range(len(box)):
+ if len(box[i]) > 0:
+ word += box[i][0]
+ box[i] = box[i][1:]
+ encoded.append(word)
+ return encoded
+
+print(' '.join(encode(''.join(sys.argv[1:]))))