aboutsummaryrefslogtreecommitdiff
path: root/challenge-093
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-01-03 21:46:39 +0000
committerGitHub <noreply@github.com>2021-01-03 21:46:39 +0000
commitee0a81857ae300c849e9ba56ff8d2e5c9f98bbd9 (patch)
tree4573a323dede4b07fcce5fda8782dc8774640fd0 /challenge-093
parent90402149a391172fbabc44937465b81a07cbd254 (diff)
parent5f79fea8fd1037de395ef13cfa66ff0b3ca6d679 (diff)
downloadperlweeklychallenge-club-ee0a81857ae300c849e9ba56ff8d2e5c9f98bbd9.tar.gz
perlweeklychallenge-club-ee0a81857ae300c849e9ba56ff8d2e5c9f98bbd9.tar.bz2
perlweeklychallenge-club-ee0a81857ae300c849e9ba56ff8d2e5c9f98bbd9.zip
Merge pull request #3141 from adamcrussell/challenge-093
Perl solutions for Challenge 093.
Diffstat (limited to 'challenge-093')
-rw-r--r--challenge-093/adam-russell/blog.txt1
-rw-r--r--challenge-093/adam-russell/perl/ch-1.pl46
-rw-r--r--challenge-093/adam-russell/perl/ch-2.pl55
3 files changed, 102 insertions, 0 deletions
diff --git a/challenge-093/adam-russell/blog.txt b/challenge-093/adam-russell/blog.txt
new file mode 100644
index 0000000000..f4fa07936b
--- /dev/null
+++ b/challenge-093/adam-russell/blog.txt
@@ -0,0 +1 @@
+http://www.rabbitfarm.com/cgi-bin/blosxom/perl/2021/01/03
diff --git a/challenge-093/adam-russell/perl/ch-1.pl b/challenge-093/adam-russell/perl/ch-1.pl
new file mode 100644
index 0000000000..67a781840d
--- /dev/null
+++ b/challenge-093/adam-russell/perl/ch-1.pl
@@ -0,0 +1,46 @@
+use strict;
+use warnings;
+##
+# You are given set of co-ordinates @N.
+# Write a script to count maximum points
+# on a straight line when given co-ordinates
+# plotted on 2-d plane.
+##
+sub triangle_area{
+ my($i, $j, $k) = @_;
+ return ($i->[0] * ($j->[1] - $k->[1]))
+ + ($j->[0] * ($k->[1] - $i->[1]))
+ + ($k->[0] * ($i->[1] - $j->[1]));
+}
+
+sub collinear_points{
+ my(@points) = @_;
+ my @collinear;
+ for my $i (@points){
+ for my $j (@points){
+ for my $k (@points){
+ if(triangle_area($i, $j, $k) == 0){
+ my $i_string = join(",", @{$i});
+ my $j_string = join(",", @{$j});
+ my $k_string = join(",", @{$k});
+ if(($i_string ne $j_string) && ($i_string ne $k_string) && ($j_string ne $k_string)){
+ my $has_i = grep { $i_string eq join(",", @{$_}) } @collinear;
+ push @collinear, $i if !$has_i;
+ my $has_j = grep { $j_string eq join(",", @{$_}) } @collinear;
+ push @collinear, $j if !$has_j;
+ my $has_k = grep { $k_string eq join(",", @{$_}) } @collinear;
+ push @collinear, $k if !$has_k;
+ }
+ }
+ }
+ }
+ }
+ return @collinear;
+}
+
+MAIN:{
+ my @N;
+ @N = ([5,3], [1,1], [2,2], [3,1], [1,3]);
+ my @collinear = collinear_points(@N);
+ print "There are a maximum of " . @collinear . " collinear points.\n"
+}
diff --git a/challenge-093/adam-russell/perl/ch-2.pl b/challenge-093/adam-russell/perl/ch-2.pl
new file mode 100644
index 0000000000..d9a02d6316
--- /dev/null
+++ b/challenge-093/adam-russell/perl/ch-2.pl
@@ -0,0 +1,55 @@
+use strict;
+use warnings;
+##
+# You are given a binary tree containing
+# only the numbers 0-9.
+# Write a script to sum all possible paths
+# from root to leaf.
+##
+use Graph;
+
+sub travserse_sum{
+ my($tree) = @_;
+ my @paths = build_paths($tree);
+ my $path_sum = 0;
+ for my $path (@paths){
+ $path_sum += unpack("%32C*", pack("C*", @{$path}));
+ }
+ return $path_sum;
+}
+
+sub build_paths {
+ my ($graph) = @_;
+ my @paths;
+ local *_helper = sub{
+ my $v = $_[-1];
+ my @successors = $graph->successors($v);
+ if(@successors){
+ _helper(@_, $_) for @successors;
+ }
+ else{
+ push @paths, [@_];
+ }
+ };
+ _helper($_) for $graph->source_vertices();
+ return @paths;
+}
+
+MAIN:{
+ my $Tree;
+ $Tree = new Graph();
+ $Tree->add_vertices(1, 2, 3, 4);
+ $Tree->add_edge(1, 2);
+ $Tree->add_edge(2, 3);
+ $Tree->add_edge(2, 4);
+ print travserse_sum($Tree) . "\n";
+
+ $Tree = new Graph();
+ $Tree->add_vertices(1, 2, 3, 4, 5, 6);
+ $Tree->add_edge(1, 2);
+ $Tree->add_edge(1, 3);
+ $Tree->add_edge(2, 4);
+ $Tree->add_edge(3, 5);
+ $Tree->add_edge(3, 6);
+ print travserse_sum($Tree) . "\n";
+}