diff options
23 files changed, 574 insertions, 140 deletions
diff --git a/challenge-012/paulo-custodio/Makefile b/challenge-012/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-012/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-012/paulo-custodio/python/ch-1.py b/challenge-012/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..91509d2ce1 --- /dev/null +++ b/challenge-012/paulo-custodio/python/ch-1.py @@ -0,0 +1,36 @@ +#!/usr/bin/python3 + +# Challenge 012 +# +# Challenge #1 +# The numbers formed by adding one to the products of the smallest primes are +# called the Euclid Numbers (see wiki). Write a script that finds the smallest +# Euclid Number that is not prime. This challenge was proposed by +# Laurent Rosenfeld. + +import sys +from primePy import primes + +def next_prime(n): + if n <= 1: + return 2 + else: + n += 1 + while not primes.check(n): + n += 1 + return n + +def euclid_iter(): + prime = 1 + prime_prod = 1 + while True: + prime = next_prime(prime) + prime_prod *= prime + yield prime_prod+1 + +for n in euclid_iter(): + if primes.check(n): + pass + else: + print(n) + break diff --git a/challenge-012/paulo-custodio/python/ch-2.py b/challenge-012/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..2fb304f188 --- /dev/null +++ b/challenge-012/paulo-custodio/python/ch-2.py @@ -0,0 +1,53 @@ +#!/usr/bin/python3 + +# Challenge 012 +# +# Challenge #2 +# Write a script that finds the common directory path, given a collection of +# paths and directory separator. For example, if the following paths are +# supplied. +# /a/b/c/d +# /a/b/cd +# /a/b/cc +# /a/b/c/d/e +# and the path separator is /. Your script should return /a/b as common +# directory path. + +import sys + +def extract_common_prefix(paths): + # check if all paths have common prefix + dir = None + for path in paths: + if len(path)==0: + return False, "" + elif dir is None: + dir = path[0] + else: + if path[0]!=dir: + return False, "" + + # remove common prefix + for path in paths: + path.pop(0) + + return True, dir + +def common_prefix(sep, paths): + # split paths by separator + for i in range(len(paths)): + paths[i] = paths[i].split(sep) + + # find common prefix + prefix = [] + while True: + found, dir = extract_common_prefix(paths) + if not found: + break + prefix.append(dir) + + return sep.join(prefix) + +sep = sys.argv[1] +paths = sys.argv[2:] +print(common_prefix(sep, paths)) diff --git a/challenge-012/paulo-custodio/test.pl b/challenge-012/paulo-custodio/test.pl deleted file mode 100644 index ba6c37260b..0000000000 --- a/challenge-012/paulo-custodio/test.pl +++ /dev/null @@ -1,4 +0,0 @@ -#!/usr/bin/env perl -use Modern::Perl; -use Test::More; -require '../../challenge-001/paulo-custodio/test.pl'; diff --git a/challenge-013/paulo-custodio/Makefile b/challenge-013/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-013/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-013/paulo-custodio/python/ch-1.py b/challenge-013/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..1a9f4ee1d8 --- /dev/null +++ b/challenge-013/paulo-custodio/python/ch-1.py @@ -0,0 +1,42 @@ +#!/usr/bin/python3 + +# Challenge 013 +# +# Challenge #1 +# Write a script to print the date of last Friday of every month of a given year. +# For example, if the given year is 2019 then it should print the following: +# +# 2019/01/25 +# 2019/02/22 +# 2019/03/29 +# 2019/04/26 +# 2019/05/31 +# 2019/06/28 +# 2019/07/26 +# 2019/08/30 +# 2019/09/27 +# 2019/10/25 +# 2019/11/29 +# 2019/12/27 + +import sys +import datetime + +def last_day_of_month(year, month): + dt = datetime.date(year, month, 28) + while dt.month==month: + dt += datetime.timedelta(days=1) + dt -= datetime.timedelta(days=1) + return dt + +def last_friday(dt): + while dt.isoweekday()!=5: + dt -= datetime.timedelta(days=1) + return dt + +def print_last_fridays(year): + for month in range(1, 13): + dt = last_friday(last_day_of_month(year, month)) + print(dt.strftime("%Y/%m/%d")) + +print_last_fridays(int(sys.argv[1])) diff --git a/challenge-013/paulo-custodio/python/ch-2.py b/challenge-013/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..1bea5c0d1c --- /dev/null +++ b/challenge-013/paulo-custodio/python/ch-2.py @@ -0,0 +1,31 @@ +#!/usr/bin/python3 + +# Challenge 013 +# +# Challenge #2 +# Write a script to demonstrate Mutually Recursive methods. Two methods are +# mutually recursive if the first method calls the second and the second calls +# first in turn. Using the mutually recursive methods, generate Hofstadter +# Female and Male sequences. +# +# F ( 0 ) = 1 ; M ( 0 ) = 0 +# F ( n ) = n - M ( F ( n - 1 ) ) , n > 0 +# M ( n ) = n - F ( M ( n - 1 ) ) , n > 0. + +import sys + +def F(n): + if n==0: + return 1 + else: + return n-M(F(n-1)) + +def M(n): + if n==0: + return 0 + else: + return n-F(M(n-1)) + +N = int(sys.argv[1]) +print("F: "+", ".join([str(F(x)) for x in range(N)])+", ...") +print("M: "+", ".join([str(M(x)) for x in range(N)])+", ...") diff --git a/challenge-013/paulo-custodio/t/test-1.yaml b/challenge-013/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..119e80d038 --- /dev/null +++ b/challenge-013/paulo-custodio/t/test-1.yaml @@ -0,0 +1,34 @@ +- setup: + cleanup: + args: 2019 + input: + output: | + 2019/01/25 + 2019/02/22 + 2019/03/29 + 2019/04/26 + 2019/05/31 + 2019/06/28 + 2019/07/26 + 2019/08/30 + 2019/09/27 + 2019/10/25 + 2019/11/29 + 2019/12/27 +- setup: + cleanup: + args: 2021 + input: + output: | + 2021/01/29 + 2021/02/26 + 2021/03/26 + 2021/04/30 + 2021/05/28 + 2021/06/25 + 2021/07/30 + 2021/08/27 + 2021/09/24 + 2021/10/29 + 2021/11/26 + 2021/12/31 diff --git a/challenge-013/paulo-custodio/t/test-2.yaml b/challenge-013/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..743cfc0c4a --- /dev/null +++ b/challenge-013/paulo-custodio/t/test-2.yaml @@ -0,0 +1,14 @@ +- setup: + cleanup: + args: 3 + input: + output: | + F: 1, 1, 2, ... + M: 0, 0, 1, ... +- setup: + cleanup: + args: 21 + input: + output: | + F: 1, 1, 2, 2, 3, 3, 4, 5, 5, 6, 6, 7, 8, 8, 9, 9, 10, 11, 11, 12, 13, ... + M: 0, 0, 1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 7, 8, 9, 9, 10, 11, 11, 12, 12, ... diff --git a/challenge-013/paulo-custodio/test.pl b/challenge-013/paulo-custodio/test.pl deleted file mode 100644 index 91c463d690..0000000000 --- a/challenge-013/paulo-custodio/test.pl +++ /dev/null @@ -1,55 +0,0 @@ -#!/usr/bin/perl - -use Modern::Perl; -use Test::More; - -is capture("perl perl/ch-1.pl 2019"), <<END; -2019/01/25 -2019/02/22 -2019/03/29 -2019/04/26 -2019/05/31 -2019/06/28 -2019/07/26 -2019/08/30 -2019/09/27 -2019/10/25 -2019/11/29 -2019/12/27 -END - -is capture("perl perl/ch-1.pl 2021"), <<END; -2021/01/29 -2021/02/26 -2021/03/26 -2021/04/30 -2021/05/28 -2021/06/25 -2021/07/30 -2021/08/27 -2021/09/24 -2021/10/29 -2021/11/26 -2021/12/31 -END - - -is capture("perl perl/ch-2.pl 3"), <<END; -F: 1, 1, 2, ... -M: 0, 0, 1, ... -END - -is capture("perl perl/ch-2.pl 21"), <<END; -F: 1, 1, 2, 2, 3, 3, 4, 5, 5, 6, 6, 7, 8, 8, 9, 9, 10, 11, 11, 12, 13, ... -M: 0, 0, 1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 7, 8, 9, 9, 10, 11, 11, 12, 12, ... -END - -done_testing; - - -sub capture { - my($cmd) = @_; - my $out = `$cmd`; - $out =~ s/[ \r\t]*\n/\n/g; - return $out; -} diff --git a/challenge-014/paulo-custodio/Makefile b/challenge-014/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-014/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-014/paulo-custodio/perl/ch-2.pl b/challenge-014/paulo-custodio/perl/ch-2.pl index b02097809c..6549b608a1 100644 --- a/challenge-014/paulo-custodio/perl/ch-2.pl +++ b/challenge-014/paulo-custodio/perl/ch-2.pl @@ -15,10 +15,61 @@ # California + Louisiana + Massachusetts + Rhode Island = Calamari use Modern::Perl; -use Locale::US; -my $u = Locale::US->new; -my @codes = $u->all_state_codes; # all states two letter codes +my %us_states = ( + AL => 'Alabama', + AK => 'Alaska', + AZ => 'Arizona', + AR => 'Arkansas', + CA => 'California', + CO => 'Colorado', + CT => 'Connecticut', + DE => 'Delaware', + FL => 'Florida', + GA => 'Georgia', + HI => 'Hawaii', + ID => 'Idaho', + IL => 'Illinois', + IN => 'Indiana', + IA => 'Iowa', + KS => 'Kansas', + KY => 'Kentucky', + LA => 'Louisiana', + ME => 'Maine', + MD => 'Maryland', + MA => 'Massachusetts', + MI => 'Michigan', + MN => 'Minnesota', + MS => 'Mississippi', + MO => 'Missouri', + MT => 'Montana', + NE => 'Nebraska', + NV => 'Nevada', + NH => 'New Hampshire', + NJ => 'New Jersey', + NM => 'New Mexico', + NY => 'New York', + NC => 'North Carolina', + ND => 'North Dakota', + OH => 'Ohio', + OK => 'Oklahoma', + OR => 'Oregon', + PA => 'Pennsylvania', + RI => 'Rhode Island', + SC => 'South Carolina', + SD => 'South Dakota', + TN => 'Tennessee', + TX => 'Texas', + UT => 'Utah', + VT => 'Vermont', + VA => 'Virginia', + WA => 'Washington', + WV => 'West Virginia', + WI => 'Wisconsin', + WY => 'Wyoming', +); + +my @codes = keys %us_states; # all states two letter codes my $codes = join("|", @codes); # regex to match any codes my $regex = qr/^($codes)+$/i; # regex to match word composed of codes @@ -38,6 +89,6 @@ while (<$fh>) { # show longest words in form: word = state + state + ... for my $word (@longest) { - my @states = map {$_ = $u->{code2state}{uc($_)}} grep {$_} split /(..)/, $word; + my @states = map {$_ = $us_states{uc($_)}} grep {$_} split /(..)/, $word; say $word, " = ", join(" + ", @states); } diff --git a/challenge-014/paulo-custodio/python/ch-1.py b/challenge-014/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..6852909991 --- /dev/null +++ b/challenge-014/paulo-custodio/python/ch-1.py @@ -0,0 +1,40 @@ +#!/usr/bin/python3 + +# Challenge 014 +# +# Challenge #1 +# Write a script to generate Van Eck's sequence starts with 0. For more +# information, please check out wikipedia page. This challenge was proposed by +# team member Andrezgz. + +def van_eck_iter(): + hist = [] + # first two terms + hist.append(0) + yield hist[-1] + + hist.append(0) + yield hist[-1] + + while True: + found = False + for m in range(len(hist)-2, -1, -1): + if hist[m]==hist[-1]: + hist.append(len(hist)-1-m) + yield hist[-1] + found = True + break + if not found: + hist.append(0) + yield hist[-1] + +sep = "" +output = "" +count = 0 +for n in van_eck_iter(): + output += sep + str(n) + sep = ", " + count += 1 + if count > 96: + break +print(output) diff --git a/challenge-014/paulo-custodio/python/ch-2.py b/challenge-014/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..fd1890993e --- /dev/null +++ b/challenge-014/paulo-custodio/python/ch-2.py @@ -0,0 +1,119 @@ +#!/usr/bin/python3 + +# Challenge 014 +# +# Challenge #2 +# Using only the official postal (2-letter) abbreviations for the 50 U.S. +# states, write a script to find the longest English word you can spell? Here +# is the list of U.S. states abbreviations as per wikipedia page. This challenge +# was proposed by team member Neil Bowers. +# +# For example, +# Pennsylvania + Connecticut = PACT +# Wisconsin + North Dakota = WIND +# Maine + Alabama = MEAL +# California + Louisiana + Massachusetts + Rhode Island = Calamari + +import re +import sys + +us_states = { + 'AL':'Alabama', + 'AK':'Alaska', + 'AZ':'Arizona', + 'AR':'Arkansas', + 'CA':'California', + 'CO':'Colorado', + 'CT':'Connecticut', + 'DE':'Delaware', + 'FL':'Florida', + 'GA':'Georgia', + 'HI':'Hawaii', + 'ID':'Idaho', + 'IL':'Illinois', + 'IN':'Indiana', + 'IA':'Iowa', + 'KS':'Kansas', + 'KY':'Kentucky', + 'LA':'Louisiana', + 'ME':'Maine', + 'MD':'Maryland', + 'MA':'Massachusetts', + 'MI':'Michigan', + 'MN':'Minnesota', + 'MS':'Mississippi', + 'MO':'Missouri', + 'MT':'Montana', + 'NE':'Nebraska', + 'NV':'Nevada', + 'NH':'New Hampshire', + 'NJ':'New Jersey', + 'NM':'New Mexico', + 'NY':'New York', + 'NC':'North Carolina', + 'ND':'North Dakota', + 'OH':'Ohio', + 'OK':'Oklahoma', + 'OR':'Oregon', + 'PA':'Pennsylvania', + 'RI':'Rhode Island', + 'SC':'South Carolina', + 'SD':'South Dakota', + 'TN':'Tennessee', + 'TX':'Texas', + 'UT':'Utah', + 'VT':'Vermont', + 'VA':'Virginia', + 'WA':'Washington', + 'WV':'West Virginia', + 'WI':'Wisconsin', + 'WY':'Wyoming', +} + +def read_file(filename): + with open(filename) as f: + return f.readlines() + +def read_words(lines): + words = [] + for line in lines: + word = line.strip() + if not re.search(r"\W", word): + words.append(word) + return words + +def word_regexp(): + state_abbr = [] + for state in us_states: + state_abbr.append(state) + return r"(?i:^("+"|".join(state_abbr)+r")+$)" + +def words_like_states(words): + matched = [] + regexp = word_regexp() + for word in words: + if re.match(regexp, word): + matched.append(word) + return matched + +def longest_words(words): + longest = [] + size = 0 + for word in words: + if len(word) > size: + longest = [word] + size = len(word) + elif len(word) == size: + longest.append(word) + return longest + +def word_to_states(word): + states = [] + for i in range(0, len(word), 2): + cc = word[i:i+2].upper() + states.append(us_states[cc]) + return " + ".join(states) + +words = longest_words(words_like_states(read_words(read_file("words.txt")))) +for word in words: + print(word+" = "+word_to_states(word)) diff --git a/challenge-014/paulo-custodio/t/test-1.yaml b/challenge-014/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..ebf698340d --- /dev/null +++ b/challenge-014/paulo-custodio/t/test-1.yaml @@ -0,0 +1,5 @@ +- setup: + cleanup: + args: 2019 + input: + output: 0, 0, 1, 0, 2, 0, 2, 2, 1, 6, 0, 5, 0, 2, 6, 5, 4, 0, 5, 3, 0, 3, 2, 9, 0, 4, 9, 3, 6, 14, 0, 6, 3, 5, 15, 0, 5, 3, 5, 2, 17, 0, 6, 11, 0, 3, 8, 0, 3, 3, 1, 42, 0, 5, 15, 20, 0, 4, 32, 0, 3, 11, 18, 0, 4, 7, 0, 3, 7, 3, 2, 31, 0, 6, 31, 3, 6, 3, 2, 8, 33, 0, 9, 56, 0, 3, 8, 7, 19, 0, 5, 37, 0, 3, 8, 8, 1 diff --git a/challenge-014/paulo-custodio/t/test-2.yaml b/challenge-014/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..c89ea9f41a --- /dev/null +++ b/challenge-014/paulo-custodio/t/test-2.yaml @@ -0,0 +1,20 @@ +- setup: 0==system("aspell -d en dump master | aspell -l en expand > words.txt") + cleanup: unlink("words.txt") + args: + input: + output: | + armorial = Arkansas + Missouri + Rhode Island + Alabama + calamine = California + Louisiana + Michigan + Nebraska + coalmine = Colorado + Alabama + Michigan + Nebraska + calamari = California + Louisiana + Massachusetts + Rhode Island + Concorde = Colorado + North Carolina + Oregon + Delaware + Ganymede = Georgia + New York + Maine + Delaware + landmine = Louisiana + North Dakota + Michigan + Nebraska + melamine = Maine + Louisiana + Michigan + Nebraska + moorland = Missouri + Oregon + Louisiana + North Dakota + malarial = Massachusetts + Louisiana + Rhode Island + Alabama + memorial = Maine + Missouri + Rhode Island + Alabama + mainland = Massachusetts + Indiana + Louisiana + North Dakota + Mandarin = Massachusetts + North Dakota + Arkansas + Indiana + mandarin = Massachusetts + North Dakota + Arkansas + Indiana + mescalin = Maine + South Carolina + Alabama + Indiana diff --git a/challenge-014/paulo-custodio/test.pl b/challenge-014/paulo-custodio/test.pl deleted file mode 100644 index 0a5cd72e18..0000000000 --- a/challenge-014/paulo-custodio/test.pl +++ /dev/null @@ -1,49 +0,0 @@ -#!/usr/bin/perl - -use Modern::Perl; -use Test::More; - -# build list of words for testing -ok 0==system("aspell -d en dump master | aspell -l en expand > words.txt"); - - -is capture("perl perl/ch-1.pl 2019"), <<END; -0, 0, 1, 0, 2, 0, 2, 2, 1, 6, 0, 5, 0, 2, 6, 5, 4, 0, 5, 3, 0, 3, 2, 9, 0, 4, 9, 3, 6, 14, 0, 6, 3, 5, 15, 0, 5, 3, 5, 2, 17, 0, 6, 11, 0, 3, 8, 0, 3, 3, 1, 42, 0, 5, 15, 20, 0, 4, 32, 0, 3, 11, 18, 0, 4, 7, 0, 3, 7, 3, 2, 31, 0, 6, 31, 3, 6, 3, 2, 8, 33, 0, 9, 56, 0, 3, 8, 7, 19, 0, 5, 37, 0, 3, 8, 8, 1 -END - -is capture("perl perl/ch-2.pl"), <<END; -armorial = ARKANSAS + MISSOURI + RHODE ISLAND + ALABAMA -inguinal = INDIANA + GUAM + INDIANA + ALABAMA -calamine = CALIFORNIA + LOUISIANA + MICHIGAN + NEBRASKA -coalmine = COLORADO + ALABAMA + MICHIGAN + NEBRASKA -calamari = CALIFORNIA + LOUISIANA + MASSACHUSETTS + RHODE ISLAND -complain = COLORADO + NORTHERN MARIANA ISLANDS + LOUISIANA + INDIANA -Campinas = CALIFORNIA + NORTHERN MARIANA ISLANDS + INDIANA + AMERICAN SAMOA -Concorde = COLORADO + NORTH CAROLINA + OREGON + DELAWARE -Ganymede = GEORGIA + NEW YORK + MAINE + DELAWARE -cascaras = CALIFORNIA + SOUTH CAROLINA + ARKANSAS + AMERICAN SAMOA -landmine = LOUISIANA + NORTH DAKOTA + MICHIGAN + NEBRASKA -melamine = MAINE + LOUISIANA + MICHIGAN + NEBRASKA -moorland = MISSOURI + OREGON + LOUISIANA + NORTH DAKOTA -malarial = MASSACHUSETTS + LOUISIANA + RHODE ISLAND + ALABAMA -memorial = MAINE + MISSOURI + RHODE ISLAND + ALABAMA -mainland = MASSACHUSETTS + INDIANA + LOUISIANA + NORTH DAKOTA -mandalas = MASSACHUSETTS + NORTH DAKOTA + ALABAMA + AMERICAN SAMOA -Mandarin = MASSACHUSETTS + NORTH DAKOTA + ARKANSAS + INDIANA -mandarin = MASSACHUSETTS + NORTH DAKOTA + ARKANSAS + INDIANA -mescalin = MAINE + SOUTH CAROLINA + ALABAMA + INDIANA -mascaras = MASSACHUSETTS + SOUTH CAROLINA + ARKANSAS + AMERICAN SAMOA -Victoria = VIRGIN ISLANDS + CONNECTICUT + OREGON + IOWA -Vineland = VIRGIN ISLANDS + NEBRASKA + LOUISIANA + NORTH DAKOTA -END - -unlink "words.txt"; -done_testing; - - -sub capture { - my($cmd) = @_; - my $out = `$cmd`; - $out =~ s/[ \r\t]*\n/\n/g; - return $out; -} diff --git a/challenge-015/paulo-custodio/Makefile b/challenge-015/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-015/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-015/paulo-custodio/python/ch-1.py b/challenge-015/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..d1e6cfc85c --- /dev/null +++ b/challenge-015/paulo-custodio/python/ch-1.py @@ -0,0 +1,55 @@ +#!/usr/bin/python3 + +# Challenge 015 +# +# Task #1 +# Write a script to generate first 10 strong and weak prime numbers. +# +# For example, the nth prime number is represented by p(n). +# +# p(1) = 2 +# p(2) = 3 +# p(3) = 5 +# p(4) = 7 +# p(5) = 11 +# +# Strong Prime number p(n) when p(n) > [ p(n-1) + p(n+1) ] / 2 +# Weak Prime number p(n) when p(n) < [ p(n-1) + p(n+1) ] / 2 + +import sys +from primePy import primes + +def next_prime(n): + if n <= 1: + return 2 + else: + n += 1 + while not primes.check(n): + n += 1 + return n + +def prime_iter(strong): + last = [1,2,3] # 1 + first two primes; 1 is discarded on first iteration + while True: + # get next prime, drop oldest + last.pop(0) + last.append(next_prime(last[-1])) + + avg = (last[-3]+last[-1])/2 + if strong: + if last[-2] > avg: + yield last[-2] + else: + if last[-2] < avg: + yield last[-2] + +def primes_list(num, strong): + result = [] + for p in prime_iter(strong): + result.append(p) + if len(result) >= num: + return result + +num = int(sys.argv[1]) +print("Strong Prime: "+", ".join([str(x) for x in primes_list(num, True)])) +print("Weak Prime: "+", ".join([str(x) for x in primes_list(num, False)])) diff --git a/challenge-015/paulo-custodio/python/ch-2.py b/challenge-015/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..3d2de230da --- /dev/null +++ b/challenge-015/paulo-custodio/python/ch-2.py @@ -0,0 +1,38 @@ +#!/usr/bin/python3 + +# Challenge 015 +# +# Task #2 +# Write a script to implement Vigenere cipher. The script should be able encode +# and decode. Checkout wiki page for more information. + +import sys + +def text_codes(text): + codes = [] + for c in text.upper(): + if c.isalpha(): + codes.append(ord(c)-ord('A')) + return codes + +def codes_text(codes): + return "".join([chr(ord('A')+x) for x in codes]) + +def vigenere(plain, key, encode): + plain = text_codes(plain) + key = text_codes(key) + ikey = 0 + cipher = [] + for c in plain: + if encode: + cipher.append((c + key[ikey])%26) + else: + cipher.append((c - key[ikey] + 26)%26) + ikey = (ikey+1) % len(key) + return codes_text(cipher) + +encode = True if sys.argv[1]=="encode" else False +key = sys.argv[2] +text = " ".join(sys.argv[3:]) + +print(vigenere(text, key, encode)) diff --git a/challenge-015/paulo-custodio/t/test-1.yaml b/challenge-015/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..2d9c887792 --- /dev/null +++ b/challenge-015/paulo-custodio/t/test-1.yaml @@ -0,0 +1,14 @@ +- setup: + cleanup: + args: 10 + input: + output: | + Strong Prime: 11, 17, 29, 37, 41, 59, 67, 71, 79, 97 + Weak Prime: 3, 7, 13, 19, 23, 31, 43, 47, 61, 73 +- setup: + cleanup: + args: 47 + input: + output: | + Strong Prime: 11, 17, 29, 37, 41, 59, 67, 71, 79, 97, 101, 107, 127, 137, 149, 163, 179, 191, 197, 223, 227, 239, 251, 269, 277, 281, 307, 311, 331, 347, 367, 379, 397, 419, 431, 439, 457, 461, 479, 487, 499, 521, 541, 557, 569, 587, 599 + Weak Prime: 3, 7, 13, 19, 23, 31, 43, 47, 61, 73, 83, 89, 103, 109, 113, 131, 139, 151, 167, 181, 193, 199, 229, 233, 241, 271, 283, 293, 313, 317, 337, 349, 353, 359, 383, 389, 401, 409, 421, 433, 443, 449, 463, 467, 491, 503, 509 diff --git a/challenge-015/paulo-custodio/t/test-2.yaml b/challenge-015/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..4e5149b8a9 --- /dev/null +++ b/challenge-015/paulo-custodio/t/test-2.yaml @@ -0,0 +1,10 @@ +- setup: + cleanup: + args: encode LEMON ATTACKATDAWN + input: + output: LXFOPVEFRNHR +- setup: + cleanup: + args: decode LEMON LXFOPVEFRNHR + input: + output: ATTACKATDAWN diff --git a/challenge-015/paulo-custodio/test.pl b/challenge-015/paulo-custodio/test.pl deleted file mode 100644 index 6959f9ba6e..0000000000 --- a/challenge-015/paulo-custodio/test.pl +++ /dev/null @@ -1,28 +0,0 @@ -#!/usr/bin/perl - -use Modern::Perl; -use Test::More; - -is capture("perl perl/ch-1.pl"), <<END; -Strong Prime: 11, 17, 29, 37, 41, 59, 67, 71, 79, 97 -Weak Prime: 3, 7, 13, 19, 23, 31, 43, 47, 61, 73 -END - -is capture("perl perl/ch-1.pl 47"), <<END; -Strong Prime: 11, 17, 29, 37, 41, 59, 67, 71, 79, 97, 101, 107, 127, 137, 149, 163, 179, 191, 197, 223, 227, 239, 251, 269, 277, 281, 307, 311, 331, 347, 367, 379, 397, 419, 431, 439, 457, 461, 479, 487, 499, 521, 541, 557, 569, 587, 599 -Weak Prime: 3, 7, 13, 19, 23, 31, 43, 47, 61, 73, 83, 89, 103, 109, 113, 131, 139, 151, 167, 181, 193, 199, 229, 233, 241, 271, 283, 293, 313, 317, 337, 349, 353, 359, 383, 389, 401, 409, 421, 433, 443, 449, 463, 467, 491, 503, 509 -END - - -is capture("perl perl/ch-2.pl encode LEMON ATTACKATDAWN"), "LXFOPVEFRNHR\n"; -is capture("perl perl/ch-2.pl decode LEMON LXFOPVEFRNHR"), "ATTACKATDAWN\n"; - - -done_testing; - -sub capture { - my($cmd) = @_; - my $out = `$cmd`; - $out =~ s/[ \t\v\f\r]*\n/\n/g; - return $out; -} |
