Task 1: Sunday Christmas
Write a script to list dates for Sunday Christmas (25 Dec) between 2019 and 2100.
Example Output
25.12.2022
25.12.2033
25.12.2039
Logic
We loop through the years from 2019 to 2100. For each year, we construct a Date/DateTime object representing December 25th. We then query the day of the week; if it is Sunday (value 7), we print/store the year.
Perl Solution
ch-1.pl
use DateTime;
sub is_sunday_date {
my $year = shift;
my $dt=DateTime->new(
year => $year,
month => 12,
day => 25,);
$dt->day_of_week == 7 ? 1 : 0;
}
Python Solution
ch-1.py
import datetime as dt
def sunday_christmas_years(start: int = 2019, end: int = 2100) -> list[int]:
return [year for year in range(start, end + 1) if dt.date(year, 12, 25).isoweekday() == 7]
Task 2: Triples Summing to 12
Write a script to print all possible series of 3 positive numbers, where in each series at least one of the numbers is even and the sum of the three numbers is always 12.
Example Output
1,2,9
1,3,8
1,4,7
1,5,6
2,3,7
2,4,6
3,4,5
Logic
We search for unique integer triples $(i, j, k)$ such that $i < j < k$, their sum is 12, and at least one of the numbers is even. We can enforce uniqueness and order by using nested loops where the next starting index is always greater than the current index, checking if $k = 12 - i - j$ is greater than $j$, and checking if any elements are divisible by 2.
Perl Solution
ch-2.pl
sub is_ok_triple {
my ( $x, $y, $z ) = @_;
return 0 unless $x + $y + $z == 12;
return 0 unless ( $x % 2 == 0 ) or ( $y % 2 == 0 ) or ( $z % 2 == 0 );
return 1;
}
Python Solution
ch-2.py
def series(total: int) -> list[tuple[int, int, int]]:
result = []
for i in range(1, total + 1):
for j in range(i + 1, total + 1):
k = total - i - j
if k <= j:
continue
if any(n % 2 == 0 for n in (i, j, k)):
result.append((i, j, k))
return result