aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLubos Kolouch <lubos@kolouch.net>2023-08-05 18:00:13 +0200
committerLubos Kolouch <lubos@kolouch.net>2023-08-05 18:00:13 +0200
commit68326ff7ce1e1b759ff70a63f50497f855c3b432 (patch)
tree5b0ff2453fe0b903a3447419087afcbb26ff6fd7
parentdaab59609b4b4fd0d3e2c248202371dfe00cf33c (diff)
downloadperlweeklychallenge-club-68326ff7ce1e1b759ff70a63f50497f855c3b432.tar.gz
perlweeklychallenge-club-68326ff7ce1e1b759ff70a63f50497f855c3b432.tar.bz2
perlweeklychallenge-club-68326ff7ce1e1b759ff70a63f50497f855c3b432.zip
feat(challenge-183/lubos-kolouch/perl,python/): Challenge 183 LK Perl Python
-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
4 files changed, 126 insertions, 0 deletions
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