From 05ec7ce53c5bb5bb898c992d782ddc322099cdf7 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Wed, 24 Nov 2021 11:38:35 +0000 Subject: Add Python solution to challenge 20 --- challenge-020/paulo-custodio/Makefile | 2 ++ challenge-020/paulo-custodio/perl/ch-1.pl | 6 ++--- challenge-020/paulo-custodio/python/ch-1.py | 22 +++++++++++++++++++ challenge-020/paulo-custodio/python/ch-2.py | 34 +++++++++++++++++++++++++++++ challenge-020/paulo-custodio/t/test-1.yaml | 6 +++++ challenge-020/paulo-custodio/t/test-2.yaml | 5 +++++ challenge-020/paulo-custodio/test.pl | 20 ----------------- 7 files changed, 72 insertions(+), 23 deletions(-) create mode 100644 challenge-020/paulo-custodio/Makefile create mode 100644 challenge-020/paulo-custodio/python/ch-1.py create mode 100644 challenge-020/paulo-custodio/python/ch-2.py create mode 100644 challenge-020/paulo-custodio/t/test-1.yaml create mode 100644 challenge-020/paulo-custodio/t/test-2.yaml delete mode 100644 challenge-020/paulo-custodio/test.pl diff --git a/challenge-020/paulo-custodio/Makefile b/challenge-020/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-020/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-020/paulo-custodio/perl/ch-1.pl b/challenge-020/paulo-custodio/perl/ch-1.pl index 4940788eb0..64fcbe2d9d 100644 --- a/challenge-020/paulo-custodio/perl/ch-1.pl +++ b/challenge-020/paulo-custodio/perl/ch-1.pl @@ -4,8 +4,8 @@ # # Task #1 # Write a script to accept a string from command line and split it on change -# of character. For example, if the string is “ABBCDEEF”, then it should split -# like “A”, “BB”, “C”, “D”, “EE”, “F”. +# of character. For example, if the string is "ABBCDEEF", then it should split +# like "A", "BB", "C", "D", "EE", "F". use Modern::Perl; @@ -17,4 +17,4 @@ while ($str ne '') { push @segs, $1; } -say join(", ", map {'"'.$_.'"'} @segs), "."; +say join(", ", map {'"'.$_.'"'} @segs); diff --git a/challenge-020/paulo-custodio/python/ch-1.py b/challenge-020/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..350bd40aa8 --- /dev/null +++ b/challenge-020/paulo-custodio/python/ch-1.py @@ -0,0 +1,22 @@ +#!/usr/bin/python3 + +# Challenge 020 +# +# Task #1 +# Write a script to accept a string from command line and split it on change +# of character. For example, if the string is "ABBCDEEF", then it should split +# like "A", "BB", "C", "D", "EE", "F". + +import sys +import re + +str = sys.argv[1] +segs = [] +while True: + matches = re.match(r"((.)\2*)", str) + if not matches: + break + segs.append(matches.group(1)) + str = str[matches.end(0):] + +print(", ".join(['"'+x+'"' for x in segs])) diff --git a/challenge-020/paulo-custodio/python/ch-2.py b/challenge-020/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..1f67e330b9 --- /dev/null +++ b/challenge-020/paulo-custodio/python/ch-2.py @@ -0,0 +1,34 @@ +#!/usr/bin/python3 + +# Challenge 020 +# +# Task #2 +# Write a script to print the smallest pair of Amicable Numbers. For more +# information, please checkout wikipedia page. + +import math + +def divisors(n): + div_low = [] + div_high = [] + for i in range(1, int(math.sqrt(n)+1)): + if n%i==0: + div_low.append(i) + if n/i!=i: + div_high.insert(0, int(n/i)) + return [*div_low, *div_high] + +def proper_divisors(n): + return filter(lambda x:x!=n, divisors(n)) + +def smallest_amicable_pair(): + n = 1 + while True: + n += 1 + sum1 = sum(proper_divisors(n)) + sum2 = sum(proper_divisors(sum1)) + if sum2==n and n