diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2019-10-20 17:09:13 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-10-20 17:09:13 +0100 |
| commit | fe5eb73797e87d5bbe5f958673c998463a3795e7 (patch) | |
| tree | 2c2a0e4b6901c66f8556e734733b5c6c453e26b9 /challenge-030 | |
| parent | 2b154ac51912a58942eba06e37d0642bdd258afd (diff) | |
| parent | 1e05dac9e7c3d2b5fd5c6a911435104912d86a52 (diff) | |
| download | perlweeklychallenge-club-fe5eb73797e87d5bbe5f958673c998463a3795e7.tar.gz perlweeklychallenge-club-fe5eb73797e87d5bbe5f958673c998463a3795e7.tar.bz2 perlweeklychallenge-club-fe5eb73797e87d5bbe5f958673c998463a3795e7.zip | |
Merge pull request #811 from dcw803/master
two very straighforward problems this week.. no particular subtlety..
Diffstat (limited to 'challenge-030')
| -rw-r--r-- | challenge-030/duncan-c-white/README | 28 | ||||
| -rwxr-xr-x | challenge-030/duncan-c-white/perl5/ch-1.pl | 23 | ||||
| -rwxr-xr-x | challenge-030/duncan-c-white/perl5/ch-2.pl | 27 |
3 files changed, 59 insertions, 19 deletions
diff --git a/challenge-030/duncan-c-white/README b/challenge-030/duncan-c-white/README index b00307c5eb..2d973d0053 100644 --- a/challenge-030/duncan-c-white/README +++ b/challenge-030/duncan-c-white/README @@ -1,23 +1,13 @@ -Challenge 1: "Write a script to demonstrate brace expansion. For example, -script would take command line argument Perl {Daily,Weekly,Monthly,Yearly} -Challenge and should expand it and print like below: +Challenge 1: "Write a script to list dates for Sunday Christmas between + 2019 and 2100. For example, 25 Dec 2022 is Sunday." - Perl Daily Challenge - Perl Weekly Challenge - Perl Monthly Challenge - Perl Yearly Challenge +My notes: Very well defined, another job for Date::Manip or Date::Simple.. -My notes: Nested braces may be slightly tricky, first I tried a recursive -function to find and expand the innermost {}, but that produced the -right output in a weird order, with duplicates. -Instead, try a state machine to track the locations of the outer level -elements {,}. Extract $before, $after and an array of alternatives @alt, -then recombine them, until there are no { left. +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: "Write a script to demonstrate calling a C function. It -could be any user defined or standard C function." - -My notes: Hmm, either XS or Inline::C. I've never used either:-) -Wrote simple sqrt routine (algorithm: squaring the rectangle) in C, -wrote the same in Perl, then used Benchmark to benchmark both versions. +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). diff --git a/challenge-030/duncan-c-white/perl5/ch-1.pl b/challenge-030/duncan-c-white/perl5/ch-1.pl new file mode 100755 index 0000000000..2b44f2522c --- /dev/null +++ b/challenge-030/duncan-c-white/perl5/ch-1.pl @@ -0,0 +1,23 @@ +#!/usr/bin/perl +# +# Challenge 1: "Write a script to list dates for Sunday Christmas between +# 2019 and 2100. For example, 25 Dec 2022 is Sunday." +# +# My notes: Very well defined, another job for Date::Manip or Date::Simple.. +# + +use v5.10; # to get "say" +use strict; +use warnings; +use Date::Simple qw(date today); + +die "Usage: ch-1.pl [STARTYEAR [ENDYEAR]]\n" if @ARGV > 2; +my $startyear = shift // 2019; +my $endyear = shift // 2100; + +foreach my $year ($startyear..$endyear) +{ + my $dstr = "$year-12-25"; + my $dow = date( $dstr )->day_of_week; + say $dstr if $dow == 0; +} diff --git a/challenge-030/duncan-c-white/perl5/ch-2.pl b/challenge-030/duncan-c-white/perl5/ch-2.pl new file mode 100755 index 0000000000..7dab1e155c --- /dev/null +++ b/challenge-030/duncan-c-white/perl5/ch-2.pl @@ -0,0 +1,27 @@ +#!/usr/bin/perl +# +# 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." +# +# 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). +# + +use v5.10; # for "say" +use strict; +use warnings; +#use Data::Dumper; + + +foreach my $first (1..10) +{ + foreach my $second (1..11-$first) + { + my $third = 12 - $first - $second; + next if $third<1; + next if $first%2==1 && $second%2==1 && $third%2==1; + say "$first-$second-$third"; + } +} |
