aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorwanderdoc <wanderdoc@googlemail.com>2021-08-01 15:24:38 +0200
committerwanderdoc <wanderdoc@googlemail.com>2021-08-01 15:24:38 +0200
commitf0b0f5aad61ba8f39e7556d8c23ced2576f00cee (patch)
treeeb714c8b37da20f1d4f79353970bc2e033537acb
parent5f8333fec725cf031970bf676fa86aa532e4e23e (diff)
downloadperlweeklychallenge-club-f0b0f5aad61ba8f39e7556d8c23ced2576f00cee.tar.gz
perlweeklychallenge-club-f0b0f5aad61ba8f39e7556d8c23ced2576f00cee.tar.bz2
perlweeklychallenge-club-f0b0f5aad61ba8f39e7556d8c23ced2576f00cee.zip
Solutions to challenge-123
-rw-r--r--challenge-123/wanderdoc/perl/ch-1.pl41
-rw-r--r--challenge-123/wanderdoc/perl/ch-2.pl64
2 files changed, 105 insertions, 0 deletions
diff --git a/challenge-123/wanderdoc/perl/ch-1.pl b/challenge-123/wanderdoc/perl/ch-1.pl
new file mode 100644
index 0000000000..f2c9b71366
--- /dev/null
+++ b/challenge-123/wanderdoc/perl/ch-1.pl
@@ -0,0 +1,41 @@
+#!perl
+use strict;
+use warnings FATAL => qw(all);
+
+=prompt
+You are given an integer $n >= 1. Write a script to find the $nth element of Ugly Numbers.
+Ugly numbers are those number whose prime factors are 2, 3 or 5. For example, the first 10 Ugly Numbers are 1, 2, 3, 4, 5, 6, 8, 9, 10, 12.
+Example
+Input: $n = 7 Output: 8
+Input: $n = 10 Output: 12
+=cut
+
+
+
+
+
+
+
+use List::Util qw(min);
+
+my $N = shift or die "Number?$/";
+my $idx2 = my $idx3 = my $idx5 = 1;
+my @num;
+
+my $i = 1;
+
+$num[$i] = 1;
+
+while ( $i < $N )
+{
+ my $last = $num[$i];
+ $idx2++ while $num[$idx2] * 2 <= $last;
+ $idx3++ while $num[$idx3] * 3 <= $last;
+ $idx5++ while $num[$idx5] * 5 <= $last;
+ $i++;
+ $num[$i] = min($num[$idx2] * 2, $num[$idx3] * 3, $num[$idx5] * 5);
+
+}
+
+
+print $num[$i], $/; \ No newline at end of file
diff --git a/challenge-123/wanderdoc/perl/ch-2.pl b/challenge-123/wanderdoc/perl/ch-2.pl
new file mode 100644
index 0000000000..6e3fc6983f
--- /dev/null
+++ b/challenge-123/wanderdoc/perl/ch-2.pl
@@ -0,0 +1,64 @@
+#!perl
+use strict;
+use warnings FATAL => qw(all);
+
+=prompt
+You are given coordinates of four points i.e. (x1, y1), (x2, y2), (x3, y3) and (x4, y4).
+Write a script to find out if the given four points form a square.
+=cut
+
+
+
+
+
+
+
+
+
+
+use Struct::Dumb;
+struct Point => [qw( x y )];
+
+sub check_square
+{
+ my @points = @_;
+ my %distances;
+
+ for my $i ( 0 .. 3 )
+ {
+ for my $j ( 1 .. 3 )
+ {
+ next if $i >= $j;
+ $distances{$i}{$j} = calc_distance(@points[$i, $j]);
+ return 0 if 0 == $distances{$i}{$j};
+ }
+ }
+
+ return 1
+ if
+
+
+ (
+ ($distances{0}{1} == $distances{0}{2} and $distances{0}{3} == 2 * $distances{0}{1} and $distances{0}{1} == $distances{2}{3})
+ or
+ ($distances{0}{1} == $distances{0}{3} and $distances{0}{2} == 2 * $distances{0}{1} and $distances{0}{1} == $distances{2}{3})
+ or
+ ($distances{1}{2} == $distances{1}{3} and $distances{0}{1} == 2 * $distances{1}{2} and $distances{1}{2} == $distances{0}{3})
+ );
+ return 0;
+
+}
+
+sub calc_distance # distance _squared_.
+{
+ my @pts = @_;
+ return (($pts[0]->x - $pts[1]->x) * ($pts[0]->x - $pts[1]->x) +
+ ($pts[0]->y - $pts[1]->y) * ($pts[0]->y - $pts[1]->y));
+}
+
+
+print check_square(Point(10, 20), Point(20, 20), Point(20, 10), Point(10, 10)), $/; # 1
+print check_square(Point(12, 24), Point(16, 10), Point(20, 12), Point(18, 16)), $/; # 0
+print check_square(Point(10, 30), Point(20, 10), Point(30, 40), Point(40, 20)), $/; # 1
+print check_square(Point(-2, -1), Point(-2, 2), Point( 1, -1), Point( 1, 2)), $/; # 1
+print check_square(Point(10, 10), Point(10, 20), Point(30, 10), Point(30, 20)), $/; # 0 \ No newline at end of file