diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-03-04 11:33:23 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-03-04 11:33:23 +0000 |
| commit | 0db2ce216b027c4f67ec7f52cc13b56243e85788 (patch) | |
| tree | 53f1d767f2bc51b0473055c531d0fbad21c02b4b | |
| parent | 02d069b0c634ebe4bb986224846c85889b9e214c (diff) | |
| parent | 234e36c18af3f3009930b02740b0dc3e302b6f16 (diff) | |
| download | perlweeklychallenge-club-0db2ce216b027c4f67ec7f52cc13b56243e85788.tar.gz perlweeklychallenge-club-0db2ce216b027c4f67ec7f52cc13b56243e85788.tar.bz2 perlweeklychallenge-club-0db2ce216b027c4f67ec7f52cc13b56243e85788.zip | |
Merge pull request #7644 from pjcs00/wk206
Week 206
| -rw-r--r-- | challenge-206/peter-campbell-smith/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-206/peter-campbell-smith/perl/ch-01.pl | 54 | ||||
| -rwxr-xr-x | challenge-206/peter-campbell-smith/perl/ch-02.pl | 32 |
3 files changed, 87 insertions, 0 deletions
diff --git a/challenge-206/peter-campbell-smith/blog.txt b/challenge-206/peter-campbell-smith/blog.txt new file mode 100644 index 0000000000..909c6150e8 --- /dev/null +++ b/challenge-206/peter-campbell-smith/blog.txt @@ -0,0 +1 @@ +http://ccgi.campbellsmiths.force9.co.uk/challenge/206/1 diff --git a/challenge-206/peter-campbell-smith/perl/ch-01.pl b/challenge-206/peter-campbell-smith/perl/ch-01.pl new file mode 100755 index 0000000000..578dce976b --- /dev/null +++ b/challenge-206/peter-campbell-smith/perl/ch-01.pl @@ -0,0 +1,54 @@ +#!/usr/bin/perl + +# Peter Campbell Smith - 2023-02-20 + +use v5.28; +use utf8; +use warnings; + +# Task: You are given a list of time points, at least 2, in the 24-hour clock format HH:MM. Write a script +# to find out the shortest time in minutes between any two time points. + +# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge/206/1 + +my (@time, $j); + +shortest_interval("00:01", "01:05", "01:12", "23:59"); +shortest_interval("01:01", "00:50", "00:57"); +shortest_interval("10:10", "09:30", "09:00", "09:55"); +shortest_interval("08:59", "21:00"); +shortest_interval("09:01", "21:00"); + +for $j (0 .. 20) { + @time[$j] = sprintf('%02d:%02d', rand(24), rand(60)); +} +shortest_interval(@time); + +sub shortest_interval { + + my (@times, $minutes, $shortest, $j, $gap, $this_minutes); + + # sort the times so that the shortest gap is bewteen 2 consecutive entries + @times = sort @_; + + # add one to the end that is 24hr after the first, in case the shortest bridges midnight + $times[0] =~ m|^(\d\d).(\d\d)|; + $times[scalar(@times)] = sprintf('%02d:%02d', $1 + 24, $2); + $minutes = $1 * 60 + $2; + + # pass along the list comparing each element with the previous one + $shortest = 9999; + for $j (1 .. scalar(@times) - 1) { + $times[$j] =~ m|^(\d\d).(\d\d)|; + $this_minutes = $1 * 60 + $2; + $gap = $this_minutes - $minutes; + $shortest = $gap if $gap < $shortest; + last if $shortest == 0; + $minutes = $this_minutes; + } + + say qq[\nInput: \@time = ("] . join('", "', @_) . qq[")\nOutput: $shortest]; +} + + + diff --git a/challenge-206/peter-campbell-smith/perl/ch-02.pl b/challenge-206/peter-campbell-smith/perl/ch-02.pl new file mode 100755 index 0000000000..b30a8f2d53 --- /dev/null +++ b/challenge-206/peter-campbell-smith/perl/ch-02.pl @@ -0,0 +1,32 @@ +#!/usr/bin/perl + +# Peter Campbell Smith - 2023-02-27 + +use v5.28; +use utf8; +use warnings; + +# Task: You are given an array of integers having even number of elements. Divide the array into +# all possible pairs of elements. Write a script to find the two pairs whose minima sum to the +# maximum value. + +# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge/206/1 + + +max_min(1, 2, 3, 4); +max_min(0, 2, 1, 3); +max_min(3, 7, 4, 12, 5, 1, 0, 10, 9, 2, 11, 13, -1, 6, 2); + +sub max_min { + + # reverse sort the array numerically + my @array = reverse sort { $a <=> $b} @_; + + # show the results (see blog for explanation) + say qq[\nInput: \@array = (] . join(', ', @_) . qq[)]; + say qq[Output: ] . ($array[1] + $array[3]) . + qq[ = min($array[0], $array[1]) + min($array[2], $array[3])]; +} + + + |
