diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-10-03 19:21:47 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-10-03 19:21:47 +0100 |
| commit | b5e925cbb6a70718f825833c6682ad7e6c7f0f4f (patch) | |
| tree | a11aef914a5407a7ce38e0ea2ac3837db8ceb7e4 /challenge-080 | |
| parent | 696f1ad5ee44d1e0c77956e7c2efff1116e0ac7f (diff) | |
| parent | a19e1fe614f7a3caef487fda4c130cda89085891 (diff) | |
| download | perlweeklychallenge-club-b5e925cbb6a70718f825833c6682ad7e6c7f0f4f.tar.gz perlweeklychallenge-club-b5e925cbb6a70718f825833c6682ad7e6c7f0f4f.tar.bz2 perlweeklychallenge-club-b5e925cbb6a70718f825833c6682ad7e6c7f0f4f.zip | |
Merge pull request #2438 from Abigail/abigail/week-080
Abigail/week 080
Diffstat (limited to 'challenge-080')
| -rw-r--r-- | challenge-080/abigail/input-1-1 | 3 | ||||
| -rw-r--r-- | challenge-080/abigail/input-2-1 | 2 | ||||
| -rw-r--r-- | challenge-080/abigail/output-1-1.exp | 3 | ||||
| -rw-r--r-- | challenge-080/abigail/output-2-1.exp | 2 | ||||
| -rw-r--r-- | challenge-080/abigail/perl/ch-1.pl | 38 | ||||
| -rw-r--r-- | challenge-080/abigail/perl/ch-2.pl | 66 | ||||
| -rwxr-xr-x | challenge-080/abigail/test.pl | 77 |
7 files changed, 191 insertions, 0 deletions
diff --git a/challenge-080/abigail/input-1-1 b/challenge-080/abigail/input-1-1 new file mode 100644 index 0000000000..8ddac85304 --- /dev/null +++ b/challenge-080/abigail/input-1-1 @@ -0,0 +1,3 @@ +5 2 -2 0 +1 8 -1 +2 0 -1 diff --git a/challenge-080/abigail/input-2-1 b/challenge-080/abigail/input-2-1 new file mode 100644 index 0000000000..7dc81da6aa --- /dev/null +++ b/challenge-080/abigail/input-2-1 @@ -0,0 +1,2 @@ +1 2 2 +1 4 3 2 diff --git a/challenge-080/abigail/output-1-1.exp b/challenge-080/abigail/output-1-1.exp new file mode 100644 index 0000000000..e13c5bfaa6 --- /dev/null +++ b/challenge-080/abigail/output-1-1.exp @@ -0,0 +1,3 @@ +1 +2 +1 diff --git a/challenge-080/abigail/output-2-1.exp b/challenge-080/abigail/output-2-1.exp new file mode 100644 index 0000000000..945d41e4e3 --- /dev/null +++ b/challenge-080/abigail/output-2-1.exp @@ -0,0 +1,2 @@ +4 +7 diff --git a/challenge-080/abigail/perl/ch-1.pl b/challenge-080/abigail/perl/ch-1.pl new file mode 100644 index 0000000000..2bb4050970 --- /dev/null +++ b/challenge-080/abigail/perl/ch-1.pl @@ -0,0 +1,38 @@ +#!/opt/perl/bin/perl + +use 5.032; + +use strict; +use warnings; +no warnings 'syntax'; + +use experimental 'signatures'; +use experimental 'lexical_subs'; + + +# +# Challenge 1: +# +# You are given unsorted list of integers @N. +# +# Write a script to find out the smallest positive number missing. +# + +while (<>) { + # + # Read a line of input, extract the integers, and store + # them in a hash %N. + # + my %N; + @N {/-?[0-9]+/g} = (); + + # + # Find the missing number: start with 1, increament as long + # as it's in %N. We'll stop as soon as we find the missing number. + # + my $try = 1; + $try ++ while exists $N {$try}; + say $try; +} + +__END__ diff --git a/challenge-080/abigail/perl/ch-2.pl b/challenge-080/abigail/perl/ch-2.pl new file mode 100644 index 0000000000..325ff17c73 --- /dev/null +++ b/challenge-080/abigail/perl/ch-2.pl @@ -0,0 +1,66 @@ +#!/opt/perl/bin/perl + +use 5.032; + +use strict; +use warnings; +no warnings 'syntax'; + +use experimental 'signatures'; +use experimental 'lexical_subs'; + + +# +# +# Challenge 2: +# +# You are given rankings of @N candidates. +# +# Write a script to find out the total candies needed for all candidates. +# You are asked to follow the rules below: +# a) You must given at least one candy to each candidate. +# b) Candidate with higher ranking get more candies than their immediate +# neighbors on either side. +# + +# +# Note: +# +# - It is not given that rankings are positive integers, or even integers. +# They may be reals. +# +# - The number of candies handed out is twice the number of candidates +# minus 1, minus the number of pairs of consecutive candidates with +# the same ranking. +# +# We can easily see this by looking at each consecutive pair of +# candidates. If their rankings are unequal, exactly one of them +# gets an extra candy. If the rankings are the same, none of them +# gets an extra candy. Since we have N - 1 such pairs, and every +# candidate gets at least once candy, the total number of candies +# is 2 * N - 1, minus the number of consecutive candidates with the +# same ranking. +# +# This, of course, is the same as N plus the number of consecutive +# pairs with different rankings. +# + + +while (<>) { + # + # Read a line of input, split on whitespace, and put the results + # in an array @N. Initialize the number of candies to the number + # of candidates. + # + my $candies = my @N = split; + + # + # Add a candy of each pair of consecutive candidates with different + # rankings. + # + $N [$_] == $N [$_ + 1] || $candies ++ for 0 .. @N - 2; + + say $candies; +} + +__END__ diff --git a/challenge-080/abigail/test.pl b/challenge-080/abigail/test.pl new file mode 100755 index 0000000000..310382b62b --- /dev/null +++ b/challenge-080/abigail/test.pl @@ -0,0 +1,77 @@ +#!/opt/perl/bin/perl + +# +# Test the solutions. Either call it with the directory name you +# want to test in, or call it as "../test.pl" from within the directory. +# + +use 5.032; + +use strict; +use warnings; +no warnings 'syntax'; + +chdir ".." if -f "../test.pl"; + +use experimental 'signatures'; + +use Test::More; + + +my %languages = ( + Perl => { + exe => "/opt/perl/bin/perl", + ext => "pl", + }, + JavaScript => { + exe => "/usr/local/bin/node", + ext => "js", + dir => "node", + }, + bc => { + exe => "/usr/bin/bc", + ext => "bc", + filter => 's/.*/main($&)/', + }, + awk => { + exe => "/usr/bin/awk", + ext => "awk", + args => ["-f"], + }, +); + +my $perl_exe = $languages {Perl} {exe}; + +foreach my $challenge (1, 2) { + my @inputs = <input-$challenge-*> or next; + subtest "Challenge $challenge" => sub { + foreach my $language (sort keys %languages) { + my $info = $languages {$language}; + my $exe = $$info {exe}; + my $ext = $$info {ext}; + my $dir = $$info {dir} // lc $language; + my @args = @{$$info {args} // []}; + my $filter = $$info {filter} // ''; + my $solution = "$dir/ch-$challenge.$ext"; + next unless -r $solution; + + subtest $language => sub { + foreach my $input (@inputs) { + my $output_exp = ($input =~ s/input/output/r) . ".exp"; + my $exp = `cat $output_exp`; + + my $got = `$perl_exe -ple '$filter' $input |\ + $exe @args ./$solution`; + + s/\h+$//gm for $exp, $got; + is $got, $exp, $input; + } + } + } + } +} + +done_testing; + + +__END__ |
