diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2022-04-04 17:22:49 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2022-04-04 17:22:49 +0100 |
| commit | 72b9ae6794ce48ed1047879ae943008437fceace (patch) | |
| tree | 9961d5cba5269c982d9483e2fd74706609cebf78 | |
| parent | 6d1d4fab6f6f2a764988af40d5b00e0eaa4854df (diff) | |
| download | perlweeklychallenge-club-72b9ae6794ce48ed1047879ae943008437fceace.tar.gz perlweeklychallenge-club-72b9ae6794ce48ed1047879ae943008437fceace.tar.bz2 perlweeklychallenge-club-72b9ae6794ce48ed1047879ae943008437fceace.zip | |
Add Perl solutions
| -rw-r--r-- | challenge-038/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-038/paulo-custodio/README | 1 | ||||
| -rw-r--r-- | challenge-038/paulo-custodio/perl/ch-1.pl | 35 | ||||
| -rw-r--r-- | challenge-038/paulo-custodio/perl/ch-2.pl | 80 | ||||
| -rw-r--r-- | challenge-038/paulo-custodio/t/test-1.yaml | 10 | ||||
| -rw-r--r-- | challenge-038/paulo-custodio/t/test-2.yaml | 5 | ||||
| -rw-r--r-- | challenge-159/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-159/paulo-custodio/README | 1 | ||||
| -rw-r--r-- | challenge-159/paulo-custodio/perl/ch-1.pl | 43 | ||||
| -rw-r--r-- | challenge-159/paulo-custodio/perl/ch-2.pl | 26 | ||||
| -rw-r--r-- | challenge-159/paulo-custodio/t/test-1.yaml | 15 | ||||
| -rw-r--r-- | challenge-159/paulo-custodio/t/test-2.yaml | 15 |
12 files changed, 235 insertions, 0 deletions
diff --git a/challenge-038/paulo-custodio/Makefile b/challenge-038/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-038/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-038/paulo-custodio/README b/challenge-038/paulo-custodio/README new file mode 100644 index 0000000000..87dc0b2fbd --- /dev/null +++ b/challenge-038/paulo-custodio/README @@ -0,0 +1 @@ +Solution by Paulo Custodio diff --git a/challenge-038/paulo-custodio/perl/ch-1.pl b/challenge-038/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..b6582aea32 --- /dev/null +++ b/challenge-038/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,35 @@ +#!/usr/bin/env perl + +# Challenge 038 +# +# TASK #1 +# Date Finder +# Create a script to accept a 7 digits number, where the first number can only +# be 1 or 2. The second and third digits can be anything 0-9. The fourth and +# fifth digits corresponds to the month i.e. 01,02,03
,11,12. And the last +# 2 digits respresents the days in the month i.e. 01,02,03
.29,30,31. Your +# script should validate if the given number is valid as per the rule and then +# convert into human readable format date. +# +# RULES +# If 1st digit is 1, then prepend 20 otherwise 19 to the 2nd and 3rd digits to +# make it 4-digits year. +# +# The 4th and 5th digits together should be a valid month. +# +# The 6th and 7th digits together should be a valid day for the above month. +# +# For example, the given number is 2230120, it should print 1923-01-20. + +use Modern::Perl; +use DateTime; + +my $input = shift || ""; +$input =~ /^([12])(\d\d)([01]\d)([0-3]\d)$/ or die "malformed input\n"; +my($year_msb, $year_lsb, $month, $day) = ($1, $2, $3, $4); +my $year = ($year_msb==1 ? 2000 : 1900)+$year_lsb; +$month>=1 && $month<=12 or die "malformed month\n"; +my $dt = DateTime->new(year => $year, month => $month, day => 1); +$day>=1 && $day<=$dt->month_length or die "malformed day\n"; +$dt = DateTime->new(year => $year, month => $month, day => $day); +say $dt->strftime("%Y-%m-%d"); diff --git a/challenge-038/paulo-custodio/perl/ch-2.pl b/challenge-038/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..c0567184d5 --- /dev/null +++ b/challenge-038/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,80 @@ +#!/usr/bin/env perl + +# Challenge 038 +# +# TASK #2 +# Word Game +# Lets assume we have tiles as listed below, with an alphabet (A..Z) printed +# on them. Each tile has a value, e.g. A (1 point), B (4 points) etc. You are +# allowed to draw 7 tiles from the lot randomly. Then try to form a word using +# the 7 tiles with maximum points altogether. You dont have to use all the +# 7 tiles to make a word. You should try to use as many tiles as possible to +# get the maximum points. +# +# For example, A (x8) means there are 8 tiles with letter A. +# +# 1 point +# A (x8), G (x3), I (x5), S (x7), U (x5), X (x2), Z (x5) +# +# 2 points +# E (x9), J (x3), L (x3), R (x3), V (x3), Y (x5) +# +# 3 points +# F (x3), D (x3), P (x5), W (x5) +# +# 4 points +# B (x5), N (x4) +# +# 5 points +# T (x5), O (x3), H (x3), M (x4), C (x4) +# +# 10 points +# K (x2), Q (x2) + +use Modern::Perl; + +my %score = (a=>1,g=>1,i=>1,s=>1,u=>1,x=>1,z=>1, + e=>2,j=>2,l=>2,r=>2,v=>2,y=>2, + f=>3,d=>3,p=>3,w=>3, + b=>4,n=>4, + t=>5,o=>5,h=>5,m=>5,c=>5, + k=>10,q=>10); +my %count = (a=>8,g=>3,i=>5,s=>7,u=>5,x=>2,z=>5, + e=>9,j=>3,l=>3,r=>3,v=>3,y=>5, + f=>3,d=>3,p=>5,w=>5, + b=>5,n=>4, + t=>5,o=>3,h=>3,m=>4,c=>4, + k=>2,q=>2); + +my $max_score = 0; +my $max_word = ""; +while (<>) { + chomp; + my $score = check_word($_); + if ($score > 0 && $score > $max_score) { + ($max_score, $max_word) = ($score, $_); + } +} + +say "$max_score $max_word"; + +sub check_word { + my($word) = @_; + + # maximium 7 letters, only lower case letters + return -1 unless $word =~ /^[a-z]{1,7}$/; + + # less than %count tiles for each letter + for my $letter (split //, $word) { + my $count = $word =~ tr/$letter/$letter/; + return -1 if $count > $count{$letter}; + } + + # compute score + my $score = 0; + for my $letter (split //, $word) { + $score += $score{$letter}; + } + + return $score; +} diff --git a/challenge-038/paulo-custodio/t/test-1.yaml b/challenge-038/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..208793c972 --- /dev/null +++ b/challenge-038/paulo-custodio/t/test-1.yaml @@ -0,0 +1,10 @@ +- setup: + cleanup: + args: 2230120 + input: + output: 1923-01-20 +- setup: + cleanup: + args: 1230120 + input: + output: 2023-01-20 diff --git a/challenge-038/paulo-custodio/t/test-2.yaml b/challenge-038/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..c5c7f00cb1 --- /dev/null +++ b/challenge-038/paulo-custodio/t/test-2.yaml @@ -0,0 +1,5 @@ +- setup: 0==system("aspell -d en dump master | aspell -l en expand > words.txt"); + cleanup: unlink("words.txt") + args: words.txt + input: + output: 39 knocked diff --git a/challenge-159/paulo-custodio/Makefile b/challenge-159/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-159/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-159/paulo-custodio/README b/challenge-159/paulo-custodio/README new file mode 100644 index 0000000000..87dc0b2fbd --- /dev/null +++ b/challenge-159/paulo-custodio/README @@ -0,0 +1 @@ +Solution by Paulo Custodio diff --git a/challenge-159/paulo-custodio/perl/ch-1.pl b/challenge-159/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..de8cf8b75c --- /dev/null +++ b/challenge-159/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,43 @@ +#!/usr/bin/env perl + +# Challenge 159 +# +# TASK #1 Farey Sequence +# Submitted by: Mohammad S Anwar +# You are given a positive number, $n. +# +# Write a script to compute Farey Sequence of the order $n. +# +# Example 1: +# Input: $n = 5 +# Output: 0/1, 1/5, 1/4, 1/3, 2/5, 1/2, 3/5, 2/3, 3/4, 4/5, 1/1. +# Example 2: +# Input: $n = 7 +# Output: 0/1, 1/7, 1/6, 1/5, 1/4, 2/7, 1/3, 2/5, 3/7, 1/2, 4/7, 3/5, 2/3, 5/7, 3/4, 4/5, 5/6, 6/7, 1/1. +# Example 3: +# Input: $n = 4 +# Output: 0/1, 1/4, 1/3, 1/2, 2/3, 3/4, 1/1. + +use Modern::Perl; +use ntheory qw( gcd ); + +my $n = shift || 1; +say join(", ", farey_sequence($n)); + +sub farey_sequence { + my($n) = @_; + my @seq = ([0,1], [1,1]); # first and last terms + + for my $i (1..$n) { + for my $j ($i+1..$n) { + if (gcd($i, $j)==1) { + push @seq, [$i,$j]; + } + } + } + + @seq = sort { $a->[0]/$a->[1] <=> $b->[0]/$b->[1] } @seq; + @seq = map {$_->[0].'/'.$_->[1]} @seq; + + return @seq; +} diff --git a/challenge-159/paulo-custodio/perl/ch-2.pl b/challenge-159/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..0b8df98a0b --- /dev/null +++ b/challenge-159/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,26 @@ +#!/usr/bin/env perl + +# Challenge 159 +# +# TASK #2 Moebius Number +# Submitted by: Mohammad S Anwar +# You are given a positive number $n. +# +# Write a script to generate the Moebius Number for the given number. +# Please refer to wikipedia page for more informations. +# +# Example 1: +# Input: $n = 5 +# Output: -1 +# Example 2: +# Input: $n = 10 +# Output: 1 +# Example 3: +# Input: $n = 20 +# Output: 0 + +use Modern::Perl; +use ntheory qw( moebius ); + +my $n = shift || 1; +say moebius($n); diff --git a/challenge-159/paulo-custodio/t/test-1.yaml b/challenge-159/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..fe5e1a56ee --- /dev/null +++ b/challenge-159/paulo-custodio/t/test-1.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 4 + input: + output: 0/1, 1/4, 1/3, 1/2, 2/3, 3/4, 1/1 +- setup: + cleanup: + args: 5 + input: + output: 0/1, 1/5, 1/4, 1/3, 2/5, 1/2, 3/5, 2/3, 3/4, 4/5, 1/1 +- setup: + cleanup: + args: 7 + input: + output: 0/1, 1/7, 1/6, 1/5, 1/4, 2/7, 1/3, 2/5, 3/7, 1/2, 4/7, 3/5, 2/3, 5/7, 3/4, 4/5, 5/6, 6/7, 1/1 diff --git a/challenge-159/paulo-custodio/t/test-2.yaml b/challenge-159/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..146c50f10c --- /dev/null +++ b/challenge-159/paulo-custodio/t/test-2.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 5 + input: + output: -1 +- setup: + cleanup: + args: 10 + input: + output: 1 +- setup: + cleanup: + args: 20 + input: + output: 0 |
