aboutsummaryrefslogtreecommitdiff
path: root/challenge-031
diff options
context:
space:
mode:
authordcw <d.white@imperial.ac.uk>2019-10-27 22:36:40 +0000
committerdcw <d.white@imperial.ac.uk>2019-10-27 22:36:40 +0000
commitea45ca0f6265ff53e071bc25c909ac9f720a5359 (patch)
tree4cea8724cb2599cea08a2b9b7c18e6ab75aec8ea /challenge-031
parentc33b84f8742bfc295f9111a580b7453927030385 (diff)
downloadperlweeklychallenge-club-ea45ca0f6265ff53e071bc25c909ac9f720a5359.tar.gz
perlweeklychallenge-club-ea45ca0f6265ff53e071bc25c909ac9f720a5359.tar.bz2
perlweeklychallenge-club-ea45ca0f6265ff53e071bc25c909ac9f720a5359.zip
imported my solutions to challenge 31
Diffstat (limited to 'challenge-031')
-rw-r--r--challenge-031/duncan-c-white/README16
-rwxr-xr-xchallenge-031/duncan-c-white/perl5/ch-1.pl24
-rwxr-xr-xchallenge-031/duncan-c-white/perl5/ch-2.pl34
3 files changed, 65 insertions, 9 deletions
diff --git a/challenge-031/duncan-c-white/README b/challenge-031/duncan-c-white/README
index 2d973d0053..5e1aee6697 100644
--- a/challenge-031/duncan-c-white/README
+++ b/challenge-031/duncan-c-white/README
@@ -1,13 +1,11 @@
-Challenge 1: "Write a script to list dates for Sunday Christmas between
- 2019 and 2100. For example, 25 Dec 2022 is Sunday."
+Challenge 1: "Create a function to check divide by zero error without
+ checking if the denominator is zero."
-My notes: Very well defined, another job for Date::Manip or Date::Simple..
+My notes: so eval then?
-Challenge 2: "Write a script to print all possible series of 3 positive
- numbers, where in each series at least one of the number is even and
- sum of the three numbers is always 12. For example, 3,4,5."
+Challenge 2: "Create a script to demonstrate creating dynamic variable
+ name, assign a value to the variable and finally print the
+ variable. The variable name is passed as a command line argument."
-My notes: Well defined, looks straightforward by brute forcish search (with
-the third number in the sequence being fixed as 12-first-second of course,
-so only two degrees of freedom).
+My notes: so eval again?
diff --git a/challenge-031/duncan-c-white/perl5/ch-1.pl b/challenge-031/duncan-c-white/perl5/ch-1.pl
new file mode 100755
index 0000000000..5a55e655c2
--- /dev/null
+++ b/challenge-031/duncan-c-white/perl5/ch-1.pl
@@ -0,0 +1,24 @@
+#!/usr/bin/perl
+#
+# Challenge 1: "Create a function to check divide by zero error without
+# checking if the denominator is zero."
+#
+# My notes: so eval then?
+#
+
+use v5.10; # to get "say"
+use strict;
+use warnings;
+
+die "Usage: ch-1.pl a b\n" unless @ARGV==2;
+my $a = shift;
+my $b = shift;
+
+my $x;
+if( defined eval { $x = $a / $b } )
+{
+ say "$a / $b = $x";
+} else
+{
+ say "error: $@";
+}
diff --git a/challenge-031/duncan-c-white/perl5/ch-2.pl b/challenge-031/duncan-c-white/perl5/ch-2.pl
new file mode 100755
index 0000000000..3a00f4e73b
--- /dev/null
+++ b/challenge-031/duncan-c-white/perl5/ch-2.pl
@@ -0,0 +1,34 @@
+#!/usr/bin/perl
+#
+# Challenge 2: "Create a script to demonstrate creating dynamic variable
+# name, assign a value to the variable and finally print the
+# variable. The variable name is passed as a command line argument."
+#
+# My notes: so eval again? let's take a list of variable names,
+# and increment each variable each time..
+#
+
+use v5.10; # for "say"
+use strict;
+use warnings;
+use Data::Dumper;
+
+die "Usage: ch-2.pl [x|y]+\n" if @ARGV==0;
+my @bad = grep { ! "x" && ! "y" } @ARGV;
+
+die "ch-2.pl: bad variables @bad, not x or y\n" unless @bad==0;
+
+my $x=0;
+my $y=0;
+
+foreach my $v (@ARGV)
+{
+ my $var = '$'.$v;
+ #say "debug: var=$var";
+
+ my $str = $var.'++';
+ #say "debug: str=$str";
+ eval $str;
+}
+
+say "x=$x, y=$y";