aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-02-23 19:24:49 +0000
committerGitHub <noreply@github.com>2021-02-23 19:24:49 +0000
commitd24b75d409f95146f9040f5880d8d4ac543decca (patch)
treefebd73513fef4b20ca62a7003639326c4b65291f
parent791f8bef30156b790485c42da5cf1dd5fdff882f (diff)
parentd5197ab4ad2f0f72effa31ec1276013172012dc7 (diff)
downloadperlweeklychallenge-club-d24b75d409f95146f9040f5880d8d4ac543decca.tar.gz
perlweeklychallenge-club-d24b75d409f95146f9040f5880d8d4ac543decca.tar.bz2
perlweeklychallenge-club-d24b75d409f95146f9040f5880d8d4ac543decca.zip
Merge pull request #3605 from Firedrake/rogerbw-challenge-101
Solutions for challenge #101
-rwxr-xr-xchallenge-101/roger-bell-west/perl/ch-1.pl70
-rwxr-xr-xchallenge-101/roger-bell-west/perl/ch-2.pl31
-rwxr-xr-xchallenge-101/roger-bell-west/python/ch-1.py72
-rwxr-xr-xchallenge-101/roger-bell-west/python/ch-2.py30
-rwxr-xr-xchallenge-101/roger-bell-west/raku/ch-1.p668
-rwxr-xr-xchallenge-101/roger-bell-west/raku/ch-2.p630
-rwxr-xr-xchallenge-101/roger-bell-west/ruby/ch-1.rb80
-rwxr-xr-xchallenge-101/roger-bell-west/ruby/ch-2.rb36
-rwxr-xr-xchallenge-101/roger-bell-west/rust/ch-1.rs83
-rwxr-xr-xchallenge-101/roger-bell-west/rust/ch-2.rs35
10 files changed, 535 insertions, 0 deletions
diff --git a/challenge-101/roger-bell-west/perl/ch-1.pl b/challenge-101/roger-bell-west/perl/ch-1.pl
new file mode 100755
index 0000000000..9279421de9
--- /dev/null
+++ b/challenge-101/roger-bell-west/perl/ch-1.pl
@@ -0,0 +1,70 @@
+#! /usr/bin/perl
+
+use strict;
+use warnings;
+
+use Test::More tests => 3;
+
+is_deeply(pas([1..4]),[[1,2],[4,3]],'example 1');
+is_deeply(pas([1..6]),[[1,2,3],[6,5,4]],'example 2');
+is_deeply(pas([1..12]),[[1,2,3,4],[10,11,12,5],[9,8,7,6]],'example 3');
+
+sub pas {
+ my $a=shift;
+ my @a=reverse @{$a};
+ my $n=scalar @a;
+ my $f2=int(sqrt($n));
+ my $f1;
+ while (1) {
+ if ($n % $f2 == 0) {
+ $f1=$n/$f2;
+ last;
+ }
+ $f2--;
+ }
+ my @out;
+ foreach (1..$f2) {
+ push @out,[(0) x $f1];
+ }
+ my ($x,$y)=(-1,0);
+ my $maxx=$f1-1;
+ my $maxy=$f2-1;
+ my $minx=0;
+ my $miny=1;
+ ARR:
+ while (1) {
+ while ($x < $maxx) {
+ $x++;
+ $out[$y][$x]=pop @a;
+ unless (@a) {
+ last ARR;
+ }
+ }
+ $maxx-=1;
+ while ($y < $maxy) {
+ $y++;
+ $out[$y][$x]=pop @a;
+ unless (@a) {
+ last ARR;
+ }
+ }
+ $maxy-=1;
+ while ($x > $minx) {
+ $x--;
+ $out[$y][$x]=pop @a;
+ unless (@a) {
+ last ARR;
+ }
+ }
+ $minx++;
+ while ($y > $miny) {
+ $y--;
+ $out[$y][$x]=pop @a;
+ unless (@a) {
+ last ARR;
+ }
+ }
+ $miny++;
+ }
+ return \@out;
+}
diff --git a/challenge-101/roger-bell-west/perl/ch-2.pl b/challenge-101/roger-bell-west/perl/ch-2.pl
new file mode 100755
index 0000000000..e545e836d6
--- /dev/null
+++ b/challenge-101/roger-bell-west/perl/ch-2.pl
@@ -0,0 +1,31 @@
+#! /usr/bin/perl
+
+use strict;
+use warnings;
+
+use Test::More tests => 3;
+
+is_deeply(ot([0,1],[1,0],[2,2]),0,'example 1');
+is_deeply(ot([1,1],[-1,1],[0,-3]),1,'example 2');
+is_deeply(ot([0,1],[2,0],[-6,0]),1,'example 3');
+
+sub ot {
+ my @points=@_;
+ $points[3]=$points[0];
+ my @xp;
+ foreach my $i (0..2) {
+ push @xp,($points[$i][0] *
+ ($points[$i+1][1]-$points[$i][1]))
+ -
+ ($points[$i][1] *
+ ($points[$i+1][0]-$points[$i][0]));
+ }
+ @xp=sort @xp;
+ if ($xp[0]<=0 && $xp[2] <=0) {
+ return 1;
+ }
+ if ($xp[0]>=0 && $xp[2] >=0) {
+ return 1;
+ }
+ return 0;
+}
diff --git a/challenge-101/roger-bell-west/python/ch-1.py b/challenge-101/roger-bell-west/python/ch-1.py
new file mode 100755
index 0000000000..214318e188
--- /dev/null
+++ b/challenge-101/roger-bell-west/python/ch-1.py
@@ -0,0 +1,72 @@
+#! /usr/bin/python3
+
+import math
+
+def pas(aa):
+ a=aa[::-1]
+ n=len(a)
+ f2=int(math.sqrt(n))
+ f1=0
+ while 1:
+ if n % f2 == 0:
+ f1=int(n/f2)
+ break
+ f2 -= 1
+ out=list()
+ for i in range(f2):
+ out.append([0] * f1)
+ x=-1
+ y=0
+ maxx=f1-1
+ maxy=f2-1
+ minx=0
+ miny=1
+ cnt=1
+ while cnt:
+ while x < maxx:
+ x += 1
+ out[y][x]=a.pop()
+ if len(a)==0:
+ cnt=0
+ break
+ maxx -= 1
+ if cnt:
+ while y < maxy:
+ y += 1
+ out[y][x]=a.pop()
+ if len(a)==0:
+ cnt=0
+ break
+ maxy -= 1
+ if cnt:
+ while x > minx:
+ x -= 1
+ out[y][x]=a.pop()
+ if len(a)==0:
+ cnt=0
+ break
+ minx += 1
+ if cnt:
+ while y > miny:
+ y -= 1
+ out[y][x]=a.pop()
+ if len(a)==0:
+ cnt=0
+ break
+ miny += 1
+ return out
+
+import unittest
+
+class TestPas(unittest.TestCase):
+
+ def test_ex1(self):
+ self.assertEqual(pas([*range(1,4+1)]),[[1,2],[4,3]],'example 1')
+
+ def test_ex2(self):
+ self.assertEqual(pas([*range(1,6+1)]),[[1,2,3],[6,5,4]],'example 2')
+
+ def test_ex3(self):
+ self.assertEqual(pas([*range(1,12+1)]),[[1,2,3,4],[10,11,12,5],[9,8,7,6]],'example 3')
+
+unittest.main()
diff --git a/challenge-101/roger-bell-west/python/ch-2.py b/challenge-101/roger-bell-west/python/ch-2.py
new file mode 100755
index 0000000000..9c83201572
--- /dev/null
+++ b/challenge-101/roger-bell-west/python/ch-2.py
@@ -0,0 +1,30 @@
+#! /usr/bin/python3
+
+import unittest
+
+def ot(pp):
+ points=pp
+ points.append(points[0])
+ xp=list()
+ for i in range(3):
+ xp.append(points[i][0] * (points[i+1][1]-points[i][1])
+ -points[i][1]*(points[i+1][0]-points[i][0]))
+ xp.sort()
+ if xp[0] <= 0 and xp[2] <= 0:
+ return 1
+ if xp[0] >= 0 and xp[2] >= 0:
+ return 1
+ return 0
+
+class TestPas(unittest.TestCase):
+
+ def test_ex1(self):
+ self.assertEqual(ot([[0,1],[1,0],[2,2]]),0,'example 1')
+
+ def test_ex2(self):
+ self.assertEqual(ot([[1,1],[-1,1],[0,-3]]),1,'example 2')
+
+ def test_ex3(self):
+ self.assertEqual(ot([[0,1],[2,0],[-6,0]]),1,'example 3')
+
+unittest.main()
diff --git a/challenge-101/roger-bell-west/raku/ch-1.p6 b/challenge-101/roger-bell-west/raku/ch-1.p6
new file mode 100755
index 0000000000..11d534dd66
--- /dev/null
+++ b/challenge-101/roger-bell-west/raku/ch-1.p6
@@ -0,0 +1,68 @@
+#! /usr/bin/perl6
+
+use Test;
+
+plan 3;
+
+is-deeply(pas([1..4]),[[1,2],[4,3]],'example 1');
+is-deeply(pas([1..6]),[[1,2,3],[6,5,4]],'example 2');
+is-deeply(pas([1..12]),[[1,2,3,4],[10,11,12,5],[9,8,7,6]],'example 3');
+
+sub pas(@aa) {
+ my @a=reverse @aa;
+ my $n=@a.elems;
+ my $f2=floor(sqrt($n));
+ my $f1;
+ while (1) {
+ if ($n % $f2 == 0) {
+ $f1=$n/$f2;
+ last;
+ }
+ $f2--;
+ }
+ my @out;
+ for (1..$f2) {
+ push @out,[(0) xx $f1];
+ }
+ my ($x,$y)=(-1,0);
+ my $maxx=$f1-1;
+ my $maxy=$f2-1;
+ my $minx=0;
+ my $miny=1;
+ ARR:
+ while (1) {
+ while ($x < $maxx) {
+ $x++;
+ @out[$y][$x]=pop @a;
+ unless (@a.elems) {
+ last ARR;
+ }
+ }
+ $maxx-=1;
+ while ($y < $maxy) {
+ $y++;
+ @out[$y][$x]=pop @a;
+ unless (@a.elems) {
+ last ARR;
+ }
+ }
+ $maxy-=1;
+ while ($x > $minx) {
+ $x--;
+ @out[$y][$x]=pop @a;
+ unless (@a.elems) {
+ last ARR;
+ }
+ }
+ $minx++;
+ while ($y > $miny) {
+ $y--;
+ @out[$y][$x]=pop @a;
+ unless (@a.elems) {
+ last ARR;
+ }
+ }
+ $miny++;
+ }
+ return @out;
+}
diff --git a/challenge-101/roger-bell-west/raku/ch-2.p6 b/challenge-101/roger-bell-west/raku/ch-2.p6
new file mode 100755
index 0000000000..607bf860f9
--- /dev/null
+++ b/challenge-101/roger-bell-west/raku/ch-2.p6
@@ -0,0 +1,30 @@
+#! /usr/bin/perl6
+
+use Test;
+
+plan 3;
+
+is-deeply(ot([[0,1],[1,0],[2,2]]),0,'example 1');
+is-deeply(ot([[1,1],[-1,1],[0,-3]]),1,'example 2');
+is-deeply(ot([[0,1],[2,0],[-6,0]]),1,'example 3');
+
+sub ot(@points) {
+ my @pp=@points;
+ push @pp,@points[0];
+ my @xp;
+ for (0..2) -> $i {
+ push @xp,(@pp[$i][0] *
+ (@pp[$i+1][1]-@pp[$i][1]))
+ -
+ (@pp[$i][1] *
+ (@pp[$i+1][0]-@pp[$i][0]));
+ }
+ @xp=sort @xp;
+ if (@xp[0] <=0 && @xp[2] <=0) {
+ return 1;
+ }
+ if (@xp[0] >=0 && @xp[2] >=0) {
+ return 1;
+ }
+ return 0;
+}
diff --git a/challenge-101/roger-bell-west/ruby/ch-1.rb b/challenge-101/roger-bell-west/ruby/ch-1.rb
new file mode 100755
index 0000000000..56554aa2d8
--- /dev/null
+++ b/challenge-101/roger-bell-west/ruby/ch-1.rb
@@ -0,0 +1,80 @@
+#! /usr/bin/ruby
+
+def pas(aa)
+ a=aa.reverse
+ n=a.length
+ f2=Integer.sqrt(n)
+ f1=0
+ while 1
+ if n % f2 == 0
+ f1=n/f2
+ break
+ end
+ f2 -= 1
+ end
+ out=Array.new
+ 1.upto(f2) do
+ out.push([0] * f1)
+ end
+ x=-1
+ y=0
+ maxx=f1-1
+ maxy=f2-1
+ minx=0
+ miny=1
+ catch :arr do
+ while 1
+ while x < maxx
+ x += 1
+ out[y][x]=a.pop()
+ if a.length==0 then
+ throw :arr
+ end
+ end
+ maxx -= 1
+ while y < maxy
+ y += 1
+ out[y][x]=a.pop()
+ if a.length==0 then
+ throw :arr
+ end
+ end
+ maxy -= 1
+ while x > minx
+ x -= 1
+ out[y][x]=a.pop()
+ if a.length==0 then
+ throw :arr
+ end
+ end
+ minx += 1
+ while y > miny
+ y -= 1
+ out[y][x]=a.pop()
+ if a.length==0 then
+ throw :arr
+ end
+ end
+ miny += 1
+ end
+ end
+ return out
+end
+
+require 'test/unit'
+
+class TestPas < Test::Unit::TestCase
+
+ def test_ex1
+ assert_equal([[1,2],[4,3]],pas((1..4).to_a))
+ end
+
+ def test_ex2
+ assert_equal([[1,2,3],[6,5,4]],pas((1..6).to_a))
+ end
+
+ def test_ex3
+ assert_equal([[1,2,3,4],[10,11,12,5],[9,8,7,6]],pas((1..12).to_a))
+ end
+
+end
diff --git a/challenge-101/roger-bell-west/ruby/ch-2.rb b/challenge-101/roger-bell-west/ruby/ch-2.rb
new file mode 100755
index 0000000000..17553ff167
--- /dev/null
+++ b/challenge-101/roger-bell-west/ruby/ch-2.rb
@@ -0,0 +1,36 @@
+#! /usr/bin/ruby
+
+def ot(pp)
+ points=pp
+ points.push(pp[0])
+ xp=Array.new()
+ 0.upto(2) do |i|
+ xp.append(points[i][0] * (points[i+1][1]-points[i][1])-points[i][1]*(points[i+1][0]-points[i][0]))
+ end
+ xp.sort!
+ if xp[0] <= 0 and xp[2] <= 0 then
+ return 1
+ end
+ if xp[0] >= 0 and xp[2] >= 0 then
+ return 1
+ end
+ return 0
+end
+
+require 'test/unit'
+
+class TestPas < Test::Unit::TestCase
+
+ def test_ex1
+ assert_equal(0,ot([[0,1],[1,0],[2,2]]))
+ end
+
+ def test_ex2
+ assert_equal(1,ot([[1,1],[-1,1],[0,-3]]))
+ end
+
+ def test_ex3
+ assert_equal(1,ot([[0,1],[2,0],[-6,0]]))
+ end
+
+end
diff --git a/challenge-101/roger-bell-west/rust/ch-1.rs b/challenge-101/roger-bell-west/rust/ch-1.rs
new file mode 100755
index 0000000000..9eefd4904f
--- /dev/null
+++ b/challenge-101/roger-bell-west/rust/ch-1.rs
@@ -0,0 +1,83 @@
+#! /bin/sh
+//usr/bin/env rustc --test $0 -o ${0}x && ./${0}x; rm -f ${0}x ; exit
+
+#[test]
+fn test_ex1() {
+ assert_eq!(pas(vec![1,2,3,4]),vec![vec![1,2],vec![4,3]]);
+}
+
+#[test]
+fn test_ex2() {
+ assert_eq!(pas(vec![1,2,3,4,5,6]),vec![vec![1,2,3],vec![6,5,4]]);
+}
+
+#[test]
+fn test_ex3() {
+ assert_eq!(pas(vec![1,2,3,4,5,6,7,8,9,10,11,12]),vec![vec![1,2,3,4],vec![10,11,12,5],vec![9,8,7,6]]);
+}
+
+fn pas(aa: Vec<i32>) -> Vec<Vec<i32>> {
+ let mut a=aa;
+ a.reverse();
+ let n=a.len();
+ let mut f2: usize=(n as f64).sqrt() as usize;
+ let f1: usize;
+ loop {
+ if n % f2 == 0 {
+ f1=n/f2;
+ break;
+ }
+ f2 -= 1;
+ }
+ let mut out: Vec<Vec<i32>>=vec![vec![0;f1]; f2];
+ let mut x: usize=0;
+ let mut y: usize=0;
+ let mut minx: usize=0;
+ let mut maxx: usize=f1-1;
+ let mut miny: usize=1;
+ let mut maxy: usize=f2-1;
+ let mut first: bool=true;
+ 'arr: loop {
+ while x < maxx {
+ if first {
+ first=false;
+ } else {
+ x += 1;
+ }
+ out[y][x]=a.pop().unwrap();
+ if a.len()==0 {
+ break 'arr;
+ }
+ }
+ if maxx > 0 {
+ maxx -= 1;
+ }
+ while y < maxy {
+ y += 1;
+ out[y][x]=a.pop().unwrap();
+ if a.len()==0 {
+ break 'arr;
+ }
+ }
+ if maxy > 0 {
+ maxy -= 1;
+ }
+ while x > minx {
+ x -= 1;
+ out[y][x]=a.pop().unwrap();
+ if a.len()==0 {
+ break 'arr;
+ }
+ }
+ minx += 1;
+ while y > miny {
+ y -= 1;
+ out[y][x]=a.pop().unwrap();
+ if a.len()==0 {
+ break 'arr;
+ }
+ }
+ miny += 1;
+ }
+ return out;
+}
diff --git a/challenge-101/roger-bell-west/rust/ch-2.rs b/challenge-101/roger-bell-west/rust/ch-2.rs
new file mode 100755
index 0000000000..7c3618a0f4
--- /dev/null
+++ b/challenge-101/roger-bell-west/rust/ch-2.rs
@@ -0,0 +1,35 @@
+#! /bin/sh
+//usr/bin/env rustc --test $0 -o ${0}x && ./${0}x; rm -f ${0}x ; exit
+
+#[test]
+fn test_ex1() {
+ assert_eq!(ot(vec![vec![0,1],vec![1,0],vec![2,2]]),0);
+}
+
+#[test]
+fn test_ex2() {
+ assert_eq!(ot(vec![vec![1,1],vec![-1,1],vec![0,-3]]),1);
+}
+
+#[test]
+fn test_ex3() {
+ assert_eq!(ot(vec![vec![0,1],vec![2,0],vec![-6,0]]),1);
+}
+
+fn ot(pp: Vec<Vec<i32>>) -> i32 {
+ let mut points=pp.clone();
+ points.push(pp[0].clone());
+ let mut xp: Vec<i32>=vec![];
+ for i in 0..3 {
+ xp.push(points[i][0]*(points[i+1][1]-points[i][1])
+ -points[i][1]*(points[i+1][0]-points[i][0]));
+ }
+ xp.sort();
+ if xp[0] <= 0 && xp[2] <= 0 {
+ return 1;
+ }
+ if xp[0] >= 0 && xp[2] >= 0 {
+ return 1;
+ }
+ return 0;
+}