diff options
| author | Lubos Kolouch <lubos@kolouch.net> | 2023-08-05 18:57:07 +0200 |
|---|---|---|
| committer | Lubos Kolouch <lubos@kolouch.net> | 2023-08-05 18:57:07 +0200 |
| commit | 6d835b28e1d4e4323bdafaac2c8bf1b5730efd0f (patch) | |
| tree | a76586a8f3cfc2f077e50c2eb35e51293f324c3f | |
| parent | 68326ff7ce1e1b759ff70a63f50497f855c3b432 (diff) | |
| download | perlweeklychallenge-club-6d835b28e1d4e4323bdafaac2c8bf1b5730efd0f.tar.gz perlweeklychallenge-club-6d835b28e1d4e4323bdafaac2c8bf1b5730efd0f.tar.bz2 perlweeklychallenge-club-6d835b28e1d4e4323bdafaac2c8bf1b5730efd0f.zip | |
feat(challenge-187/lubos-kolouch/perl,python/): Challenge 187 LK Perl Python
| -rw-r--r-- | challenge-187/lubos-kolouch/perl/ch-1.pl | 30 | ||||
| -rw-r--r-- | challenge-187/lubos-kolouch/perl/ch-2.pl | 18 | ||||
| -rw-r--r-- | challenge-187/lubos-kolouch/python/ch-1.py | 27 | ||||
| -rw-r--r-- | challenge-187/lubos-kolouch/python/ch-2.py | 16 |
4 files changed, 91 insertions, 0 deletions
diff --git a/challenge-187/lubos-kolouch/perl/ch-1.pl b/challenge-187/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..49860b7ac3 --- /dev/null +++ b/challenge-187/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,30 @@ +use strict; +use warnings; +use DateTime::Format::Strptime; + +sub days_together { + my ( $foo_start, $foo_end, $bar_start, $bar_end ) = @_; + + my $strp = DateTime::Format::Strptime->new( + pattern => '%d-%m', + on_error => 'croak', + ); + + my $foo_start_date = $strp->parse_datetime($foo_start); + my $foo_end_date = $strp->parse_datetime($foo_end); + my $bar_start_date = $strp->parse_datetime($bar_start); + my $bar_end_date = $strp->parse_datetime($bar_end); + + my $start = + $foo_start_date > $bar_start_date ? $foo_start_date : $bar_start_date; + my $end = $foo_end_date < $bar_end_date ? $foo_end_date : $bar_end_date; + + my $overlap = $end->delta_days($start)->in_units('days') + 1; + + return $overlap > 0 ? $overlap : 0; +} + +print days_together( '12-01', '20-01', '15-01', '18-01' ), "\n"; # Output: 4 +print days_together( '02-03', '12-03', '13-03', '14-03' ), "\n"; # Output: 0 +print days_together( '02-03', '12-03', '11-03', '15-03' ), "\n"; # Output: 2 +print days_together( '30-03', '05-04', '28-03', '02-04' ), "\n"; # Output: 4 diff --git a/challenge-187/lubos-kolouch/perl/ch-2.pl b/challenge-187/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..1b46a1e0e5 --- /dev/null +++ b/challenge-187/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,18 @@ +use strict; +use warnings; + +sub find_triplet { + my @arr = @_; + @arr = sort { $b <=> $a } @arr; + for my $i ( 0 .. $#arr - 2 ) { + if ( $arr[$i] < $arr[ $i + 1 ] + $arr[ $i + 2 ] ) { + return ( $arr[$i], $arr[ $i + 1 ], $arr[ $i + 2 ] ); + } + } + return (); +} + +print join( ", ", find_triplet( 1, 2, 3, 2 ) ), "\n"; # Output: 3, 2, 2 +print join( ", ", find_triplet( 1, 3, 2 ) ), "\n"; # Output: +print join( ", ", find_triplet( 1, 1, 2, 3 ) ), "\n"; # Output: +print join( ", ", find_triplet( 2, 4, 3 ) ), "\n"; # Output: 4, 3, 2 diff --git a/challenge-187/lubos-kolouch/python/ch-1.py b/challenge-187/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..a5a7f36de4 --- /dev/null +++ b/challenge-187/lubos-kolouch/python/ch-1.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from datetime import datetime + + +def days_together(foo_start, foo_end, bar_start, bar_end): + # Convert strings to datetime objects + foo_start_date = datetime.strptime(foo_start, "%d-%m") + foo_end_date = datetime.strptime(foo_end, "%d-%m") + bar_start_date = datetime.strptime(bar_start, "%d-%m") + bar_end_date = datetime.strptime(bar_end, "%d-%m") + + # Calculate the overlapping dates + start = max(foo_start_date, bar_start_date) + end = min(foo_end_date, bar_end_date) + + # Calculate the number of overlapping days + overlapping_days = (end - start).days + 1 + + return max(0, overlapping_days) # Return 0 if dates do not overlap + + +print(days_together("12-01", "20-01", "15-01", "18-01")) # Output: 4 +print(days_together("02-03", "12-03", "13-03", "14-03")) # Output: 0 +print(days_together("02-03", "12-03", "11-03", "15-03")) # Output: 2 +print(days_together("30-03", "05-04", "28-03", "02-04")) # Output: 4 diff --git a/challenge-187/lubos-kolouch/python/ch-2.py b/challenge-187/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..1219271b38 --- /dev/null +++ b/challenge-187/lubos-kolouch/python/ch-2.py @@ -0,0 +1,16 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + + +def find_triplet(arr): + arr.sort(reverse=True) + for i in range(len(arr) - 2): + if arr[i] < arr[i + 1] + arr[i + 2]: + return (arr[i], arr[i + 1], arr[i + 2]) + return () + + +print(find_triplet([1, 2, 3, 2])) # Output: (3, 2, 2) +print(find_triplet([1, 3, 2])) # Output: () +print(find_triplet([1, 1, 2, 3])) # Output: () +print(find_triplet([2, 4, 3])) # Output: (4, 3, 2) |
