diff options
| author | Simon Proctor <simon.proctor@gmail.com> | 2021-09-29 10:45:39 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-09-29 10:45:39 +0100 |
| commit | 4058c50b78b010e77cc716eb7002a90c6588b1a0 (patch) | |
| tree | 8041559c37674b0dceb0cb501061a93b078418c0 | |
| parent | 87756cf5bf5e6407df984b6d922058f62df8ea71 (diff) | |
| parent | 2d8cc4c294ce0969f262b680991f12c3ed2a0a24 (diff) | |
| download | perlweeklychallenge-club-4058c50b78b010e77cc716eb7002a90c6588b1a0.tar.gz perlweeklychallenge-club-4058c50b78b010e77cc716eb7002a90c6588b1a0.tar.bz2 perlweeklychallenge-club-4058c50b78b010e77cc716eb7002a90c6588b1a0.zip | |
Merge branch 'manwar:master' into master
42 files changed, 2983 insertions, 1781 deletions
diff --git a/challenge-132/cheok-yin-fung/java/MirrorDates.java b/challenge-132/cheok-yin-fung/java/MirrorDates.java new file mode 100644 index 0000000000..238f259d83 --- /dev/null +++ b/challenge-132/cheok-yin-fung/java/MirrorDates.java @@ -0,0 +1,28 @@ +// The Weekly Challenge - 132 +// Task 1 Mirror Dates +// Usage: java MirrorDates YYYY MM DD + +import java.time.LocalDate; +import java.time.YearMonth; + +public class MirrorDates +{ + public static void main(String[] args) + { + int year = Integer.parseInt(args[0]); + int month = Integer.parseInt(args[1]); + int day = Integer.parseInt(args[2]); + + mirror_dates(year, month, day); + } + + public static void mirror_dates (int birth_year,int birth_month, int birth_day) { + LocalDate my_today = LocalDate.of(2021,9,22); + LocalDate birthday = LocalDate.of(birth_year, birth_month, birth_day); + long y1 = my_today.toEpochDay() - birthday.toEpochDay(); + LocalDate d_senior = birthday.minusDays(y1); + LocalDate d_junior = my_today.plusDays(y1); + System.out.println(d_senior); + System.out.println(d_junior); + } +} diff --git a/challenge-132/cheok-yin-fung/julia/ch-1.jl b/challenge-132/cheok-yin-fung/julia/ch-1.jl new file mode 100644 index 0000000000..24eb6f5aa3 --- /dev/null +++ b/challenge-132/cheok-yin-fung/julia/ch-1.jl @@ -0,0 +1,34 @@ +# The Weekly Challenge Week 132 +# Task 1 Mirror Dates +# Usage: include("ch-1.jl") +# mirror_dates(Date(YYYY,MM,DD)) + +using Dates + +function mirror_dates(my_date_of_birth) + my_today = Date(2021,09,22) + y1 = Dates.values(my_today - my_date_of_birth) + d_senior = my_date_of_birth - Dates.Day(y1) + d_junior = my_today + Dates.Day(y1) + println(d_senior) + println(d_junior) +end + + +# julia> include("ch-1.jl") + +# #Example 1 +# julia> mirror_dates(Date(2021,09,18)) +# 2021-09-14 +# 2021-09-26 + +# #Example 2 +# julia> mirror_dates(Date(1975,10,10)) +# 1929-10-27 +# 2067-09-05 + +# #Example 3 +# julia> mirror_dates(Date(1967,02,14)) +# 1912-07-08 +# 2076-04-30 + diff --git a/challenge-132/cheok-yin-fung/perl/ch-1.pl b/challenge-132/cheok-yin-fung/perl/ch-1.pl new file mode 100644 index 0000000000..7bfe736e7d --- /dev/null +++ b/challenge-132/cheok-yin-fung/perl/ch-1.pl @@ -0,0 +1,42 @@ +#!/usr/bin/perl +# The Weekly Challenge 131 +# Task 1 Mirror Dates +# Usage: ch-1.pl YYYY/MM/DD +use v5.24.0; +use warnings; +use Time::Local qw'timelocal timegm_nocheck'; +use Test::More tests => 3; + +say mirror_str($ARGV[0]) if defined($ARGV[0]); + +sub mirror { + my @arr_today = (22, 8, 2021); # Wed Sep 22 2021 + my $_today = timelocal(0, 0, 0, @arr_today); + my @arr_birth = ($_[2], $_[1]-1, $_[0]); + my $_birth = timelocal(0, 0, 0, @arr_birth); + my $sec_diff = $_today - $_birth; + my $y1 = int (($_today - $_birth)/86400); + my @d_senior = localtime timegm_nocheck 0, 0, 0, $arr_birth[0]-$y1, $arr_birth[1], $arr_birth[2]; + my @d_junior = localtime timegm_nocheck 0, 0, 0, $arr_today[0]+$y1, $arr_today[1], $arr_today[2]; + return [ [@d_senior], [@d_junior] ]; +} + +sub mirror_str { + my ($byear, $bmonth, $bday) = split /\//, $_[0]; + $bmonth =~ s/^0//; # remove leading zeros + $bday =~ s/^0//; # remove leading zeros + my ($d_s, $d_j) = mirror($byear, $bmonth, $bday)->@*; + + return + ($d_s->[5]+1900)."/" + .($d_s->[4]<=8 ? 0 : "").($d_s->[4]+1)."/" + .($d_s->[3]<10 ? 0 : "").($d_s->[3]) + .", " + .($d_j->[5]+1900)."/" + .($d_j->[4]<=8 ? 0 : "").($d_j->[4]+1)."/" + .($d_j->[3]<10 ? 0 : "").($d_j->[3]); +} + +ok mirror_str("2021/09/18") eq "2021/09/14, 2021/09/26", "Example 1"; +ok mirror_str("1975/10/10") eq "1929/10/27, 2067/09/05", "Example 2"; +ok mirror_str("1967/02/14") eq "1912/07/08, 2076/04/30", "Example 3"; diff --git a/challenge-132/eric-cheung/excel-vba/Challenge_132.xlsm b/challenge-132/eric-cheung/excel-vba/Challenge_132.xlsm Binary files differnew file mode 100755 index 0000000000..9b96837c5c --- /dev/null +++ b/challenge-132/eric-cheung/excel-vba/Challenge_132.xlsm diff --git a/challenge-132/eric-cheung/excel-vba/ch-1.bas b/challenge-132/eric-cheung/excel-vba/ch-1.bas new file mode 100755 index 0000000000..3f187820bd --- /dev/null +++ b/challenge-132/eric-cheung/excel-vba/ch-1.bas @@ -0,0 +1,27 @@ +Attribute VB_Name = "ModTask_01"
+Option Explicit
+Public Const strMyTitle As String = "Eric Cheung"
+
+Sub Task_01()
+
+ Dim strMsg As String
+ Dim nDayAge As Long
+ Dim strDateInput As String, strToday As String
+ Dim Date_01 As Date, Date_02 As Date
+
+ '' strDateInput = "2021-09-18" '' Example 1:
+ '' strDateInput = "1975-10-10" '' Example 2:
+ strDateInput = "1967-02-14" '' Example 3:
+
+ strToday = "2021-09-22"
+
+ nDayAge = DateDiff("d", strDateInput, strToday)
+
+ Date_01 = DateAdd("d", -nDayAge, strDateInput)
+ Date_02 = DateAdd("d", nDayAge, strToday)
+
+ strMsg = Format(Date_01, "yyyy-mm-dd") & ", " & Format(Date_02, "yyyy-mm-dd")
+
+ MsgBox strMsg, vbOKOnly, strMyTitle
+
+End Sub
diff --git a/challenge-132/eric-cheung/excel-vba/ch-2.bas b/challenge-132/eric-cheung/excel-vba/ch-2.bas new file mode 100755 index 0000000000..cd79514af0 --- /dev/null +++ b/challenge-132/eric-cheung/excel-vba/ch-2.bas @@ -0,0 +1,47 @@ +Attribute VB_Name = "ModTask_02"
+Option Explicit
+Option Base 1
+
+Sub Task_02()
+ ''https://stackoverflow.com/questions/10951687/how-to-search-for-string-in-an-array
+ ''https://www.automateexcel.com/vba/find-value-in-array/
+ ''https://www.excelfunctions.net/vba-filter-function.html
+
+ Dim strMsg As String
+ Dim arrAge As Variant, arrName As Variant
+ Dim arrFirstName As Variant, arrSurName As Variant, arrFullName As Variant
+ Dim arrTemp As Variant
+ Dim nLoop As Integer, nSubLoop As Integer
+
+ ReDim arrFullName(1 To 1)
+
+ arrAge = Array(20, 28, 38, 18, 25, 18)
+ arrName = Array("Alex", "Joe", "Mike", "Alex", "David", "Simon")
+
+ arrFirstName = Array("Alex", "Joe", "Mike", "Joe", "Alex", "Simon")
+ arrSurName = Array("Stewart", "Root", "Gatting", "Blog", "Jones", "Duane")
+
+ For nLoop = LBound(arrFirstName) To UBound(arrFirstName)
+ If nLoop > 1 Then
+ ReDim Preserve arrFullName(1 To nLoop)
+ End If
+ arrFullName(nLoop) = arrFirstName(nLoop) & ", " & arrSurName(nLoop)
+ Next nLoop
+
+ For nLoop = LBound(arrName) To UBound(arrName)
+ arrTemp = Filter(arrFullName, arrName(nLoop))
+ If UBound(arrTemp) - LBound(arrTemp) + 1 > 0 Then
+ For nSubLoop = LBound(arrTemp) To UBound(arrTemp)
+ If strMsg <> "" Then
+ strMsg = strMsg & vbNewLine
+ End If
+ strMsg = strMsg & arrAge(nLoop) & ", " & arrTemp(nSubLoop)
+ Next nSubLoop
+ Erase arrTemp
+ End If
+ Next nLoop
+
+ MsgBox strMsg, vbOKOnly, strMyTitle
+
+End Sub
+
diff --git a/challenge-132/james-smith/README.md b/challenge-132/james-smith/README.md index 5c3ce0ff49..6754349a4c 100644 --- a/challenge-132/james-smith/README.md +++ b/challenge-132/james-smith/README.md @@ -1,4 +1,4 @@ -# Perl Weekly Challenge #131 +# Perl Weekly Challenge #132 You can find more information about this weeks, and previous weeks challenges at: @@ -10,53 +10,85 @@ submit solutions in whichever language you feel comfortable with. You can find the solutions here on github at: -https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-131/james-smith/perl +https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-132/james-smith/perl -# Task 1 - Consecutive Arrays +# Task 1 - Mirror dates -***You are given a sorted list of unique positive integers. Write a script to return list of arrays where the arrays are consecutive integers.*** +***You are given a date (yyyy/mm/dd). Assuming, the given date is your date of birth. Write a script to find the mirror dates of the given date.*** ## The solution -There isn't much to the solution, we are going to return the data as an array of arrayrefs each containing consecutive numbers. +Here we use Date::Calc module to handle the date time manipulations. - * We start by creating our first arrayref containing the first value. {for the `if` code to work without an edge case we need in element in our first arrayref to compare against) - * We then loop through the values: - * if the next number is 1 greater than the last value in the last arrayref. We push it there, - * otherwise we create a new arrayref and push it on the end of our array. - * We "cheat" a bit with the `if` statement - by replace `if( $a ) { $b } else { $c }` with `($a) ? ($b) : ($c)` this means we can use it inline within a `foreach` loop. - +We compute the number of days between dob & today. We then work out which day is this distance before the dob & after today to give the two values. ```perl -sub conseq { - my @val = @{$_[0]}; - my @res = ( [shift @val] ); - ( $_ == 1 + $res[-1][-1] ) ? (push @{$res[-1]},$_) : (push @res,[$_]) for @val; - \@res; +my @TODAY = @ARGV ? split m{/}, $ARGV[0]: Today; + +sub mirror_days { + my $d = Delta_Days( @TODAY, split m{/}, $_->[0] ); + return [ + sprintf( '%04d/%02d/%02d', Add_Delta_Days( @bd, $d )), + sprintf( '%04d/%02d/%02d', Add_Delta_Days( @TODAY, -$d )), + ]; } ``` -# Task 2 - Find Pairs +# Task 2 - Hash join + +***Write a script to implement Hash Join algorithm as suggested by wikipedia.*** -***You are given a string of delimiter pairs and a string to search. Write a script to return two strings, the first with any characters matching the 'opening character' set, the second with any matching the 'closing character' set.*** + * For each tuple r in the build input R + * Add r to the in-memory hash table + * If the size of the hash table equals the maximum in-memory size: + * Scan the probe input S, and add matching join tuples to the output relation + * Reset the hash table, and continue scanning the build input R + * Do a final scan of the probe input S and add the resulting join tuples to the output relation ## Solution -We solve this with a one liner.... which is below: +The problem is "simple" seems simple to begin with - but there are two "gotchas".. + + * We have to chunk the first array up into chunks of no more than `$N`. + * The keys in the two tables are NOT unique, so we need to store multiple values based on a key; + +To solve the first problem we break the input array up into to chunks of size `$MAX`, and repeat foreach one. + +To resolve the issue of the multiple keys, instead of the value of the cache being the value itself, it is the key to an array of values. + +Although not needed - the code is written to match the description above - so works with multiple non key columns in both tables. ```perl -sub find_pairs { - map { join '', $_[1] =~ /$_/g } - map { '(['.quotemeta($_).'])' } - map { join '', $_[0] =~ /$_/g } - '(.).?', '.(.?)'; -} -``` -A bit of an explanation on this one.... +## index of key columns... +my $ages_key = 1; +my $names_key = 0; + +## Get non-key columns in the names table... +## { get all column ids and splice out the key column} +my @names_columns = 0..(@{$player_names[0]}-1); +splice @names_columns, $names_key,1; ## Remove key.... + +## Get chunk size (default to 4) +my $MAX = @ARGV ? $ARGV[0] : 4; + +my @res; + +while( my @pns = splice @player_names, 0, $MAX ) { + my %cache = (); + ## Foreach we key on the key column, and store the non-key columns + ## Because key columns not unique we have array of arrays for + ## the hash values + push @{$cache{$_->[$names_key]}},[ @{$_}[@names_columns] ] foreach @pns; + + ## Now loop through the array of ages. + ## When we find a key we dump all values. + ## We push all values in the ages table - and all values (except the key) of the names table + foreach my $p (@player_ages) { + push @res, [@{$p}, @{$_}] foreach @{$cache{$p->[$ages_key]}}; + } +} - * Working backwards we define two regex `(.).` & `.(.)` these when combined with `/g` return alternate characters in the string - either starting from the first char or the 2nd. - * We then join these together to get two lists of characters. - * We convert them into a regex by using quotemeta to remove the "specialness" and then wrapping them in "([ ])" to capture them - * We just run this regex against our original string (with `/g` again) to get results. +## Just print values +say join "\t", map { sprintf '%-15s', $_ } @{$_} foreach @res; +``` diff --git a/challenge-132/james-smith/blog.txt b/challenge-132/james-smith/blog.txt new file mode 100644 index 0000000000..daa5120ef8 --- /dev/null +++ b/challenge-132/james-smith/blog.txt @@ -0,0 +1 @@ +https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-132/james-smith diff --git a/challenge-132/james-smith/perl/ch-1.pl b/challenge-132/james-smith/perl/ch-1.pl new file mode 100644 index 0000000000..341b27629e --- /dev/null +++ b/challenge-132/james-smith/perl/ch-1.pl @@ -0,0 +1,28 @@ +#!/usr/local/bin/perl + +use strict; + +use warnings; +use feature qw(say); +use Test::More; +use Date::Calc qw( Today Delta_Days Add_Delta_Days ); + +my @TODAY = @ARGV ? split m{/}, $ARGV[0]: Today; + +my @TESTS = ( + [ '2021/09/18', '2021/09/14-2021/09/26' ], + [ '1975/10/10', '1929/10/27-2067/09/05' ], + [ '1967/02/14', '1912/07/08-2076/04/30' ], +); + +is( join( '-', @{mirror_days($_->[0])} ), $_->[1] ) foreach @TESTS; + +done_testing(); + +sub mirror_days { + my $d = Delta_Days( @TODAY, split m{/}, $_->[0] ); ## Days between today and birthday + return [ + sprintf( '%04d/%02d/%02d', Add_Delta_Days( @bd, $d )), + sprintf( '%04d/%02d/%02d', Add_Delta_Days( @TODAY, -$d )), + ]; +} diff --git a/challenge-132/james-smith/perl/ch-2.pl b/challenge-132/james-smith/perl/ch-2.pl new file mode 100644 index 0000000000..446b66833c --- /dev/null +++ b/challenge-132/james-smith/perl/ch-2.pl @@ -0,0 +1,60 @@ +#!/usr/local/bin/perl + +use strict; + +use warnings; +use feature qw(say); +use Test::More; +use Benchmark qw(cmpthese timethis); +use Data::Dumper qw(Dumper); + +my @player_ages = ( + [20, "Alex" ], + [28, "Joe" ], + [38, "Mike" ], + [18, "Alex" ], + [25, "David" ], + [18, "Simon" ], +); + +my @player_names = ( + ["Alex", "Stewart"], + ["Joe", "Root" ], + ["Mike", "Gatting"], + ["Joe", "Blog" ], + ["Alex", "Jones" ], + ["Simon","Duane" ], +); + +## index of key columns... +my $ages_key = 1; +my $names_key = 0; + +## Get non-key columns in the names table... +## { get all column ids and splice out the key column} +my @names_columns = 0..(@{$player_names[0]}-1); +splice @names_columns, $names_key,1; ## Remove key.... + +## Get chunk size (default to 4) +my $MAX = @ARGV ? $ARGV[0] : 4; + +my @res; + +while( my @pns = splice @player_names, 0, $MAX ) { + my %cache = (); + ## Foreach we key on the key column, and store the non-key columns + ## Because key columns not unique we have array of arrays for + ## the hash values + push @{$cache{$_->[$names_key]}},[ @{$_}[@names_columns] ] foreach @pns; + + ## Now loop through the array of ages. + ## When we find a key we dump all values. + ## We push all values in the ages table - and all values (except the key) of the names table + foreach my $p (@player_ages) { + push @res, [@{$p}, @{$_}] foreach @{$cache{$p->[$ages_key]}}; + } +} + +## Just print values +say join "\t", map { sprintf '%-15s', $_ } @{$_} foreach @res; + diff --git a/challenge-132/luca-ferrari/blog-1.txt b/challenge-132/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..ea33d5406c --- /dev/null +++ b/challenge-132/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2021/09/27/PerlWeeklyChallenge132.html#task1 diff --git a/challenge-132/luca-ferrari/blog-2.txt b/challenge-132/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..42eaf3228b --- /dev/null +++ b/challenge-132/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2021/09/27/PerlWeeklyChallenge132.html#task2 diff --git a/challenge-132/luca-ferrari/raku/ch-1.p6 b/challenge-132/luca-ferrari/raku/ch-1.p6 new file mode 100644 index 0000000000..933e32be36 --- /dev/null +++ b/challenge-132/luca-ferrari/raku/ch-1.p6 @@ -0,0 +1,15 @@ +#!raku + + +multi sub MAIN( Str $date where { $date ~~ / \d ** 4 '/' \d ** 2 '/' \d ** 2 / } ) { + $date ~~ / (\d ** 4 ) '/' (\d ** 2 ) '/' ( \d **2 ) /; + MAIN( $0 ~ '-' ~ $1 ~ '-' ~ $2 ); +} + +multi sub MAIN( Str $date where { $date ~~ / \d ** 4 '-' \d ** 2 '-' \d ** 2 / } ) { + my $birthday = Date.new: $date; + my $today = Date.today; + my $days = $today - $birthday; + my @dates = $birthday - $days, $today + $days; + @dates.join( ', ' ).say; +} diff --git a/challenge-132/luca-ferrari/raku/ch-2.p6 b/challenge-132/luca-ferrari/raku/ch-2.p6 new file mode 100644 index 0000000000..8a3acbdf4b --- /dev/null +++ b/challenge-132/luca-ferrari/raku/ch-2.p6 @@ -0,0 +1,50 @@ +#!raku + + +class HashTable { + # hash of the table + has Int $.size = 2; + + method !doMatch( @memory, @S, $r-index, $s-index ) { + for @memory -> $rr { + for @S -> $s { + say $rr.join( ' ' ) ~ $s[ 0 .. $s-index - 1, $s-index + 1 .. * - 1 ],join( ' ' ) if $s[ $s-index ] ~~ $rr[ $r-index ]; + } + } + + @memory = (); + } + + method match( @R, @S, Int $r-index = 0, Int $s-index = 0 ) { + my @memory; + for @R -> $r { + @memory.push: $r; + self!doMatch( @memory, @S, $r-index, $s-index ) if @memory.elems == $!size; + } + + self!doMatch( @memory, @S, $r-index, $s-index ) if @memory; + } +} + +sub MAIN() { + my @player_ages = + [20, "Alex" ], + [28, "Joe" ], + [38, "Mike" ], + [18, "Alex" ], + [25, "David" ], + [18, "Simon" ], + ; + + my @player_names = + ["Alex", "Stewart"], + ["Joe", "Root" ], + ["Mike", "Gatting"], + ["Joe", "Blog" ], + ["Alex", "Jones" ], + ["Simon","Duane" ], + ; + + my $hash = HashTable.new; + $hash.match( @player_ages, @player_names, 1, 0 ); +} diff --git a/challenge-132/mark-anderson/raku/ch-1.raku b/challenge-132/mark-anderson/raku/ch-1.raku new file mode 100644 index 0000000000..f3111a3834 --- /dev/null +++ b/challenge-132/mark-anderson/raku/ch-1.raku @@ -0,0 +1,14 @@ +#!/usr/bin/env raku + +say mirror-dates('1967/02/14'); + +sub mirror-dates($date) +{ + my $t = Date.today; + my $d = Date.new($date.trans('/' => '-')); + + my $days = $t - $d; + + ($d.earlier(:$days).yyyy-mm-dd('/'), + $t.later(:$days).yyyy-mm-dd('/')); +} diff --git a/challenge-132/mark-anderson/raku/ch-2.raku b/challenge-132/mark-anderson/raku/ch-2.raku new file mode 100644 index 0000000000..215b866c0e --- /dev/null +++ b/challenge-132/mark-anderson/raku/ch-2.raku @@ -0,0 +1,29 @@ +#!/usr/bin/env raku + +my @players-age = < 20 Alex >, + < 28 Joe >, + < 38 Mike >, + < 18 Alex >, + < 25 David >, + < 18 Simon >; + +my @players-name = < Alex Stewart >, + < Joe Root >, + < Mike Gatting >, + < Joe Blog >, + < Alex Jones >, + < Simon Duane >; + +my %a = @players-age.classify({ .[1] }, :as{ .[0] }); +my %n = @players-name.classify({ .[0] }, :as{ .[1] }); + +for %a.keys.sort -> $k +{ + if %n{$k}:exists + { + for %a{$k}<> X %n{$k}<> + { + printf("%-3d%-6s%s\n", .[0], $k, .[1]); + } + } +} diff --git a/challenge-132/perlboy1967/perl/ch-1.pl b/challenge-132/perlboy1967/perl/ch-1.pl new file mode 100755 index 0000000000..5bcfdf889a --- /dev/null +++ b/challenge-132/perlboy1967/perl/ch-1.pl @@ -0,0 +1,55 @@ +#!/bin/perl + +# Perl Weekly Challenge - 132 +# - https://perlweeklychallenge.org/blog/perl-weekly-challenge-132/#TASK1 +# +# Task 1 - Mirror Dates +# +# Author: Niels 'PerlBoy' van Dijke + +use v5.16; +use strict; +use warnings; + +use Date::Calc qw(Delta_Days Add_Delta_Days); +use POSIX qw(strftime); + +use Test::More; +use Test::Deep; + +# Prototype(s) +sub mirrorDates($$); + + +my $date = '2021/09/22'; + +is_deeply([mirrorDates('2021/09/18', $date)],['2021/09/14','2021/09/26']); +is_deeply([mirrorDates('1975/10/10', $date)],['1929/10/27','2067/09/05']); +is_deeply([mirrorDates('1967/02/14', $date)],['1912/07/08','2076/04/30']); + +done_testing; + + +foreach $date (@ARGV) { + printf "Mirror date of '%s' is '%s' and '%s'\n", + $date, mirrorDates($date, strftime('%Y/%m/%d', gmtime)); +} + + +sub mirrorDates($$) { + my @d; + + foreach (@_) { + push(@d,[$1,$2,$3]) if (m#^(\d+)\D(\d+)\D(\d+)$#); + } + + my $interval = Delta_Days(@{$d[0]}, @{$d[1]}); + + my @r = ( + [Add_Delta_Days(@{$d[0]}, -$interval)], + [Add_Delta_Days(@{$d[1]}, $interval)] + ); + + return sprintf('%d/%02d/%02d', @{$r[0]}), + sprintf('%d/%02d/%02d', @{$r[1]}); +} diff --git a/challenge-132/perlboy1967/perl/ch-2.pl b/challenge-132/perlboy1967/perl/ch-2.pl new file mode 100755 index 0000000000..64be1f3727 --- /dev/null +++ b/challenge-132/perlboy1967/perl/ch-2.pl @@ -0,0 +1,57 @@ +#!/bin/perl + +# Perl Weekly Challenge - 132 +# - https://perlweeklychallenge.org/blog/perl-weekly-challenge-132/#TASK1 +# +# Task 1 - Mirror Dates +# +# Author: Niels 'PerlBoy' van Dijke + +use v5.16; +use strict; +use warnings; + +use Test::More; +use Test::Deep; + +sub hashJoin(\@\@); + +my @player_ages = ( + [20, "Alex" ], + [28, "Joe" ], + [38, "Mike" ], + [18, "Alex" ], + [25, "David" ], + [18, "Simon" ], +); + +my @player_names = ( + ["Alex", "Stewart"], + ["Joe", "Root" ], + ["Mike", "Gatting"], + ["Joe", "Blog" ], + ["Alex", "Jones" ], + ["Simon","Duane" ], +); + + +is_deeply( + hashJoin(@player_ages, @player_names), + [ + [ 20, "Alex", "Stewart" ], + [ 18, "Alex", "Stewart" ], + [ 28, "Joe", "Root" ], + [ 38, "Mike", "Gatting" ], + [ 28, "Joe", "Blog" ], |
