From 10119b44b49c2a61fcbc6f4b495ff68db5873cba Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Fri, 10 Dec 2021 18:17:09 +0000 Subject: Add Python solution to challenge 25 --- challenge-025/paulo-custodio/Makefile | 2 + challenge-025/paulo-custodio/perl/ch-1.pl | 4 +- challenge-025/paulo-custodio/python/ch-1.py | 44 +++++++++++++++++++++ challenge-025/paulo-custodio/python/ch-2.py | 60 +++++++++++++++++++++++++++++ challenge-025/paulo-custodio/t/test-1.yaml | 5 +++ challenge-025/paulo-custodio/t/test-2.yaml | 10 +++++ challenge-025/paulo-custodio/test.pl | 22 ----------- 7 files changed, 123 insertions(+), 24 deletions(-) create mode 100644 challenge-025/paulo-custodio/Makefile create mode 100644 challenge-025/paulo-custodio/python/ch-1.py create mode 100644 challenge-025/paulo-custodio/python/ch-2.py create mode 100644 challenge-025/paulo-custodio/t/test-1.yaml create mode 100644 challenge-025/paulo-custodio/t/test-2.yaml delete mode 100644 challenge-025/paulo-custodio/test.pl diff --git a/challenge-025/paulo-custodio/Makefile b/challenge-025/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-025/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-025/paulo-custodio/perl/ch-1.pl b/challenge-025/paulo-custodio/perl/ch-1.pl index 22f59dca77..765a8c77f0 100644 --- a/challenge-025/paulo-custodio/perl/ch-1.pl +++ b/challenge-025/paulo-custodio/perl/ch-1.pl @@ -1,8 +1,8 @@ -#!/usr/bin/env perl +#!/usr/bin/perl # Challenge 025 # -# Generate a longest sequence of the following “English Pokemon” names where +# Generate a longest sequence of the following "English Pokemon" names where # each name starts with the last letter of previous name. use Modern::Perl; diff --git a/challenge-025/paulo-custodio/python/ch-1.py b/challenge-025/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..48e3403468 --- /dev/null +++ b/challenge-025/paulo-custodio/python/ch-1.py @@ -0,0 +1,44 @@ +#!/usr/bin/python3 + +# Challenge 025 +# +# Generate a longest sequence of the following "English Pokemon" names where +# each name starts with the last letter of previous name. + +names = """ + audino bagon baltoy banette bidoof braviary bronzor carracosta + charmeleon cresselia croagunk darmanitan deino emboar emolga + exeggcute gabite girafarig gulpin haxorus heatmor heatran + ivysaur jellicent jumpluff kangaskhan kricketune landorus + ledyba loudred lumineon lunatone machamp magnezone mamoswine + nosepass petilil pidgeotto pikachu pinsir poliwrath poochyena + porygon2 porygonz registeel relicanth remoraid rufflet sableye + scolipede scrafty seaking sealeo silcoon simisear snivy snorlax + spoink starly tirtouga trapinch treecko tyrogue vigoroth + vulpix wailord wartortle whismur wingull yamask +""".split() + +def find_longest_seq(words): + longest_path = [] + + def find_longest(path): + nonlocal longest_path, words + + # find words that can still be used + pending = [] + for word in words: + if word not in path: + if len(path)==0 or path[-1][-1]==word[0]: + pending.append(word) + if len(pending)==0: # end of search + if len(path) > len(longest_path): + longest_path = path + else: # find each possible path + for word in pending: + find_longest([*path, word]) + + find_longest([]) + return longest_path + +seq = find_longest_seq(names) +print(*seq) diff --git a/challenge-025/paulo-custodio/python/ch-2.py b/challenge-025/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..6a39ecf9fc --- /dev/null +++ b/challenge-025/paulo-custodio/python/ch-2.py @@ -0,0 +1,60 @@ +#!/usr/bin/python3 + +# Challenge 025 +# +# Task #2 +# Create script to implement Chaocipher. Please checkout wiki page for more +# information. + +import sys +import re + +left = "HXUCZVAMDSLKPEFJRIGTWOBNYQ" +right = "PTLNBQDEOYSFAVZKGJRIHWXUMC" + +def permute_alphabets(pos): + global left, right + + # permute left alphabet + left = left[pos:]+left[:pos] + left = left[0]+left[2:14]+left[1]+left[14:] + + # permute right alphabet + right = right[pos:]+right[:pos] + right = right[1:]+right[0] + right = right[:2]+right[3:14]+right[2]+right[14:] + +def find_pos(letter, alphabet): + found = re.search(letter, alphabet) + return found.start(0) + +def encode(text): + global left, right + + encoded = "" + for letter in text: + pos = find_pos(letter, right) + code = left[pos] + encoded += code + + permute_alphabets(pos) + return encoded + +def decode(encoded): + global left, right + + text = "" + for code in encoded: + pos = find_pos(code, left) + letter = right[pos] + text += letter + + permute_alphabets(pos) + return text + +if sys.argv[1]=="encode": + print(encode(sys.argv[2])) +elif sys.argv[1]=="decode": + print(decode(sys.argv[2])) +else: + print("Usage: ch-2.py encode|decode text") diff --git a/challenge-025/paulo-custodio/t/test-1.yaml b/challenge-025/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..26df1b4cd5 --- /dev/null +++ b/challenge-025/paulo-custodio/t/test-1.yaml @@ -0,0 +1,5 @@ +- setup: + cleanup: + args: + input: + output: machamp petilil landorus scrafty yamask kricketune emboar registeel loudred darmanitan nosepass simisear relicanth heatmor rufflet trapinch haxorus seaking girafarig gabite exeggcute emolga audino diff --git a/challenge-025/paulo-custodio/t/test-2.yaml b/challenge-025/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..603685e435 --- /dev/null +++ b/challenge-025/paulo-custodio/t/test-2.yaml @@ -0,0 +1,10 @@ +- setup: + cleanup: + args: encode WELLDONEISBETTERTHANWELLSAID + input: + output: OAHQHCNYNXTSZJRRHJBYHQKSOUJY +- setup: + cleanup: + args: decode OAHQHCNYNXTSZJRRHJBYHQKSOUJY + input: + output: WELLDONEISBETTERTHANWELLSAID diff --git a/challenge-025/paulo-custodio/test.pl b/challenge-025/paulo-custodio/test.pl deleted file mode 100644 index eb06b45b8e..0000000000 --- a/challenge-025/paulo-custodio/test.pl +++ /dev/null @@ -1,22 +0,0 @@ -#!/usr/bin/perl - -use strict; -use Modern::Perl; - -is capture("perl perl/ch-1.pl"), <