diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-10-03 12:20:37 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-10-03 12:20:37 +0100 |
| commit | 100b1847395389d1f7fcc80957f463208f5f25e1 (patch) | |
| tree | 5f2f09d375238338c566d5cc80e568b4df7f4a7b | |
| parent | 67d6c85ec412b5c79e17237f421d85e033ddf871 (diff) | |
| parent | 9aefa08d8034cd60341e0b551089ec34dda7fdca (diff) | |
| download | perlweeklychallenge-club-100b1847395389d1f7fcc80957f463208f5f25e1.tar.gz perlweeklychallenge-club-100b1847395389d1f7fcc80957f463208f5f25e1.tar.bz2 perlweeklychallenge-club-100b1847395389d1f7fcc80957f463208f5f25e1.zip | |
Merge pull request #8806 from jeanluc2020/jeanluc-237
Add solution 237
| -rw-r--r-- | challenge-237/jeanluc2020/blog-1.txt | 1 | ||||
| -rw-r--r-- | challenge-237/jeanluc2020/blog-2.txt | 1 | ||||
| -rwxr-xr-x | challenge-237/jeanluc2020/perl/ch-1.pl | 77 | ||||
| -rwxr-xr-x | challenge-237/jeanluc2020/perl/ch-2.pl | 72 | ||||
| -rwxr-xr-x | challenge-237/jeanluc2020/python/ch-1.py | 75 | ||||
| -rwxr-xr-x | challenge-237/jeanluc2020/python/ch-2.py | 65 |
6 files changed, 291 insertions, 0 deletions
diff --git a/challenge-237/jeanluc2020/blog-1.txt b/challenge-237/jeanluc2020/blog-1.txt new file mode 100644 index 0000000000..81a41b2d17 --- /dev/null +++ b/challenge-237/jeanluc2020/blog-1.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-237-1.html diff --git a/challenge-237/jeanluc2020/blog-2.txt b/challenge-237/jeanluc2020/blog-2.txt new file mode 100644 index 0000000000..7505109c69 --- /dev/null +++ b/challenge-237/jeanluc2020/blog-2.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-237-2.html diff --git a/challenge-237/jeanluc2020/perl/ch-1.pl b/challenge-237/jeanluc2020/perl/ch-1.pl new file mode 100755 index 0000000000..e7a618731a --- /dev/null +++ b/challenge-237/jeanluc2020/perl/ch-1.pl @@ -0,0 +1,77 @@ +#!/usr/bin/perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-237/#TASK1 +# +# Task 1: Seize The Day +# ===================== +# +# Given a year, a month, a weekday of month, and a day of week (1 (Mon) .. 7 (Sun)), print the day. +# +## Example 1 +## +## Input: Year = 2024, Month = 4, Weekday of month = 3, day of week = 2 +## Output: 16 +## +## The 3rd Tue of Apr 2024 is the 16th +# +## Example 2 +## +## Input: Year = 2025, Month = 10, Weekday of month = 2, day of week = 4 +## Output: 9 +## +## The 2nd Thu of Oct 2025 is the 9th +# +## Example 3 +## +## Input: Year = 2026, Month = 8, Weekday of month = 5, day of week = 3 +## Output: 0 +## +## There isn't a 5th Wed in Aug 2026 +# +############################################################ +## +## discussion +## +############################################################ +# +# This is a case for the DateTime module: +# - First we calculate the day of week for the 1st of the given month by +# creating a DateTime object +# - Then we calculate the first day of the month that matches our target +# day of week +# - From that, we calculate the weekday of month's date by adding another +# 7 for each week we're out from the first week +# - If we went past the end of the month, we return 0, otherwise the +# calculated date + +use strict; +use warnings; +use DateTime; + + +seize_the_day(2024, 4, 3, 2); +seize_the_day(2025, 10, 2, 4); +seize_the_day(2026, 8, 5, 3); + +sub seize_the_day { + my ($year, $month, $weekday_of_month, $day_of_week) = @_; + print "Input: Year = $year, month = $month, weekday of month = $weekday_of_month, day of week = $day_of_week\n"; + if($weekday_of_month < 1 or $weekday_of_month > 5) { + print "Output: 0\n"; + return; + } + my $dt = DateTime->new( year => $year, month => $month, day => 1 ); + my $days_per_month = { + 1 => 31, 2 => $dt->is_leap_year ? 29 : 28, + 3 => 31, 4 => 30, 5 => 31, 6 => 30, 7 => 31, + 8 => 31, 9 => 30, 10 => 31, 11 => 30, 12 => 31 + }; + my $dow_1st = $dt->day_of_week; + my $first_appearance_of_dow = $day_of_week >= $dow_1st ? ( $day_of_week - $dow_1st + 1 ) : ( $day_of_week - $dow_1st + 8); + my $nth_appearance_of_dow = $first_appearance_of_dow + ($weekday_of_month-1) * 7; + if($nth_appearance_of_dow > $days_per_month->{$month}) { + print "Output: 0\n"; + return; + } + print "Output: $nth_appearance_of_dow\n"; +} + diff --git a/challenge-237/jeanluc2020/perl/ch-2.pl b/challenge-237/jeanluc2020/perl/ch-2.pl new file mode 100755 index 0000000000..788060c5a9 --- /dev/null +++ b/challenge-237/jeanluc2020/perl/ch-2.pl @@ -0,0 +1,72 @@ +#!/usr/bin/perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-237/#TASK2 +# +# Task 2: Maximise Greatness +# ========================== +# +# You are given an array of integers. +# +# Write a script to permute the given array such that you get the maximum +# possible greatness. +# +### To determine greatness, nums[i] < perm[i] where 0 <= i < nums.length +# +## Example 1 +## +## Input: @nums = (1, 3, 5, 2, 1, 3, 1) +## Output: 4 +## +## One possible permutation: (2, 5, 1, 3, 3, 1, 1) which returns 4 greatness as below: +## nums[0] < perm[0] +## nums[1] < perm[1] +## nums[3] < perm[3] +## nums[4] < perm[4] +# +## Example 2 +## +## Input: @ints = (1, 2, 3, 4) +## Output: 3 +## +## One possible permutation: (2, 3, 4, 1) which returns 3 greatness as below: +## nums[0] < perm[0] +## nums[1] < perm[1] +## nums[2] < perm[2] +# +############################################################ +## +## discussion +## +############################################################ +# +# Calculate all permutations and check the greatness for each permutation +# Keep the maximum + +use strict; +use warnings; +use Algorithm::Permute; + +maximise_greatness(1, 3, 5, 2, 1, 3, 1); +maximise_greatness(1, 2, 3, 4); + +sub maximise_greatness { + my @ints = @_; + print "Input: (" . join(", ", @ints) . ")\n"; + my $max = 0; + my $p_iterator = Algorithm::Permute->new ( \@ints ); + while(my @perm = $p_iterator->next) { + my $current = greatness( [@_], [@perm] ); + $max = $current if $current > $max; + } + print "Output: $max\n"; +} + +sub greatness { + my ($nums, $perm) = @_; + my @ints = @$nums; + my $greatness = 0; + foreach my $i (0..$#ints) { + $greatness++ if $nums->[$i] < $perm->[$i]; + } + return $greatness; +} + diff --git a/challenge-237/jeanluc2020/python/ch-1.py b/challenge-237/jeanluc2020/python/ch-1.py new file mode 100755 index 0000000000..c8d33b1687 --- /dev/null +++ b/challenge-237/jeanluc2020/python/ch-1.py @@ -0,0 +1,75 @@ +#!/usr/bin/python3 +# https://theweeklychallenge.org/blog/perl-weekly-challenge-237/#TASK1 +# +# Task 1: Seize The Day +# ===================== +# +# Given a year, a month, a weekday of month, and a day of week (1 (Mon) .. 7 (Sun)), print the day. +# +## Example 1 +## +## Input: Year = 2024, Month = 4, Weekday of month = 3, day of week = 2 +## Output: 16 +## +## The 3rd Tue of Apr 2024 is the 16th +# +## Example 2 +## +## Input: Year = 2025, Month = 10, Weekday of month = 2, day of week = 4 +## Output: 9 +## +## The 2nd Thu of Oct 2025 is the 9th +# +## Example 3 +## +## Input: Year = 2026, Month = 8, Weekday of month = 5, day of week = 3 +## Output: 0 +## +## There isn't a 5th Wed in Aug 2026 +# +############################################################ +## +## discussion +## +############################################################ +# +# This is a case for the datetime module: +# - First we calculate the day of week for the 1st of the given month by +# creating a DateTime object +# - Then we calculate the first day of the month that matches our target +# day of week +# - From that, we calculate the weekday of month's date by adding another +# 7 for each week we're out from the first week +# - If we went past the end of the month, we return 0, otherwise the +# calculated date + +from datetime import date +import calendar + +def seize_the_day(year: int, month: int, weekday_of_month: int, day_of_week: int): + print(f"Input: Year = {year}, month = {month}, weekday of month = {weekday_of_month}, day of week = {day_of_week}") + if weekday_of_month < 1 or weekday_of_month > 5: + print("Output: 0") + return + dtformat = "{year:4d}-{month:02d}-{day:02d}" + dt = date.fromisoformat(dtformat.format(year = year, month = month, day = 1)) + days_per_month = { 1:31, 2:28, 3:31, 4:30, 5:31, 6:30, 7:31, 8:31, 9:30, 10:31, 11:30, 12:31 } + if calendar.isleap(year): + days_per_month[2] = 29 + + dow_1st = dt.isoweekday() + first_appearance_of_dow = day_of_week - dow_1st +1 + if day_of_week < dow_1st: + first_appearance_of_dow += 7 + nth_appearance_of_dow = first_appearance_of_dow + (weekday_of_month-1) * 7 + if nth_appearance_of_dow > days_per_month[month]: + print("Output: 0") + return + print(f"Output: {nth_appearance_of_dow}") + + +seize_the_day(year = 2024, month = 4, weekday_of_month = 3, day_of_week = 2); +seize_the_day(year = 2025, month = 10, weekday_of_month = 2, day_of_week = 4); +seize_the_day(year = 2026, month = 8, weekday_of_month = 5, day_of_week = 3); + + diff --git a/challenge-237/jeanluc2020/python/ch-2.py b/challenge-237/jeanluc2020/python/ch-2.py new file mode 100755 index 0000000000..0125a8b508 --- /dev/null +++ b/challenge-237/jeanluc2020/python/ch-2.py @@ -0,0 +1,65 @@ +#!/usr/bin/python3 +# https://theweeklychallenge.org/blog/perl-weekly-challenge-237/#TASK2 +# +# Task 2: Maximise Greatness +# ========================== +# +# You are given an array of integers. +# +# Write a script to permute the given array such that you get the maximum +# possible greatness. +# +### To determine greatness, nums[i] < perm[i] where 0 <= i < nums.length +# +## Example 1 +## +## Input: @nums = (1, 3, 5, 2, 1, 3, 1) +## Output: 4 +## +## One possible permutation: (2, 5, 1, 3, 3, 1, 1) which returns 4 greatness as below: +## nums[0] < perm[0] +## nums[1] < perm[1] +## nums[3] < perm[3] +## nums[4] < perm[4] +# +## Example 2 +## +## Input: @ints = (1, 2, 3, 4) +## Output: 3 +## +## One possible permutation: (2, 3, 4, 1) which returns 3 greatness as below: +## nums[0] < perm[0] +## nums[1] < perm[1] +## nums[2] < perm[2] +# +############################################################ +## +## discussion +## +############################################################ +# +# Calculate all permutations and check the greatness for each permutation +# Keep the maximum + +from itertools import permutations + +def greatness(nums: list, perm: list) -> int: + greatness = 0 + indices = list(range(len(nums))) + for i in indices: + if nums[i] < perm[i]: + greatness+=1 + return greatness + +def maximise_greatness(ints: list): + print("Input: (" + ', '.join(str(x) for x in ints) + ")") + max = 0 + for perm in permutations(ints): + current = greatness(ints, perm) + if current > max: + max = current + print(f"Output: {max}") + + +maximise_greatness([1, 3, 5, 2, 1, 3, 1]) +maximise_greatness([1, 2, 3, 4]) |
