diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2021-02-21 23:54:52 +0000 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2021-02-21 23:54:52 +0000 |
| commit | e1063e0ee6699cb5674339fe60ae49cde4160a0a (patch) | |
| tree | d99d0072f68a4eef81e850d802417e884912f623 /challenge-100 | |
| parent | 4bd3b1032e4ca56ecfa05e942c4cb9b0dd9f52e1 (diff) | |
| download | perlweeklychallenge-club-e1063e0ee6699cb5674339fe60ae49cde4160a0a.tar.gz perlweeklychallenge-club-e1063e0ee6699cb5674339fe60ae49cde4160a0a.tar.bz2 perlweeklychallenge-club-e1063e0ee6699cb5674339fe60ae49cde4160a0a.zip | |
- Added solutions by Colin Crain.
Diffstat (limited to 'challenge-100')
| -rw-r--r-- | challenge-100/colin-crain/perl/ch-1.pl | 122 | ||||
| -rw-r--r-- | challenge-100/colin-crain/perl/ch-2.pl | 98 | ||||
| -rw-r--r-- | challenge-100/colin-crain/raku/ch-1.raku | 46 | ||||
| -rw-r--r-- | challenge-100/colin-crain/raku/ch-2.raku | 54 |
4 files changed, 320 insertions, 0 deletions
diff --git a/challenge-100/colin-crain/perl/ch-1.pl b/challenge-100/colin-crain/perl/ch-1.pl new file mode 100644 index 0000000000..57866a9240 --- /dev/null +++ b/challenge-100/colin-crain/perl/ch-1.pl @@ -0,0 +1,122 @@ +#! /opt/local/bin/perl
+#
+# fun-time-ta-go-go.pl
+#
+# TASK #1 Ý Fun Time
+# Submitted by: Mohammad S Anwar
+# You are given a time (12 hour / 24 hour).
+#
+# Write a script to convert the given time from 12 hour format to 24
+# hour format and vice versa.
+#
+# Ideally we expect a one-liner.
+#
+# Example 1:
+# Input: 05:15 pm or 05:15pm
+# Output: 17:15
+# Example 2:
+# Input: 19:15
+# Output: 07:15 pm or 07:15pm
+#
+# © 2021 colin crain
+## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
+
+
+
+use warnings;
+use strict;
+use feature ":5.26";
+
+
+sub timef {
+ local $_= shift;
+
+ /^ (\d+)(:\d+)\s?(am|pm)* $/xi;
+
+ my $hr = $1;
+ my $cyc = 'am';
+
+ if ( $3 ) { ## 12hr->24hr
+ $hr = sprintf "%02d", $1 % 12;
+ $hr += 12 if $3 eq 'pm';
+ return "$hr$2";
+ }
+ else { ## 24hr->12hr
+ $cyc = 'pm' if $hr > 11;
+ $hr %= 12;
+ $hr ||= 12;
+ return "$hr$2$cyc";
+ }
+}
+
+sub timef_refactored {
+ local $_= shift;
+
+ /^ (\d+)(:\d+)\s?(am|pm)* $/xi;
+
+ my $cyc = $1 > 11 ? 'pm' : 'am';
+ my $hr = $1 % 12;
+
+ if ( $3 ) {
+ $hr += 12 if $3 eq 'pm';
+ return sprintf "%02d%s", $hr, $2;
+ }
+ else {
+ $hr ||= 12;
+ return "$hr$2$cyc";
+ }
+}
+
+sub timefl {
+ no strict 'vars';
+ local $_ = shift;
+ /^(\d+)(:\d+)\s?(am|pm)*$/i;
+ $c=$1>11?'pm':'am';
+ $h=$1%12;
+ if($3){
+ $h+=12if$3eq"pm";
+ sprintf"%02d%s",$h,$2;
+ }
+ else{
+ $h||=12;
+ "$h$2$c";
+ }
+}
+
+=pod
+
+as a one-liner we need to change the I/O a bit:
+
+perl -E'@ARGV[0]=~/^(\d+)(:\d+)\s?(am|pm)*$/i;$c=$1>11?'pm':'am';$h=$1%12;if($3){$3eq"pm"and$h+=12;printf"%02d%s\n",$h,$2;}else{$h||=12;say"$h$2$c"}'
+
+perl -E '$_=@ARGV[0];/^(\d+)(:\d+)\s?(am|pm)*$/i;$c=$1>11?'pm':'am';$h=$1%12;if($3){$3eq"pm"and$h+=12;printf"%02d%s\n",$h,$2}else{$h||=12;say"$h$2$c"}
+
+=cut
+
+use Test::More;
+
+is timef("5:15AM"), "05:15", '12->24am';
+is timef("5:15 am"), "05:15", '12->24am space';
+is timef("5:15pm"), "17:15", '12->24pm';
+is timef("5:15 pm"), "17:15", '12->24pm space';
+is timef("12:15am"), "00:15", '12->24am-mid';
+is timef("12:15pm"), "12:15", '12->24pm-noon';
+is timef("5:15"), "5:15am", '24->12am';
+is timef("17:15"), "5:15pm", '12->24am';
+is timef("12:00"), "12:00pm", '24->12-noon';
+is timef("00:00"), "12:00am", '24->12-mid';
+say '';
+is timefl("5:15AM"), "05:15", '12->24am';
+is timefl("5:15 am"), "05:15", '12->24am space';
+is timefl("5:15pm"), "17:15", '12->24pm';
+is timefl("5:15 pm"), "17:15", '12->24pm space';
+is timefl("12:15am"), "00:15", '12->24am-mid';
+is timefl("12:15pm"), "12:15", '12->24pm-noon';
+
+is timefl("5:15"), "5:15am", '24->12am';
+is timefl("17:15"), "5:15pm", '12->24am';
+is timefl("12:00"), "12:00pm", '24->12-noon';
+is timefl("00:00"), "12:00am", '24->12-mid';
+
+
+done_testing();
diff --git a/challenge-100/colin-crain/perl/ch-2.pl b/challenge-100/colin-crain/perl/ch-2.pl new file mode 100644 index 0000000000..360f83b29b --- /dev/null +++ b/challenge-100/colin-crain/perl/ch-2.pl @@ -0,0 +1,98 @@ +#! /opt/local/bin/perl
+#
+# pyramid-power.pl
+#
+# Triangle Sum
+# Submitted by: Mohammad S Anwar
+# You are given triangle array.
+#
+# Write a script to find the minimum path sum from top to bottom.
+#
+# When you are on index i on the current row then you may move to either index i or index i + 1 on the next row.
+#
+# Example 1:
+# Input: Triangle = [ [1], [2,4], [6,4,9], [5,1,7,2] ]
+# Output: 8
+#
+# Explanation: The given triangle
+#
+# 1
+# 2 4
+# 6 4 9
+# 5 1 7 2
+#
+# The minimum path sum from top to bottom: 1 + 2 + 4 + 1 = 8
+#
+# [1]
+# [2] 4
+# 6 [4] 9
+# 5 [1] 7 2
+#
+# Example 2:
+# Input: Triangle = [ [3], [3,1], [5,2,3], [4,3,1,3] ]
+# Output: 7
+#
+# Explanation: The given triangle
+#
+# 3
+# 3 1
+# 5 2 3
+# 4 3 1 3
+#
+# The minimum path sum from top to bottom: 3 + 1 + 2 + 1 = 7
+#
+# [3]
+# 3 [1]
+# 5 [2] 3
+# 4 3 [1] 3
+#
+# © 2021 colin crain
+## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
+
+
+
+use warnings;
+use strict;
+use feature ":5.26";
+use feature qw(signatures);
+no warnings 'experimental::signatures';
+
+
+sub findpath ($arr) {
+## the data structure: [ sum, [arr of values along path], last index ]
+
+ my $root = $arr->[0][0];
+ my @data = [$root, [$root], 0];
+
+ for my $depth ( 0..$#$arr-1 ) {
+ for my $pos ( 0..2**$depth-1 ) {
+ my $path = shift @data;
+ for (0,1) {
+ my ($sum, $trace, $last) = @$path;
+ my $value = $arr->[$depth+1][$last+$_];
+ my $newpath = [ $sum + $value,
+ [$trace->@*, $value],
+ $last + $_ ];
+ push @data, $newpath;
+ }
+ }
+ }
+
+ my $minpath = (sort {$a->[0] <=> $b->[0]} @data)[0];
+
+ say "minimum path sum: ", $minpath->[0];
+ say "path:";
+ say join ' -> ', $minpath->[1]->@*;
+
+ return $minpath->[0];
+}
+
+
+use Test::More;
+
+is findpath( [[1], [2,4], [6,4,9], [5,1,7,2]] ), 8, 'ex-1';
+is findpath( [[3], [3,1], [5,2,3], [4,3,1,3]] ), 7, 'ex-2';
+
+
+
+done_testing();
diff --git a/challenge-100/colin-crain/raku/ch-1.raku b/challenge-100/colin-crain/raku/ch-1.raku new file mode 100644 index 0000000000..57e353ef4f --- /dev/null +++ b/challenge-100/colin-crain/raku/ch-1.raku @@ -0,0 +1,46 @@ +#!/usr/bin/env perl6 +# +# +# time-travel.raku +# +# one-liner: +# +# raku -e '$_=@*ARGS[0];m/^(\d+)(":"\d+)\s?(am|pm)*$/;my$c=$0>11??"pm"!!"am";my$h=$0%12;if $2 {$2~~"pm"and$h+=12;"%02d%s\n".printf($h,$1)}else{$h||=12;"$h$1$c".say}' +# +# © 2021 colin crain +## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## + + + +unit sub MAIN () ; + +use Test; + +plan 8; + +is timef("5:15am"), "05:15", '12->24am'; +is timef("5:15pm"), "17:15", '12->24pm'; +is timef("12:15am"), "00:15", '12->24am-mid'; +is timef("12:15pm"), "12:15", '12->24pm-noon'; +is timef("5:15"), "5:15am", '24->12am'; +is timef("17:15"), "5:15pm", '12->24am'; +is timef("12:00"), "12:00pm", '24->12-noon'; +is timef("00:00"), "12:00am", '24->12-mid'; + + + +sub timef ($_) { + m:i/^ (\d+) (":"\d+) \s? (am|pm)* $/; + my $c = $0 > 11 ?? 'pm' + !! 'am'; + my $h = $0 % 12; + if ($2) { + $h += 12 if $2 eq "pm"; + "%02d%s".sprintf($h,$1); + } + else{ + $h ||= 12; + "$h$1$c"; + } +} + diff --git a/challenge-100/colin-crain/raku/ch-2.raku b/challenge-100/colin-crain/raku/ch-2.raku new file mode 100644 index 0000000000..3171b0d458 --- /dev/null +++ b/challenge-100/colin-crain/raku/ch-2.raku @@ -0,0 +1,54 @@ +#!/usr/bin/env perl6 +# +# +# pyramid-power.raku +# +# +# +# © 2021 colin crain +## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## + + + +unit sub MAIN () ; + +use Test; +plan 3; + +is findpath( [[1], [2,3], [4,5,6], [7,8,9,10]] ), 14, "BFO triangle test"; +is findpath( [[1], [2,4], [6,4,9], [5,1,7,2]] ) , 8, "ex-1"; +is findpath( [[3], [3,1], [5,2,3], [4,3,1,3]] ) , 7, 'ex-2'; + +sub findpath (@arr) { +## the data structure: [ sum, [arr of values along path], last index ] + + my $root = @arr[0][0]; + my @root = [$root, [$root], 0]; + my @data.push: @root; + + for 0..@arr.elems-2 -> $depth { + for 0..2**$depth-1 -> $pos { + my $path = @data.shift; + for 0,1 { + my ($sum, $trace, $last) = $path; + my $value = @arr[$depth+1][$last+$_]; + my @newpath = $sum + $value, + [|$trace, $value], + $last + $_ ; + push @data, @newpath; + } + } + } + + ## a little verbose output + my $minpath = @data.min( { $^a[0] } ); + + say ''; + say "minimum path sum: ", $minpath[0]; + say "path:"; + say $minpath[1].join: ' -> '; + + ## the requested value + return $minpath[0]; +} + |
