aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-02-28 22:54:50 +0000
committerGitHub <noreply@github.com>2021-02-28 22:54:50 +0000
commit8ca0d2d72b29262307991157657b3bad4419ad43 (patch)
tree095a3b2e2617f47bc079e9cee207382d9eb249da
parentf2930393ade898ac55caba840cc0c52cc998ab00 (diff)
parent9c96552f6dd2f520d854c30d39edfbc7ee737d2f (diff)
downloadperlweeklychallenge-club-8ca0d2d72b29262307991157657b3bad4419ad43.tar.gz
perlweeklychallenge-club-8ca0d2d72b29262307991157657b3bad4419ad43.tar.bz2
perlweeklychallenge-club-8ca0d2d72b29262307991157657b3bad4419ad43.zip
Merge pull request #3633 from mimosinnet/branch-for-challenge-101
Branch for challenge 101
-rw-r--r--challenge-101/mimosinnet/raku/ch-1.raku78
-rw-r--r--challenge-101/mimosinnet/raku/ch-2.raku77
2 files changed, 155 insertions, 0 deletions
diff --git a/challenge-101/mimosinnet/raku/ch-1.raku b/challenge-101/mimosinnet/raku/ch-1.raku
new file mode 100644
index 0000000000..71bab88a23
--- /dev/null
+++ b/challenge-101/mimosinnet/raku/ch-1.raku
@@ -0,0 +1,78 @@
+# https://perlweeklychallenge.org/blog/perl-weekly-challenge-101/
+
+# This exercise is unfinished. It works for the examples provided,
+# but it has issues with complex ones.
+
+#|
+sub challenge( @array ) {
+
+ my $cols = sqrt(@array.elems).ceiling - 1;
+ my $rows = sqrt(@array.elems).floor - 1;
+ my @spiral;
+
+ sub array_boundary( $rows, $row is copy, $cols, $col is copy, @array ) {
+ my $array_area = 2 * ( $cols + $rows );
+ # We start at the highest row, lowest column
+ my $col_inc = 1;
+ my $row_inc = 0;
+ # First value
+ @spiral[ $row ][ $col ] = @array.shift;
+ for 2..$array_area {
+ last unless @array.elems;
+ if $col == $cols and $row == $rows { $col_inc = 0; $row_inc = -1 }
+ elsif $col == $cols and $row == 0 { $col_inc = -1; $row_inc = 0 }
+ elsif $col == 0 and $row == 0 { $col_inc = 0; $row_inc = 1 }
+ $row += $row_inc;
+ $col += $col_inc;
+ @spiral[ $row ][ $col ] = @array.shift;
+ }
+ return unless @array.elems;
+ array_boundary($rows - 1, $rows -1, $cols - 1, 1, @array);
+ }
+ array_boundary($rows, $rows, $cols, 0, @array.Array);
+ return @spiral;
+}
+
+multi sub MAIN( $a ) {
+ say 'Input: @A = (',$a,')';
+ say 'Output:';
+ for challenge($a) -> @a {
+ say @a;
+ }
+}
+
+multi sub MAIN( 'challenge' ) {
+ my @challenge = (
+ (1..4),
+ (1..6),
+ (1..12)
+ );
+
+ for @challenge -> $a {
+ MAIN($a);
+ }
+}
+
+multi sub MAIN( 'test' ) {
+ use Test;
+
+ my @test = (
+ (
+ (1..4),
+ [[4,3], [1,2]]
+ ),
+ (
+ (1..6),
+ [[6,5,4], [1,2,3]]
+ ),
+ (
+ (1..12),
+ [[9,8,7,6], [10,11,12,5], [1,2,3,4]]
+ )
+ );
+
+ for @test -> ($a, $b ) {
+ is-deeply challenge($a), $b;
+ }
+
+}
diff --git a/challenge-101/mimosinnet/raku/ch-2.raku b/challenge-101/mimosinnet/raku/ch-2.raku
new file mode 100644
index 0000000000..2b28bcf170
--- /dev/null
+++ b/challenge-101/mimosinnet/raku/ch-2.raku
@@ -0,0 +1,77 @@
+# https://perlweeklychallenge.org/blog/perl-weekly-challenge-1001/
+
+# https://www.geeksforgeeks.org/check-whether-a-given-point-lies-inside-a-triangle-or-not/
+# Point P is inside Triangle A, B, C if:
+# Area ABC = Area ABP + Area ACP + Area BCP
+
+class Vertex
+{
+ has Int $.x;
+ has Int $.y;
+}
+
+class Triangle
+{
+ has Vertex $.a;
+ has Vertex $.b;
+ has Vertex $.c;
+
+ method area()
+ {
+ abs(
+ $!a.x * ( $!b.y - $!c.y ) +
+ $!b.x * ( $!c.y - $!a.y ) +
+ $!c.x * ( $!a.y - $!b.y )
+ ) / 2;
+ }
+}
+
+sub challenge( $a, $b, $c, --> Int ) {
+
+ # Vertex
+ my $Va = Vertex.new( x => $a[0], y => $a[1] );
+ my $Vb = Vertex.new( x => $b[0], y => $b[1] );
+ my $Vc = Vertex.new( x => $c[0], y => $c[1] );
+ my $V0 = Vertex.new( x => 0, y => 0 );
+
+ # Area
+ my $Aabc = Triangle.new( a => $Va, b => $Vb, c => $Vc ).area;
+ my $Aab0 = Triangle.new( a => $Va, b => $Vb, c => $V0 ).area;
+ my $Abc0 = Triangle.new( a => $Vb, b => $Vc, c => $V0 ).area;
+ my $Aac0 = Triangle.new( a => $Va, b => $Vc, c => $V0 ).area;
+
+ return ( $Aabc == $Aab0 + $Abc0 + $Aac0 ).Int;
+}
+
+multi sub MAIN( $a, $b, $c ) {
+ say 'Input: A=',$a,', B=',$b,' and C=',$c;
+ say 'Output: ',challenge($a, $b, $c),"\n";
+}
+
+multi sub MAIN( 'challenge' ) {
+
+ my @challenge = (
+ ((0, 1), ( 1, 0), ( 2, 2)),
+ ((1, 1), (-1, 1), ( 0, -3)),
+ ((0, 1), ( 2, 0), (-6, 0))
+ );
+
+ for @challenge -> ($a, $b, $c) {
+ MAIN($a,$b,$c);
+ }
+}
+
+multi sub MAIN( 'test' ) {
+ use Test;
+
+ my @test = (
+ ((0, 1), ( 1, 0), ( 2, 2), 0),
+ ((1, 1), (-1, 1), ( 0, -3), 1),
+ ((0, 1), ( 2, 0), (-6, 0), 1)
+ );
+
+ for @test -> ($a, $b, $c, $d ) {
+ is challenge($a,$b, $c), $d;
+ }
+
+}