aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2021-10-20 18:25:33 +0100
committerPaulo Custodio <pauloscustodio@gmail.com>2021-10-21 17:51:11 +0100
commitd589883f349965841c2ad248872f9ceae3a8c8af (patch)
tree73285caa4982efe7d453e099c5563903bdef40d3
parent0435951baf034a11383702b67e15c74ef6242cd6 (diff)
downloadperlweeklychallenge-club-d589883f349965841c2ad248872f9ceae3a8c8af.tar.gz
perlweeklychallenge-club-d589883f349965841c2ad248872f9ceae3a8c8af.tar.bz2
perlweeklychallenge-club-d589883f349965841c2ad248872f9ceae3a8c8af.zip
Add Perl and Python solutions to challenge 132
-rw-r--r--challenge-132/paulo-custodio/perl/ch-1.pl60
-rw-r--r--challenge-132/paulo-custodio/perl/ch-2.pl100
-rw-r--r--challenge-132/paulo-custodio/python/ch-1.py50
-rw-r--r--challenge-132/paulo-custodio/python/ch-2.py100
-rw-r--r--challenge-132/paulo-custodio/t/test-1.yaml15
-rw-r--r--challenge-132/paulo-custodio/t/test-2.yaml13
-rw-r--r--challenge-132/paulo-custodio/test.pl4
7 files changed, 342 insertions, 0 deletions
diff --git a/challenge-132/paulo-custodio/perl/ch-1.pl b/challenge-132/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..5b5990ef2c
--- /dev/null
+++ b/challenge-132/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,60 @@
+#!/usr/bin/env perl
+
+# TASK #1 > Mirror Dates
+# Submitted by: Mark Anderson
+# 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.
+#
+# Dave Cross has built cool site that does something similar.
+#
+# Assuming today is 2021/09/22.
+# Example 1:
+# Input: 2021/09/18
+# Output: 2021/09/14, 2021/09/26
+#
+# On the date you were born, someone who was your current age, would have
+# been born on 2021/09/14.
+# Someone born today will be your current age on 2021/09/26.
+# Example 2:
+# Input: 1975/10/10
+# Output: 1929/10/27, 2067/09/05
+#
+# On the date you were born, someone who was your current age, would have
+# been born on 1929/10/27.
+# Someone born today will be your current age on 2067/09/05.
+# Example 3:
+# Input: 1967/02/14
+# Output: 1912/07/08, 2076/04/30
+#
+# On the date you were born, someone who was your current age, would have
+# been born on 1912/07/08.
+# Someone born today will be your current age on 2076/04/30.
+
+use Modern::Perl;
+use DateTime;
+
+@ARGV==2 or die "Usage: ch-1.pl birth-date today\n";
+my($birth_text, $today_text) = @ARGV;
+my $birth = parse_date($birth_text);
+my $today = parse_date($today_text);
+
+my $days = $birth->delta_days($today);
+my $mirror1 = $birth->clone->subtract($days);
+my $mirror2 = $today->clone->add($days);
+
+say $mirror1->ymd('/'), ", ", $mirror2->ymd('/');
+
+sub parse_date {
+ my($str) = @_;
+ $str =~ m{^(\d{4})/(\d{2})/(\d{2})$}
+ or die "date format should be yyyy/mm/dd\n";
+ my($yy,$mm,$dd) = ($1,$2,$3);
+ my $date = DateTime->new(
+ year => $yy,
+ month => $mm,
+ day => $dd,
+ );
+ return $date;
+}
diff --git a/challenge-132/paulo-custodio/perl/ch-2.pl b/challenge-132/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..2fb0f1ca4f
--- /dev/null
+++ b/challenge-132/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,100 @@
+#!/usr/bin/env perl
+
+# TASK #2 > Hash Join
+# Submitted by: Mohammad S Anwar
+# Write a script to implement Hash Join algorithm as suggested
+# by wikipedia.
+#
+# 1. For each tuple r in the build input R
+# 1.1 Add r to the in-memory hash table
+# 1.2 If the size of the hash table equals the maximum
+# in-memory size:
+# 1.2.1 Scan the probe input S, and add matching join
+# tuples to the output relation
+# 1.2.2 Reset the hash table, and continue scanning the
+# build input R
+# 2. Do a final scan of the probe input S and add the resulting
+# join tuples to the output relation
+# Example
+# Input:
+#
+# @player_ages = (
+# [20, "Alex" ],
+# [28, "Joe" ],
+# [38, "Mike" ],
+# [18, "Alex" ],
+# [25, "David" ],
+# [18, "Simon" ],
+# );
+#
+# @player_names = (
+# ["Alex", "Stewart"],
+# ["Joe", "Root" ],
+# ["Mike", "Gatting"],
+# ["Joe", "Blog" ],
+# ["Alex", "Jones" ],
+# ["Simon","Duane" ],
+# );
+#
+# Output:
+#
+# Based on index = 1 of @players_age and
+# index = 0 of @players_name.
+#
+# 20, "Alex", "Stewart"
+# 20, "Alex", "Jones"
+# 18, "Alex", "Stewart"
+# 18, "Alex", "Jones"
+# 28, "Joe", "Root"
+# 28, "Joe", "Blog"
+# 38, "Mike", "Gatting"
+# 18, "Simon", "Duane"
+
+use Modern::Perl;
+use Data::Dump 'dump';
+
+my @player_ages = (
+ [20, "Alex" ],
+ [28, "Joe" ],
+ [38, "Mike" ],
+ [18, "Alex" ],
+ [25, "David" ],
+ [18, "Simon" ],
+);
+my $player_ages_key = 1;
+
+my @player_names = (
+ ["Alex", "Stewart"],
+ ["Joe", "Root" ],
+ ["Mike", "Gatting"],
+ ["Joe", "Blog" ],
+ ["Alex", "Jones" ],
+ ["Simon","Duane" ],
+);
+my $player_names_key = 0;
+
+my @result = hash_join(\@player_ages, $player_ages_key,
+ \@player_names, $player_names_key);
+for (@result) {
+ say join(", ", map {dump($_)} @$_);
+}
+
+sub hash_join {
+ my($table1, $key1, $table2, $key2) = @_;
+ my %build;
+ for my $row (@$table1) {
+ my $key = $row->[$key1];
+ $build{$key} ||= [];
+ push @{$build{$key}}, $row;
+ }
+ my @result;
+ for my $row_probe (@$table2) {
+ my $key = $row_probe->[$key2];
+ for my $row_build (@{$build{$key}}) {
+ my @row = (@$row_build,
+ @{$row_probe}[0..$key2-1, $key2+1..$#$row_probe]);
+ push @result, \@row;
+ }
+ }
+ return @result;
+}
diff --git a/challenge-132/paulo-custodio/python/ch-1.py b/challenge-132/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..adc4693080
--- /dev/null
+++ b/challenge-132/paulo-custodio/python/ch-1.py
@@ -0,0 +1,50 @@
+#!/usr/bin/env python
+
+# TASK #1 > Mirror Dates
+# Submitted by: Mark Anderson
+# 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.
+#
+# Dave Cross has built cool site that does something similar.
+#
+# Assuming today is 2021/09/22.
+# Example 1:
+# Input: 2021/09/18
+# Output: 2021/09/14, 2021/09/26
+#
+# On the date you were born, someone who was your current age, would have
+# been born on 2021/09/14.
+# Someone born today will be your current age on 2021/09/26.
+# Example 2:
+# Input: 1975/10/10
+# Output: 1929/10/27, 2067/09/05
+#
+# On the date you were born, someone who was your current age, would have
+# been born on 1929/10/27.
+# Someone born today will be your current age on 2067/09/05.
+# Example 3:
+# Input: 1967/02/14
+# Output: 1912/07/08, 2076/04/30
+#
+# On the date you were born, someone who was your current age, would have
+# been born on 1912/07/08.
+# Someone born today will be your current age on 2076/04/30.
+
+import sys
+import datetime
+
+def parse_date(s):
+ return datetime.datetime.strptime(s, "%Y/%m/%d")
+
+def format_date(d):
+ return d.strftime("%Y/%m/%d")
+
+birth = parse_date(sys.argv[1])
+today = parse_date(sys.argv[2])
+days = today - birth
+mirror1 = birth - days
+mirror2 = today + days
+
+print(format_date(mirror1)+", "+format_date(mirror2))
diff --git a/challenge-132/paulo-custodio/python/ch-2.py b/challenge-132/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..6d48ba2c8c
--- /dev/null
+++ b/challenge-132/paulo-custodio/python/ch-2.py
@@ -0,0 +1,100 @@
+#!/usr/bin/env python
+
+# TASK #2 > Hash Join
+# Submitted by: Mohammad S Anwar
+# Write a script to implement Hash Join algorithm as suggested
+# by wikipedia.
+#
+# 1. For each tuple r in the build input R
+# 1.1 Add r to the in-memory hash table
+# 1.2 If the size of the hash table equals the maximum
+# in-memory size:
+# 1.2.1 Scan the probe input S, and add matching join
+# tuples to the output relation
+# 1.2.2 Reset the hash table, and continue scanning the
+# build input R
+# 2. Do a final scan of the probe input S and add the resulting
+# join tuples to the output relation
+# Example
+# Input:
+#
+# @player_ages = (
+# [20, "Alex" ],
+# [28, "Joe" ],
+# [38, "Mike" ],
+# [18, "Alex" ],
+# [25, "David" ],
+# [18, "Simon" ],
+# );
+#
+# @player_names = (
+# ["Alex", "Stewart"],
+# ["Joe", "Root" ],
+# ["Mike", "Gatting"],
+# ["Joe", "Blog" ],
+# ["Alex", "Jones" ],
+# ["Simon","Duane" ],
+# );
+#
+# Output:
+#
+# Based on index = 1 of @players_age and
+# index = 0 of @players_name.
+#
+# 20, "Alex", "Stewart"
+# 20, "Alex", "Jones"
+# 18, "Alex", "Stewart"
+# 18, "Alex", "Jones"
+# 28, "Joe", "Root"
+# 28, "Joe", "Blog"
+# 38, "Mike", "Gatting"
+# 18, "Simon", "Duane"
+
+player_ages = [
+ [20, "Alex" ],
+ [28, "Joe" ],
+ [38, "Mike" ],
+ [18, "Alex" ],
+ [25, "David" ],
+ [18, "Simon" ],
+]
+player_ages_key = 1
+
+player_names = [
+ ["Alex", "Stewart"],
+ ["Joe", "Root" ],
+ ["Mike", "Gatting"],
+ ["Joe", "Blog" ],
+ ["Alex", "Jones" ],
+ ["Simon","Duane" ],
+]
+player_names_key = 0
+
+def format_col(s):
+ if type(s) == int:
+ return str(s)
+ else:
+ return '"'+s+'"'
+
+def hash_join(table1, key1, table2, key2):
+ build = {}
+ for row in table1:
+ key = row[key1]
+ if key not in build:
+ build[key] = []
+ build[key].append(row)
+ result = []
+ for row_probe in table2:
+ key = row_probe[key2]
+ for row_build in build[key]:
+ row = row_build + \
+ row_probe[:key2] + \
+ row_probe[key2+1:]
+ row = [format_col(x) for x in row]
+ result.append(row)
+ return result
+
+result = hash_join(player_ages, player_ages_key,
+ player_names, player_names_key)
+for row in result:
+ print(', '.join(row))
diff --git a/challenge-132/paulo-custodio/t/test-1.yaml b/challenge-132/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..32cff5c56f
--- /dev/null
+++ b/challenge-132/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,15 @@
+- setup:
+ cleanup:
+ args: 2021/09/18 2021/09/22
+ input:
+ output: 2021/09/14, 2021/09/26
+- setup:
+ cleanup:
+ args: 1975/10/10 2021/09/22
+ input:
+ output: 1929/10/27, 2067/09/05
+- setup:
+ cleanup:
+ args: 1967/02/14 2021/09/22
+ input:
+ output: 1912/07/08, 2076/04/30
diff --git a/challenge-132/paulo-custodio/t/test-2.yaml b/challenge-132/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..f57a59a386
--- /dev/null
+++ b/challenge-132/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,13 @@
+- setup:
+ cleanup:
+ args:
+ input:
+ output: |
+ 20, "Alex", "Stewart"
+ 18, "Alex", "Stewart"
+ 28, "Joe", "Root"
+ 38, "Mike", "Gatting"
+ 28, "Joe", "Blog"
+ 20, "Alex", "Jones"
+ 18, "Alex", "Jones"
+ 18, "Simon", "Duane" \ No newline at end of file
diff --git a/challenge-132/paulo-custodio/test.pl b/challenge-132/paulo-custodio/test.pl
new file mode 100644
index 0000000000..ba6c37260b
--- /dev/null
+++ b/challenge-132/paulo-custodio/test.pl
@@ -0,0 +1,4 @@
+#!/usr/bin/env perl
+use Modern::Perl;
+use Test::More;
+require '../../challenge-001/paulo-custodio/test.pl';