Task1
In the first task we must count the number of Friday 13ths and we can do so by iterating months of the given year and checking if the 13th day of each month is a Friday and increment the count. We can use the Time::Piece module for this purpose:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #!/usr/bin/env perl use strict; use warnings; use Time::Piece; sub friday_13th{ my ($count,$year) = (0,@_); foreach my $month(1..12){ my $d = Time::Piece->strptime("$year-$month-13", "%Y-%m-%d"); $count++ if $d->day_of_week == 5; } $count } printf "%d\n", friday_13th(2023); |
Task2
For the second task we are asked to do 2-term arithmetic with Roman numerals. We can also use the Roman or Math::Roman modules of cpan for this purpose:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #!/usr/bin/env perl use strict; use warnings; use Roman; sub roman_maths { my ($n1,$n2,$r) = (arabic($_[0]),arabic($_[2])); eval "\$r = $n1 $_[1] $n2;"; die "Error: $@" if $@; if($r != int($r) || $r < 0 || $r > 3999){return "non potest"} elsif($r == 0){return "nulla"} uc(roman($r)); } printf "%s\n", roman_maths('IV','+','V'); printf "%s\n", roman_maths('M','-','I'); printf "%s\n", roman_maths('X','/','II'); printf "%s\n", roman_maths('XI','*','VI'); printf "%s\n", roman_maths('VII','**','III'); printf "%s\n", roman_maths('V','-','V'); printf "%s\n", roman_maths('V','/','II'); printf "%s\n", roman_maths('MMM','+','M'); printf "%s\n", roman_maths('V','-','X'); |
No comments:
Post a Comment