diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-10-21 18:02:49 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-10-21 18:02:49 +0100 |
| commit | 6f040443c022eda8d7e10afe871d6b79acd1dced (patch) | |
| tree | 0cf29a24645ce5a9cac503b294da1d338d63cbd9 | |
| parent | bf5e9e280452e2d4691e548d75ac3a902130af9f (diff) | |
| parent | f80c4a9204af27e6e37e1a7f726a914c45ea06e8 (diff) | |
| download | perlweeklychallenge-club-6f040443c022eda8d7e10afe871d6b79acd1dced.tar.gz perlweeklychallenge-club-6f040443c022eda8d7e10afe871d6b79acd1dced.tar.bz2 perlweeklychallenge-club-6f040443c022eda8d7e10afe871d6b79acd1dced.zip | |
Merge pull request #6938 from steve-g-lynn/branch-for-challenge-187
pwc 187
| -rw-r--r-- | challenge-187/steve-g-lynn/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-187/steve-g-lynn/julia/ch-1.jl | 42 | ||||
| -rwxr-xr-x | challenge-187/steve-g-lynn/julia/ch-2.jl | 33 | ||||
| -rwxr-xr-x | challenge-187/steve-g-lynn/perl/ch-1.pl | 60 | ||||
| -rwxr-xr-x | challenge-187/steve-g-lynn/perl/ch-2.pl | 36 | ||||
| -rwxr-xr-x | challenge-187/steve-g-lynn/raku/ch-1.p6 | 42 | ||||
| -rwxr-xr-x | challenge-187/steve-g-lynn/raku/ch-2.p6 | 31 |
7 files changed, 245 insertions, 0 deletions
diff --git a/challenge-187/steve-g-lynn/blog.txt b/challenge-187/steve-g-lynn/blog.txt new file mode 100644 index 0000000000..09683b80b6 --- /dev/null +++ b/challenge-187/steve-g-lynn/blog.txt @@ -0,0 +1 @@ +https://thiujiac.blogspot.com/2022/10/pwc-187.html diff --git a/challenge-187/steve-g-lynn/julia/ch-1.jl b/challenge-187/steve-g-lynn/julia/ch-1.jl new file mode 100755 index 0000000000..04bc8407b7 --- /dev/null +++ b/challenge-187/steve-g-lynn/julia/ch-1.jl @@ -0,0 +1,42 @@ +#!/usr/bin/env julia + +using Dates + +function parse_date(str::String) ::Date + day_mth=split(str,"-") + return Date(2022,parse(Int64,day_mth[2]),parse(Int64,day_mth[1])) +end + +function days_together(foo_sd::String, foo_ed::String, bar_sd::String, bar_ed::String) ::Day + + # Get later start date + if parse_date(foo_sd) > parse_date(bar_sd) + later_sd = parse_date(foo_sd) + else + later_sd = parse_date(bar_sd) + end + + # get earlier end date + if parse_date(foo_ed) > parse_date(bar_ed) + earlier_ed = parse_date(bar_ed) + else + earlier_ed = parse_date(foo_ed) + end + + #-- get date difference + if later_sd > earlier_ed + return Day(0) + else + return earlier_ed - later_sd + Day(1) + end +end + +println( days_together("12-01","20-01","15-01","18-01") ) +#4 days +println( days_together("02-03","12-03","13-03","14-03") ) +#0 days +println( days_together("02-03","12-03","11-03","15-03") ) +#2 days +println( days_together("30-03","05-04","20-03","02-04") ) +#4 days + diff --git a/challenge-187/steve-g-lynn/julia/ch-2.jl b/challenge-187/steve-g-lynn/julia/ch-2.jl new file mode 100755 index 0000000000..904f0b820d --- /dev/null +++ b/challenge-187/steve-g-lynn/julia/ch-2.jl @@ -0,0 +1,33 @@ +#!/usr/bin/env julia + +function magical_triplet(n::Vector{Float64}) ::Vector{Float64} + + + #-- find the three biggest elements (Their sum is max) + candidate = reverse(sort(n))[1:3] + + #-- test if any element is >= than the sum of the other two + mysum=sum(candidate) + for i in candidate + if (mysum - i <= i) + return Float64[] + end + end + + #-- if no element is >= the sum of the other two, we are through. + return candidate +end + + +println(magical_triplet([1.,2.,3.,2.])) +#[3.0, 2.0, 2.0] + +println(magical_triplet([1.,3.,2.])) +#Float64[] + +println(magical_triplet([1.,1.,2.,3.])) +#Float64[] + +println(magical_triplet([2.,4.,3.])) +#[4.0, 3.0, 2.0] + diff --git a/challenge-187/steve-g-lynn/perl/ch-1.pl b/challenge-187/steve-g-lynn/perl/ch-1.pl new file mode 100755 index 0000000000..69777dffae --- /dev/null +++ b/challenge-187/steve-g-lynn/perl/ch-1.pl @@ -0,0 +1,60 @@ +#!/usr/bin/env perl +use strict; +use warnings; + +use Date::Calc qw(Delta_Days); + +print &days_together('12-01','20-01','15-01','18-01'),"\n"; +#4 +print &days_together('02-03','12-03','13-03','14-03'),"\n"; +#0 +print &days_together('02-03','12-03','11-03','15-03'),"\n"; +#2 +print &days_together('30-03','05-04','28-03','02-04'),"\n"; +#4 + +sub days_together { + my ($foo_sd, $foo_ed, $bar_sd, $bar_ed)=@_; + + #-- get the later start date + my @later_start = &later( + &parse_datestring( $foo_sd ), + &parse_datestring( $bar_sd ) + ); + + #-- get the earlier end date + my @earlier_end = &earlier( + &parse_datestring( $foo_ed ), + &parse_datestring( $bar_ed ) + ); + + #-- get time between later start date and earlier end date + #-- add 1 to Delta_Days output to include both dates + #-- return 0 if the time is negative + + my $retval=Delta_Days(2022,@later_start,2022,@earlier_end); + ($retval >= 0) ? + (return $retval+1) : + (return 0); +} + +sub parse_datestring { + my ($datestring)=@_; + my ($day,$month)=split(/-/,$datestring); + return ($month,$day); +} + +sub earlier { + my ($month1, $day1, $month2, $day2) = @_; + (Delta_Days(2022,$month1,$day1,2022,$month2,$day2) > 0) ? + (return ($month1,$day1)) : + (return ($month2,$day2)); +} + +sub later { + my ($month1, $day1, $month2, $day2) = @_; + (Delta_Days(2022,$month1,$day1,2022,$month2,$day2) > 0) ? + (return ($month2,$day2)) : + (return ($month1,$day1)); +} + diff --git a/challenge-187/steve-g-lynn/perl/ch-2.pl b/challenge-187/steve-g-lynn/perl/ch-2.pl new file mode 100755 index 0000000000..d359124f3a --- /dev/null +++ b/challenge-187/steve-g-lynn/perl/ch-2.pl @@ -0,0 +1,36 @@ +#!/usr/bin/env perl + +use List::Util qw(sum); + +&print_list(&magical_triplet(1,2,3,2)); +#3 2 2 + +&print_list(&magical_triplet(1,3,2)); +# + +&print_list(&magical_triplet(1,1,2,3)); +# + +&print_list(&magical_triplet(2,4,3)); +#4,3,2 + + + +sub magical_triplet(@n){ + + + #-- find the three biggest elements (Their sum is max) + my @candidate = (sort {$b <=> $a} @_)[0 .. 2]; + + #-- test if any element is >= than the sum of the other two + my $sum=sum @candidate; + for my $i (@candidate) { ($i >= ($sum-$i)) && (return ()) } + + #-- if no element is >= the sum of the other two, we are through. + return @candidate; +} + +sub print_list { + print "@_\n"; +} + diff --git a/challenge-187/steve-g-lynn/raku/ch-1.p6 b/challenge-187/steve-g-lynn/raku/ch-1.p6 new file mode 100755 index 0000000000..cde3cb539d --- /dev/null +++ b/challenge-187/steve-g-lynn/raku/ch-1.p6 @@ -0,0 +1,42 @@ +#!/usr/bin/env perl6 + + +say &days_together('12-01','20-01','15-01','18-01'); +#4 +say &days_together('02-03','12-03','13-03','14-03'); +#0 +say &days_together('02-03','12-03','11-03','15-03'); +#2 +say &days_together('30-03','05-04','28-03','02-04'); +#4 + +sub days_together ($foo_sd, $foo_ed, $bar_sd, $bar_ed) { + + #-- get the later start date + my $later-sd = ( + (&parse-date($foo_sd) > &parse-date($bar_sd)) ?? + &parse-date($foo_sd) !! + &parse-date($bar_sd); + ); + #-- get the earlier end date + my $earlier-ed = ( + (&parse-date($foo_ed) > &parse-date($bar_ed)) ?? + &parse-date($bar_ed) !! + &parse-date($foo_ed); + ); + + #-- get time between later start date and earlier end date + #-- add 1 to Raku date difference to include both dates + #-- return 0 if the time is negative + + my $retval= $earlier-ed - $later-sd; + ($retval >= 0) ?? + (return $retval+1) !! + (return 0); +} + +sub parse-date( $datestring ) { + my ($day,$month)=split('-',$datestring); + return Date.new(2022,$month,$day); +} + diff --git a/challenge-187/steve-g-lynn/raku/ch-2.p6 b/challenge-187/steve-g-lynn/raku/ch-2.p6 new file mode 100755 index 0000000000..1dcd07a065 --- /dev/null +++ b/challenge-187/steve-g-lynn/raku/ch-2.p6 @@ -0,0 +1,31 @@ +#!/usr/bin/env perl6 + + +say &magical_triplet((1,2,3,2)); +#[3 2 2] + +say &magical_triplet((1,3,2)); +#() + +say &magical_triplet((1,1,2,3)); +#() + +say &magical_triplet((2,4,3)); +#(4,3,2) + + + +sub magical_triplet(@n){ + + #-- find the three biggest elements (Their sum is max) + my @candidate = @n.sort({$^b > $^a}).head(3); + + #-- test if any element is >= than the sum of the other two + my $sum=@candidate.sum; + for (@candidate) -> $i { ($i >= ($sum-$i)) && (return ()) } + + #-- if no element is >= the sum of the other two, we are through. + return @candidate; +} + + |
