diff options
37 files changed, 3179 insertions, 1602 deletions
diff --git a/challenge-101/jaldhar-h-vyas/blog.txt b/challenge-101/jaldhar-h-vyas/blog.txt new file mode 100644 index 0000000000..e6cbb408fd --- /dev/null +++ b/challenge-101/jaldhar-h-vyas/blog.txt @@ -0,0 +1 @@ +https://www.braincells.com/perl/2021/02/perl_weekly_challenge_week_101.html diff --git a/challenge-101/jaldhar-h-vyas/perl/ch-1.pl b/challenge-101/jaldhar-h-vyas/perl/ch-1.pl new file mode 100644 index 0000000000..5de2416954 --- /dev/null +++ b/challenge-101/jaldhar-h-vyas/perl/ch-1.pl @@ -0,0 +1,83 @@ +#!/usr/bin/perl +use 5.020; +use warnings; + +sub factors { + my ($size) = @_; + + my @tightest = ($size, 1); + my $minimum = abs($tightest[0] - $tightest[1]); + + for my $m (grep { $size % $_ == 0; } 2 .. $size / 2) { + my $n = $size / $m; + my $difference = abs($m - $n); + if ($difference < $minimum) { + $minimum = $difference; + @tightest = ($m, $n); + } + } + + return @tightest; +} + +sub spiral { + my ($a, $m, $n) = @_; + + my $top = 0; + my $bottom = $m - 1; + my $left = 0; + my $right = $n - 1; + my $index = 0; + my @matrix; + + while (1) { + if ($left > $right) { + last; + } + + for (my $i = $left; $i <= $right; $i++) { + $matrix[$bottom][$i] = $a->[$index++]; + } + $bottom--; + + if ($top > $bottom) { + last; + } + + for (my $i = $bottom; $i >= $top; $i--) { + $matrix[$i][$right] = $a->[$index++]; + } + $right--; + + if ($left > $right) { + last; + } + + for (my $i = $right; $i >= $left; $i--) { + $matrix[$top][$i] = $a->[$index++]; + } + $top++; + + if ($top > $bottom) { + last; + } + + for (my $i = $top; $i <= $bottom; $i++) { + $matrix[$i][$left] = $a->[$index++]; + } + $left++; + } + + for my $i (0 .. scalar @matrix - 1) { + for my $j (0 .. scalar @{$matrix[$i]} - 1) { + printf '%2d ', $matrix[$i][$j] // 0; + } + print "\n"; + } +} + +my @A = @ARGV; + +my @tightest = factors(scalar @A); + +spiral(\@A, $tightest[0], $tightest[1]); diff --git a/challenge-101/jaldhar-h-vyas/perl/ch-2.pl b/challenge-101/jaldhar-h-vyas/perl/ch-2.pl new file mode 100644 index 0000000000..854eb8d530 --- /dev/null +++ b/challenge-101/jaldhar-h-vyas/perl/ch-2.pl @@ -0,0 +1,54 @@ +#!/usr/bin/perl +use 5.020; +use warnings; +use English qw/ _no_match_vars /; + +sub usage { + print<<"-USAGE-"; +Usage: + $PROGRAM_NAME <Point1> <Point2> <Point3> -- a point is a pair of integers separated by a comma +-USAGE- + exit 0; +} + +sub area { + my ($p1, $p2, $p3) = @_; + + return abs( + ( + $p1->[0] * ($p2->[1] - $p3->[1]) + + $p2->[0] * ($p3->[1] - $p1->[1]) + + $p3->[0] * ($p1->[1] - $p2->[1]) + ) / 2.0 + ); +} + +sub isInside { + my ($a, $b, $c, $p) = @_; + + my $area0 = area($a, $b, $c); + my $area1 = area($p, $b, $c); + my $area2 = area($a, $p, $c); + my $area3 = area($a, $b, $p); + + return ($area0 == $area1 + $area2 + $area3); +} + +if (scalar @ARGV != 3) { + usage; +} + +my $pointrx = qr/\A (-*\d+) \, (-*\d+) \z /msx; + +$ARGV[0] =~ /$pointrx/; +my @a = ($1, $2); + +$ARGV[1] =~ /$pointrx/; +my @b = ($1, $2); + +$ARGV[2] =~ /$pointrx/; +my @c = ($1, $2); + +my @p = (0, 0); + +say isInside(\@a, \@b, \@c, \@p) ? 1 : 0;
\ No newline at end of file diff --git a/challenge-101/jaldhar-h-vyas/raku/ch-1.raku b/challenge-101/jaldhar-h-vyas/raku/ch-1.raku new file mode 100644 index 0000000000..82a807f25d --- /dev/null +++ b/challenge-101/jaldhar-h-vyas/raku/ch-1.raku @@ -0,0 +1,80 @@ +#!/usr/bin/raku + +sub factors(Int $size) { + + my @tightest = ($size, 1); + my $minimum = abs(@tightest[0] - @tightest[1]); + + for (2 .. $size div 2).grep({ $size %% $_ }) -> $m { + my $n = $size div $m; + my $difference = abs($m - $n); + if ($difference < $minimum) { + $minimum = $difference; + @tightest = ($m, $n); + } + } + + return @tightest; +} + +sub spiral(Int $m, Int $n, *@a) { + my $top = 0; + my $bottom = $m - 1; + my $left = 0; + my $right = $n - 1; + my $index = 0; + my @matrix; + + loop { + if ($left > $right) { + last; + } + + for $left .. $right -> $i { + @matrix[$bottom][$i] = @a[$index++]; + } + $bottom--; + + if ($top > $bottom) { + last; + } + + for ($top .. $bottom).reverse -> $i { + @matrix[$i][$right] = @a[$index++]; + } + $right--; + + if ($left > $right) { + last; + } + + for ($left .. $right).reverse -> $i { + @matrix[$top][$i] = @a[$index++]; + } + $top++; + + if ($top > $bottom) { + last; + } + + for $top .. $bottom -> $i { + @matrix[$i][$left] = @a[$index++]; + } + $left++; + } + + for 0 .. @matrix.elems - 1 -> $i { + for 0 .. @matrix[$i].elems - 1 -> $j { + printf '%2d ', @matrix[$i][$j]; + } + print "\n"; + } +} + +sub MAIN( + *@A +) { + my @tightest = factors(@A.elems); + + spiral(@tightest[0], @tightest[1], @A); +}
\ No newline at end of file diff --git a/challenge-101/jaldhar-h-vyas/raku/ch-2.raku b/challenge-101/jaldhar-h-vyas/raku/ch-2.raku new file mode 100644 index 0000000000..c20e4080f8 --- /dev/null +++ b/challenge-101/jaldhar-h-vyas/raku/ch-2.raku @@ -0,0 +1,60 @@ +#!/usr/bin/raku + +class Point { + has Int $.x; + has Int $.y; + + multi method new (Str $str) { + $str ~~ / ^ ( \-* \d+ ) \, ( \-* \d+ ) $ /; + self.bless( x => $0.Int, y => $1.Int ); + } + + multi method new (Int $x, Int $y) { + self.bless( x => $x, y => $y); + } +} + +class Triangle { + has Point $!p1; + has Point $!p2; + has Point $!p3; + has Numeric $.area; + + method new (Point $a, Point $b, Point $c) { + self.bless(p1 => $a, p2 => $b, p3 => $c); + } + + submethod BUILD (Point :$!p1, Point :$!p2, Point :$!p3) { + $!area = abs( + ( + $!p1.x * ($!p2.y - $!p3.y) + + $!p2.x * ($!p3.y - $!p1.y) + + $!p3.x * ($!p1.y - $!p2.y) + ) / 2.0 + ); + } +} + +sub isInside (Point $a, Point $b, Point $c, Point $p) { + + my $area0 = Triangle.new($a, $b, $c).area; + my $area1 = Triangle.new($p, $b, $c).area; + my $area2 = Triangle.new($a, $p, $c).area; + my $area3 = Triangle.new($a, $b, $p).area; + + return ($area0 == $area1 + $area2 + $area3); +} + +sub MAIN ( + #= a point is a pair of integers separated by a comma + Str $Point1, + Str $Point2, + Str $Point3 +) { + my $a = Point.new($Point1); + my $b = Point.new($Point2); + my $c = Point.new($Point3); + my $p = Point.new(0, 0); + + say isInside($a, $b, $c, $p) ?? 1 !! 0; +}
\ No newline at end of file diff --git a/challenge-101/perlboy1967/perl/ch-2.pl b/challenge-101/perlboy1967/perl/ch-2.pl index 5af3cf8aa7..cea46714dc 100755 --- a/challenge-101/perlboy1967/perl/ch-2.pl +++ b/challenge-101/perlboy1967/perl/ch-2.pl @@ -24,6 +24,7 @@ my %triangles = ( 'Example 1' => [0,1,1,0,2,2], 'Example 2' => [1,1,-1,1,0,-3], 'Example 3' => [0,1,2,0,-6,0], + 'Example 4' => [-1,0,0,0,0,1], ); foreach my $c (sort keys %triangles) { @@ -51,6 +52,10 @@ sub calcZAngle($$$$) { sub originInsideTriangle (@) { my ($x1,$y1,$x2,$y2,$x3,$y3) = @_; + return 1 if ($x1 == 0 and $y1 == 0); + return 1 if ($x2 == 0 and $y2 == 0); + return 1 if ($x3 == 0 and $y3 == 0); + # calculate angles: # AzB, BzC and AzC my %angle; diff --git a/challenge-102/roger-bell-west/perl/ch-1.pl b/challenge-102/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..050f185a40 --- /dev/null +++ b/challenge-102/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,86 @@ +#! /usr/bin/perl + +use strict; +use warnings; + +use Test::More tests => 3; + +is_deeply(rn(2),[65],'example 1'); +is_deeply(rn(6),[621770],'example 2'); +is_deeply(rn(9),[281089082],'example 3'); + +sub rn { + my $d=shift; + my @out; + my $mxm=10**($d-2)-1; + foreach my $a (2,4,6,8) { + foreach my $q (0,2,3,5,7,8) { + if ($a==2 && $q!=2) { + next; + } + if ($a==4 && $q!=0) { + next; + } + if ($a==6 && $q!=0 && $q!=5) { + next; + } + if ($a==8 && $q!=2 && $q!=3 && $q!=7 && $q!=8) { + next; + } + if ($d==2) { + my $t="$a$q"; + if (is_rare($t)) { + push @out,$t; + } + } else { + foreach my $middledigits (map {sprintf('%0'.($d-2).'d',$_)} 0..$mxm) { + my $b=substr($middledigits,0,1); + my $p=substr($middledigits,-1,1); + if ($a==2 && $b!=$p) { + next; + } + if ($a==4 && abs($b-$p)%2 != 0) { + next; + } + if ($a==6 && abs($b-$p)%2 != 1) { + next; + } + if ($a==8) { + if ($q==2 && $b+$p != 9) { + next; + } elsif ($q==3 && $b-$p != 7 && $p-$b != 3) { + next; + } elsif ($q==7 && $b+$p != 1 && $b+$p != 11) { + next; + } elsif ($q==8 && $b!=$p) { + next; + } + } + my $t="$a$middledigits$q"; + if (is_rare($t)) { + push @out,$t; + } + } + } + } + } + return \@out; +} + +sub is_rare { + my $t=shift; + my $d=join('',reverse(split '',$t)); + if ($d >= $t) { + return 0; + } + foreach my $c ($t+$d,$t-$d) { + if ($c =~ /[2378]$/) { + return 0; + } + my $s=int(sqrt($c)); + unless ($s*$s==$c) { + return 0; + } + } + return 1; +} diff --git a/challenge-102/roger-bell-west/perl/ch-2.pl b/challenge-102/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..a7bdd1e712 --- /dev/null +++ b/challenge-102/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,46 @@ +#! /usr/bin/perl + +use strict; +use warnings; + +use Test::More tests => 5; + +is(hcs(1),'#','example 1'); +is(hcs(2),'2#','example 2'); +is(hcs(3),'#3#','example 3'); +is(hcs(10),'#3#5#7#10#','example 4'); +is(hcs(14),'2#4#6#8#11#14#','example 5'); + +use List::Util qw(sum); + +sub hcs { + my $n=shift; + my @s; + my @t; + while (1) { + @s=(); + my $l=0; + if (@t) { + @s=@{pop @t}; + $l=sum(map {($_==1?0:length($_))+1} @s); + } + if ($l==$n) { + last; + } + if ($l > $n) { + next; + } + my $c=$l; + while (1) { + my $tt=($c==1?0:length($c))+$l+1; + if ($c==$tt) { + push @t,[@s,$c]; + } + if ($c > $tt) { + last; + } + $c++; + } + } + return join('',map {($_==1?'':$_).'#'} @s); +} diff --git a/challenge-102/roger-bell-west/python/ch-1.py b/challenge-102/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..9440eb25eb --- /dev/null +++ b/challenge-102/roger-bell-west/python/ch-1.py @@ -0,0 +1,76 @@ +#! /usr/bin/python3 + +import math + +def is_rare(t: str): + d=t[::-1] + dn=int(d) + tn=int(t) + if dn >= tn: + return 0 + for cn in [tn+dn,tn-dn]: + cm=cn % 10 + if cm==2 or cm==3 or cm==7 or cm==8: + return 0 + s=int(math.sqrt(cn)) + if s*s != cn: + return 0 + return 1 + +def rn(d): + out=list() + mxm=10**(d-2)-1 + for a in [2,4,6,8]: + for q in [0,2,3,5,7,8]: + if a==2 and q!=2: + continue + if a==4 and q!=0: + continue + if a==6 and q!=0 and q!=5: + continue + if a==8 and q!=2 and q!=3 and q!=7 and q!=8: + continue + if d==2: + ts=str(a) + str(q) + if is_rare(ts): + out.append(int(ts)) + else: + for md in range(mxm+1): + mds=("{:0"+str(d-2)+"d}").format(md) + b=int(mds[0]) + p=int(mds[len(mds)-1]) + if a==2 and b!=p: + continue + if a==4 and abs(b-p)%2 != 0: + continue + if a==6 and abs(b-p)%2 != 1: + continue + if a==8: + if q==2 and b+p != 9: + continue + if q==3 and b-p != 7 and p-b != 3: + continue + if q==7 and b+p != 1 and b+p != 11: + continue + if q==8 and b!=p: + continue + ts=str(a)+mds+str(q) + if is_rare(ts): + out.append(int(ts)) + return out + + +import unittest + +class TestRn(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(rn(2),[65],'example 1') + + def test_ex2(self): + self.assertEqual(rn(6),[621770],'example 2') + + def test_ex3(self): + self.assertEqual(rn(9),[281089082],'example 3') + +unittest.main() diff --git a/challenge-102/roger-bell-west/python/ch-2.py b/challenge-102/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..7812a33fe8 --- /dev/null +++ b/challenge-102/roger-bell-west/python/ch-2.py @@ -0,0 +1,48 @@ +#! /usr/bin/python3 + +def hcs(n): + s=list() + t=list() + while 1: + s=list() + l=0 + if len(t)>0: + s=t.pop() + l=sum((0 if i==1 else len(str(i)))+1 for i in s) + if l==n: + break + if l > n: + continue + c=l + while 1: + tt=(0 if c==1 else len(str(c)))+l+1 + if c==tt: + k=s.copy() + k.append(c) + t.append(k) + if c > tt: + break + c+=1 + return ''.join((("" if i==1 else str(i)) + "#") for i in s) + + +import unittest + +class TestHcs(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(hcs(1),'#','example 1') + + def test_ex2(self): + self.assertEqual(hcs(2),'2#','example 2') + + def test_ex3(self): + self.assertEqual(hcs(3),'#3#','example 3') + + def test_ex4(self): + self.assertEqual(hcs(10),'#3#5#7#10#','example 4') + + def test_ex5(self): + self.assertEqual(hcs(14),'2#4#6#8#11#14#','example 5') + +unittest.main() diff --git a/challenge-102/roger-bell-west/raku/ch-1.p6 b/challenge-102/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..e76051e4a3 --- /dev/null +++ b/challenge-102/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,83 @@ +#! /usr/bin/perl6 + +use Test; + +plan 3; + +is-deeply(rn(2),[65],'example 1'); +is-deeply(rn(6),[621770],'example 2'); +is-deeply(rn(9),[281089082],'example 3'); + +sub rn($d) { + my @out; + my $mxm=10**($d-2)-1; + for (2,4,6,8) -> $a { + for (0,2,3,5,7,8) -> $q { + if ($a==2 && $q != 2) { + next; + } + if ($a==4 && $q != 0) { + next; + } + if ($a==6 && $q != 0 && $q != 5) { + next; + } + if ($a==8 && $q != 2 && $q != 3 && $q != 7 && $q != 8) { + next; + } + if ($d==2) { + my $t="$a$q"; + if (is_rare($t)) { + push @out,$t+0; + } + } else { + for map {sprintf('%0' ~ ($d-2) ~ 'd',$_)}, (0..$mxm) -> $middledigits { + my $b=substr($middledigits,0,1); + my $p=substr($middledigits,*-1,1); + if ($a==2 && $b != $p) { + next; + } + if ($a==4 && abs($b-$p)%2 != 0) { + next; + } + if ($a==6 && abs($b-$p)%2 != 1) { + next; + } + if ($a==8) { + if ($q==2 && $b+$p != 9) { + next; + } elsif ($q==3 && $b-$p != 7 && $p-$b != 3) { + next; + } elsif ($q==7 && $b+$p != 1 && $b+$p != 11) { + next; + } elsif ($q==8 && $b != $p) { + next; + } + } + my $t="$a$middledigits$q"; + if (is_rare($t)) { + push @out,$t+0; + } + } + } + } + } + return @out; +} + +sub is_rare($t) { + my $d=$t.comb.reverse.join(''); + if ($d >= $t) { + return 0; + } + for ($t+$d,$t-$d) -> $c { + if ($c ~~ /<[2378]>$/) { + return 0; + } + my $s=floor(sqrt($c)); + unless ($s*$s==$c) { + return 0; + } + } + return 1; +} diff --git a/challenge-102/roger-bell-west/raku/ch-2.p6 b/challenge-102/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..4c688319dd --- /dev/null +++ b/challenge-102/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,44 @@ +#! /usr/bin/perl6 + +use Test; + +plan 5; + +is(hcs(1),'#','example 1'); +is(hcs(2),'2#','example 2'); +is(hcs(3),'#3#','example 3'); +is(hcs(10),'#3#5#7#10#','example 4'); +is(hcs(14),'2#4#6#8#11#14#','example 5'); + +sub hcs($n) { + my @s; + my @t; + while (1) { + @s=(); + my $l=0; + if (@t.elems) { + @s=(pop @t).flat; + $l=sum(map {($_==1 ?? 0 !! chars($_))+1}, @s); + } + if ($l==$n) { + last; + } + if ($l > $n) { + next; + } + my $c=$l; + while (1) { + my $tt=($c==1 ?? 0 !! chars($c))+$l+1; + if ($c==$tt) { + my @k=(@s».List.flat); + push @k,$c; + push @t,@k; + } + if ($c > $tt) { + last; + } + $c++; + } + } + return join('',map {($_==1 ?? '' !! $_) ~ '#'}, @s); +} diff --git a/challenge-102/roger-bell-west/ruby/ch-1.rb b/challenge-102/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..64f6c8407b --- /dev/null +++ b/challenge-102/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,100 @@ +#! /usr/bin/ruby + +def rn(d) + out=Array.new + mxm=10**(d-2)-1 + [2,4,6,8].each do |a| + [0,2,3,5,7,8].each do |q| + if a==2 && q!=2 then + next + end + if a==4 && q!=0 then + next + end + if a==6 && q!=0 && q!=5 then + next + end + if a==8 && q!=2 && q!=3 && q!=7 && q!=8 then + next + end + if d==2 then + t="#{a}#{q}" + if is_rare(t) then + out.push(Integer(t)) + end + else + 0.upto(mxm) do |md| + mds=sprintf('%0' + "#{d-2}" + 'd',md) + b=Integer(mds[0]) + p=Integer(mds[-1]) + if a==2 && b!=p then + next + end + if a==4 && ((b-p).abs)%2 != 0 then + next + end + if a==6 && ((b-p).abs)%2 != 1 then + next + end + if a==8 then + if q==2 && b+p != 9 then + next + end + if q==3 && b-p != 7 && p-b != 3 then + next + end + if q==7 && b+p != 1 && b+p != 11 then + next + end + if q==8 && b!=p then + next + end + end + t="#{a}#{mds}#{q}" + if is_rare(t) then + out.push(Integer(t)) + end + end + end + end + end + return out +end + +def is_rare(t) + tn=Integer(t) + ds=t.reverse() + ds.gsub!(/^0+/,"") + d=Integer(ds) + if d >= tn then + return nil + end + [tn+d,tn-d].each do |c| + if c =~ /[2378]$/ then + return nil + end + s=Integer.sqrt(c) + if s*s != c then + return nil + end + end + return 1 +end + +require 'test/unit' + +class TestRn < Test::Unit::TestCase + + def test_ex1 + assert_equal([65],rn(2)) + end + + def test_ex2 + assert_equal([621770],rn(6)) + end + + def test_ex3 + assert_equal([281089082],rn(9)) + end + +end diff --git a/challenge-102/roger-bell-west/ruby/ch-2.rb b/challenge-102/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..11a01dde2a --- /dev/null +++ b/challenge-102/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,60 @@ +#! /usr/bin/ruby + +require 'test/unit' + +def hcs(n) + s=Array.new + t=Array.new + while 1 + s=Array.new + l=0 + if t.length>0 then + s=t.pop + l=s.map{|i| (i==1 ? 0 : "#{i}".length)+1}.sum + end + if l==n then + break + end + if l > n then + next + end + c=l |
