diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2024-08-31 11:15:00 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2024-08-31 11:15:00 +0100 |
| commit | 5fd0e13ee01e48f3d8b10f02f42b58bcdcca157e (patch) | |
| tree | 9c1fd6c1b73da884fdbc7d0fbce8327e60c2e02d /challenge-045 | |
| parent | e5b5d21abbae5d4dd147ec2bf9d9cec96457c744 (diff) | |
| download | perlweeklychallenge-club-5fd0e13ee01e48f3d8b10f02f42b58bcdcca157e.tar.gz perlweeklychallenge-club-5fd0e13ee01e48f3d8b10f02f42b58bcdcca157e.tar.bz2 perlweeklychallenge-club-5fd0e13ee01e48f3d8b10f02f42b58bcdcca157e.zip | |
Add Python solution to challenge 045
Diffstat (limited to 'challenge-045')
| -rw-r--r-- | challenge-045/paulo-custodio/Makefile | 3 | ||||
| -rw-r--r-- | challenge-045/paulo-custodio/python/ch-1.py | 51 | ||||
| -rw-r--r-- | challenge-045/paulo-custodio/python/ch-2.py | 15 |
3 files changed, 68 insertions, 1 deletions
diff --git a/challenge-045/paulo-custodio/Makefile b/challenge-045/paulo-custodio/Makefile index 6a9bfeacd6..612e70ec86 100644 --- a/challenge-045/paulo-custodio/Makefile +++ b/challenge-045/paulo-custodio/Makefile @@ -1,3 +1,4 @@ all: perl ../../challenge-001/paulo-custodio/test.pl - perl ./perl/ch-2.pl | diff - ./perl/ch-2.pl
\ No newline at end of file + perl ./perl/ch-2.pl | diff - ./perl/ch-2.pl + python3 ./python/ch-2.py | diff - ./python/ch-2.py 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:])))) diff --git a/challenge-045/paulo-custodio/python/ch-2.py b/challenge-045/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..28fd2e75d4 --- /dev/null +++ b/challenge-045/paulo-custodio/python/ch-2.py @@ -0,0 +1,15 @@ +#!/usr/bin/env python3 + +# Challenge 045 +# +# TASK #2 +# Source Dumper +# Write a script that dumps its own source code. For example, say, the script +# name is ch-2.pl then the following command should returns nothing. +# +# $ perl ch-2.pl | diff - ch-2.pl + +import sys + +file = open(sys.argv[0]) +print(file.read(), end='') |
