aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-06-07 00:49:07 +0100
committerGitHub <noreply@github.com>2021-06-07 00:49:07 +0100
commit3f0461a6a14a706d5a121700f69e7447d242496d (patch)
tree1a1bd848d93d08fa976bfef8b711b99636689592
parent040636ba800110c76d302f6e9af3a39abceb444f (diff)
parent984026df4e81dda0c8899ff5aa1ad7e6241ffe65 (diff)
downloadperlweeklychallenge-club-3f0461a6a14a706d5a121700f69e7447d242496d.tar.gz
perlweeklychallenge-club-3f0461a6a14a706d5a121700f69e7447d242496d.tar.bz2
perlweeklychallenge-club-3f0461a6a14a706d5a121700f69e7447d242496d.zip
Merge pull request #4212 from pauloscustodio/paulo-custodio
Add python solution to challenge 004
-rw-r--r--challenge-001/paulo-custodio/test.pl8
-rw-r--r--challenge-004/paulo-custodio/perl/ch-1.pl4
-rw-r--r--challenge-004/paulo-custodio/perl/ch-2.pl4
-rwxr-xr-xchallenge-004/paulo-custodio/python/ch-1.py13
-rwxr-xr-xchallenge-004/paulo-custodio/python/ch-2.py40
-rwxr-xr-x[-rw-r--r--]challenge-004/paulo-custodio/test.pl6
-rw-r--r--challenge-106/paulo-custodio/perl/ch-1.pl12
-rw-r--r--challenge-106/paulo-custodio/perl/ch-2.pl12
-rw-r--r--challenge-106/paulo-custodio/t/test-1.yaml2
-rw-r--r--challenge-108/paulo-custodio/perl/ch-1.pl14
-rw-r--r--challenge-108/paulo-custodio/perl/ch-2.pl67
-rw-r--r--challenge-108/paulo-custodio/t/test-2.yaml5
-rw-r--r--challenge-108/paulo-custodio/test.pl7
13 files changed, 166 insertions, 28 deletions
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
index a61c28ebb7..921572d853 100644..100755
--- 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';
diff --git a/challenge-106/paulo-custodio/perl/ch-1.pl b/challenge-106/paulo-custodio/perl/ch-1.pl
index 3ffc4020ff..90324d33ea 100644
--- a/challenge-106/paulo-custodio/perl/ch-1.pl
+++ b/challenge-106/paulo-custodio/perl/ch-1.pl
@@ -5,19 +5,19 @@
# TASK #1 › Maximum Gap
# Submitted by: Mohammad S Anwar
# You are given an array of integers @N.
-#
+#
# Write a script to display the maximum difference between two successive
# elements once the array is sorted.
-#
+#
# If the array contains only 1 element then display 0.
-#
+#
# Example
# Input: @N = (2, 9, 3, 5)
# Output: 4
-#
+#
# Input: @N = (1, 3, 8, 2, 0)
# Output: 5
-#
+#
# Input: @N = (5)
# Output: 0
@@ -30,7 +30,7 @@ sub max_gap {
my(@n) = @_;
return 0 if @n < 2;
@n = sort @n;
-
+
my $max_gap = 0;
for my $i (0..$#n-1) {
my $gap = $n[$i+1] - $n[$i];
diff --git a/challenge-106/paulo-custodio/perl/ch-2.pl b/challenge-106/paulo-custodio/perl/ch-2.pl
index 020663f23f..390fa68cb1 100644
--- a/challenge-106/paulo-custodio/perl/ch-2.pl
+++ b/challenge-106/paulo-custodio/perl/ch-2.pl
@@ -3,17 +3,17 @@
# TASK #2 › Decimal String
# Submitted by: Mohammad S Anwar
# You are given numerator and denominator i.e. $N and $D.
-#
+#
# Write a script to convert the fraction into decimal string. If the fractional
# part is recurring then put it in parenthesis.
-#
+#
# Example
# Input: $N = 1, $D = 3
# Output: "0.(3)"
-#
+#
# Input: $N = 1, $D = 2
# Output: "0.5"
-#
+#
# Input: $N = 5, $D = 66
# Output: "0.0(75)"
@@ -28,12 +28,12 @@ sub decimal {
Math::BigFloat->round_mode('trunc'); # so that 1/6=0.16666
Math::BigFloat->accuracy(1000); # very long list of digits
-
+
my $N = Math::BigFloat->new($n);
my $D = Math::BigFloat->new($d);
my $Q = $N->copy()->bdiv($D);
$Q =~ s/(\.\d+?)0+$/$1/; # remove 00000 from 2.30000
-
+
# naive solution: finds repetitions by string match
for my $rept (1..100) {
my $code = "return \$Q if \$Q =~ s/((\\d{$rept})\\2+)\\d*?\$/\\(\$2\\)/;";
diff --git a/challenge-106/paulo-custodio/t/test-1.yaml b/challenge-106/paulo-custodio/t/test-1.yaml
index 7f4993223e..23d76e1249 100644
--- a/challenge-106/paulo-custodio/t/test-1.yaml
+++ b/challenge-106/paulo-custodio/t/test-1.yaml
@@ -1,6 +1,6 @@
- setup:
cleanup:
- args:
+ args:
input:
output: 0
- setup:
diff --git a/challenge-108/paulo-custodio/perl/ch-1.pl b/challenge-108/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..333532278a
--- /dev/null
+++ b/challenge-108/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,14 @@
+#!/usr/bin/perl
+
+# Challenge 108
+#
+# TASK #1 › Locate Memory
+# Submitted by: Mohammad S Anwar
+#
+# Write a script to declare a variable or constant and print it’s location in
+# the memory.
+
+use Modern::Perl;
+
+my $var = "hello world";
+say \$var;
diff --git a/challenge-108/paulo-custodio/perl/ch-2.pl b/challenge-108/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..d76209a4e8
--- /dev/null
+++ b/challenge-108/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,67 @@
+#!/usr/bin/perl
+
+# Challenge 108
+#
+# TASK #2 › Bell Numbers
+# Submitted by: Mohammad S Anwar
+#
+# Write a script to display top 10 Bell Numbers. Please refer to wikipedia page
+# for more informations.
+#
+# Example:
+# B0: 1 as you can only have one partition of zero element set
+# B1: 1 as you can only have one partition of one element set {a}.
+# B2: 2
+# {a}{b}
+# {a,b}
+# B3: 5
+# {a}{b}{c}
+# {a,b}{c}
+# {a}{b,c}
+# {a,c}{b}
+# {a,b,c}
+# B4: 15
+# {a}{b}{c}{d}
+# {a,b,c,d}
+# {a,b}{c,d}
+# {a,c}{b,d}
+# {a,d}{b,c}
+# {a,b}{c}{d}
+# {a,c}{b}{d}
+# {a,d}{b}{c}
+# {b,c}{a}{d}
+# {b,d}{a}{c}
+# {c,d}{a}{b}
+# {a}{b,c,d}
+# {b}{a,c,d}
+# {c}{a,b,d}
+# {d}{a,b,c}
+
+use Modern::Perl;
+use Data::Dump 'dump';
+
+my $N = shift || 10;
+my $bell = bell();
+for (1..$N) {
+ print $bell->(), " ";
+}
+print "\n";
+
+sub bell {
+ my $n = -1;
+ my @bell;
+ return sub {
+ $n++;
+ if ($n==0) {
+ push @bell, [1];
+ return 1;
+ }
+ else {
+ push @bell, [$bell[$n-1][$n-1]];
+ for my $i (1..$n) {
+ $bell[$n][$i] = $bell[$n-1][$i-1] + $bell[$n][$i-1];
+ }
+ return $bell[$n][0];
+ }
+ };
+}
diff --git a/challenge-108/paulo-custodio/t/test-2.yaml b/challenge-108/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..db839db250
--- /dev/null
+++ b/challenge-108/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,5 @@
+- setup:
+ cleanup:
+ args:
+ input:
+ output: 1 1 2 5 15 52 203 877 4140 21147
diff --git a/challenge-108/paulo-custodio/test.pl b/challenge-108/paulo-custodio/test.pl
new file mode 100644
index 0000000000..01ed2b83cd
--- /dev/null
+++ b/challenge-108/paulo-custodio/test.pl
@@ -0,0 +1,7 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use 5.030;
+
+require '../../challenge-001/paulo-custodio/test.pl';