aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-027/ruben-westerberg/README15
-rwxr-xr-xchallenge-027/ruben-westerberg/perl5/ch-1.pl24
-rwxr-xr-xchallenge-027/ruben-westerberg/perl5/ch-2.pl33
-rwxr-xr-xchallenge-027/ruben-westerberg/perl6/ch-1.p620
-rwxr-xr-xchallenge-027/ruben-westerberg/perl6/ch-2.p618
5 files changed, 102 insertions, 8 deletions
diff --git a/challenge-027/ruben-westerberg/README b/challenge-027/ruben-westerberg/README
index b22be03bd3..c17b59a016 100644
--- a/challenge-027/ruben-westerberg/README
+++ b/challenge-027/ruben-westerberg/README
@@ -2,15 +2,14 @@ Solution by Ruben Westerberg
ch-1.pl and ch-1.p6
===
-Stone and Jewels
-Run the program with two arguments, the first one being the stone, and the second one the jewel.
-The total number of letters in the jewel present in the stone is printed.
-If no arguments are given a test set is used.
+Intersecting lines.
+Run the program. you will be propted to enter two points for the first line and then two points for the second line.
+Points are intered as interleved xy pairs
+example:
+ When prompted to enter the line starting at the origin and ending at x=5 and y=10, the two points are entered as follows:
+ 0 0 5 10
ch-2.pl and ch-2.p6
===
-Angular average
-Run the program with at least one argument. Each argument is an angle in degrees.
-The output is the average of the list of angles supplied.
-If no arguments are given, a test list is used.
+Run the program to demonstrate history of every assignement to a variable.
diff --git a/challenge-027/ruben-westerberg/perl5/ch-1.pl b/challenge-027/ruben-westerberg/perl5/ch-1.pl
new file mode 100755
index 0000000000..8e55486e6d
--- /dev/null
+++ b/challenge-027/ruben-westerberg/perl5/ch-1.pl
@@ -0,0 +1,24 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+
+my @l;
+while (@l<2) {
+ print "Enter line".(@l+1).": x1 y1 x2 y2\n";
+ my $l=<STDIN>; my @p=split " ", $l;
+ if (@p==4) {
+ push @l, {px=>[@p[0,2]],py=>[@p[1,3]],m=>undef,c=>undef};
+ }
+ else {
+ print "not a valid line! \n";
+ }
+}
+
+for (@l) {
+ $$_{m}=($$_{py}[1]-$$_{py}[0])/($$_{px}[1]-$$_{px}[0]);
+ $$_{c}=$$_{py}[0]-($$_{m}*$$_{px}[0]);
+}
+my $x=($l[0]{c}-$l[1]{c})/( $l[1]{m}-$l[0]{m});
+my $y=$l[0]{m}*$x+$l[0]{c};
+
+print "Intercept point: $x, $y\n";
diff --git a/challenge-027/ruben-westerberg/perl5/ch-2.pl b/challenge-027/ruben-westerberg/perl5/ch-2.pl
new file mode 100755
index 0000000000..569388237f
--- /dev/null
+++ b/challenge-027/ruben-westerberg/perl5/ch-2.pl
@@ -0,0 +1,33 @@
+#!/usr/bin/env perl
+use warnings;
+use strict;
+use Tie::Scalar;
+
+package myclass;
+sub TIESCALAR {
+ my $class=shift;
+ my $h=shift;
+ bless ({history=>$h}, $class);
+}
+
+sub FETCH {
+ my $self=shift;
+ my $h=$self->{history};
+ $h->[-1];
+}
+sub STORE {
+ my $self=shift;
+ my $value=shift;
+ push @{$self->{history}},$value;
+
+}
+
+package main;
+my $test;
+my @history;
+tie $test, 'myclass', \@history;
+$test=10;
+$test=1;
+$test=15;
+print "Current Value of variable: $test\n";
+print "Historical Values: @history\n";
diff --git a/challenge-027/ruben-westerberg/perl6/ch-1.p6 b/challenge-027/ruben-westerberg/perl6/ch-1.p6
new file mode 100755
index 0000000000..9718354d9d
--- /dev/null
+++ b/challenge-027/ruben-westerberg/perl6/ch-1.p6
@@ -0,0 +1,20 @@
+#!/usr/bin/env perl6
+my @l;
+while @l < 2 {
+ my @p=split " ", prompt("Enter line"~(@l+1)~": x1 y1 x2 y2\n"), :skip-empty;
+ if (@p==4) {
+ push @l, {px=>[@p[0,2]],py=>[@p[1,3]],m=>Any,c=>Any};
+ }
+ else {
+ print "not a valid line! \n";
+ }
+}
+
+for @l {
+ $_<m>=($_<py>[1]-$_<py>[0])/($_<px>[1]-$_<px>[0]);
+ $_<c>=$_<py>[0]- ($_<m>*$_<px>[0]);
+}
+my $x=(@l[0]<c>-@l[1]<c>)/( @l[1]<m>-@l[0]<m>);
+my $y=@l[0]<m>*$x+@l[0]<c>;
+
+put "Intercept point: $x, $y";
diff --git a/challenge-027/ruben-westerberg/perl6/ch-2.p6 b/challenge-027/ruben-westerberg/perl6/ch-2.p6
new file mode 100755
index 0000000000..ec2d2af03d
--- /dev/null
+++ b/challenge-027/ruben-westerberg/perl6/ch-2.p6
@@ -0,0 +1,18 @@
+#!/usr/bin/env perl6
+
+my @history;
+my $value:=remembering(@history);
+$value=4;
+$value=10;
+$value=100;
+put "Current value of variable: $value";
+put "Historical Values: " , join " ",@history;
+
+
+sub remembering (@history) {
+ return-rw Proxy.new(
+ FETCH => method () {@history[*-1]},
+ STORE => method ($new) {;@history.push($new)}
+ );
+}
+