diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-03-18 21:30:33 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-03-18 21:30:33 +0000 |
| commit | 03dddf36060a3f882ab7610894a21a41fa93359d (patch) | |
| tree | 2a966306dd56dc718fa407cc2b23a312b62d139c | |
| parent | 399bcd91439b82b0f396d8c9c3656c9485db15af (diff) | |
| parent | 9e4665f45a81b8e4758bab1c5e87b759e140822f (diff) | |
| download | perlweeklychallenge-club-03dddf36060a3f882ab7610894a21a41fa93359d.tar.gz perlweeklychallenge-club-03dddf36060a3f882ab7610894a21a41fa93359d.tar.bz2 perlweeklychallenge-club-03dddf36060a3f882ab7610894a21a41fa93359d.zip | |
Merge pull request #7734 from pjcs00/wk208
Weekly Challenge 208
| -rw-r--r-- | challenge-208/peter-campbell-smith/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-208/peter-campbell-smith/perl/ch-1.pl | 64 | ||||
| -rwxr-xr-x | challenge-208/peter-campbell-smith/perl/ch-2.pl | 56 |
3 files changed, 121 insertions, 0 deletions
diff --git a/challenge-208/peter-campbell-smith/blog.txt b/challenge-208/peter-campbell-smith/blog.txt new file mode 100644 index 0000000000..8e5f36a31d --- /dev/null +++ b/challenge-208/peter-campbell-smith/blog.txt @@ -0,0 +1 @@ +http://ccgi.campbellsmiths.force9.co.uk/challenge/208 diff --git a/challenge-208/peter-campbell-smith/perl/ch-1.pl b/challenge-208/peter-campbell-smith/perl/ch-1.pl new file mode 100755 index 0000000000..4d15cdec2b --- /dev/null +++ b/challenge-208/peter-campbell-smith/perl/ch-1.pl @@ -0,0 +1,64 @@ +#!/usr/bin/perl + +# Peter Campbell Smith - 2023-03-13 + +use v5.28; +use utf8; +use warnings; + +# You are given two arrays of strings. Write a script to find out all +# common strings in the given two arrays with minimum index sum. If no +# common strings are found, return an empty list. + +# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge/208/1 + +my ($j, @list1, @list2); + +min_index_sum(["Perl", "Raku", "Love"], ["Raku", "Perl", "Hate"]); +min_index_sum(['A', 'B', 'C'], ['D', 'E', 'F']); +min_index_sum(['A', 'B', 'C'], ['C', 'A', 'B']); + +# make a longer example - 100 random letters in each list +for $j (0 .. 99) { + $list1[$j] = chr(ord('a') + int(rand(26))); + $list2[$j] = chr(ord('a') + int(rand(26))); +} +min_index_sum(\@list1, \@list2); + +sub min_index_sum { + + my (@list1, @list2, $j, %index, $least, $rubric1, $sum, $index1, $index2); + + @list1 = @{$_[0]}; + @list2 = @{$_[1]}; + + # make index of first occurrence of word in @list1 - eg $index{"Raku"} == 1 + for $j (0 .. scalar @list1 - 1) { + $index{$list1[$j]} = $j unless $index{$list1[$j]}; + } + + # scan through @list2 + $least = 10^6; + $rubric1 = ''; + for $index2 (0 .. scalar @list2 - 1) { + last if $index2 > $least; + $index1 = $index{$list2[$index2]}; + + # is this $list2 word in @list1? + if (defined $index1) { + $sum = $index1 + $index2; + next if $sum > $least; + + # found a lesser sum of indices + $rubric1 = '' if $sum < $least; + $least = $sum; + $rubric1 .= qq["$list2[$index2]", ]; + } + } + + # show the answers + say qq[\nInput: \@list1 = ("] . join('", "', @list1) . q[")]; + say qq[ \@list2 = ("] . join('", "', @list2) . q[")]; + say qq[Output: (] . substr($rubric1, 0, -2) . q[)]; +} + diff --git a/challenge-208/peter-campbell-smith/perl/ch-2.pl b/challenge-208/peter-campbell-smith/perl/ch-2.pl new file mode 100755 index 0000000000..862280b9b4 --- /dev/null +++ b/challenge-208/peter-campbell-smith/perl/ch-2.pl @@ -0,0 +1,56 @@ +#!/usr/bin/perl + +# Peter Campbell Smith - 2023-03-13 + +use v5.28; +use utf8; +use warnings; + +# You are given an array of integers in sequence with one missing and one duplicate. +# Write a script to find the duplicate and missing integer in the given array. +# Return -1 if none found. You may assume the array contains no more than one duplicate +# and missing. + +# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge/208/2 + +dup_and_miss(1, 2, 3, 3, 5); +dup_and_miss(7, 7, 8, 9, 11); +dup_and_miss(7, 8, 9, 11, 11); +dup_and_miss(4, 5, 6, 7, 8, 8, 9, 10, 11, 12, 13, 15); +dup_and_miss(1, 2, 3, 4, 5); + +sub dup_and_miss { + + my (@list, $j, $duplicate, $missing); + + @list = @_; + + # scan through the list up to the second last entry + for $j (0 .. scalar @list - 2) { + + # if this one is the same as the next one, then this one's a duplicate + if ($list[$j] == $list[$j + 1]) { + invalid(@list) if $duplicate; + $duplicate = $list[$j]; + + # if this one is 2 less than the next one, then this one plus one is missing + } elsif ($list[$j] + 2 == $list[$j + 1]) { + invalid(@list) if $missing; + $missing = $list[$j] + 1; + + # or the list is invalid + } elsif ($list[$j] + 1 != $list[$j + 1]) { + invalid(@list); + } + } + + say qq[\nInput: \@nums = (] . join(', ', @list) . q[)]; + say qq[Output: ] . ((defined $duplicate and defined $missing) ? + qq[Duplicate is $duplicate and Missing is $missing.] : -1); +} + +sub invalid { + say qq[\nInvalid input: \@list = (] . join(', ', @_) . q[)]; + exit; +} + |
