diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-03-18 20:53:51 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-03-18 20:53:51 +0000 |
| commit | 5b1b01c793b7ca20b641ef021acece0a80ef2c98 (patch) | |
| tree | d9354b3109640f8b0f0eb1f41610b2d6813bda54 | |
| parent | b663066018dbec2cca0bd4b572f116dfa3825f0a (diff) | |
| parent | ae9116a3dd63815d8427f239ceb28506973a20f0 (diff) | |
| download | perlweeklychallenge-club-5b1b01c793b7ca20b641ef021acece0a80ef2c98.tar.gz perlweeklychallenge-club-5b1b01c793b7ca20b641ef021acece0a80ef2c98.tar.bz2 perlweeklychallenge-club-5b1b01c793b7ca20b641ef021acece0a80ef2c98.zip | |
Merge pull request #7727 from wlmb/challenges
Solve PWC208
| -rw-r--r-- | challenge-208/wlmb/blog.txt | 2 | ||||
| -rwxr-xr-x | challenge-208/wlmb/perl/ch-1.pl | 30 | ||||
| -rwxr-xr-x | challenge-208/wlmb/perl/ch-2.pl | 24 |
3 files changed, 56 insertions, 0 deletions
diff --git a/challenge-208/wlmb/blog.txt b/challenge-208/wlmb/blog.txt new file mode 100644 index 0000000000..f9c74067a0 --- /dev/null +++ b/challenge-208/wlmb/blog.txt @@ -0,0 +1,2 @@ +https://wlmb.github.io/2023/03/13/PWC208/ + diff --git a/challenge-208/wlmb/perl/ch-1.pl b/challenge-208/wlmb/perl/ch-1.pl new file mode 100755 index 0000000000..5ad20f583b --- /dev/null +++ b/challenge-208/wlmb/perl/ch-1.pl @@ -0,0 +1,30 @@ +#!/usr/bin/env perl +# Perl weekly challenge 208 +# Task 1: Minimum Index Sum +# +# See https://wlmb.github.io/2023/03/13/PWC208/#task-1-minimum-index-sum +use v5.36; +use List::UtilsBy qw(nsort_by); +use feature qw(refaliasing declared_refs); +no warnings qw(experimental::refaliasing experimental::declared_refs); +die <<~"FIN" unless @ARGV==2; + Usage: $0 S1 S2 + to find the common space separated substrings of S1 and S2 with the minimum index sum + FIN +my (\@list1, \@list2)=map {[split " "]} @ARGV; # Save a couple of $'s +my (%list1, %list2); +@list1{@list1} = (0..@list1-1); +@list2{@list2} = (0..@list2-1); +my $smallest; +my @results = map {$_->[0]} # extract string part + grep { + $smallest//=$_->[1]; # initialize with lowest index sum + $smallest==$_->[1] # compare index sum with lowest + } + nsort_by {$_->[1]} # sort by index sums + map { + [$_, $list1{$_}+$list2{$_}] # [string, index sum] + } + grep {defined $list2{$_}} # common strings go through + keys %list1; +say "(@list1), (@list2) -> (@results)"; # print result diff --git a/challenge-208/wlmb/perl/ch-2.pl b/challenge-208/wlmb/perl/ch-2.pl new file mode 100755 index 0000000000..b8fccdfa89 --- /dev/null +++ b/challenge-208/wlmb/perl/ch-2.pl @@ -0,0 +1,24 @@ +#!/usr/bin/env perl +# Perl weekly challenge 208 +# Task 2: Duplicate and Missing +# +# See https://wlmb.github.io/2023/03/13/PWC208/#task-2-duplicate-and-missing +use v5.36; +die <<~"FIN" unless @ARGV; + Usage: $0 N1 [N2...] + to find missing numbers and duplicates in the list N1 N2... + FIN +my @list=sort {$a <=> $b} @ARGV; +my $previous=shift @list; +my @duplicates; +my @missing; +for(@list){ + push @duplicates, $_ if $_==$previous; + push @missing, $previous+1..$_-1; + $previous=$_ +} +push @missing, $previous+1 unless @missing; # missing after last for default +die "More than one duplicate\n" if @duplicates>1; +die "More than one missing\n" if @missing>1; +my $result=@duplicates?"(@duplicates @missing)":-1; +say "@ARGV -> $result"; |
