aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2023-08-06 20:01:26 +0100
committerGitHub <noreply@github.com>2023-08-06 20:01:26 +0100
commit2a5c3638192a903ede793501fa166ad32432fd3b (patch)
treefd9dc81d216f62b6a8510090a0214cd56a217dcc
parent9027feaa28d00417544f0d8940caf01e095bc51f (diff)
parent6d835b28e1d4e4323bdafaac2c8bf1b5730efd0f (diff)
downloadperlweeklychallenge-club-2a5c3638192a903ede793501fa166ad32432fd3b.tar.gz
perlweeklychallenge-club-2a5c3638192a903ede793501fa166ad32432fd3b.tar.bz2
perlweeklychallenge-club-2a5c3638192a903ede793501fa166ad32432fd3b.zip
Merge pull request #8500 from LubosKolouch/master
Challenge 182 183 187 LK Perl Python
-rw-r--r--challenge-182/lubos-kolouch/perl/ch-1.pl19
-rw-r--r--challenge-182/lubos-kolouch/perl/ch-2.pl25
-rw-r--r--challenge-182/lubos-kolouch/python/ch-1.py10
-rw-r--r--challenge-182/lubos-kolouch/python/ch-2.py23
-rw-r--r--challenge-183/lubos-kolouch/perl/ch-1.pl29
-rw-r--r--challenge-183/lubos-kolouch/perl/ch-2.pl46
-rw-r--r--challenge-183/lubos-kolouch/python/ch-1.py16
-rw-r--r--challenge-183/lubos-kolouch/python/ch-2.py35
-rw-r--r--challenge-187/lubos-kolouch/perl/ch-1.pl30
-rw-r--r--challenge-187/lubos-kolouch/perl/ch-2.pl18
-rw-r--r--challenge-187/lubos-kolouch/python/ch-1.py27
-rw-r--r--challenge-187/lubos-kolouch/python/ch-2.py16
12 files changed, 294 insertions, 0 deletions
diff --git a/challenge-182/lubos-kolouch/perl/ch-1.pl b/challenge-182/lubos-kolouch/perl/ch-1.pl
new file mode 100644
index 0000000000..a9401e81e8
--- /dev/null
+++ b/challenge-182/lubos-kolouch/perl/ch-1.pl
@@ -0,0 +1,19 @@
+use strict;
+use warnings;
+
+sub max_index {
+ my @lst = @_;
+ my $max = $lst[0];
+ my $index = 0;
+ for my $i ( 1 .. $#lst ) {
+ if ( $lst[$i] > $max ) {
+ $max = $lst[$i];
+ $index = $i;
+ }
+ }
+ return $index;
+}
+
+print max_index( 5, 2, 9, 1, 7, 6 ); # Output: 2
+print "\n";
+print max_index( 4, 2, 3, 1, 5, 0 ); # Output: 4
diff --git a/challenge-182/lubos-kolouch/perl/ch-2.pl b/challenge-182/lubos-kolouch/perl/ch-2.pl
new file mode 100644
index 0000000000..295bd547c8
--- /dev/null
+++ b/challenge-182/lubos-kolouch/perl/ch-2.pl
@@ -0,0 +1,25 @@
+use strict;
+use warnings;
+use List::Util qw/all/;
+
+sub common_path {
+ my @paths = @_;
+
+ my @parts = map { [ split '/', $_ ] } @paths;
+
+ my @common;
+ for my $i ( 0 .. $#{ $parts[0] } ) {
+ my $part = $parts[0][$i];
+ last unless all { $_->[$i] eq $part } @parts;
+ push @common, $part;
+ }
+
+ return join '/', @common;
+}
+
+my @paths = (
+ "/a/b/c/1/x.pl", "/a/b/c/d/e/2/x.pl",
+ "/a/b/c/d/3/x.pl", "/a/b/c/4/x.pl",
+ "/a/b/c/d/5/x.pl"
+);
+print common_path(@paths), "\n"; # Output: /a/b/c
diff --git a/challenge-182/lubos-kolouch/python/ch-1.py b/challenge-182/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..373f34c2c8
--- /dev/null
+++ b/challenge-182/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,10 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+
+def max_index(lst):
+ return lst.index(max(lst))
+
+
+print(max_index([5, 2, 9, 1, 7, 6])) # Output: 2
+print(max_index([4, 2, 3, 1, 5, 0])) # Output: 4
diff --git a/challenge-182/lubos-kolouch/python/ch-2.py b/challenge-182/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..2ebe227ab7
--- /dev/null
+++ b/challenge-182/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,23 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+
+def common_path(paths):
+ paths = [path.split("/") for path in paths]
+ common = []
+ for items in zip(*paths):
+ if all(item == items[0] for item in items):
+ common.append(items[0])
+ else:
+ break
+ return "/".join(common)
+
+
+paths = [
+ "/a/b/c/1/x.pl",
+ "/a/b/c/d/e/2/x.pl",
+ "/a/b/c/d/3/x.pl",
+ "/a/b/c/4/x.pl",
+ "/a/b/c/d/5/x.pl",
+]
+print(common_path(paths)) # Output: /a/b/c
diff --git a/challenge-183/lubos-kolouch/perl/ch-1.pl b/challenge-183/lubos-kolouch/perl/ch-1.pl
new file mode 100644
index 0000000000..819904a845
--- /dev/null
+++ b/challenge-183/lubos-kolouch/perl/ch-1.pl
@@ -0,0 +1,29 @@
+use strict;
+use warnings;
+use Data::Dumper;
+
+sub remove_duplicates {
+ my @array = @_;
+ my %seen;
+ my @result;
+
+ local $Data::Dumper::Terse = 1; # remove "$VAR1 = " prefix
+ local $Data::Dumper::Indent = 0; # output in a single line
+
+ for my $element (@array) {
+ my $key = Dumper($element);
+ if ( !exists $seen{$key} ) {
+ push @result, $element;
+ $seen{$key} = 1;
+ }
+ }
+
+ return @result;
+}
+
+my @list1 = ( [ 1, 2 ], [ 3, 4 ], [ 5, 6 ], [ 1, 2 ] );
+my @list2 = ( [ 9, 1 ], [ 3, 7 ], [ 2, 5 ], [ 2, 5 ] );
+
+print Dumper( remove_duplicates(@list1) );
+print "\n";
+print Dumper( remove_duplicates(@list2) );
diff --git a/challenge-183/lubos-kolouch/perl/ch-2.pl b/challenge-183/lubos-kolouch/perl/ch-2.pl
new file mode 100644
index 0000000000..f9a06169af
--- /dev/null
+++ b/challenge-183/lubos-kolouch/perl/ch-2.pl
@@ -0,0 +1,46 @@
+use strict;
+use warnings;
+use Time::Piece;
+use Time::Seconds;
+
+sub date_difference {
+ my ( $date1, $date2 ) = @_;
+
+ # convert string to Time::Piece object
+ my $date1_obj = Time::Piece->strptime( $date1, "%Y-%m-%d" );
+ my $date2_obj = Time::Piece->strptime( $date2, "%Y-%m-%d" );
+
+ # calculate difference
+ my $diff = $date2_obj - $date1_obj;
+ my $years = int( $diff->years );
+ my $days = $diff->days % 365;
+
+ # format output
+ my $output = "";
+ if ( $years == 1 ) {
+ $output .= "1 year ";
+ }
+ elsif ( $years > 1 ) {
+ $output .= "$years years ";
+ }
+ if ( $days == 1 ) {
+ $output .= "1 day";
+ }
+ elsif ( $days > 1 ) {
+ $output .= "$days days";
+ }
+ return $output;
+}
+
+print date_difference( '2019-02-10', '2022-11-01' )
+ . "\n"; # Output: 3 years 264 days
+print date_difference( '2020-09-15', '2022-03-29' )
+ . "\n"; # Output: 1 year 195 days
+print date_difference( '2019-12-31', '2020-01-01' ) . "\n"; # Output: 1 day
+print date_difference( '2019-12-01', '2019-12-31' ) . "\n"; # Output: 30 days
+print date_difference( '2019-12-31', '2020-12-31' ) . "\n"; # Output: 1 year
+print date_difference( '2019-12-31', '2021-12-31' ) . "\n"; # Output: 2 years
+print date_difference( '2020-09-15', '2021-09-16' )
+ . "\n"; # Output: 1 year 1 day
+print date_difference( '2019-09-15', '2021-09-16' )
+ . "\n"; # Output: 2 years 1 day
diff --git a/challenge-183/lubos-kolouch/python/ch-1.py b/challenge-183/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..062e74b4be
--- /dev/null
+++ b/challenge-183/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,16 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+
+def remove_duplicates(lst):
+ # Convert each list to a tuple so it can be added to a set
+ tuples = [tuple(i) for i in lst]
+ # Create a set to remove duplicates, then convert each tuple back to a list
+ return [list(i) for i in set(tuples)]
+
+
+list1 = [[1, 2], [3, 4], [5, 6], [1, 2]]
+list2 = [[9, 1], [3, 7], [2, 5], [2, 5]]
+
+print(remove_duplicates(list1)) # Output: [[1, 2], [3, 4], [5, 6]]
+print(remove_duplicates(list2)) # Output: [[9, 1], [2, 5], [3, 7]]
diff --git a/challenge-183/lubos-kolouch/python/ch-2.py b/challenge-183/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..629657fa38
--- /dev/null
+++ b/challenge-183/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,35 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+from datetime import datetime
+
+
+def date_difference(date1, date2):
+ # convert string to datetime object
+ date1 = datetime.strptime(date1, "%Y-%m-%d")
+ date2 = datetime.strptime(date2, "%Y-%m-%d")
+ # calculate difference
+ diff = date2 - date1
+ years = diff.days // 365
+ days = diff.days % 365
+ # format output
+ output = ""
+ if years == 1:
+ output += "1 year "
+ elif years > 1:
+ output += f"{years} years "
+ if days == 1:
+ output += "1 day"
+ elif days > 1:
+ output += f"{days} days"
+ return output.strip()
+
+
+print(date_difference("2019-02-10", "2022-11-01")) # Output: 3 years 264 days
+print(date_difference("2020-09-15", "2022-03-29")) # Output: 1 year 195 days
+print(date_difference("2019-12-31", "2020-01-01")) # Output: 1 day
+print(date_difference("2019-12-01", "2019-12-31")) # Output: 30 days
+print(date_difference("2019-12-31", "2020-12-31")) # Output: 1 year
+print(date_difference("2019-12-31", "2021-12-31")) # Output: 2 years
+print(date_difference("2020-09-15", "2021-09-16")) # Output: 1 year 1 day
+print(date_difference("2019-09-15", "2021-09-16")) # Output: 2 years 1 day
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)