diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2021-12-03 10:37:53 +0000 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2021-12-03 10:37:53 +0000 |
| commit | cc1e200e05cfd8fda451a2b68c66445500569d4c (patch) | |
| tree | ef0d23a14c50034a8d62f57e514a3ce85d966810 /challenge-023 | |
| parent | bf990af024f1ea0279927e4c89b2146b86e15e3b (diff) | |
| download | perlweeklychallenge-club-cc1e200e05cfd8fda451a2b68c66445500569d4c.tar.gz perlweeklychallenge-club-cc1e200e05cfd8fda451a2b68c66445500569d4c.tar.bz2 perlweeklychallenge-club-cc1e200e05cfd8fda451a2b68c66445500569d4c.zip | |
Add Python solution to challenge 23
Diffstat (limited to 'challenge-023')
| -rw-r--r-- | challenge-023/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-023/paulo-custodio/perl/ch-1.pl | 4 | ||||
| -rw-r--r-- | challenge-023/paulo-custodio/perl/ch-2.pl | 7 | ||||
| -rw-r--r-- | challenge-023/paulo-custodio/python/ch-1.py | 32 | ||||
| -rw-r--r-- | challenge-023/paulo-custodio/python/ch-2.py | 38 | ||||
| -rw-r--r-- | challenge-023/paulo-custodio/t/test-1.yaml | 15 | ||||
| -rw-r--r-- | challenge-023/paulo-custodio/t/test-2.yaml | 55 | ||||
| -rw-r--r-- | challenge-023/paulo-custodio/test.pl | 30 |
8 files changed, 148 insertions, 35 deletions
diff --git a/challenge-023/paulo-custodio/Makefile b/challenge-023/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-023/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-023/paulo-custodio/perl/ch-1.pl b/challenge-023/paulo-custodio/perl/ch-1.pl index ee500f38dc..581caa45f9 100644 --- a/challenge-023/paulo-custodio/perl/ch-1.pl +++ b/challenge-023/paulo-custodio/perl/ch-1.pl @@ -1,4 +1,4 @@ -#!/usr/bin/env perl +#!/usr/bin/perl # Challenge 023 # @@ -19,7 +19,7 @@ use Modern::Perl; my($n, @seq) = @ARGV; -say join(", ", nth_forward_diff($n, @seq)), "."; +say join(", ", nth_forward_diff($n, @seq)); sub forward_diff { diff --git a/challenge-023/paulo-custodio/perl/ch-2.pl b/challenge-023/paulo-custodio/perl/ch-2.pl index 62f450ba8d..695e54128d 100644 --- a/challenge-023/paulo-custodio/perl/ch-2.pl +++ b/challenge-023/paulo-custodio/perl/ch-2.pl @@ -1,16 +1,17 @@ -#!/usr/bin/env perl +#!/usr/bin/perl # Challenge 023 # # Task #2 # Create a script that prints Prime Decomposition of a given number. The prime # decomposition of a number is defined as a list of prime numbers which when -# all multiplied together, are equal to that number. For example, the Prime decomposition of 228 is 2,2,3,19 as 228 = 2 * 2 * 3 * 19. +# all multiplied together, are equal to that number. For example, the Prime +# decomposition of 228 is 2,2,3,19 as 228 = 2 * 2 * 3 * 19. use Modern::Perl; my($n) = @ARGV; -say join(", ", prime_decomposition($n)), "."; +say join(", ", prime_decomposition($n)); # check if number is prime diff --git a/challenge-023/paulo-custodio/python/ch-1.py b/challenge-023/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..5baa57d74a --- /dev/null +++ b/challenge-023/paulo-custodio/python/ch-1.py @@ -0,0 +1,32 @@ +#!/usr/bin/python3 + +# Challenge 023 +# +# Task #1 +# Create a script that prints nth order forward difference series. You should +# be a able to pass the list of numbers and order number as command line +# parameters. Let me show you with an example. +# +# Suppose we have list (X) of numbers: 5, 9, 2, 8, 1, 6 and we would like to +# create 1st order forward difference series (Y). So using the formula +# Y(i) = X(i+1) - X(i), we get the following numbers: +# (9-5), (2-9), (8-2), (1-8), (6-1). +# In short, the final series would be: 4, -7, 6, -7, 5. +# If you noticed, it has one less number than the original series. +# Similarly you can carry on 2nd order forward difference series like: +# (-7-4), (6+7), (-7-6), (5+7) => -11, 13, -13, 12. + +import sys + +def forward_diff(seq): + return [seq[i+1]-seq[i] for i in range(len(seq)-1)] + +def nth_forward_diff(n ,seq): + for i in range(n): + seq = forward_diff(seq) + return seq + +n = int(sys.argv[1]) +seq = [int(x) for x in sys.argv[2:]] +seq = nth_forward_diff(n ,seq) +print(", ".join([str(x) for x in seq])) diff --git a/challenge-023/paulo-custodio/python/ch-2.py b/challenge-023/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..4438d1af8a --- /dev/null +++ b/challenge-023/paulo-custodio/python/ch-2.py @@ -0,0 +1,38 @@ +#!/usr/bin/python3 + +# Challenge 023 +# +# Task #2 +# Create a script that prints Prime Decomposition of a given number. The prime +# decomposition of a number is defined as a list of prime numbers which when +# all multiplied together, are equal to that number. For example, the Prime +# decomposition of 228 is 2,2,3,19 as 228 = 2 * 2 * 3 * 19. + +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_decomposition(n): + if n<2: + return [n] + + f = [] + p = 2 + while n>1: + if n%p == 0: + f.append(p) + n //= p + else: + p = next_prime(p) + return f + +f = prime_decomposition(int(sys.argv[1])) +print(", ".join([str(x) for x in f])) diff --git a/challenge-023/paulo-custodio/t/test-1.yaml b/challenge-023/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..d93bef9607 --- /dev/null +++ b/challenge-023/paulo-custodio/t/test-1.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 1 5 9 2 8 1 6 + input: + output: 4, -7, 6, -7, 5 +- setup: + cleanup: + args: 2 5 9 2 8 1 6 + input: + output: -11, 13, -13, 12 +- setup: + cleanup: + args: 1 4 -7 6 -7 5 + input: + output: -11, 13, -13, 12 diff --git a/challenge-023/paulo-custodio/t/test-2.yaml b/challenge-023/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..f94852e4f0 --- /dev/null +++ b/challenge-023/paulo-custodio/t/test-2.yaml @@ -0,0 +1,55 @@ +- setup: + cleanup: + args: 1 + input: + output: 1 +- setup: + cleanup: + args: 2 + input: + output: 2 +- setup: + cleanup: + args: 3 + input: + output: 3 +- setup: + cleanup: + args: 4 + input: + output: 2, 2 +- setup: + cleanup: + args: 5 + input: + output: 5 +- setup: + cleanup: + args: 6 + input: + output: 2, 3 +- setup: + cleanup: + args: 7 + input: + output: 7 +- setup: + cleanup: + args: 8 + input: + output: 2, 2, 2 +- setup: + cleanup: + args: 9 + input: + output: 3, 3 +- setup: + cleanup: + args: 10 + input: + output: 2, 5 +- setup: + cleanup: + args: 228 + input: + output: 2, 2, 3, 19 diff --git a/challenge-023/paulo-custodio/test.pl b/challenge-023/paulo-custodio/test.pl deleted file mode 100644 index bc90aec76b..0000000000 --- a/challenge-023/paulo-custodio/test.pl +++ /dev/null @@ -1,30 +0,0 @@ -#!/usr/bin/perl - -use Modern::Perl; -use Test::More; -use Path::Tiny; - -is capture("perl perl/ch-1.pl 1 5 9 2 8 1 6"), "4, -7, 6, -7, 5.\n"; -is capture("perl perl/ch-1.pl 2 5 9 2 8 1 6"), "-11, 13, -13, 12.\n"; -is capture("perl perl/ch-1.pl 1 4 -7 6 -7 5"), "-11, 13, -13, 12.\n"; - -is capture("perl perl/ch-2.pl 1"), "1.\n"; -is capture("perl perl/ch-2.pl 2"), "2.\n"; -is capture("perl perl/ch-2.pl 3"), "3.\n"; -is capture("perl perl/ch-2.pl 4"), "2, 2.\n"; -is capture("perl perl/ch-2.pl 5"), "5.\n"; -is capture("perl perl/ch-2.pl 6"), "2, 3.\n"; -is capture("perl perl/ch-2.pl 7"), "7.\n"; -is capture("perl perl/ch-2.pl 8"), "2, 2, 2.\n"; -is capture("perl perl/ch-2.pl 9"), "3, 3.\n"; -is capture("perl perl/ch-2.pl 10"), "2, 5.\n"; -is capture("perl perl/ch-2.pl 228"), "2, 2, 3, 19.\n"; - -done_testing; - -sub capture { - my($cmd) = @_; - my $out = `$cmd`; - $out =~ s/[ \t\v\f\r]*\n/\n/g; - return $out; -} |
