diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-10-24 00:52:45 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-10-24 00:52:45 +0100 |
| commit | 5d32b8acf31595bad34bbcebd9451b986af9edea (patch) | |
| tree | 6fc4f895b915bb3c39d39e5982e7af43cc2d7cb5 | |
| parent | 7571ef68a4764f5f6bfd9eb3f50612a72996b210 (diff) | |
| parent | 3372aa59aa755e499917b0cb31b4d385c30f1ae1 (diff) | |
| download | perlweeklychallenge-club-5d32b8acf31595bad34bbcebd9451b986af9edea.tar.gz perlweeklychallenge-club-5d32b8acf31595bad34bbcebd9451b986af9edea.tar.bz2 perlweeklychallenge-club-5d32b8acf31595bad34bbcebd9451b986af9edea.zip | |
Merge pull request #6947 from adamcrussell/challenge-187
initial commit
| -rw-r--r-- | challenge-187/adam-russell/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-187/adam-russell/blog1.txt | 1 | ||||
| -rw-r--r-- | challenge-187/adam-russell/perl/ch-1.pl | 48 | ||||
| -rw-r--r-- | challenge-187/adam-russell/perl/ch-2.pl | 38 | ||||
| -rw-r--r-- | challenge-187/adam-russell/prolog/ch-2.p | 10 |
5 files changed, 98 insertions, 0 deletions
diff --git a/challenge-187/adam-russell/blog.txt b/challenge-187/adam-russell/blog.txt new file mode 100644 index 0000000000..5cc1e4bcea --- /dev/null +++ b/challenge-187/adam-russell/blog.txt @@ -0,0 +1 @@ +http://www.rabbitfarm.com/cgi-bin/blosxom/perl/2022/10/23
\ No newline at end of file diff --git a/challenge-187/adam-russell/blog1.txt b/challenge-187/adam-russell/blog1.txt new file mode 100644 index 0000000000..5abed41bec --- /dev/null +++ b/challenge-187/adam-russell/blog1.txt @@ -0,0 +1 @@ +http://www.rabbitfarm.com/cgi-bin/blosxom/prolog/2022/10/23
\ No newline at end of file diff --git a/challenge-187/adam-russell/perl/ch-1.pl b/challenge-187/adam-russell/perl/ch-1.pl new file mode 100644 index 0000000000..bf21b2369c --- /dev/null +++ b/challenge-187/adam-russell/perl/ch-1.pl @@ -0,0 +1,48 @@ +use v5.36; +use strict; +use warnings; +## +# Two friends, Foo and Bar gone on holidays seperately to the same city. You are given their schedule i.e. start date and end date. +# To keep the task simple, the date is in the form DD-MM and all dates belong to the same calendar year i.e. between 01-01 and 31-12. +# Also the year is non-leap year and both dates are inclusive. +# Write a script to find out for the given schedule, how many days they spent together in the city, if at all. +## +use Time::Piece; +use Time::Seconds; + +sub days_together{ + my($together) = @_; + my $days_together = 0; + my($start, $end); + my $foo_start = Time::Piece->strptime($together->{Foo}->{SD}, q/%d-%m/); + my $bar_start = Time::Piece->strptime($together->{Bar}->{SD}, q/%d-%m/); + my $foo_end = Time::Piece->strptime($together->{Foo}->{ED}, q/%d-%m/); + my $bar_end = Time::Piece->strptime($together->{Bar}->{ED}, q/%d-%m/); + $start = $foo_start; + $start = $bar_start if $bar_start > $foo_start; + $end = $foo_end; + $end = $bar_end if $bar_end < $foo_end; + { + $days_together++ if $start <= $end; + $start += ONE_DAY; + redo if $start <= $end; + } + return $days_together; +} + + +MAIN:{ + my $days; + $days = days_together({Foo => {SD => q/12-01/, ED => q/20-01/}, + Bar => {SD => q/15-01/, ED => q/18-01/}}); + say $days; + $days = days_together({Foo => {SD => q/02-03/, ED => q/12-03/}, + Bar => {SD => q/13-03/, ED => q/14-03/}}); + say $days; + $days = days_together({Foo => {SD => q/02-03/, ED => q/12-03/}, + Bar => {SD => q/11-03/, ED => q/15-03/}}); + say $days; + $days = days_together({Foo => {SD => q/30-03/, ED => q/05-04/}, + Bar => {SD => q/28-03/, ED => q/02-04/}}); + say $days; +} diff --git a/challenge-187/adam-russell/perl/ch-2.pl b/challenge-187/adam-russell/perl/ch-2.pl new file mode 100644 index 0000000000..79bedcce41 --- /dev/null +++ b/challenge-187/adam-russell/perl/ch-2.pl @@ -0,0 +1,38 @@ +use v5.36; +use strict; +use warnings; +## +# You are given a list of positive numbers, @n, having at least 3 numbers. +# Write a script to find the triplets (a, b, c) from the given list that satisfies +# the following rules. +# a + b > c +# b + c > a +# a + c > b +# a + b + c is maximum. +## +use Hash::MultiKey; +use Math::Combinatorics; + +sub magical_triples{ + my(@numbers) = @_; + my %triple_sum; + tie %triple_sum, q/Hash::MultiKey/; + my $combinations = Math::Combinatorics->new(count => 3, data => [@numbers]); + my($s, $t, $u); + while(my @combination = $combinations->next_combination()){ + my($s, $t, $u) = @combination; + my $sum; + $sum = $s + $t + $u if $s + $t > $u && $t + $u > $s && $s + $u > $t; + $triple_sum{[$s, $t, $u]} = $sum if $sum; + } + my @triples_sorted = sort {$triple_sum{$b} <=> $triple_sum{$a}} keys %triple_sum; + return ($triples_sorted[0]->[0], $triples_sorted[0]->[1], $triples_sorted[0]->[2]) if @triples_sorted; + return (); +} + +MAIN:{ + say "(" . join(", ", magical_triples(1, 2, 3, 2)) . ")"; + say "(" . join(", ", magical_triples(1, 3, 2)) . ")"; + say "(" . join(", ", magical_triples(1, 1, 2, 3)) . ")"; + say "(" . join(", ", magical_triples(2, 4, 3)) . ")"; +}
\ No newline at end of file diff --git a/challenge-187/adam-russell/prolog/ch-2.p b/challenge-187/adam-russell/prolog/ch-2.p new file mode 100644 index 0000000000..a7c0c86c0b --- /dev/null +++ b/challenge-187/adam-russell/prolog/ch-2.p @@ -0,0 +1,10 @@ +magical_triple_sum(Numbers, Triple, TripleSum):- + sublist([A, B, C], Numbers), + A + B > C, + B + C > A, + A + C > B, + Triple = [A, B, C], + sum_list(Triple, TripleSum). + +magical_triple(Numbers, Triple):- + fd_maximize(magical_triple_sum(Numbers, Triple, TripleSum), TripleSum).
\ No newline at end of file |
