diff options
| -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 | ||||
| -rw-r--r-- | challenge-046/paulo-custodio/perl/ch-1.pl | 8 | ||||
| -rw-r--r-- | challenge-046/paulo-custodio/python/ch-1.py | 56 | ||||
| -rw-r--r-- | challenge-046/paulo-custodio/python/ch-2.py | 25 | ||||
| -rw-r--r-- | challenge-046/paulo-custodio/t/test-1.yaml | 18 |
7 files changed, 167 insertions, 9 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='') diff --git a/challenge-046/paulo-custodio/perl/ch-1.pl b/challenge-046/paulo-custodio/perl/ch-1.pl index e5461d09b6..33d8ece866 100644 --- a/challenge-046/paulo-custodio/perl/ch-1.pl +++ b/challenge-046/paulo-custodio/perl/ch-1.pl @@ -28,13 +28,7 @@ use Modern::Perl; -my @input = - ("P+2l!ato", - "1e80R\$4u", - "5-r]+a>/", - "Pxwlb3k\\", - "2e35R8yu", - "<!r^()k0"); +chomp(my @input = <>); say decode(@input); diff --git a/challenge-046/paulo-custodio/python/ch-1.py b/challenge-046/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..bf49bd63c2 --- /dev/null +++ b/challenge-046/paulo-custodio/python/ch-1.py @@ -0,0 +1,56 @@ +#!/usr/bin/env python3 + +# Challenge 046 +# +# TASK #1 +# Cryptic Message +# The communication system of an office is broken and message received are not +# completely reliable. To send message Hello, it ended up sending these following: +# +# H x l 4 ! +# c e - l o +# z e 6 l g +# H W l v R +# q 9 m # o +# Similary another day we received a message repeatedly like below: +# +# P + 2 l ! a t o +# 1 e 8 0 R $ 4 u +# 5 - r ] + a > / +# P x w l b 3 k \ +# 2 e 3 5 R 8 y u +# < ! r ^ ( ) k 0 +# Write a script to decrypt the above repeated message (one message repeated 6 +# times). +# +# HINT: Look for characters repeated in a particular position in all six messages +# received. + +import sys + +def read_input(): + input = [] + for line in sys.stdin: + input.append(line.rstrip()) + return input + +def decode(input): + decoded = "" + for col in range(len(input[0])): + hist = {} + max_letter, max_count = '', 0 + for row in range(len(input)): + letter = input[row][col] + if letter not in hist: + hist[letter] = 1 + else: + hist[letter] += 1 + if hist[letter] > max_count: + max_letter, max_count = letter, hist[letter] + decoded += max_letter + return decoded + + +input = read_input() +decoded = decode(input) +print(decoded) diff --git a/challenge-046/paulo-custodio/python/ch-2.py b/challenge-046/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..8f4165d4c4 --- /dev/null +++ b/challenge-046/paulo-custodio/python/ch-2.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python3 + +# Challenge 046 +# +# TASK #2 +# Is the room open? +# There are 500 rooms in a hotel with 500 employees having keys to all the rooms. +# The first employee opened main entrance door of all the rooms. The second +# employee then closed the doors of room numbers 2,4,6,8,10 and so on to 500. The +# third employee then closed the door if it was opened or opened the door if it +# was closed of rooms 3,6,9,12,15 and so on to 500. Similarly the fourth employee +# did the same as the third but only room numbers 4,8,12,16 and so on to 500. +# This goes on until all employees has had a turn. +# +# Write a script to find out all the rooms still open at the end. + +locked = [True for x in range(501)] +for emp in range(1, 501): + for room in range(emp, 501, emp): + locked[room] = not locked[room] +rooms_open = [] +for room in range(1, 501): + if not locked[room]: + rooms_open.append(room) +print(", ".join([str(x) for x in rooms_open])) diff --git a/challenge-046/paulo-custodio/t/test-1.yaml b/challenge-046/paulo-custodio/t/test-1.yaml index d4da5ac307..2607509e10 100644 --- a/challenge-046/paulo-custodio/t/test-1.yaml +++ b/challenge-046/paulo-custodio/t/test-1.yaml @@ -1,5 +1,21 @@ - setup: cleanup: args: - input: + input: | + |Hxl4! + |ce-lo + |ze6lg + |HWlvR + |q9m#o + output: Hello +- setup: + cleanup: + args: + input: | + |P+2l!ato + |1e80R$4u + |5-r]+a>/ + |Pxwlb3k\ + |2e35R8yu + |<!r^()k0 output: PerlRaku |
