diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-06-24 23:37:00 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-06-24 23:37:00 +0100 |
| commit | 5d3cbaedcd479f50731b4e3f30ba5e292a347776 (patch) | |
| tree | 6a9edd66ceadb423518ec4bbf1c14fc1a04d017e | |
| parent | 9d954375e0d1633a07bb90cc604038778710510c (diff) | |
| parent | 16248e9919dba10def1ff1596ca84ad0b460b3f0 (diff) | |
| download | perlweeklychallenge-club-5d3cbaedcd479f50731b4e3f30ba5e292a347776.tar.gz perlweeklychallenge-club-5d3cbaedcd479f50731b4e3f30ba5e292a347776.tar.bz2 perlweeklychallenge-club-5d3cbaedcd479f50731b4e3f30ba5e292a347776.zip | |
Merge pull request #12228 from pjcs00/wk327
Week 327 - Missing and mad
| -rw-r--r-- | challenge-327/peter-campbell-smith/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-327/peter-campbell-smith/perl/ch-1.pl | 34 | ||||
| -rwxr-xr-x | challenge-327/peter-campbell-smith/perl/ch-2.pl | 42 |
3 files changed, 77 insertions, 0 deletions
diff --git a/challenge-327/peter-campbell-smith/blog.txt b/challenge-327/peter-campbell-smith/blog.txt new file mode 100644 index 0000000000..9d37e43d9a --- /dev/null +++ b/challenge-327/peter-campbell-smith/blog.txt @@ -0,0 +1 @@ +http://ccgi.campbellsmiths.force9.co.uk/challenge/327 diff --git a/challenge-327/peter-campbell-smith/perl/ch-1.pl b/challenge-327/peter-campbell-smith/perl/ch-1.pl new file mode 100755 index 0000000000..6bf57a15b6 --- /dev/null +++ b/challenge-327/peter-campbell-smith/perl/ch-1.pl @@ -0,0 +1,34 @@ +#!/usr/bin/perl + +# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge + +use v5.26; # The Weekly Challenge - 2025-06-23 +use utf8; # Week 327 - task 1 - Missing integers +use warnings; # Peter Campbell Smith +binmode STDOUT, ':utf8'; +use Encode; + +missing_integers(1, 2, 1, 3, 2, 5); +missing_integers(1, 1, 1); +missing_integers(2, 2, 1); +missing_integers(7, 8, 9); +missing_integers(5, 5, 5, 5, 5); +missing_integers(5, 4, 3, 2, 1); + +sub missing_integers { + + my (@ints, @present, @absent); + + # initialise + @ints = @_; + + # set $present[$j] to 1 if $j is present in @ints + $present[$ints[$_]] = defined $ints[$_] ? 1 : 0 for 0 .. $#ints; + + # push $j onto @absent if $present[$j] is not defined + push @absent, grep {not defined $present[$_]} 1 .. scalar @ints; + + # report results + say qq[\nInput: (] . join(', ', @ints) . q[)]; + say qq[Output: (] . join(', ', @absent) . q[)]; +} diff --git a/challenge-327/peter-campbell-smith/perl/ch-2.pl b/challenge-327/peter-campbell-smith/perl/ch-2.pl new file mode 100755 index 0000000000..c76b53670a --- /dev/null +++ b/challenge-327/peter-campbell-smith/perl/ch-2.pl @@ -0,0 +1,42 @@ +#!/usr/bin/perl + +# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge + +use v5.26; # The Weekly Challenge - 2025-06-23 +use utf8; # Week 327 - task 2 - Mad +use warnings; # Peter Campbell Smith +binmode STDOUT, ':utf8'; +use Encode; + +mad(4, 1, 2, 3); +mad(1, 3, 7, 11, 15); +mad(1, 5, 3, 8); + +# bigger example +my (@input); +push @input, int(rand(10000)) for 0 .. 74; +mad(@input); + +sub mad { + + my(@input, $i, $j, %difference, @sorted); + + # initialise (and sort to match Mohammad's output) + @input = sort @_; + + # create all pairs + for $i (0 .. $#input - 1) { + for $j ($i + 1 .. $#input) { + + # store the differences in $difference{$diff} + $difference{abs($input[$i] - $input[$j])} .= qq[[$input[$i], $input[$j]], ]; + } + } + + # sort the keys of @difference - so $sorted[0] is the least + @sorted = sort {$a <=> $b} keys %difference; + + # report + say qq[\nInput: (] . join(', ', @_) . q[)]; + say qq[Output: ] . substr($difference{$sorted[0]}, 0, -2); +} |
