diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-02-23 21:53:35 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-02-23 21:53:35 +0000 |
| commit | bb4a389da2267616216621b4a937daecf5893277 (patch) | |
| tree | 3e2081dc08d99caf7aebf9bc0b1d7e8f9307d9e3 /challenge-101 | |
| parent | 9ec653210a62f5081970d7ff199cfa0dadc72ef9 (diff) | |
| parent | e799bafc952a2ac4e04da646163e354f4a7dd316 (diff) | |
| download | perlweeklychallenge-club-bb4a389da2267616216621b4a937daecf5893277.tar.gz perlweeklychallenge-club-bb4a389da2267616216621b4a937daecf5893277.tar.bz2 perlweeklychallenge-club-bb4a389da2267616216621b4a937daecf5893277.zip | |
Merge pull request #3613 from pauloscustodio/paulo-custodio
Add Perl solution to challenge 101
Diffstat (limited to 'challenge-101')
| -rw-r--r-- | challenge-101/paulo-custodio/perl/ch-1.pl | 80 | ||||
| -rw-r--r-- | challenge-101/paulo-custodio/perl/ch-2.pl | 37 | ||||
| -rw-r--r-- | challenge-101/paulo-custodio/t/test-1.yaml | 101 | ||||
| -rw-r--r-- | challenge-101/paulo-custodio/t/test-2.yaml | 15 | ||||
| -rw-r--r-- | challenge-101/paulo-custodio/test.pl | 7 |
5 files changed, 240 insertions, 0 deletions
diff --git a/challenge-101/paulo-custodio/perl/ch-1.pl b/challenge-101/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..3e637254fb --- /dev/null +++ b/challenge-101/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,80 @@ +#!/usr/bin/perl + +# You are given an array @A of items (integers say, but they can be anything). +# +# Your task is to pack that array into an MxN matrix spirally counterclockwise, +# as tightly as possible. +# +# ‘Tightly’ means the absolute value |M-N| of the difference has to be as small +# as possible. + +use strict; +use warnings; +use 5.030; + +my @list = @ARGV; + +my($m, $n) = smallest_rect(scalar(@list)); +my @rect = spiral($m, $n, @list); + +for my $r (1 .. $m) { + for my $c (1 .. $n) { + print $rect[$r][$c], " "; + } + say ""; +} + +sub smallest_rect { + my($n) = @_; + my $low = 1; + my $high = $n; + for (my $i = 1; $i*$i <= $n; $i++) { + if (($n % $i) == 0) { + $low = $i; + $high = $n/$i; + } + } + return ($low, $high); +} + +sub spiral { + my($m, $n, @list) = @_; + + # find max width of elements + my $width = 1; + for (@list) { + $width = length($_) if length($_) > $width; + } + + # build rectangle + my @rect; + my $r = $m; my $c = 1; + while (@list) { + # go East + while (@list && $c <= $n && !defined($rect[$r][$c])) { + $rect[$r][$c] = sprintf("%*s", $width, shift @list); + $c++; + } + $c--; $r--; + # go North + while (@list && $r >= 1 && !defined($rect[$r][$c])) { + $rect[$r][$c] = sprintf("%*s", $width, shift @list); + $r--; + } + $r++; $c--; + # go West + while (@list && $c >= 1 && !defined($rect[$r][$c])) { + $rect[$r][$c] = sprintf("%*s", $width, shift @list); + $c--; + } + $c++; $r++; + # go South + while (@list && $r <= $m && !defined($rect[$r][$c])) { + $rect[$r][$c] = sprintf("%*s", $width, shift @list); + $r++; + } + $r--; $c++; + } + + return @rect; +} diff --git a/challenge-101/paulo-custodio/perl/ch-2.pl b/challenge-101/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..c95d924af1 --- /dev/null +++ b/challenge-101/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,37 @@ +#!/usr/bin/perl + +# TASK #2 › Origin-containing Triangle +# Submitted by: Stuart Little +# You are given three points in the plane, as a list of six co-ordinates: +# A=(x1,y1), B=(x2,y2) and C=(x3,y3). +# +# Write a script to find out if the triangle formed by the given three +# co-ordinates contain origin (0,0). +# +# Print 1 if found otherwise 0. + +use strict; +use warnings; +use 5.030; + +my($x1,$y1,$x2,$y2,$x3,$y3) = @ARGV; +say point_in_triangle(0,0, $x1,$y1,$x2,$y2,$x3,$y3) ? 1 : 0; + + +sub sign { + my($x1,$y1,$x2,$y2,$x3,$y3) = @_; + return ($x1 - $x3) * ($y2 - $y3) - ($x2 - $x3) * ($y1 - $y3); +} + +sub point_in_triangle { + my($xp,$yp, $x1,$y1,$x2,$y2,$x3,$y3) = @_; + + my $d1 = sign($xp,$yp, $x1,$y1, $x2,$y2); + my $d2 = sign($xp,$yp, $x2,$y2, $x3,$y3); + my $d3 = sign($xp,$yp, $x3,$y3, $x1,$y1); + + my $has_neg = ($d1 < 0) || ($d2 < 0) || ($d3 < 0); + my $has_pos = ($d1 > 0) || ($d2 > 0) || ($d3 > 0); + + return !($has_neg && $has_pos); +} diff --git a/challenge-101/paulo-custodio/t/test-1.yaml b/challenge-101/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..5a686187ef --- /dev/null +++ b/challenge-101/paulo-custodio/t/test-1.yaml @@ -0,0 +1,101 @@ +- setup: + cleanup: + args: 1 + input: + output: | + |1 +- setup: + cleanup: + args: 1 2 + input: + output: | + |1 2 +- setup: + cleanup: + args: 1 2 3 + input: + output: | + |1 2 3 +- setup: + cleanup: + args: 1 2 3 4 + input: + output: | + |4 3 + |1 2 +- setup: + cleanup: + args: 1 2 3 4 5 + input: + output: | + |1 2 3 4 5 +- setup: + cleanup: + args: 1 2 3 4 5 6 + input: + output: | + |6 5 4 + |1 2 3 +- setup: + cleanup: + args: 1 2 3 4 5 6 7 + input: + output: | + |1 2 3 4 5 6 7 +- setup: + cleanup: + args: 1 2 3 4 5 6 + input: + output: | + |6 5 4 + |1 2 3 +- setup: + cleanup: + args: 1 2 3 4 5 6 7 8 9 + input: + output: | + |7 6 5 + |8 9 4 + |1 2 3 +- setup: + cleanup: + args: 1 2 3 4 5 6 7 8 9 10 + input: + output: | + |10 9 8 7 6 + | 1 2 3 4 5 +- setup: + cleanup: + args: 1 2 3 4 5 6 7 8 9 10 11 + input: + output: | + | 1 2 3 4 5 6 7 8 9 10 11 +- setup: + cleanup: + args: 1 2 3 4 5 6 7 8 9 10 11 12 + input: + output: | + | 9 8 7 6 + |10 11 12 5 + | 1 2 3 4 +- setup: + cleanup: + args: 1 2 3 4 5 6 7 8 9 10 11 12 13 + input: + output: | + | 1 2 3 4 5 6 7 8 9 10 11 12 13 +- setup: + cleanup: + args: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 + input: + output: | + |14 13 12 11 10 9 8 + | 1 2 3 4 5 6 7 +- setup: + cleanup: + args: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 + input: + output: | + |11 10 9 8 7 + |12 13 14 15 6 + | 1 2 3 4 5 diff --git a/challenge-101/paulo-custodio/t/test-2.yaml b/challenge-101/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..a90cb7c6ef --- /dev/null +++ b/challenge-101/paulo-custodio/t/test-2.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 0 1 1 0 2 2 + input: + output: 0 +- setup: + cleanup: + args: 1 1 -1 1 0 -3 + input: + output: 1 +- setup: + cleanup: + args: 0 1 2 0 -6 0 + input: + output: 1 diff --git a/challenge-101/paulo-custodio/test.pl b/challenge-101/paulo-custodio/test.pl new file mode 100644 index 0000000000..01ed2b83cd --- /dev/null +++ b/challenge-101/paulo-custodio/test.pl @@ -0,0 +1,7 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use 5.030; + +require '../../challenge-001/paulo-custodio/test.pl'; |
