diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2024-08-18 22:18:31 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2024-08-18 22:18:31 +0100 |
| commit | 774f46dff430c3ac4d85635e184465702b69f1f4 (patch) | |
| tree | de52537811d35ffeeea012fcee8bc4d9c2f2c6bb | |
| parent | 7a6f61b8cb38ea32ab526a93798f7b515f9233b4 (diff) | |
| download | perlweeklychallenge-club-774f46dff430c3ac4d85635e184465702b69f1f4.tar.gz perlweeklychallenge-club-774f46dff430c3ac4d85635e184465702b69f1f4.tar.bz2 perlweeklychallenge-club-774f46dff430c3ac4d85635e184465702b69f1f4.zip | |
Add Perl solution to challenge 227
| -rw-r--r-- | challenge-227/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-227/paulo-custodio/perl/ch-1.pl | 33 | ||||
| -rw-r--r-- | challenge-227/paulo-custodio/perl/ch-2.pl | 49 | ||||
| -rw-r--r-- | challenge-227/paulo-custodio/t/test-1.yaml | 5 | ||||
| -rw-r--r-- | challenge-227/paulo-custodio/t/test-2.yaml | 45 |
5 files changed, 134 insertions, 0 deletions
diff --git a/challenge-227/paulo-custodio/Makefile b/challenge-227/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-227/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-227/paulo-custodio/perl/ch-1.pl b/challenge-227/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..11e8553803 --- /dev/null +++ b/challenge-227/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,33 @@ +#!/usr/bin/env perl + +# Challenge 227 +# +# Task 1: Friday 13th +# Submitted by: Peter Campbell Smith +# You are given a year number in the range 1753 to 9999. +# +# Write a script to find out how many dates in the year are Friday 13th, assume +# that the current Gregorian calendar applies. +# +# Example +# Input: $year = 2023 +# Output: 2 +# +# Since there are only 2 Friday 13th in the given year 2023 i.e. 13th Jan and 13th Oct. + +use Modern::Perl; +use DateTime; + +my $year = shift or die "Usage: $0 year\n"; +say num_friday13th($year); + +sub num_friday13th { + my($year) = @_; + my $count = 0; + for my $month (1..12) { + my $dt = DateTime->new(year=>$year, month=>$month, day=>13); + my $dow = $dt->day_of_week; + $count++ if $dow==5; # friday + } + return $count; +} diff --git a/challenge-227/paulo-custodio/perl/ch-2.pl b/challenge-227/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..0169b78150 --- /dev/null +++ b/challenge-227/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,49 @@ +#!/usr/bin/env perl + +# Challenge 227 +# +# Task 2: Roman Maths +# Submitted by: Peter Campbell Smith +# Write a script to handle a 2-term arithmetic operation expressed in Roman numeral. +# +# Example +# IV + V => IX +# M - I => CMXCIX +# X / II => V +# XI * VI => LXVI +# VII ** III => CCCXLIII +# V - V => nulla (they knew about zero but didn't have a symbol) +# V / II => non potest (they didn't do fractions) +# MMM + M => non potest (they only went up to 3999) +# V - X => non potest (they didn't do negative numbers) + +use Modern::Perl; +use Math::Roman; + +my $roman_re = qr/[IVXLCDM]+/; +my $op_re = qr/\*\*|[-+*\/]/; + +my $expr = "@ARGV"; +$expr =~ /^\s*($roman_re)\s*($op_re)\s*($roman_re)\s*$/ or die "invalid expression\n"; +my($a, $op, $b) = ($1, $2, $3); +$a = Math::Roman->new($a); +$b = Math::Roman->new($b); +my $num_expr = $a->as_number().$op.$b->as_number(); +my $result = eval($num_expr); +$@ and die "$num_expr: $@"; + +if ($result == 0) { + say "nulla"; +} +elsif (int($result) != $result) { + say "non potest"; +} +elsif ($result < 0) { + say "non potest"; +} +elsif ($result > 3999) { + say "non potest"; +} +else { + say Math::Roman->new($result); +} diff --git a/challenge-227/paulo-custodio/t/test-1.yaml b/challenge-227/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..0e8cdf943b --- /dev/null +++ b/challenge-227/paulo-custodio/t/test-1.yaml @@ -0,0 +1,5 @@ +- setup: + cleanup: + args: 2023 + input: + output: 2 diff --git a/challenge-227/paulo-custodio/t/test-2.yaml b/challenge-227/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..368d7b3582 --- /dev/null +++ b/challenge-227/paulo-custodio/t/test-2.yaml @@ -0,0 +1,45 @@ +- setup: + cleanup: + args: '"IV + V"' + input: + output: IX +- setup: + cleanup: + args: '"M - I"' + input: + output: CMXCIX +- setup: + cleanup: + args: '"X / II"' + input: + output: V +- setup: + cleanup: + args: '"XI * VI"' + input: + output: LXVI +- setup: + cleanup: + args: '"VII ** III"' + input: + output: CCCXLIII +- setup: + cleanup: + args: '"V - V"' + input: + output: nulla +- setup: + cleanup: + args: '"V / II"' + input: + output: non potest +- setup: + cleanup: + args: '"MMM + M"' + input: + output: non potest +- setup: + cleanup: + args: '"V - X"' + input: + output: non potest |
