From 3c31e63adaad68da1414fefd38eeb8e15c7127f7 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Mon, 7 Jun 2021 00:10:37 +0100 Subject: Add python solution to challenge 004 --- challenge-001/paulo-custodio/test.pl | 8 +++--- challenge-004/paulo-custodio/perl/ch-1.pl | 4 +-- challenge-004/paulo-custodio/perl/ch-2.pl | 4 +-- challenge-004/paulo-custodio/python/ch-1.py | 13 ++++++++++ challenge-004/paulo-custodio/python/ch-2.py | 40 +++++++++++++++++++++++++++++ challenge-004/paulo-custodio/test.pl | 6 ++--- 6 files changed, 60 insertions(+), 15 deletions(-) create mode 100755 challenge-004/paulo-custodio/python/ch-1.py create mode 100755 challenge-004/paulo-custodio/python/ch-2.py mode change 100644 => 100755 challenge-004/paulo-custodio/test.pl diff --git a/challenge-001/paulo-custodio/test.pl b/challenge-001/paulo-custodio/test.pl index 611b563640..bc1f0a9a13 100644 --- a/challenge-001/paulo-custodio/test.pl +++ b/challenge-001/paulo-custodio/test.pl @@ -1,10 +1,8 @@ -#!/usr/bin/perl +#!/usr/bin/env perl # run tests described in t/test-N.yaml -use strict; -use warnings; -use 5.030; +use Modern::Perl; use Test::More; use Path::Tiny; use YAML::Tiny; @@ -164,7 +162,7 @@ sub build { return "perl $prog"; } if (/^python$/) { - return "python $prog"; + return "python3 $prog"; } die "unsupported language $lang"; } diff --git a/challenge-004/paulo-custodio/perl/ch-1.pl b/challenge-004/paulo-custodio/perl/ch-1.pl index 356e712c44..82b75fe8d4 100644 --- a/challenge-004/paulo-custodio/perl/ch-1.pl +++ b/challenge-004/paulo-custodio/perl/ch-1.pl @@ -8,9 +8,7 @@ # # we need a big-math library to compute any large number of digits -use strict; -use warnings; -use 5.030; +use Modern::Perl; use Math::BigFloat; say Math::BigFloat->bpi(-s $0); diff --git a/challenge-004/paulo-custodio/perl/ch-2.pl b/challenge-004/paulo-custodio/perl/ch-2.pl index faaa875806..13e66397e8 100644 --- a/challenge-004/paulo-custodio/perl/ch-2.pl +++ b/challenge-004/paulo-custodio/perl/ch-2.pl @@ -10,9 +10,7 @@ # use all the letters. # (Disclaimer: The challenge was proposed by Scimon Proctor) -use strict; -use warnings; -use 5.030; +use Modern::Perl; @ARGV==1 or die "Usage: ch-2.pl letters\n"; my($letters) = @ARGV; diff --git a/challenge-004/paulo-custodio/python/ch-1.py b/challenge-004/paulo-custodio/python/ch-1.py new file mode 100755 index 0000000000..c1f19cec8d --- /dev/null +++ b/challenge-004/paulo-custodio/python/ch-1.py @@ -0,0 +1,13 @@ +#!/usr/bin/env python3 + +# Challenge 004 +# +# Challenge #1 +# Write a script to output the same number of PI digits as the size of your script. +# Say, if your script size is 10, it should print 3.141592653. + +import math_pi # pip install math-pi +import os; + +size = os.path.getsize(__file__) +print(math_pi.pi(b=size-1)) # -1 to account for "3." diff --git a/challenge-004/paulo-custodio/python/ch-2.py b/challenge-004/paulo-custodio/python/ch-2.py new file mode 100755 index 0000000000..8cadfda8c8 --- /dev/null +++ b/challenge-004/paulo-custodio/python/ch-2.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python3 + +# Challenge 004 +# +# Challenge #2 +# You are given a file containing a list of words (case insensitive 1 word per +# line) and a list of letters. Print each word from the file that can be made +# using only letters from the list. You can use each letter only once (though +# there can be duplicates and you can use each of them once), you don't have to +# use all the letters. +# (Disclaimer: The challenge was proposed by Scimon Proctor) + +import sys +import re + +def isalpha(word): + if re.fullmatch(r"[a-zA-Z]+", word): + return True + else: + return False + +def matches_letters(word, letters): + for c in letters: + word = re.sub(c, "", word, 1) + if word == "": + return True + return False + +def print_matching(file, letters): + letters = letters.lower() + fp = open(file, 'r') + for line in fp.readlines(): + word = line.strip() + if isalpha(word) and len(word) >= 2 and matches_letters(word, letters): + print(word) + +if len(sys.argv) != 2: + print("Usage: ch-2.py letters") +else: + print_matching("words.txt", sys.argv[1]) diff --git a/challenge-004/paulo-custodio/test.pl b/challenge-004/paulo-custodio/test.pl old mode 100644 new mode 100755 index a61c28ebb7..921572d853 --- a/challenge-004/paulo-custodio/test.pl +++ b/challenge-004/paulo-custodio/test.pl @@ -1,8 +1,6 @@ -#!/usr/bin/perl +#!/usr/bin/env perl -use strict; -use warnings; +use Modern::Perl; use Test::More; -use 5.030; require '../../challenge-001/paulo-custodio/test.pl'; -- cgit