aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-09-25 20:26:31 +0100
committerGitHub <noreply@github.com>2022-09-25 20:26:31 +0100
commit718d031342ca181a7276261edec61a1c5294c948 (patch)
tree226b7dbe46e8a6937c07bf45fd129bce7bfdde48
parent5979e8be2ae466fad1c654d93f79cf183a4d9766 (diff)
parent8453d5495d2017374aabb52d1ae93e997a90b113 (diff)
downloadperlweeklychallenge-club-718d031342ca181a7276261edec61a1c5294c948.tar.gz
perlweeklychallenge-club-718d031342ca181a7276261edec61a1c5294c948.tar.bz2
perlweeklychallenge-club-718d031342ca181a7276261edec61a1c5294c948.zip
Merge pull request #6786 from simongreen-net/master
sgreen solutions to challenge 183
-rw-r--r--challenge-181/sgreen/README.md3
-rw-r--r--challenge-182/sgreen/README.md3
-rw-r--r--challenge-183/sgreen/README.md4
-rw-r--r--challenge-183/sgreen/blog.txt1
-rwxr-xr-xchallenge-183/sgreen/perl/ch-1.pl45
-rwxr-xr-xchallenge-183/sgreen/perl/ch-2.pl50
-rwxr-xr-xchallenge-183/sgreen/python/ch-1.py28
-rwxr-xr-xchallenge-183/sgreen/python/ch-2.py41
8 files changed, 167 insertions, 8 deletions
diff --git a/challenge-181/sgreen/README.md b/challenge-181/sgreen/README.md
deleted file mode 100644
index 3827d96ce5..0000000000
--- a/challenge-181/sgreen/README.md
+++ /dev/null
@@ -1,3 +0,0 @@
-# The Weekly Challenge 180
-
-[Blog](https://dev.to/simongreennet/weekly-challenge-180-4mi5)
diff --git a/challenge-182/sgreen/README.md b/challenge-182/sgreen/README.md
deleted file mode 100644
index 3827d96ce5..0000000000
--- a/challenge-182/sgreen/README.md
+++ /dev/null
@@ -1,3 +0,0 @@
-# The Weekly Challenge 180
-
-[Blog](https://dev.to/simongreennet/weekly-challenge-180-4mi5)
diff --git a/challenge-183/sgreen/README.md b/challenge-183/sgreen/README.md
index 3827d96ce5..dab33e5312 100644
--- a/challenge-183/sgreen/README.md
+++ b/challenge-183/sgreen/README.md
@@ -1,3 +1,3 @@
-# The Weekly Challenge 180
+# The Weekly Challenge 183
-[Blog](https://dev.to/simongreennet/weekly-challenge-180-4mi5)
+Blog: [Unique differences](https://dev.to/simongreennet/unique-differences-1cci)
diff --git a/challenge-183/sgreen/blog.txt b/challenge-183/sgreen/blog.txt
new file mode 100644
index 0000000000..c9968de7e1
--- /dev/null
+++ b/challenge-183/sgreen/blog.txt
@@ -0,0 +1 @@
+https://dev.to/simongreennet/weekly-challenge-180-4mi5 \ No newline at end of file
diff --git a/challenge-183/sgreen/perl/ch-1.pl b/challenge-183/sgreen/perl/ch-1.pl
new file mode 100755
index 0000000000..ba88778a56
--- /dev/null
+++ b/challenge-183/sgreen/perl/ch-1.pl
@@ -0,0 +1,45 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use experimental 'signatures';
+use feature 'say';
+
+use JSON;
+
+sub same_array ( $c, $d ) {
+ # Determine if they are the same array
+
+ # They aren't if they are different lengths
+ return if $#$c != $#$d;
+ for my $x ( 0 .. $#$c ) {
+ # Or if the value is different
+ return if $c->[$x] ne $d->[$x];
+ }
+
+ # They are the same!
+ return 1;
+}
+
+sub main ($y) {
+ my $array = decode_json($y);
+
+ my @solution = ();
+ I: foreach my $i ( 0 .. $#$array ) {
+
+ # See if we have found this list before
+ my $is_unique = 1;
+ foreach my $j ( 0 .. $i - 1 ) {
+ if ( same_array( $array->[$i], $array->[$j] ) ) {
+ next I;
+ }
+ }
+
+ # We haven't. Add it to the solutions
+ push @solution, $array->[$i];
+ }
+
+ say '(', join( ', ', map( "[" . join( ', ', @$_ ) . ']', @solution ) ), ')';
+}
+
+main( $ARGV[0] ); \ No newline at end of file
diff --git a/challenge-183/sgreen/perl/ch-2.pl b/challenge-183/sgreen/perl/ch-2.pl
new file mode 100755
index 0000000000..6d8b7831ab
--- /dev/null
+++ b/challenge-183/sgreen/perl/ch-2.pl
@@ -0,0 +1,50 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+use Date::Calc 'Delta_Days';
+
+sub main ( $date1, $date2 ) {
+ # Make date2 the latest date
+ if ( $date2 lt $date1 ) {
+ ( $date2, $date1 ) = ( $date1, $date2 );
+ }
+
+ # Split the date parts
+ my ( $year1, $month1, $day1 ) = split /-/, $date1;
+ my ( $year2, $month2, $day2 ) = split /-/, $date2;
+
+ # Calculate the years between the two dates. If date2 is earlier in the
+ # year than date1, then the year difference is one less
+ my $years = $year2 - $year1;
+ if ( $month2 < $month1 or ( $month2 == $month1 and $day2 < $day1 ) ) {
+ $years--;
+ }
+
+ # Calculate the days difference
+ my $days =
+ Delta_Days( $year1, $month1, $day1, $year2 - $years, $month2, $day2 );
+
+ # Print the output
+ my @diff = ();
+ if ( $years > 1 ) {
+ push @diff, "$years years";
+ }
+ elsif ( $years == 1 ) {
+ push @diff, '1 year';
+ }
+
+ if ( $days > 1 ) {
+ push @diff, "$days days";
+ }
+ elsif ( $days == 1 ) {
+ push @diff, '1 day';
+ }
+
+ say join ' ', @diff;
+}
+
+main(@ARGV); \ No newline at end of file
diff --git a/challenge-183/sgreen/python/ch-1.py b/challenge-183/sgreen/python/ch-1.py
new file mode 100755
index 0000000000..64bd316ef4
--- /dev/null
+++ b/challenge-183/sgreen/python/ch-1.py
@@ -0,0 +1,28 @@
+#!/usr/bin/env python
+
+import sys
+import yaml
+
+
+def main(y):
+ array = yaml.safe_load(y)
+
+ solution = []
+ for i in range(len(array)):
+
+ # See if we have found this list before
+ is_unique = True
+ for j in range(i):
+ if array[i] == array[j]:
+ is_unique = False
+ break
+
+ if is_unique:
+ # We haven't. Add it to the solutions
+ solution.append(array[i])
+
+ print(*solution, sep=', ')
+
+
+if __name__ == '__main__':
+ main(sys.argv[1])
diff --git a/challenge-183/sgreen/python/ch-2.py b/challenge-183/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..92a8493bc5
--- /dev/null
+++ b/challenge-183/sgreen/python/ch-2.py
@@ -0,0 +1,41 @@
+#!/usr/bin/env python3
+
+import sys
+from datetime import date
+from dateutil.relativedelta import relativedelta
+
+
+def main(date1, date2):
+ date1 = date.fromisoformat(date1)
+ date2 = date.fromisoformat(date2)
+
+ # Make date2 the latest date
+ if date2 < date1:
+ date2, date1 = date1, date2
+
+ # Calculate the years between the two dates. If date2 is earlier in the
+ # year than date1, then the year difference is one less
+ years = date2.year - date1.year
+ if date2.month < date1.month or (date2.month == date1.month and date2.day < date1.day):
+ years -= 1
+
+ # Calculate the days difference
+ days = (date2 - relativedelta(years=years) - date1).days
+
+ # Print the output
+ diff = []
+ if years > 1:
+ diff.append(f'{years} years')
+ elif years == 1:
+ diff.append(f'1 year')
+
+ if days > 1:
+ diff.append(f'{days} days')
+ elif days == 1:
+ diff.append(f'1 day')
+
+ print(*diff, sep=' ')
+
+
+if __name__ == '__main__':
+ main(sys.argv[1], sys.argv[2])