From 621e9c06533a574e60805826d2fb85ce59c9b073 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Mon, 12 Apr 2021 21:17:21 +0100 Subject: Add Perl solution to challenge 108 --- challenge-106/paulo-custodio/perl/ch-1.pl | 12 +++--- challenge-106/paulo-custodio/perl/ch-2.pl | 12 +++--- challenge-106/paulo-custodio/t/test-1.yaml | 2 +- challenge-108/paulo-custodio/perl/ch-1.pl | 14 +++++++ challenge-108/paulo-custodio/perl/ch-2.pl | 67 ++++++++++++++++++++++++++++++ challenge-108/paulo-custodio/t/test-2.yaml | 5 +++ challenge-108/paulo-custodio/test.pl | 7 ++++ 7 files changed, 106 insertions(+), 13 deletions(-) create mode 100644 challenge-108/paulo-custodio/perl/ch-1.pl create mode 100644 challenge-108/paulo-custodio/perl/ch-2.pl create mode 100644 challenge-108/paulo-custodio/t/test-2.yaml create mode 100644 challenge-108/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'; -- cgit 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