diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-09-27 20:26:36 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-09-27 20:26:36 +0100 |
| commit | 7cbb35a0b17f38f742ac5d58f1f1a6f998d6599b (patch) | |
| tree | 824fc0a5178e09ce771e003dc51415cb103b6dcf | |
| parent | 5fcd70c1a734cb266d63fe0512bf9cfeb1efe04b (diff) | |
| parent | ae6d43c0c10007b66010c469f849be430ef4f015 (diff) | |
| download | perlweeklychallenge-club-7cbb35a0b17f38f742ac5d58f1f1a6f998d6599b.tar.gz perlweeklychallenge-club-7cbb35a0b17f38f742ac5d58f1f1a6f998d6599b.tar.bz2 perlweeklychallenge-club-7cbb35a0b17f38f742ac5d58f1f1a6f998d6599b.zip | |
Merge pull request #4938 from Firedrake/rogerbw-challenge-132
Solutions for challenge #132
| -rwxr-xr-x | challenge-132/roger-bell-west/perl/ch-1.pl | 56 | ||||
| -rwxr-xr-x | challenge-132/roger-bell-west/perl/ch-2.pl | 56 | ||||
| -rw-r--r-- | challenge-132/roger-bell-west/postscript/ch-1.ps | 89 | ||||
| -rwxr-xr-x | challenge-132/roger-bell-west/python/ch-1.py | 30 | ||||
| -rwxr-xr-x | challenge-132/roger-bell-west/python/ch-2.py | 27 | ||||
| -rwxr-xr-x | challenge-132/roger-bell-west/raku/ch-1.p6 | 42 | ||||
| -rwxr-xr-x | challenge-132/roger-bell-west/raku/ch-2.p6 | 58 | ||||
| -rwxr-xr-x | challenge-132/roger-bell-west/ruby/ch-1.rb | 51 | ||||
| -rwxr-xr-x | challenge-132/roger-bell-west/ruby/ch-2.rb | 64 | ||||
| -rw-r--r-- | challenge-132/roger-bell-west/rust/ch-1.rs | 35 |
10 files changed, 508 insertions, 0 deletions
diff --git a/challenge-132/roger-bell-west/perl/ch-1.pl b/challenge-132/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..fd29a18dd7 --- /dev/null +++ b/challenge-132/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,56 @@ +#! /usr/bin/perl + +use strict; + +use Time::Local; + +use Test::More tests => 3; + +is_deeply(mirdat( + [2021,9,18],[2021,9,22] + ), + [[2021,9,14],[2021,9,26]], + 'example 1' + ); + +is_deeply(mirdat( + [1975,10,10],[2021,9,22] + ), + [[1929,10,27],[2067,9,5]], + 'example 2' + ); + +is_deeply(mirdat( + [1967,2,14],[2021,9,22] + ), + [[1912,7,8],[2076,4,30]], + 'example 3' + ); + +sub mirdat { + my $then=shift; + my $thent=ymd2ut($then); + my $now=shift; + if ($now->[0]==0) { + $now=ut2ymd(time); + } + my $nowt=ymd2ut($now); + my $delta=$nowt-$thent; + my @o; + foreach my $targett ($thent-$delta,$nowt+$delta) { + push @o,ut2ymd($targett); + } + return \@o; +} + +sub ymd2ut { + my $ta=shift; + my ($y,$m,$d)=@{$ta}; + return timegm(0,0,0,$d,$m-1,$y); +} + +sub ut2ymd { + my $ut=shift; + my @t=localtime($ut); + return [$t[5]+1900,$t[4]+1,$t[3]]; +} diff --git a/challenge-132/roger-bell-west/perl/ch-2.pl b/challenge-132/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..7184e04f70 --- /dev/null +++ b/challenge-132/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,56 @@ +#! /usr/bin/perl + +use strict; + +use Test::More tests => 1; + +is_deeply(hj([ + [20, "Alex" ], + [28, "Joe" ], + [38, "Mike" ], + [18, "Alex" ], + [25, "David" ], + [18, "Simon" ], + ], + 1, + [ + ["Alex", "Stewart"], + ["Joe", "Root" ], + ["Mike", "Gatting"], + ["Joe", "Blog" ], + ["Alex", "Jones" ], + ["Simon","Duane" ], + ], + 0, + ),[ + [ 20, "Alex", "Stewart"], + [ 20, "Alex", "Jones"], + [ 18, "Alex", "Stewart"], + [ 18, "Alex", "Jones"], + [ 28, "Joe", "Root" ], + [ 28, "Joe", "Blog"], + [ 38, "Mike", "Gatting"], + [ 18, "Simon", "Duane"], + ],'example 1'); + +sub hj { + my @h; + my @i; + ($h[0],$i[0],$h[1],$i[1])=@_; + my %m; + foreach my $x (0,1) { + my $j=1-$i[$x]; + foreach my $y (0..$#{$h[$x]}) { + push @{$m{$h[$x][$y][$i[$x]]}[$x]},$h[$x][$y][$j]; + } + } + my @o; + foreach my $k (sort keys %m) { + foreach my $a (@{$m{$k}[0]}) { + foreach my $b (@{$m{$k}[1]}) { + push @o,[$a,$k,$b]; + } + } + } + return \@o; +} diff --git a/challenge-132/roger-bell-west/postscript/ch-1.ps b/challenge-132/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..7653b038db --- /dev/null +++ b/challenge-132/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,89 @@ +%!PS + +/dmy2jd { + /d exch def + /m exch def + /y exch def + /mn m 14 sub 12 idiv def + y 4800 add mn add 1461 mul 4 idiv + mn 12 mul neg 2 sub m add 367 mul 12 idiv add + y 4900 add mn add 100 idiv 3 mul 4 idiv sub + d add + 32075 sub +} def + +(test dmy2jd) = +2013 1 1 dmy2jd 2456294 eq { (Pass) } { (Fail) } ifelse = + +/jd2dmy { + /y 4716 def + /v 3 def + /j 1401 def + /u 5 def + /m 2 def + /s 153 def + /n 12 def + /w 2 def + /r 4 def + /B 274277 def + /p 1461 def + /C -38 def + dup + 4 mul B add 146097 idiv 3 mul 4 idiv C add j add add /f exch def + r f mul v add /e exch def + e p mod r idiv u mul w add /h exch def + /day h s mod u idiv 1 add def + /month h s idiv m add n mod 1 add def + /year e p idiv y sub n m add month sub n idiv add def + year month day +} def + +(test jd2dmy) = +2456294 jd2dmy +1 eq { (Pass) } { (Fail) } ifelse print ( ) print +1 eq { (Pass) } { (Fail) } ifelse print ( ) print +2013 eq { (Pass) } { (Fail) } ifelse = + +/mirdat { + /dn exch def + /mn exch def + /yn exch def + yn 0 eq { + (%Calendar%) currentdevparams dup dup + /Year get /yn exch def + /Month get /mn exch def + /Day get /dn exch def + } if + yn mn dn dmy2jd /tn exch def + dmy2jd /tt exch def + /delta tn tt sub def + tt delta sub jd2dmy + tn delta add jd2dmy +} def + +(test 1) = +2021 9 18 2021 9 22 mirdat +26 eq { (Pass) } { (Fail) } ifelse print ( ) print +9 eq { (Pass) } { (Fail) } ifelse print ( ) print +2021 eq { (Pass) } { (Fail) } ifelse print ( ) print +14 eq { (Pass) } { (Fail) } ifelse print ( ) print +9 eq { (Pass) } { (Fail) } ifelse print ( ) print +2021 eq { (Pass) } { (Fail) } ifelse = + +(test 2) = +1975 10 10 2021 9 22 mirdat +5 eq { (Pass) } { (Fail) } ifelse print ( ) print +9 eq { (Pass) } { (Fail) } ifelse print ( ) print +2067 eq { (Pass) } { (Fail) } ifelse print ( ) print +27 eq { (Pass) } { (Fail) } ifelse print ( ) print +10 eq { (Pass) } { (Fail) } ifelse print ( ) print +1929 eq { (Pass) } { (Fail) } ifelse = + +(test 3) = +1967 2 14 2021 9 22 mirdat +30 eq { (Pass) } { (Fail) } ifelse print ( ) print +4 eq { (Pass) } { (Fail) } ifelse print ( ) print +2076 eq { (Pass) } { (Fail) } ifelse print ( ) print +8 eq { (Pass) } { (Fail) } ifelse print ( ) print +7 eq { (Pass) } { (Fail) } ifelse print ( ) print +1912 eq { (Pass) } { (Fail) } ifelse = diff --git a/challenge-132/roger-bell-west/python/ch-1.py b/challenge-132/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..86f18098d6 --- /dev/null +++ b/challenge-132/roger-bell-west/python/ch-1.py @@ -0,0 +1,30 @@ +#! /usr/bin/python3 + +import unittest + +from datetime import date,timedelta + +def mirdat(then,now): + if now[0]==0: + nowt=date.today() + else: + nowt=date(*now) + thent=date(*then) + delta=nowt-thent + o=[] + for targett in [thent-delta,nowt+delta]: + o.append([targett.year,targett.month,targett.day]) + return o + +class TestMirdat(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(mirdat([2021,9,18],[2021,9,22]),[[2021,9,14],[2021,9,26]],'example 1') + + def test_ex2(self): + self.assertEqual(mirdat([1975,10,10],[2021,9,22]),[[1929,10,27],[2067,9,5]],'example 2') + + def test_ex3(self): + self.assertEqual(mirdat([1967,2,14],[2021,9,22]),[[1912,7,8],[2076,4,30]],'example 3') + +unittest.main() diff --git a/challenge-132/roger-bell-west/python/ch-2.py b/challenge-132/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..d54d79bfa1 --- /dev/null +++ b/challenge-132/roger-bell-west/python/ch-2.py @@ -0,0 +1,27 @@ +#! /usr/bin/python3 + +import unittest + +def hj(a,b,c,d): + h=[a,c] + i=[b,d] + m=dict() + for x in range(2): + j=1-i[x] + for y in range(len(h[x])): + if not h[x][y][i[x]] in m: + m[h[x][y][i[x]]]=[[],[]] + m[h[x][y][i[x]]][x].append(h[x][y][j]) + o=[] + for k in sorted(m.keys()): + for a in m[k][0]: + for b in m[k][1]: + o.append([a,k,b]) + return o + +class TestHj(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(hj([[20,"Alex"],[28,"Joe"],[38,"Mike"],[18,"Alex"],[25,"David"],[18,"Simon"]],1,[["Alex","Stewart"],["Joe","Root"],["Mike","Gatting"],["Joe","Blog"],["Alex","Jones"],["Simon","Duane"]],0),[[20,"Alex","Stewart"],[20,"Alex","Jones"],[18,"Alex","Stewart"],[18,"Alex","Jones"],[28,"Joe","Root"],[28,"Joe","Blog"],[38,"Mike","Gatting"],[18,"Simon","Duane"],],'example 1') + +unittest.main() diff --git a/challenge-132/roger-bell-west/raku/ch-1.p6 b/challenge-132/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..d867273244 --- /dev/null +++ b/challenge-132/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,42 @@ +#! /usr/bin/perl6 + +use Test; + +plan 3; + +is-deeply(mirdat( + [2021,9,18],[2021,9,22] + ), + [[2021,9,14],[2021,9,26]], + 'example 1' + ); + +is-deeply(mirdat( + [1975,10,10],[2021,9,22] + ), + [[1929,10,27],[2067,9,5]], + 'example 2' + ); + +is-deeply(mirdat( + [1967,2,14],[2021,9,22] + ), + [[1912,7,8],[2076,4,30]], + 'example 3' + ); + +sub mirdat(@then,@now) { + my $thent=Date.new(@then[0],@then[1],@then[2]); + my $nowt; + if (@now[0]==0) { + $nowt=Date.today; + } else { + $nowt=Date.new(@now[0],@now[1],@now[2]); + } + my $delta=$nowt.daycount-$thent.daycount; + my @o; + for $thent.earlier(day => $delta),$nowt.later(day => $delta) -> $targett { + push @o,[$targett.year,$targett.month,$targett.day]; + } + return @o; +} diff --git a/challenge-132/roger-bell-west/raku/ch-2.p6 b/challenge-132/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..5bec34c386 --- /dev/null +++ b/challenge-132/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,58 @@ +#! /usr/bin/perl6 + +use Test; + +plan 1; + +is-deeply(hj([ + [20, "Alex" ], + [28, "Joe" ], + [38, "Mike" ], + [18, "Alex" ], + [25, "David" ], + [18, "Simon" ], + ], + 1, + [ + ["Alex", "Stewart"], + ["Joe", "Root" ], + ["Mike", "Gatting"], + ["Joe", "Blog" ], + ["Alex", "Jones" ], + ["Simon","Duane" ], + ], + 0, + ),[ + [ 20, "Alex", "Stewart"], + [ 20, "Alex", "Jones"], + [ 18, "Alex", "Stewart"], + [ 18, "Alex", "Jones"], + [ 28, "Joe", "Root" ], + [ 28, "Joe", "Blog"], + [ 38, "Mike", "Gatting"], + [ 18, "Simon", "Duane"], + ],'example 1'); + +sub hj($a,$b,$c,$d) { + my @h=($a,$c); + my @i=($b,$d); + my %m; + for 0,1 -> $x { + my $j=1-@i[$x]; + for 0..@h[$x].end -> $y { + unless (%m{@h[$x][$y][@i[$x]]}:exists) { + %m{@h[$x][$y][@i[$x]]} = [Array.new,Array.new]; + } + push @(%m{@h[$x][$y][@i[$x]]}[$x]),@h[$x][$y][$j]; + } + } + my @o; + for (%m.keys.sort) -> $k { + for @(%m{$k}[0]) -> $a { + for @(%m{$k}[1]) -> $b { + push @o,[$a,$k,$b]; + } + } + } + return @o; +} diff --git a/challenge-132/roger-bell-west/ruby/ch-1.rb b/challenge-132/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..4f8a6336ec --- /dev/null +++ b/challenge-132/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,51 @@ +#! /usr/bin/ruby + +require 'date' + +def mirdat(thn,now) + thnt=Date.new(*thn) + if now[0]==0 then + nowt=Date.now + else + nowt=Date.new(*now) + end + delta=nowt-thnt + o=[] + for targett in [thnt-delta,nowt+delta] + o.push([targett.year,targett.month,targett.day]) + end + return o +end + +require 'test/unit' + +class TestMirdat < Test::Unit::TestCase + + def test_ex1 + assert_equal( + [[2021,9,14],[2021,9,26]], + mirdat( + [2021,9,18],[2021,9,22] + ) + ) + end + + def test_ex2 + assert_equal( + [[1929,10,27],[2067,9,5]], + mirdat( + [1975,10,10],[2021,9,22] + ) + ) + end + + def test_ex3 + assert_equal( + [[1912,7,8],[2076,4,30]], + mirdat( + [1967,2,14],[2021,9,22] + ) + ) + end + +end diff --git a/challenge-132/roger-bell-west/ruby/ch-2.rb b/challenge-132/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..ca96b4aba6 --- /dev/null +++ b/challenge-132/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,64 @@ +#! /usr/bin/ruby + +def hj(a,b,c,d) + h=[a,c] + i=[b,d] + m=Hash.new + 0.upto(1) do |x| + j=1-i[x] + 0.upto(h[x].length-1) do |y| + unless m.has_key?(h[x][y][i[x]]) then + m[h[x][y][i[x]]]=[[],[]] + end + m[h[x][y][i[x]]][x].push(h[x][y][j]) + end + end + o=[] + for k in m.sort_by {|kx, vx| kx} + for a in k[1][0] + for b in k[1][1] + o.push([a,k[0],b]) + end + end + end + return o +end + +require 'test/unit' + +class TestHj < Test::Unit::TestCase + + def test_ex1 + assert_equal(hj( + [ + [20, "Alex" ], + [28, "Joe" ], + [38, "Mike" ], + [18, "Alex" ], + [25, "David" ], + [18, "Simon" ], + ], + 1, + [ + ["Alex", "Stewart"], + ["Joe", "Root" ], + ["Mike", "Gatting"], + ["Joe", "Blog" ], + ["Alex", "Jones" ], + ["Simon","Duane" ], + ], + 0 + ),[ + [ 20, "Alex", "Stewart"], + [ 20, "Alex", "Jones"], + [ 18, "Alex", "Stewart"], + [ 18, "Alex", "Jones"], + [ 28, "Joe", "Root" ], + [ 28, "Joe", "Blog"], + [ 38, "Mike", "Gatting"], + [ 18, "Simon", "Duane"] + ] + ) + end + +end diff --git a/challenge-132/roger-bell-west/rust/ch-1.rs b/challenge-132/roger-bell-west/rust/ch-1.rs new file mode 100644 index 0000000000..1274163166 --- /dev/null +++ b/challenge-132/roger-bell-west/rust/ch-1.rs @@ -0,0 +1,35 @@ +use chrono::{Utc, TimeZone, Datelike}; + +// [dependencies] +// chrono = "0.4.19" + +#[test] +fn test_ex1() { + assert_eq!(mirdat((2021,9,18),(2021,9,22)),vec![(2021,9,14),(2021,9,26)]); +} + +#[test] +fn test_ex2() { + assert_eq!(mirdat((1975,10,10),(2021,9,22)),vec![(1929,10,27),(2067,9,5)]); +} + +#[test] +fn test_ex3() { + assert_eq!(mirdat((1967,2,14),(2021,9,22)),vec![(1912,7,8),(2076,4,30)]); +} + +fn mirdat(thn: (i32,u32,u32), now: (i32,u32,u32)) -> Vec<(i32,u32,u32)> { + let thnt=Utc.ymd(thn.0,thn.1,thn.2); + let nowt; + if now.0==0 { + nowt=Utc::today(); + } else { + nowt=Utc.ymd(now.0,now.1,now.2); + } + let delta=nowt-thnt; + let mut o: Vec<(i32,u32,u32)>=vec![]; + for targett in [thnt-delta,nowt+delta].iter() { + o.push((targett.year(),targett.month(),targett.day())); + } + return o; +} |
