diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-11-13 19:18:50 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-11-13 19:18:50 +0000 |
| commit | a5bbc536b36e9b53af5479418c2f6761e48a0713 (patch) | |
| tree | 9fae124bd051b7ae0d5a3ec122f27b02bbb91118 | |
| parent | 7d8a06e346c3817502495bf9cb5a86634892dfe1 (diff) | |
| parent | 3c29dae9851f7b85b5e840fedce0d6f5d54905fd (diff) | |
| download | perlweeklychallenge-club-a5bbc536b36e9b53af5479418c2f6761e48a0713.tar.gz perlweeklychallenge-club-a5bbc536b36e9b53af5479418c2f6761e48a0713.tar.bz2 perlweeklychallenge-club-a5bbc536b36e9b53af5479418c2f6761e48a0713.zip | |
Merge pull request #5206 from LubosKolouch/master
Challenge 138 Task 1 2 Perl LK
| -rw-r--r-- | challenge-138/lubos-kolouch/perl/ch-1.pl | 41 | ||||
| -rw-r--r-- | challenge-138/lubos-kolouch/perl/ch-2.pl | 30 | ||||
| -rw-r--r-- | challenge-138/lubos-kolouch/python/ch-1.py | 26 | ||||
| -rw-r--r-- | challenge-138/lubos-kolouch/python/ch-2.py | 21 |
4 files changed, 118 insertions, 0 deletions
diff --git a/challenge-138/lubos-kolouch/perl/ch-1.pl b/challenge-138/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..ca8301e52f --- /dev/null +++ b/challenge-138/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,41 @@ +use strict; +use warnings; +use DateTime; +use List::Util qw/min max/; + +sub get_work_days { + my $what = shift; + + my $dt_1_1 = DateTime->new( + year => $what, + month => 1, + day => 1); + + + my $dt_31_12 = DateTime->new( + year => $what, + month => 12, + day => 31); + + + my $weeks_in_year = $dt_31_12->week_number; + $weeks_in_year += 1 if $dt_1_1->week_number != 1; + + my $result = $weeks_in_year * 5; + + # deduct left over days at the end of the year + $result -= max(5-$dt_31_12->day_of_week, 0); + + # deduct days at the beginning of the year + $result -= min($dt_1_1->day_of_week-1, 5); + + return $result; +} + +use Test::More; + +is(get_work_days(2021), 261); +is(get_work_days(2020), 262); + + +done_testing; diff --git a/challenge-138/lubos-kolouch/perl/ch-2.pl b/challenge-138/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..1579dd7984 --- /dev/null +++ b/challenge-138/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,30 @@ +use strict; +use warnings; +use Algorithm::Combinatorics qw/partitions/; + +sub is_split_number { + my $what = shift; + my $sqr = int(sqrt($what)); + + my @what_arr = split //, $what; + my @what_partitions = partitions(\@what_arr); + + for my $part (@what_partitions) { + my $result = 0; + + for my $item(@$part) { + $result += join '',@$item; + } + + return 1 if $result == $sqr; + } + + return 0; +} + +use Test::More; +is(is_split_number(81), 1); +is(is_split_number(9801), 1); +is(is_split_number(36), 0); + +done_testing; diff --git a/challenge-138/lubos-kolouch/python/ch-1.py b/challenge-138/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..283d35fc55 --- /dev/null +++ b/challenge-138/lubos-kolouch/python/ch-1.py @@ -0,0 +1,26 @@ +import datetime + + +def get_work_days(what: int): + """ Calculate the work days """ + + dt_1_1 = datetime.date(what, 1, 1) + dt_31_12 = datetime.date(what, 12, 31) + + weeks_in_year = dt_31_12.isocalendar()[1] + if dt_1_1.isocalendar()[1] != 1: + weeks_in_year += 1 + + result = weeks_in_year * 5 + + # deduct left over days at the end of the year + result -= max(6 - dt_31_12.weekday() - 1, 0) + + # deduct days at the beginning of the year + result -= min(dt_1_1.weekday() - 1, 6) + + return result + + +assert get_work_days(2021) == 261 +assert get_work_days(2020) == 262 diff --git a/challenge-138/lubos-kolouch/python/ch-2.py b/challenge-138/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..9a2fcbb81a --- /dev/null +++ b/challenge-138/lubos-kolouch/python/ch-2.py @@ -0,0 +1,21 @@ +from math import sqrt +from more_itertools import partitions + + +def is_split_number(what: int): + """ check if the number is split """ + sqr = int(sqrt(what)) + + for comb in partitions(str(what)): + result = 0 + for item in comb: + result += int(''.join(item)) + if result == sqr: + return 1 + + return 0 + + +assert is_split_number(81) == 1 +assert is_split_number(9801) == 1 +assert is_split_number(36) == 0 |
