diff options
| author | Thomas Köhler <jean-luc@picard.franken.de> | 2023-07-24 18:02:57 +0200 |
|---|---|---|
| committer | Thomas Köhler <jean-luc@picard.franken.de> | 2023-07-24 18:02:57 +0200 |
| commit | 7dabef518a49a50cfb199ccc917e85f14f12743a (patch) | |
| tree | a3acdb954e972b8025f6e9c107ae66056a252b3e /challenge-227 | |
| parent | e4bdf5dcb6e741f1fb8e1b145fd2111f05ed6445 (diff) | |
| download | perlweeklychallenge-club-7dabef518a49a50cfb199ccc917e85f14f12743a.tar.gz perlweeklychallenge-club-7dabef518a49a50cfb199ccc917e85f14f12743a.tar.bz2 perlweeklychallenge-club-7dabef518a49a50cfb199ccc917e85f14f12743a.zip | |
Add solution 227
Signed-off-by: Thomas Köhler <jean-luc@picard.franken.de>
Diffstat (limited to 'challenge-227')
| -rw-r--r-- | challenge-227/jeanluc2020/blog-1.txt | 1 | ||||
| -rw-r--r-- | challenge-227/jeanluc2020/blog-2.txt | 1 | ||||
| -rwxr-xr-x | challenge-227/jeanluc2020/perl/ch-1.pl | 50 | ||||
| -rwxr-xr-x | challenge-227/jeanluc2020/perl/ch-2.pl | 64 |
4 files changed, 116 insertions, 0 deletions
diff --git a/challenge-227/jeanluc2020/blog-1.txt b/challenge-227/jeanluc2020/blog-1.txt new file mode 100644 index 0000000000..39a82175a5 --- /dev/null +++ b/challenge-227/jeanluc2020/blog-1.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-227-1.html diff --git a/challenge-227/jeanluc2020/blog-2.txt b/challenge-227/jeanluc2020/blog-2.txt new file mode 100644 index 0000000000..57782e3dad --- /dev/null +++ b/challenge-227/jeanluc2020/blog-2.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-227-2.html diff --git a/challenge-227/jeanluc2020/perl/ch-1.pl b/challenge-227/jeanluc2020/perl/ch-1.pl new file mode 100755 index 0000000000..511da5268c --- /dev/null +++ b/challenge-227/jeanluc2020/perl/ch-1.pl @@ -0,0 +1,50 @@ +#!/usr/bin/perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-227/#TASK1 +# +# Task 1: Friday 13th +# =================== +# +# 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. +# +############################################################ +## +## discussion +## +############################################################ +# +# This is a classic "use a module from CPAN" problem. +# DateTime lets us do all we need here very simple. +# Just create a new DateTime object for the 13th of each +# month of the given year and check whether its weekday is +# Friday. + +use strict; +use warnings; +use DateTime; + +friday_13th(2023); + +sub friday_13th { + my $year = shift; + print "Input: \$year = $year\n"; + my $count = 0; + foreach my $month (1..12) { + my $dt = DateTime->new( + year => $year, + month => $month, + day => 13 + ); + $count++ if $dt->day_of_week == 5; + } + print "Output: $count\n"; +} + diff --git a/challenge-227/jeanluc2020/perl/ch-2.pl b/challenge-227/jeanluc2020/perl/ch-2.pl new file mode 100755 index 0000000000..5a25d434fb --- /dev/null +++ b/challenge-227/jeanluc2020/perl/ch-2.pl @@ -0,0 +1,64 @@ +#!/usr/bin/perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-227/#TASK2 +# +# Task 2: Roman Maths +# =================== +# +# 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) +# +############################################################ +## +## discussion +## +############################################################ +# +# We use the Roman module from CPAN to convert from/to Roman +# numbers. The rest is handling the operand and the special +# cases for the results +# +use strict; +use warnings; +use Roman; + +roman_maths("IV", "+", "V"); +roman_maths("M", "-", "I"); +roman_maths("X", "/", "II"); +roman_maths("XI", "*", "VI"); +roman_maths("VII", "**", "III"); +roman_maths("V", "-", "V"); +roman_maths("V", "/", "II"); +roman_maths("MMM", "+", "M"); +roman_maths("V", "-", "X"); + +sub roman_maths { + my ($num1, $oper, $num2) = @_; + print "Input: $num1 $oper $num2\n"; + $num1 = arabic($num1); + $num2 = arabic($num2); + my $result; + eval "\$result = $num1 $oper $num2;"; + die "eval error $@" if $@; + if($result != int($result)) { + print "Output: non potest\n"; + } elsif ( $result < 0 ) { + print "Output: non potest\n"; + } elsif ( $result > 3999 ) { + print "Output: non potest\n"; + } elsif ( $result == 0) { + print "Output: nulla\n"; + } else { + print "Output: " . uc(roman($result)) . "\n"; + } +} |
