From ea45ca0f6265ff53e071bc25c909ac9f720a5359 Mon Sep 17 00:00:00 2001 From: dcw Date: Sun, 27 Oct 2019 22:36:40 +0000 Subject: imported my solutions to challenge 31 --- challenge-031/duncan-c-white/README | 16 ++++++-------- challenge-031/duncan-c-white/perl5/ch-1.pl | 24 +++++++++++++++++++++ challenge-031/duncan-c-white/perl5/ch-2.pl | 34 ++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 9 deletions(-) create mode 100755 challenge-031/duncan-c-white/perl5/ch-1.pl create mode 100755 challenge-031/duncan-c-white/perl5/ch-2.pl 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"; -- cgit