Robbie Hatley's Solutions To The Weekly Challenge #227

For those not familiar with "The Weekly Challenge", it is a weekly programming puzzle, usually with two parts, cycling every Sunday. You can find it here:

The Weekly Challenge

This week (2023-07-23 through 2023-07-29) is weekly challenge #227.

Task 1 is as follows:

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, 
assuming 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.

Let's use the number 0-6 to stand for days-of-week from a Sunday through the next Saturday. Because we're only interested in years 1753+, lets use 13 December 1752 as "epoch". That was a Wednesday, so our starting offset is 3. Then make an array to hold elements indexed from 1753 through 9999 (representing the years), with element being a ref to an array of elements indexed 0 through 11 (representing the months), with each inner element being "starting offset plus days elapsed from epoch to the 13th of the current month". The starting offset 3 only has to be added once, to the entry for 13 January 1753; every subsequent month from February 1753 through December 9999 is just the number from the previous month plus the number of days in the previous month. Then to get the "Friday The 13th"s for each year, just take each month's number modulo 7; each 5 is a Friday The 13th:

Robbie Hatley's Solution to The Weekly Challenge 227-1

Task 2 is as follows:

Task 2: Roman Maths
Submitted by: Peter Campbell Smith
Write a script to handle a 2-term arithmetic operation expressed in 
Roman numerals.
Examples:
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)

I'm sooooo not going to make roman-to-arabic or arabic-to-roman converters. Let's see what CPAN has to offer. Ah, it has "Text::Roman". Fine I'll use that. The rest is just expression validity checking, operator parsing, Latin output phrasing ("nulla" or "non potest" where appropriate), and other such details:

Robbie Hatley's Solution to The Weekly Challenge 227-2

That's it for 227; see you on 228!

Comments

Popular posts from this blog

Robbie Hatley's Solutions To The Weekly Challenge #221

Robbie Hatley's Solutions To The Weekly Challenge #239

Robbie Hatley's Solutions To The Weekly Challenge #262