diff options
| author | Dave Jacoby <jacoby.david@gmail.com> | 2021-12-07 17:08:53 -0500 |
|---|---|---|
| committer | Dave Jacoby <jacoby.david@gmail.com> | 2021-12-07 17:08:53 -0500 |
| commit | f0230b0a58c8d2a63ba172c0c04ac75e5c80e0d4 (patch) | |
| tree | a7162c0c01b282684f159edc35a2f940a8a77e38 /challenge-023/paulo-custodio/python | |
| parent | 038f05c9243b83b2b654559b5005506bc6a3fc2b (diff) | |
| parent | 59dd7ccf422a3b10f31e3ad2cc9a2704ce6b40e0 (diff) | |
| download | perlweeklychallenge-club-f0230b0a58c8d2a63ba172c0c04ac75e5c80e0d4.tar.gz perlweeklychallenge-club-f0230b0a58c8d2a63ba172c0c04ac75e5c80e0d4.tar.bz2 perlweeklychallenge-club-f0230b0a58c8d2a63ba172c0c04ac75e5c80e0d4.zip | |
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
Diffstat (limited to 'challenge-023/paulo-custodio/python')
| -rw-r--r-- | challenge-023/paulo-custodio/python/ch-1.py | 32 | ||||
| -rw-r--r-- | challenge-023/paulo-custodio/python/ch-2.py | 38 |
2 files changed, 70 insertions, 0 deletions
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])) |
