aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2021-11-24 11:38:35 +0000
committerPaulo Custodio <pauloscustodio@gmail.com>2021-11-24 11:38:35 +0000
commit05ec7ce53c5bb5bb898c992d782ddc322099cdf7 (patch)
tree36bb157495bb3a3ecff2e6df8ca7c5b79988f0d3
parentec625b645f0ab4e866858980c63d938d0403af03 (diff)
downloadperlweeklychallenge-club-05ec7ce53c5bb5bb898c992d782ddc322099cdf7.tar.gz
perlweeklychallenge-club-05ec7ce53c5bb5bb898c992d782ddc322099cdf7.tar.bz2
perlweeklychallenge-club-05ec7ce53c5bb5bb898c992d782ddc322099cdf7.zip
Add Python solution to challenge 20
-rw-r--r--challenge-020/paulo-custodio/Makefile2
-rw-r--r--challenge-020/paulo-custodio/perl/ch-1.pl6
-rw-r--r--challenge-020/paulo-custodio/python/ch-1.py22
-rw-r--r--challenge-020/paulo-custodio/python/ch-2.py34
-rw-r--r--challenge-020/paulo-custodio/t/test-1.yaml6
-rw-r--r--challenge-020/paulo-custodio/t/test-2.yaml5
-rw-r--r--challenge-020/paulo-custodio/test.pl20
7 files changed, 72 insertions, 23 deletions
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<sum1:
+ return n, sum1
+
+a, b = smallest_amicable_pair()
+print(f"({a},{b})")
diff --git a/challenge-020/paulo-custodio/t/test-1.yaml b/challenge-020/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..34c539ace7
--- /dev/null
+++ b/challenge-020/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,6 @@
+- setup:
+ cleanup:
+ args: ABBCDEEF
+ input:
+ output: |
+ "A", "BB", "C", "D", "EE", "F"
diff --git a/challenge-020/paulo-custodio/t/test-2.yaml b/challenge-020/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..5eb7bc5230
--- /dev/null
+++ b/challenge-020/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,5 @@
+- setup:
+ cleanup:
+ args:
+ input:
+ output: (220,284)
diff --git a/challenge-020/paulo-custodio/test.pl b/challenge-020/paulo-custodio/test.pl
deleted file mode 100644
index 2a03c35332..0000000000
--- a/challenge-020/paulo-custodio/test.pl
+++ /dev/null
@@ -1,20 +0,0 @@
-#!/usr/bin/perl
-
-use Modern::Perl;
-use Test::More;
-
-is capture("perl perl/ch-1.pl ABBCDEEF"), <<END;
-"A", "BB", "C", "D", "EE", "F".
-END
-
-is capture("perl perl/ch-2.pl"), "(220,284)\n";
-
-done_testing;
-
-
-sub capture {
- my($cmd) = @_;
- my $out = `$cmd`;
- $out =~ s/[ \r\t]*\n/\n/g;
- return $out;
-}