diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2021-11-17 12:50:00 +0000 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2021-11-17 12:50:00 +0000 |
| commit | 83b93c4bc81de0cada4cab2b8b3254c997bd0e93 (patch) | |
| tree | 9e9c60b4447a726821c250a90f7f3dbf73a93fde /challenge-012 | |
| parent | 429dde959e2bbde5fa2d4b9dd98fe6c2c035e597 (diff) | |
| download | perlweeklychallenge-club-83b93c4bc81de0cada4cab2b8b3254c997bd0e93.tar.gz perlweeklychallenge-club-83b93c4bc81de0cada4cab2b8b3254c997bd0e93.tar.bz2 perlweeklychallenge-club-83b93c4bc81de0cada4cab2b8b3254c997bd0e93.zip | |
Add Python solution to challenge 12
Diffstat (limited to 'challenge-012')
| -rw-r--r-- | challenge-012/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-012/paulo-custodio/python/ch-1.py | 36 | ||||
| -rw-r--r-- | challenge-012/paulo-custodio/python/ch-2.py | 53 | ||||
| -rw-r--r-- | challenge-012/paulo-custodio/test.pl | 4 |
4 files changed, 91 insertions, 4 deletions
diff --git a/challenge-012/paulo-custodio/Makefile b/challenge-012/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-012/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-012/paulo-custodio/python/ch-1.py b/challenge-012/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..91509d2ce1 --- /dev/null +++ b/challenge-012/paulo-custodio/python/ch-1.py @@ -0,0 +1,36 @@ +#!/usr/bin/python3 + +# Challenge 012 +# +# Challenge #1 +# The numbers formed by adding one to the products of the smallest primes are +# called the Euclid Numbers (see wiki). Write a script that finds the smallest +# Euclid Number that is not prime. This challenge was proposed by +# Laurent Rosenfeld. + +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 euclid_iter(): + prime = 1 + prime_prod = 1 + while True: + prime = next_prime(prime) + prime_prod *= prime + yield prime_prod+1 + +for n in euclid_iter(): + if primes.check(n): + pass + else: + print(n) + break diff --git a/challenge-012/paulo-custodio/python/ch-2.py b/challenge-012/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..2fb304f188 --- /dev/null +++ b/challenge-012/paulo-custodio/python/ch-2.py @@ -0,0 +1,53 @@ +#!/usr/bin/python3 + +# Challenge 012 +# +# Challenge #2 +# Write a script that finds the common directory path, given a collection of +# paths and directory separator. For example, if the following paths are +# supplied. +# /a/b/c/d +# /a/b/cd +# /a/b/cc +# /a/b/c/d/e +# and the path separator is /. Your script should return /a/b as common +# directory path. + +import sys + +def extract_common_prefix(paths): + # check if all paths have common prefix + dir = None + for path in paths: + if len(path)==0: + return False, "" + elif dir is None: + dir = path[0] + else: + if path[0]!=dir: + return False, "" + + # remove common prefix + for path in paths: + path.pop(0) + + return True, dir + +def common_prefix(sep, paths): + # split paths by separator + for i in range(len(paths)): + paths[i] = paths[i].split(sep) + + # find common prefix + prefix = [] + while True: + found, dir = extract_common_prefix(paths) + if not found: + break + prefix.append(dir) + + return sep.join(prefix) + +sep = sys.argv[1] +paths = sys.argv[2:] +print(common_prefix(sep, paths)) diff --git a/challenge-012/paulo-custodio/test.pl b/challenge-012/paulo-custodio/test.pl deleted file mode 100644 index ba6c37260b..0000000000 --- a/challenge-012/paulo-custodio/test.pl +++ /dev/null @@ -1,4 +0,0 @@ -#!/usr/bin/env perl -use Modern::Perl; -use Test::More; -require '../../challenge-001/paulo-custodio/test.pl'; |
