diff options
| -rw-r--r-- | challenge-015/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-015/paulo-custodio/python/ch-1.py | 55 | ||||
| -rw-r--r-- | challenge-015/paulo-custodio/python/ch-2.py | 38 | ||||
| -rw-r--r-- | challenge-015/paulo-custodio/t/test-1.yaml | 14 | ||||
| -rw-r--r-- | challenge-015/paulo-custodio/t/test-2.yaml | 10 | ||||
| -rw-r--r-- | challenge-015/paulo-custodio/test.pl | 28 |
6 files changed, 119 insertions, 28 deletions
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; -} |
