From 9a38bd2ff080991f73d7f49cebf2fbab06c69d94 Mon Sep 17 00:00:00 2001 From: Dave Jacoby Date: Mon, 24 Jul 2023 16:22:14 -0400 Subject: DAJ ccxxvii --- challenge-227/dave-jacoby/blog.txt | 1 + challenge-227/dave-jacoby/perl/ch-1.pl | 44 ++++++++++++++++++++++++++++++++++ challenge-227/dave-jacoby/perl/ch-2.pl | 38 +++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+) create mode 100644 challenge-227/dave-jacoby/blog.txt create mode 100644 challenge-227/dave-jacoby/perl/ch-1.pl create mode 100644 challenge-227/dave-jacoby/perl/ch-2.pl diff --git a/challenge-227/dave-jacoby/blog.txt b/challenge-227/dave-jacoby/blog.txt new file mode 100644 index 0000000000..0e391a293c --- /dev/null +++ b/challenge-227/dave-jacoby/blog.txt @@ -0,0 +1 @@ +https://jacoby.github.io/2023/07/24/if-sept-seven-why-is-it-the-ninth-month-weekly-challenge-ccxxvii.html diff --git a/challenge-227/dave-jacoby/perl/ch-1.pl b/challenge-227/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..bf3f29e656 --- /dev/null +++ b/challenge-227/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,44 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say postderef signatures state }; + +use DateTime; + +my @examples = ( + 1753, + 1800, + 1900, + 2000, + 2009, + 2023, + 2100, + 3000, + 4000, + 9000, + 9999 +); + +for my $year (@examples) { + my $count = find_friday13s($year); + say <<~"END"; + Input: \$year = $year + Output: $count + END +} + +sub find_friday13s ($year) { + my $count = 0; + my @months; + for my $month ( 1 .. 12 ) { + my $dt = DateTime->now(); + $dt->set_year($year); + $dt->set_day(13); + $dt->set_month($month); + $count++ if $dt->day_of_week == 5; + push @months, $month if $dt->day_of_week == 5; + + } + return $count; +} diff --git a/challenge-227/dave-jacoby/perl/ch-2.pl b/challenge-227/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..5a1c3617f7 --- /dev/null +++ b/challenge-227/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,38 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say postderef signatures state }; + +use Roman; + +my @examples = ( + "IV + V", + "M - I", + "X / II", + "XI * VI", + "VII ** III", + "V - V", + "V / II", + "MMM + M", + "V - X", +); + +for my $e (@examples) { + my $pad = ' ' x (10 - length $e); + my $output = roman_maths($e); + print <<~"END"; + $e $pad => $output + END +} + +sub roman_maths ($equation) { + my ( $first, $expression, $second ) = split /\s+/mx, $equation; + my ( $f, $s ) = map { arabic($_) } $first, $second; + my $arabic = eval( join ' ', $f, $expression, $s ); + my $roman = Roman($arabic); + $roman = undef if $arabic =~ /\./mx; + return $roman if defined $roman && $arabic > 0; + return 'nulla' if $arabic == 0; + return 'non potest' ; +} -- cgit