From c1d8cb8bebe97b1b1941033d7b04683edc7fb329 Mon Sep 17 00:00:00 2001 From: Adam Russell Date: Sun, 29 Sep 2019 09:25:52 -0400 Subject: initial commit --- challenge-027/adam-russell/blog.txt | 0 challenge-027/adam-russell/perl5/Audit.pm | 30 ++++++++++++++++++++++++++++++ challenge-027/adam-russell/perl5/ch-1.pl | 0 challenge-027/adam-russell/perl5/ch-2.pl | 0 4 files changed, 30 insertions(+) create mode 100644 challenge-027/adam-russell/blog.txt create mode 100644 challenge-027/adam-russell/perl5/Audit.pm create mode 100644 challenge-027/adam-russell/perl5/ch-1.pl create mode 100644 challenge-027/adam-russell/perl5/ch-2.pl diff --git a/challenge-027/adam-russell/blog.txt b/challenge-027/adam-russell/blog.txt new file mode 100644 index 0000000000..e69de29bb2 diff --git a/challenge-027/adam-russell/perl5/Audit.pm b/challenge-027/adam-russell/perl5/Audit.pm new file mode 100644 index 0000000000..29f0b56a1b --- /dev/null +++ b/challenge-027/adam-russell/perl5/Audit.pm @@ -0,0 +1,30 @@ +package Audit{ + use Filter::Simple; + + our %Log; + + sub audit{ + my($name, $value) = @_; + $value = "undef" if !$value; + if($Log{$name}){ + push @{$Log{$name}}, $value; + } + else{ + $Log{$name} = [$value]; + } + #print "A " . $name . " " . $value . "\n"; + } + + sub print_log{ + for my $key (keys %Log){ + print "$key: " . join(", ", @{$Log{$key}}) . "\n"; + } + } + + FILTER_ONLY code => sub{ + s|((\$[[:alpha:]]*).*;)|$1 Audit::audit('$2', $2);|g; + #print $1 . "\n"; + #print $2 . "\n"; + #print; + } +} diff --git a/challenge-027/adam-russell/perl5/ch-1.pl b/challenge-027/adam-russell/perl5/ch-1.pl new file mode 100644 index 0000000000..e69de29bb2 diff --git a/challenge-027/adam-russell/perl5/ch-2.pl b/challenge-027/adam-russell/perl5/ch-2.pl new file mode 100644 index 0000000000..e69de29bb2 -- cgit From 23ae84ea0cdc089535a857109c8f38619d4ffcb9 Mon Sep 17 00:00:00 2001 From: Adam Russell Date: Sun, 29 Sep 2019 15:58:21 -0400 Subject: solution for challenge 027 --- challenge-027/adam-russell/blog.txt | 1 + challenge-027/adam-russell/perl5/Audit.pm | 19 +++++++++++++------ challenge-027/adam-russell/perl5/ch-1.pl | 31 +++++++++++++++++++++++++++++++ challenge-027/adam-russell/perl5/ch-2.pl | 21 +++++++++++++++++++++ 4 files changed, 66 insertions(+), 6 deletions(-) diff --git a/challenge-027/adam-russell/blog.txt b/challenge-027/adam-russell/blog.txt index e69de29bb2..09b3105dab 100644 --- a/challenge-027/adam-russell/blog.txt +++ b/challenge-027/adam-russell/blog.txt @@ -0,0 +1 @@ +https://adamcrussell.livejournal.com/9508.html diff --git a/challenge-027/adam-russell/perl5/Audit.pm b/challenge-027/adam-russell/perl5/Audit.pm index 29f0b56a1b..c1d8354213 100644 --- a/challenge-027/adam-russell/perl5/Audit.pm +++ b/challenge-027/adam-russell/perl5/Audit.pm @@ -1,30 +1,37 @@ package Audit{ + use Data::Dump q/pp/; use Filter::Simple; our %Log; sub audit{ my($name, $value) = @_; - $value = "undef" if !$value; + $value = "undef" if !defined($value); if($Log{$name}){ push @{$Log{$name}}, $value; } else{ $Log{$name} = [$value]; } - #print "A " . $name . " " . $value . "\n"; } + sub clear_log{ + %Log = (); + } + sub print_log{ for my $key (keys %Log){ print "$key: " . join(", ", @{$Log{$key}}) . "\n"; } } + + sub pretty_print_log{ + for my $key (keys %Log){ + print "$key: " . join(", ", map{ pp $_ } @{$Log{$key}}) . "\n"; + } + } FILTER_ONLY code => sub{ - s|((\$[[:alpha:]]*).*;)|$1 Audit::audit('$2', $2);|g; - #print $1 . "\n"; - #print $2 . "\n"; - #print; + s|((\$[[:alnum:]_]*).*;)|$1 Audit::audit('$2', $2);|g; } } diff --git a/challenge-027/adam-russell/perl5/ch-1.pl b/challenge-027/adam-russell/perl5/ch-1.pl index e69de29bb2..f5cdf4895c 100644 --- a/challenge-027/adam-russell/perl5/ch-1.pl +++ b/challenge-027/adam-russell/perl5/ch-1.pl @@ -0,0 +1,31 @@ +use strict; +use warnings; +## +# Write a script to find the intersection of two straight lines. +# The co-ordinates of the two lines should be provided as command line parameters. +## +MAIN:{ + my @endpoints_line_0 = @ARGV[0, 1]; + my @endpoints_line_1 = @ARGV[2, 3]; + @endpoints_line_0 = map { $_=~tr/()//d; [split(/,/, $_)];} @endpoints_line_0; + @endpoints_line_1 = map { $_=~tr/()//d; [split(/,/, $_)];} @endpoints_line_1; + my($a, $b, $c, $d, $p, $q, $r, $s) = ( + $endpoints_line_0[0]->[0], + $endpoints_line_0[0]->[1], + $endpoints_line_0[1]->[0], + $endpoints_line_0[1]->[1], + $endpoints_line_1[0]->[0], + $endpoints_line_1[0]->[1], + $endpoints_line_1[1]->[0], + $endpoints_line_1[1]->[1] + ); + my $denominator = ($c - $a) * ($s - $q) - ($r - $p) * ($c - $b); + + my $x0 = ($c * $b - $a * $d) * ($r - $p) - ($r * $q - $p * $s) * ($c - $a); + my $y0 = ($c * $b - $a * $d) * ($s - $q) - ($r * $q - $p * $s) * ($d - $b); + + my $x = $x0/$denominator; + my $y = $y0/$denominator; + + print "($a, $b)--($c, $d) and ($p, $q)--($r, $s) intersect at ($x, $y)\n"; +} diff --git a/challenge-027/adam-russell/perl5/ch-2.pl b/challenge-027/adam-russell/perl5/ch-2.pl index e69de29bb2..8ebf38a6ee 100644 --- a/challenge-027/adam-russell/perl5/ch-2.pl +++ b/challenge-027/adam-russell/perl5/ch-2.pl @@ -0,0 +1,21 @@ +use strict; +use warnings; +## +# Write a script that allows you to capture/display historical data. +# It could be an object or a scalar. +## +use Audit; +use Class::Hash; + +my $test0; +$test0 = 2; +$test0 = 3; + +Audit::print_log(); +Audit::clear_log(); + +my $test1 = new Class::Hash(A => "B"); +$test1 = new Class::Hash(B => "C"); +$test1 = new Class::Hash(C => "D"); +#Audit::pretty_print_log(); +Audit::print_log(); -- cgit