diff options
| -rwxr-xr-x | challenge-123/roger-bell-west/perl/ch-1.pl | 28 | ||||
| -rwxr-xr-x | challenge-123/roger-bell-west/perl/ch-2.pl | 83 | ||||
| -rw-r--r-- | challenge-123/roger-bell-west/postscript/ch-1.ps | 53 | ||||
| -rwxr-xr-x | challenge-123/roger-bell-west/python/ch-1.py | 29 | ||||
| -rwxr-xr-x | challenge-123/roger-bell-west/python/ch-2.py | 58 | ||||
| -rwxr-xr-x | challenge-123/roger-bell-west/raku/ch-1.p6 | 25 | ||||
| -rwxr-xr-x | challenge-123/roger-bell-west/raku/ch-2.p6 | 85 | ||||
| -rwxr-xr-x | challenge-123/roger-bell-west/ruby/ch-1.rb | 35 | ||||
| -rwxr-xr-x | challenge-123/roger-bell-west/ruby/ch-2.rb | 86 | ||||
| -rwxr-xr-x | challenge-123/roger-bell-west/rust/ch-1.rs | 40 | ||||
| -rwxr-xr-x | challenge-123/roger-bell-west/rust/ch-2.rs | 91 |
11 files changed, 613 insertions, 0 deletions
diff --git a/challenge-123/roger-bell-west/perl/ch-1.pl b/challenge-123/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..9e35c9aaa0 --- /dev/null +++ b/challenge-123/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,28 @@ +#! /usr/bin/perl + +use strict; +use warnings FATAL => 'all'; + +use Test::More tests => 3; +use List::Util qw(min); + +is(un(7),8,'example 1'); +is(un(10),12,'example 2'); +is(un(200),16200,'example 3'); + +sub un { + my $m=shift; + my $n; + my @s=([1],[1],[1]); + my @c=(2,3,5); + foreach (1..$m) { + $n=min($s[0][0],$s[1][0],$s[2][0]); + foreach my $i (0..2) { + if ($s[$i][0]==$n) { + shift @{$s[$i]}; + } + push @{$s[$i]},$n*$c[$i]; + } + } + return $n; +} diff --git a/challenge-123/roger-bell-west/perl/ch-2.pl b/challenge-123/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..2d3d81b994 --- /dev/null +++ b/challenge-123/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,83 @@ +#! /usr/bin/perl + +use strict; +use warnings; + +use Test::More tests => 4; + +use List::MoreUtils qw(minmax); +use Math::Trig qw(rad2deg); + +is(sp([ + [10,20], + [20,20], + [20,10], + [10,10], + ]),1,'example 1'); + +is(sp([ + [12,24], + [16,10], + [20,12], + [18,16], + ]),0,'example 2'); + +is(sp([ + [10,10], + [20,0], + [30,10], + [20,20], + ]),1,'example 3'); + +is(sp([ + [0,0], + [3,4], + [8,4], + [5,0], + ]),0,'example 4'); + +sub sp { + my $m=shift; + foreach my $ordering ([0,1,2,3,0],[0,1,3,2,0],[0,2,1,3,0]) { + my @w=map {$m->[$ordering->[$_%4]]} (0..4); + # check for equal sides + my @ds; + foreach my $pp (0..3) { + push @ds,($w[$pp+1][0]-$w[$pp][0])**2+($w[$pp+1][1]-$w[$pp][1])**2; + } + my @t=minmax(@ds); + if ($t[0] != $t[1]) { + next; + } + # check for equal right angles + my @angles; + foreach my $pp (0..3) { + my @delta=map {$w[$pp+1][$_]-$w[$pp][$_]} (0,1); + my $angle; + if ($delta[0]==0) { + if ($delta[1]==0) { # coincident points + return 0; + } + $angle=$delta[1]>0?90:-90; + } else { + $angle=rad2deg(atan2($delta[1],$delta[0])); + } + push @angles,$angle; + } + push @angles,$angles[0]; + my $good=1; + my @deltas=map {($angles[$_+1]-$angles[$_]+360)%360} (0..3); + if ($deltas[0]!=90 && $deltas[0] != 270) { + next; + } else { + foreach my $di (1..3) { + if ($deltas[$di] != $deltas[0]) { + $good=0; + last; + } + } + } + return $good; + } + return 0; +} diff --git a/challenge-123/roger-bell-west/postscript/ch-1.ps b/challenge-123/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..783fc87ced --- /dev/null +++ b/challenge-123/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,53 @@ +%! Not DSC compliant + +/un { + /c exch def + 0 + { + 1 add + dup + [ 2 3 5 ] { + /k exch def + { + dup k mod 0 eq { + k div cvi + } { + exit + } ifelse + } loop + } forall + 1 eq { + /c c 1 sub def + } if + c 0 le { + exit + } if + } loop +} def + +/str 50 string def +/fs 12 def +/Helvetica findfont +fs scalefont setfont +20 750 translate +/line 0 def +/testnum 1 def + +/disptest { + 0 line moveto testnum str cvs show (.) show + dup + 50 line moveto str cvs show + un + dup + 100 line moveto str cvs show + eq { (Pass) } { (FAIL) } ifelse + 150 line moveto show + /line line fs 2 mul sub def + /testnum testnum 1 add def +} def + +8 7 disptest +12 10 disptest +16200 200 disptest + +showpage diff --git a/challenge-123/roger-bell-west/python/ch-1.py b/challenge-123/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..f54c43e6b4 --- /dev/null +++ b/challenge-123/roger-bell-west/python/ch-1.py @@ -0,0 +1,29 @@ +#! /usr/bin/python3 + +import unittest +from collections import deque + +def un(m): + n=0 + s=[deque([1]),deque([1]),deque([1])] + c=[2,3,5] + for j in range(0,m): + n=min(s[0][0],s[1][0],s[2][0]) + for i in range(0,2+1): + if s[i][0]==n: + s[i].popleft() + s[i].append(n*c[i]) + return n + +class TestUn(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(un(7),8,'example 1') + + def test_ex2(self): + self.assertEqual(un(10),12,'example 2') + + def test_ex3(self): + self.assertEqual(un(200),16200,'example 3') + +unittest.main() diff --git a/challenge-123/roger-bell-west/python/ch-2.py b/challenge-123/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..d3210135ef --- /dev/null +++ b/challenge-123/roger-bell-west/python/ch-2.py @@ -0,0 +1,58 @@ +#! /usr/bin/python3 + +import unittest + +from math import degrees,atan2 + +def sp(m): + for ordering in [ + [0,1,2,3,0], + [0,1,3,2,0], + [0,2,1,3,0], + ]: + w=[m[ordering[i%4]] for i in range(5)] + ds=[] + for pp in range(4): + ds.append((w[pp+1][0]-w[pp][0])**2+(w[pp+1][1]-w[pp][1])**2) + if min(ds) != max(ds): + next + angles=[] + for pp in range(4): + delta=[w[pp+1][i]-w[pp][i] for i in range(2)] + if delta[0]==0: + if delta[1]==0: + return 0 + angle=90 + if delta[1]<0: + angle=-90 + else: + angle=degrees(atan2(delta[1],delta[0])) + angles.append(angle) + angles.append(angles[0]) + good=1 + deltas=[(angles[i+1]-angles[i]+360) % 360 for i in range(4)] + if deltas[0] != 90 and deltas[0] != 270: + continue + else: + for di in range(1,4): + if deltas[di] != deltas[0]: + good=0 + break + return good + return 0 + +class TestSp(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(sp([[10,20],[20,20],[20,10],[10,10]]),1,'example 1') + + def test_ex2(self): + self.assertEqual(sp([[12,24],[16,10],[20,12],[18,16]]),0,'example 2') + + def test_ex3(self): + self.assertEqual(sp([[10,10],[20,0],[30,10],[20,20]]),1,'example 3') + + def test_ex4(self): + self.assertEqual(sp([[0,0],[3,4],[8,4],[5,0]]),0,'example 4') + +unittest.main() diff --git a/challenge-123/roger-bell-west/raku/ch-1.p6 b/challenge-123/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..0b74cae8bc --- /dev/null +++ b/challenge-123/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,25 @@ +#! /usr/bin/perl6 + +use Test; + +plan 3; + +is(un(7),8,'example 1'); +is(un(10),12,'example 2'); +is(un(200),16200,'example 3'); + +sub un($m) { + my $n; + my @s=([1],[1],[1]); + my @c=(2,3,5); + for (1..$m) { + $n=min(@s[0][0],@s[1][0],@s[2][0]); + for 0..2 -> $i { + if (@s[$i][0] == $n) { + shift @s[$i]; + } + push @s[$i],$n*@c[$i]; + } + } + return $n; +} diff --git a/challenge-123/roger-bell-west/raku/ch-2.p6 b/challenge-123/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..ce9df2da9b --- /dev/null +++ b/challenge-123/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,85 @@ +#! /usr/bin/perl6 + +use Test; + +plan 4; + +is(sp([ + [10,20], + [20,20], + [20,10], + [10,10], + ]),1,'example 1'); + +is(sp([ + [12,24], + [16,10], + [20,12], + [18,16], + ]),0,'example 2'); + +is(sp([ + [10,10], + [20,0], + [30,10], + [20,20], + ]),1,'example 3'); + +is(sp([ + [0,0], + [3,4], + [8,4], + [5,0], + ]),0,'example 4'); + +sub sp(@m) { + for ([0,1,2,3,0],[0,1,3,2,0],[0,2,1,3,0]) -> @ordering { + my @w=map {@m[@ordering[$_ % 4]]},(0..4); + # check for equal sides + my @ds; + for (0..3) -> $pp { + push @ds,(@w[$pp+1][0]-@w[$pp][0])**2+(@w[$pp+1][1]-@w[$pp][1])**2; + } + if (min(@ds) != max(@ds)) { + next; + } + # check for equal right angles + my @angles; + for 0..3 -> $pp { + my @delta=map {@w[$pp+1][$_]-@w[$pp][$_]},(0,1); + my $angle; + if (@delta[0]==0) { + if (@delta[1]==0) { # coincident points + return 0; + } + $angle=@delta[1]>0 ?? 90 !! -90; + } else { + $angle=atan2(@delta[1],@delta[0])*180/pi; + } + push @angles,$angle; + } + push @angles,@angles[0]; + my $good=1; + my @deltas=map {(@angles[$_+1]-@angles[$_]+360)%360},(0..3); + if (@deltas[0] != 90 && @deltas[0] != 270) { + next; + } else { + for 1..3 -> $di { + if (@deltas[$di] != @deltas[0]) { + $good=0; + last; + } + } + } + return $good; + } + return 0; +} + +sub eqish($a,$b) { + if ((abs($a-$b)/$a) < 0.0001) { + return 1; + } else { + return 0; + } +}
\ No newline at end of file diff --git a/challenge-123/roger-bell-west/ruby/ch-1.rb b/challenge-123/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..d01c20c137 --- /dev/null +++ b/challenge-123/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,35 @@ +#! /usr/bin/ruby + +def un(m) + n=0 + s=[[1],[1],[1]] + c=[2,3,5] + 1.upto(m) do + n=[s[0][0],s[1][0],s[2][0]].min + 0.upto(2) do |i| + if s[i][0]==n then + s[i].shift + end + s[i].push(n*c[i]) + end + end + return n +end + +require 'test/unit' + +class TestUn < Test::Unit::TestCase + + def test_ex1 + assert_equal(8,un(7)) + end + + def test_ex2 + assert_equal(12,un(10)) + end + + def test_ex3 + assert_equal(16200,un(200)) + end + +end diff --git a/challenge-123/roger-bell-west/ruby/ch-2.rb b/challenge-123/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..85a8e02b98 --- /dev/null +++ b/challenge-123/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,86 @@ +#! /usr/bin/ruby + +def sp(m) + [[0,1,2,3,0],[0,1,3,2,0],[0,2,1,3,0]].each do |ordering| + w=0.upto(4).map{|i| m[ordering[i%4]]} + ds=[] + 0.upto(3) do |pp| + ds.push((w[pp+1][0]-w[pp][0])**2+(w[pp+1][1]-w[pp][1])**2) + end + t=ds.minmax + if t[0] != t[1] then + next + end + rad2deg=90/Math.acos(0) + angles=[] + 0.upto(3) do |pp| + delta=0.upto(1).map{|i| w[pp+1][i]-w[pp][i]} + if delta[0]==0 then + if delta[1]==0 then + return 0 + end + angle=delta[1]>0 ? 90 : -90 + else + angle=Math.atan2(delta[1],delta[0])*rad2deg + end + angles.push(angle) + end + angles.push(angles[0]) + good=1 + deltas=0.upto(3).map{|i| (angles[i+1]-angles[i]+360)%360} + if deltas[0] != 90 and deltas[0] != 270 then + next + else + 1.upto(3) do |di| + if deltas[di] != deltas[0] then + good=0 + break + end + end + end + return good + end + return 0 +end + +require 'test/unit' + +class TestSp < Test::Unit::TestCase + + def test_ex1 + assert_equal(1,sp([ + [10,20], + [20,20], + [20,10], + [10,10] + ])) + end + + def test_ex2 + assert_equal(0,sp([ + [12,24], + [16,10], + [20,12], + [18,16] + ])) + end + + def test_ex3 + assert_equal(1,sp([ + [10,10], + [20,0], + [30,10], + [20,20] + ])) + end + + def test_ex4 + assert_equal(0,sp([ + [0,0], + [3,4], + [8,4], + [5,0] + ])) + end + +end diff --git a/challenge-123/roger-bell-west/rust/ch-1.rs b/challenge-123/roger-bell-west/rust/ch-1.rs new file mode 100755 index 0000000000..d02ee20c77 --- /dev/null +++ b/challenge-123/roger-bell-west/rust/ch-1.rs @@ -0,0 +1,40 @@ +#! /bin/sh +//usr/bin/env rustc --test $0 -o ${0}x && ./${0}x; rm -f ${0}x ; exit + +use std::collections::VecDeque; + +#[test] +fn test_ex1() { + assert_eq!(un(7),8); +} + +#[test] +fn test_ex2() { + assert_eq!(un(10),12); +} + +#[test] +fn test_ex3() { + assert_eq!(un(200),16200); +} + +fn un(m: u64) -> u64 { + let mut n=0; + let mut s: Vec<VecDeque<u64>>=vec![]; + for _j in 0..=2 { + let mut p: VecDeque<u64>=VecDeque::new(); + p.push_back(1); + s.push(p); + } + let c=vec![2,3,5]; + for _j in 1..=m { + n=*[s[0][0],s[1][0],s[2][0]].iter().min().unwrap(); + for i in 0..=2 { + if s[i][0]==n { + s[i].pop_front(); + } + s[i].push_back(n*c[i]); + } + } + return n; +} diff --git a/challenge-123/roger-bell-west/rust/ch-2.rs b/challenge-123/roger-bell-west/rust/ch-2.rs new file mode 100755 index 0000000000..a31f62f36f --- /dev/null +++ b/challenge-123/roger-bell-west/rust/ch-2.rs @@ -0,0 +1,91 @@ +#! /bin/sh +//usr/bin/env rustc --test $0 -o ${0}x && ./${0}x; rm -f ${0}x ; exit + +#[test] +fn test_ex1() { + assert_eq!(sp(vec![ + vec![10.0,20.0], + vec![20.0,20.0], + vec![20.0,10.0], + vec![10.0,10.0] + ]),1); +} + +#[test] +fn test_ex2() { + assert_eq!(sp(vec![ + vec![12.0,24.0], + vec![16.0,10.0], + vec![20.0,12.0], + vec![18.0,16.0] + ]),0); +} + +#[test] +fn test_ex3() { + assert_eq!(sp(vec![ + vec![10.0,10.0], + vec![20.0,0.0], + vec![30.0,10.0], + vec![20.0,20.0] + ]),1); +} + +#[test] +fn test_ex4() { + assert_eq!(sp(vec![ + vec![0.0,0.0], + vec![3.0,4.0], + vec![8.0,4.0], + vec![5.0,0.0] + ]),0); +} + +fn sp(m: Vec<Vec<f64>>) -> u8 { + for ordering in &[ + vec![0,1,2,3,0], + vec![0,1,3,2,0], + vec![0,2,1,3,0] + ] { + let w: Vec<Vec<f64>>=(0..=4).map(|i| m[ordering[i%4]].clone()).collect(); + let mut ds: Vec<f64>=vec![]; + for pp in 0..=3 { + ds.push((w[pp+1][0]-w[pp][0])*(w[pp+1][0]-w[pp][0])+(w[pp+1][1]-w[pp][1])*(w[pp+1][1]-w[pp][1])); + } + ds.sort_by(|a,b| a.partial_cmp(b).unwrap()); + if ds[0] != ds[3] { + continue; + } + let mut angles: Vec<f64>=vec![]; + for pp in 0..=3 { + let angle: f64; + let delta: Vec<f64>=(0..=1).map(|i| w[pp+1][i]-w[pp][i]).collect(); + if delta[0]==0.0 { + if delta[1]==0.0 { + return 0; + } + angle=if delta[1]>0.0 {90.0} else {-90.0}; + } else { + angle=delta[1].atan2(delta[0]).to_degrees(); + } + angles.push(angle); + } + println!("{:?}",angles); + angles.push(angles[0]); + let mut good: u8=1; + let deltas: Vec<f64>=(0..=3).map(|i| (angles[i+1]-angles[i]+360.0)%360.0).collect(); + println!("{:?}",deltas); + if deltas[0] != 90.0 && deltas[0] != 270.0 { + continue; + } else { + for di in 1..=3 { + if deltas[di] != deltas[0] { + good=0; + break; + } + } + } + return good; + } + return 0; +} |
