diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-06-15 17:03:53 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-06-15 17:03:53 +0100 |
| commit | b7b01f086db31655b507a46f15017088939c1a2d (patch) | |
| tree | 60300cc4cd2c87e027d65303f8cf8854b157302b | |
| parent | 620ea1919bb4dc79a9a4f18cb18e4362a2d6d21c (diff) | |
| parent | a9e866ad3bd05681093931779b384f1ffdc06e37 (diff) | |
| download | perlweeklychallenge-club-b7b01f086db31655b507a46f15017088939c1a2d.tar.gz perlweeklychallenge-club-b7b01f086db31655b507a46f15017088939c1a2d.tar.bz2 perlweeklychallenge-club-b7b01f086db31655b507a46f15017088939c1a2d.zip | |
Merge pull request #4266 from pauloscustodio/paulo-custodio
Paulo custodio
| -rw-r--r--[-rwxr-xr-x] | challenge-109/paulo-custodio/perl/ch-1.pl | 6 | ||||
| -rw-r--r--[-rwxr-xr-x] | challenge-109/paulo-custodio/perl/ch-2.pl | 12 | ||||
| -rw-r--r-- | challenge-110/paulo-custodio/people.txt | 5 | ||||
| -rw-r--r-- | challenge-110/paulo-custodio/perl/ch-1.pl | 29 | ||||
| -rw-r--r-- | challenge-110/paulo-custodio/perl/ch-2.pl | 42 | ||||
| -rw-r--r-- | challenge-110/paulo-custodio/phones.txt | 5 | ||||
| -rw-r--r-- | challenge-110/paulo-custodio/t/test-1.yaml | 8 | ||||
| -rw-r--r-- | challenge-110/paulo-custodio/t/test-2.yaml | 8 | ||||
| -rwxr-xr-x | challenge-110/paulo-custodio/test.pl | 7 | ||||
| -rw-r--r-- | challenge-111/paulo-custodio/perl/ch-1.pl | 87 | ||||
| -rw-r--r-- | challenge-111/paulo-custodio/perl/ch-2.pl | 27 | ||||
| -rw-r--r-- | challenge-111/paulo-custodio/t/test-1.yaml | 45 | ||||
| -rw-r--r-- | challenge-111/paulo-custodio/t/test-2.yaml | 6 | ||||
| -rwxr-xr-x | challenge-111/paulo-custodio/test.pl | 7 |
14 files changed, 285 insertions, 9 deletions
diff --git a/challenge-109/paulo-custodio/perl/ch-1.pl b/challenge-109/paulo-custodio/perl/ch-1.pl index edb3e9769d..4015a4b8b2 100755..100644 --- a/challenge-109/paulo-custodio/perl/ch-1.pl +++ b/challenge-109/paulo-custodio/perl/ch-1.pl @@ -4,10 +4,10 @@ # # TASK #1 - Chowla Numbers # Submitted by: Mohammad S Anwar -# Write a script to generate first 20 Chowla Numbers, named after, -# Sarvadaman D. S. Chowla, a London born Indian American mathematician. +# Write a script to generate first 20 Chowla Numbers, named after, +# Sarvadaman D. S. Chowla, a London born Indian American mathematician. # It is defined as: -# +# # C(n) = sum of divisors of n except 1 and n # NOTE: Updated the above definition as suggested by Abigail [2021/04/19 18:40]. # Output: diff --git a/challenge-109/paulo-custodio/perl/ch-2.pl b/challenge-109/paulo-custodio/perl/ch-2.pl index b168a71b1a..7c4e8226a3 100755..100644 --- a/challenge-109/paulo-custodio/perl/ch-2.pl +++ b/challenge-109/paulo-custodio/perl/ch-2.pl @@ -5,7 +5,7 @@ # TASK #2 - Four Squares Puzzle # Submitted by: Mohammad S Anwar # You are given four squares as below with numbers named a,b,c,d,e,f,g. -# +# # (1) (3) # ╔══════════════╗ ╔══════════════╗ # ║ ║ ║ ║ @@ -21,14 +21,14 @@ # │ │ │ │ # │ │ │ │ # └──────────────┘ └─────────────┘ -# Write a script to place the given unique numbers in the square box so that sum +# Write a script to place the given unique numbers in the square box so that sum # of numbers in each box is the same. -# +# # Example # Input: 1,2,3,4,5,6,7 -# +# # Output: -# +# # a = 6 # b = 4 # c = 1 @@ -36,7 +36,7 @@ # e = 2 # f = 3 # g = 7 -# +# # Box 1: a + b = 6 + 4 = 10 # Box 2: b + c + d = 4 + 1 + 5 = 10 # Box 3: d + e + f = 5 + 2 + 3 = 10 diff --git a/challenge-110/paulo-custodio/people.txt b/challenge-110/paulo-custodio/people.txt new file mode 100644 index 0000000000..716ebdce75 --- /dev/null +++ b/challenge-110/paulo-custodio/people.txt @@ -0,0 +1,5 @@ +name,age,sex +Mohammad,45,m +Joe,20,m +Julie,35,f +Cristina,10,f diff --git a/challenge-110/paulo-custodio/perl/ch-1.pl b/challenge-110/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..493281da53 --- /dev/null +++ b/challenge-110/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,29 @@ +#!/usr/bin/env perl + +# Challenge 110 +# +# TASK #1 - Valid Phone Numbers +# Submitted by: Mohammad S Anwar +# You are given a text file. +# +# Write a script to display all valid phone numbers in the given text file. +# +# Acceptable Phone Number Formats +# +nn nnnnnnnnnn +# (nn) nnnnnnnnnn +# nnnn nnnnnnnnnn +# Input File +# 0044 1148820341 +# +44 1148820341 +# 44-11-4882-0341 +# (44) 1148820341 +# 00 1148820341 +# Output +# 0044 1148820341 +# +44 1148820341 +# (44) 1148820341 + +use Modern::Perl; +while (<>) { + print if /^ \s* (?: \+ \d{2} | \( \d{2} \) | \d{4} ) \s+ \d{10} \s* $/x; +} diff --git a/challenge-110/paulo-custodio/perl/ch-2.pl b/challenge-110/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..2dbe0b8be8 --- /dev/null +++ b/challenge-110/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,42 @@ +#!/usr/bin/env perl + +# Challenge 110 +# +# TASK #2 - Transpose File +# Submitted by: Mohammad S Anwar +# You are given a text file. +# +# Write a script to transpose the contents of the given file. +# +# Input File +# name,age,sex +# Mohammad,45,m +# Joe,20,m +# Julie,35,f +# Cristina,10,f +# Output: +# name,Mohammad,Joe,Julie,Cristina +# age,45,20,35,10 +# sex,m,m,f,f + +use Modern::Perl; +my @data; +while (<>) { + chomp; + push @data, [split /,/, $_]; +} +for (transpose(@data)) { + say join ',', @$_; +} + +sub transpose { + my(@in) = @_; + my @out; + for my $row (0..$#in) { + for my $col (0 .. $#{$in[$row]}) { + $out[$col] ||= []; + $out[$col][$row] = $in[$row][$col]; + } + } + return @out; +} diff --git a/challenge-110/paulo-custodio/phones.txt b/challenge-110/paulo-custodio/phones.txt new file mode 100644 index 0000000000..48d6254741 --- /dev/null +++ b/challenge-110/paulo-custodio/phones.txt @@ -0,0 +1,5 @@ +0044 1148820341 + +44 1148820341 + 44-11-4882-0341 +(44) 1148820341 + 00 1148820341 diff --git a/challenge-110/paulo-custodio/t/test-1.yaml b/challenge-110/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..84a2764e1f --- /dev/null +++ b/challenge-110/paulo-custodio/t/test-1.yaml @@ -0,0 +1,8 @@ +- setup: + cleanup: + args: phones.txt + input: + output: | + 0044 1148820341 + +44 1148820341 + (44) 1148820341 diff --git a/challenge-110/paulo-custodio/t/test-2.yaml b/challenge-110/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..bf58779c88 --- /dev/null +++ b/challenge-110/paulo-custodio/t/test-2.yaml @@ -0,0 +1,8 @@ +- setup: + cleanup: + args: people.txt + input: + output: | + name,Mohammad,Joe,Julie,Cristina + age,45,20,35,10 + sex,m,m,f,f diff --git a/challenge-110/paulo-custodio/test.pl b/challenge-110/paulo-custodio/test.pl new file mode 100755 index 0000000000..cf1ced98e0 --- /dev/null +++ b/challenge-110/paulo-custodio/test.pl @@ -0,0 +1,7 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use 5.030; + +require '../../challenge-001/paulo-custodio/test.pl'; diff --git a/challenge-111/paulo-custodio/perl/ch-1.pl b/challenge-111/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..393c57bb1e --- /dev/null +++ b/challenge-111/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,87 @@ +#!/usr/bin/env perl + +# Challenge 111 +# +# TASK #1 - Search Matrix +# Submitted by: Mohammad S Anwar +# You are given 5x5 matrix filled with integers such that each row is sorted +# from left to right and the first integer of each row is greater than the +# last integer of the previous row. +# +# Write a script to find a given integer in the matrix using an efficient +# search algorithm. +# +# Example +# Matrix: [ 1, 2, 3, 5, 7 ] +# [ 9, 11, 15, 19, 20 ] +# [ 23, 24, 25, 29, 31 ] +# [ 32, 33, 39, 40, 42 ] +# [ 45, 47, 48, 49, 50 ] +# +# Input: 35 +# Output: 0 since it is missing in the matrix +# +# Input: 39 +# Output: 1 as it exists in the matrix + +use Modern::Perl; + +my @data = ([ 1, 2, 3, 5, 7 ], + [ 9, 11, 15, 19, 20 ], + [ 23, 24, 25, 29, 31 ], + [ 32, 33, 39, 40, 42 ], + [ 45, 47, 48, 49, 50 ]); +my $N = shift || 0; +say find($N) ? 1 : 0; + +sub find { + my($n) = @_; + my $row = find_row($n); + return 0 if $row < 0; + my $col = find_col($n, $row); + return 0 if $col < 0; + return 1; +} + +sub find_row { + my($n) = @_; + my $l = 0; + my $h = $#data; + return -1 if $n < $data[$l][0]; + return -1 if $n > $data[$h][-1]; + while ($l < $h) { + my $m = int(1+($l+$h)/2); + if ($n < $data[$m][0]) { + $h = $m-1; + } + elsif ($n > $data[$m][-1]) { + $l = $m+1; + } + else { + return $m; + } + } + return $l; +} + +sub find_col { + my($n, $row) = @_; + my $l = 0; + my $h = $#{$data[$row]}; + return -1 if $n < $data[$row][0]; + return -1 if $n > $data[$row][-1]; + while ($l < $h) { + my $m = int(1+($l+$h)/2); + if ($n < $data[$row][$m]) { + $h = $m-1; + } + elsif ($n > $data[$row][$m]) { + $l = $m+1; + } + else { + return $m; + } + } + return -1 if $n != $data[$row][$l]; + return $l; +} diff --git a/challenge-111/paulo-custodio/perl/ch-2.pl b/challenge-111/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..d70c838907 --- /dev/null +++ b/challenge-111/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,27 @@ +#!/usr/bin/env perl + +# Challenge 111 +# +# TASK #2 - Ordered Letters +# Submitted by: E. Choroba +# Given a word, you can sort its letters alphabetically (case insensitive). +# For example, “beekeeper” becomes “beeeeekpr” and “dictionary” becomes +# “acdiinorty”. +# +# Write a script to find the longest English words that don’t change when +# their letters are sorted. + +use Modern::Perl; + +my @words; +my $max_len = 0; +while (<>) { + chomp; + my $word = lc($_); + my $drow = join '', (sort split //, $word); + if ($word eq $drow) { + $max_len = length($word) if $max_len < length($word); + push @words, $word; + } +} +say join "\n", grep {length($_)==$max_len} @words; diff --git a/challenge-111/paulo-custodio/t/test-1.yaml b/challenge-111/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..40700d7f1a --- /dev/null +++ b/challenge-111/paulo-custodio/t/test-1.yaml @@ -0,0 +1,45 @@ +- setup: + cleanup: + args: 0 + input: + output: 0 +- setup: + cleanup: + args: 1 + input: + output: 1 +- setup: + cleanup: + args: 4 + input: + output: 0 +- setup: + cleanup: + args: 7 + input: + output: 1 +- setup: + cleanup: + args: 8 + input: + output: 0 +- setup: + cleanup: + args: 21 + input: + output: 0 +- setup: + cleanup: + args: 23 + input: + output: 1 +- setup: + cleanup: + args: 25 + input: + output: 1 +- setup: + cleanup: + args: 51 + input: + output: 0 diff --git a/challenge-111/paulo-custodio/t/test-2.yaml b/challenge-111/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..3c5bbecdbf --- /dev/null +++ b/challenge-111/paulo-custodio/t/test-2.yaml @@ -0,0 +1,6 @@ +- setup: 0==system("aspell -d en dump master | aspell -l en expand > words.txt"); + cleanup: unlink("words.txt") + args: words.txt + input: + output: | + billowy diff --git a/challenge-111/paulo-custodio/test.pl b/challenge-111/paulo-custodio/test.pl new file mode 100755 index 0000000000..cf1ced98e0 --- /dev/null +++ b/challenge-111/paulo-custodio/test.pl @@ -0,0 +1,7 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use 5.030; + +require '../../challenge-001/paulo-custodio/test.pl'; |
