diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2024-09-28 19:10:26 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2024-09-28 19:10:26 +0100 |
| commit | 19698b60b5f8bbc16989a16c47fe41fa435cede3 (patch) | |
| tree | da92bd5dae7eb408d990f04eea50b3ff2ad7044d | |
| parent | 3b9ae8e17f9ba3d1fb2a4f612e0c4e2b271f6304 (diff) | |
| download | perlweeklychallenge-club-19698b60b5f8bbc16989a16c47fe41fa435cede3.tar.gz perlweeklychallenge-club-19698b60b5f8bbc16989a16c47fe41fa435cede3.tar.bz2 perlweeklychallenge-club-19698b60b5f8bbc16989a16c47fe41fa435cede3.zip | |
Add Python solution to challenge 154
| -rw-r--r-- | challenge-154/paulo-custodio/perl/ch-1.pl | 2 | ||||
| -rw-r--r-- | challenge-154/paulo-custodio/perl/ch-2.pl | 4 | ||||
| -rw-r--r-- | challenge-154/paulo-custodio/python/ch-1.py | 25 | ||||
| -rw-r--r-- | challenge-154/paulo-custodio/python/ch-2.py | 57 |
4 files changed, 85 insertions, 3 deletions
diff --git a/challenge-154/paulo-custodio/perl/ch-1.pl b/challenge-154/paulo-custodio/perl/ch-1.pl index 7176aa1fdf..1d84fcd558 100644 --- a/challenge-154/paulo-custodio/perl/ch-1.pl +++ b/challenge-154/paulo-custodio/perl/ch-1.pl @@ -2,7 +2,7 @@ # Challenge 154 # -# TASK #1 › Missing Permutation +# TASK #1 > Missing Permutation # Submitted by: Mohammad S Anwar # You are given possible permutations of the string 'PERL'. # diff --git a/challenge-154/paulo-custodio/perl/ch-2.pl b/challenge-154/paulo-custodio/perl/ch-2.pl index c70b72378a..0b68acc1fa 100644 --- a/challenge-154/paulo-custodio/perl/ch-2.pl +++ b/challenge-154/paulo-custodio/perl/ch-2.pl @@ -2,9 +2,9 @@ # Challenge 154 # -# TASK #2 › Padovan Prime +# TASK #2 > Padovan Prime # Submitted by: Mohammad S Anwar -# A Padovan Prime is a Padovan Number that’s also prime. +# A Padovan Prime is a Padovan Number that's also prime. # # In number theory, the Padovan sequence is the sequence of integers P(n) # defined by the initial values. diff --git a/challenge-154/paulo-custodio/python/ch-1.py b/challenge-154/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..7deb2cd296 --- /dev/null +++ b/challenge-154/paulo-custodio/python/ch-1.py @@ -0,0 +1,25 @@ +#!/usr/bin/env perl + +# Challenge 154 +# +# TASK #1 > Missing Permutation +# Submitted by: Mohammad S Anwar +# You are given possible permutations of the string 'PERL'. +# +# PELR, PREL, PERL, PRLE, PLER, PLRE, EPRL, EPLR, ERPL, +# ERLP, ELPR, ELRP, RPEL, RPLE, REPL, RELP, RLPE, RLEP, +# LPER, LPRE, LEPR, LRPE, LREP +# Write a script to find any permutations missing from the list. + +from itertools import permutations + +have = { + "PELR", "PREL", "PERL", "PRLE", "PLER", "PLRE", "EPRL", "EPLR", "ERPL", + "ERLP", "ELPR", "ELRP", "RPEL", "RPLE", "REPL", "RELP", "RLPE", "RLEP", + "LPER", "LPRE", "LEPR", "LRPE", "LREP" +} + +all_permutations = {''.join(p) for p in permutations("PERL")} +missing = sorted(set(all_permutations) - have) + +print(", ".join(missing)) diff --git a/challenge-154/paulo-custodio/python/ch-2.py b/challenge-154/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..91384eb9f6 --- /dev/null +++ b/challenge-154/paulo-custodio/python/ch-2.py @@ -0,0 +1,57 @@ +#!/usr/bin/env perl + +# Challenge 154 +# +# TASK #2 > Padovan Prime +# Submitted by: Mohammad S Anwar +# A Padovan Prime is a Padovan Number that's also prime. +# +# In number theory, the Padovan sequence is the sequence of integers P(n) +# defined by the initial values. +# +# P(0) = P(1) = P(2) = 1 +# and then followed by +# +# P(n) = P(n-2) + P(n-3) +# First few Padovan Numbers are as below: +# +# 1, 1, 1, 2, 2, 3, 4, 5, 7, 9, 12, 16, 21, 28, 37, ... +# Write a script to compute first 10 distinct Padovan Primes. +# +# Expected Output +# 2, 3, 5, 7, 37, 151, 3329, 23833, 13091204281, 3093215881333057 + +from functools import lru_cache +from sympy import isprime + +@lru_cache(None) +def padovan(n): + if n < 3: + return 1 + else: + return padovan(n - 2) + padovan(n - 3) + +def padovan_iter(): + n = 0 + while True: + n += 1 + yield padovan(n) + +def padovan_prime_iter(): + padovan_it = padovan_iter() + for got in padovan_it: + if isprime(got): + yield got + +def uniq_padovan_prime_it(): + got = set() + padovan_prime_it = padovan_prime_iter() + while True: + for value in padovan_prime_it: + if value not in got: + got.add(value) + yield value + +it = uniq_padovan_prime_it() +out = [next(it) for _ in range(10)] +print(", ".join(map(str, out))) |
