diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-07-14 22:14:40 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-07-14 22:14:40 +0100 |
| commit | 8208b83aa08f2c3a8018f83a6b25fb069247c0aa (patch) | |
| tree | 4745d4a0540b28a59cadb26e12fb47b4b56ee25c | |
| parent | c0bc3dd5be59d0af0501f8a904a10d609c8a9336 (diff) | |
| parent | 6e54c0e90329ff4c9e222801ef7ef35ff499f0b2 (diff) | |
| download | perlweeklychallenge-club-8208b83aa08f2c3a8018f83a6b25fb069247c0aa.tar.gz perlweeklychallenge-club-8208b83aa08f2c3a8018f83a6b25fb069247c0aa.tar.bz2 perlweeklychallenge-club-8208b83aa08f2c3a8018f83a6b25fb069247c0aa.zip | |
Merge pull request #10424 from robbie-hatley/rh277
Tweaks.
| -rwxr-xr-x | challenge-277/robbie-hatley/perl/ch-1.pl | 29 | ||||
| -rwxr-xr-x | challenge-277/robbie-hatley/perl/ch-2.pl | 21 |
2 files changed, 30 insertions, 20 deletions
diff --git a/challenge-277/robbie-hatley/perl/ch-1.pl b/challenge-277/robbie-hatley/perl/ch-1.pl index 8509b51be7..f5ada4a4c3 100755 --- a/challenge-277/robbie-hatley/perl/ch-1.pl +++ b/challenge-277/robbie-hatley/perl/ch-1.pl @@ -60,16 +60,18 @@ Output is to STDOUT and will be each input followed by the corresponding output. use v5.38; use utf8; no warnings 'uninitialized'; - sub count_common ($aref1, $aref2) { + sub format_array :prototype(\@) ($a) {'('.join(', ', map {"\"$_\""} @$a).')'} + sub once_each :prototype(\@\@) ($a1, $a2) { my %counts; - ++$counts{$_}->[0] for @$aref1; - ++$counts{$_}->[1] for @$aref2; - my $count = 0; - for (keys %counts) { - ++$count if 1 == $counts{$_}->[0] - && 1 == $counts{$_}->[1]; + ++$counts{$_}->[0] for @$a1; + ++$counts{$_}->[1] for @$a2; + my @words = (); + for my $word (keys %counts) { + if (1 == $counts{$word}->[0] && 1 == $counts{$word}->[1]) { + push @words, $word; + } } - $count; + return @words; } # ------------------------------------------------------------------------------------------------------------ @@ -105,8 +107,11 @@ my @arrays = @ARGV ? eval($ARGV[0]) : # MAIN BODY OF PROGRAM: for my $aref (@arrays) { say ''; - my $common = count_common($aref->[0], $aref->[1]); - say "Array1 = (${\join(q(, ), map {qq(\"$_\")} @{$aref->[0]})})"; - say "Array2 = (${\join(q(, ), map {qq(\"$_\")} @{$aref->[1]})})"; - say "$common words appear once-each in the two arrays."; + my @array1 = @{$aref->[0]}; + my @array2 = @{$aref->[1]}; + my @once = once_each(@array1, @array2); + my $count = scalar(@once); + say 'Array1 = ' . format_array(@array1); + say 'Array2 = ' . format_array(@array2); + say "$count words appear once-each in the two arrays: " . format_array(@once); } diff --git a/challenge-277/robbie-hatley/perl/ch-2.pl b/challenge-277/robbie-hatley/perl/ch-2.pl index 90d9e75cd6..b89bf1817d 100755 --- a/challenge-277/robbie-hatley/perl/ch-2.pl +++ b/challenge-277/robbie-hatley/perl/ch-2.pl @@ -29,8 +29,8 @@ inequalities: 0 < |x - y| and |x - y| < min(x, y). -------------------------------------------------------------------------------------------------------------- PROBLEM NOTES: To prevent considering duplicate pairs, I make sorted, deduped copies of both arrays, then I use nested ranged -for loops to consider each unique pair, and I use && to and-together the given inequalities to determine how -many pairs are "strong". +for loops to consider each unique pair, then count each pair (x,y) such that |x - y| < min(x, y). +(No need to check that 0 < |x - y| because removing duplicates with "uniq" already ensured that.) -------------------------------------------------------------------------------------------------------------- IO NOTES: @@ -48,15 +48,17 @@ Output is to STDOUT and will be each input followed by the corresponding output. use v5.38; use List::Util 'uniq'; sub min ($x,$y) {return($y<$x?$y:$x)} - sub strong ($aref) { + sub format_array :prototype(\@) ($a) {'('.join(', ', @$a).')'} + sub format_pairs :prototype(\@) ($p) {'('.join(', ', map {'['.$_->[0].','.$_->[1].']'} @$p).')'} + sub strong :prototype(\@) ($aref) { my @uniq = uniq sort {$a<=>$b} @$aref; - my $strong = 0; + my @strong = (); for my $i ( 0 .. $#uniq - 1 ) { my $x = $$aref[$i]; for my $j ( $i + 1 .. $#uniq - 0 ) { my $y = $$aref[$j]; - ++$strong if 0 < abs($y-$x) && abs($y-$x) < min($x,$y); + push(@strong, [$x,$y]) if abs($y-$x) < min($x,$y); } } - return $strong; + return @strong; } # ------------------------------------------------------------------------------------------------------------ @@ -78,6 +80,9 @@ my @arrays = @ARGV ? eval($ARGV[0]) : # MAIN BODY OF PROGRAM: for my $aref (@arrays) { say ''; - say "Array = (${\join(q(, ), @$aref)})"; - say "Number of strong pairs = ${\strong($aref)}"; + my @array = @$aref; + my @strong = strong(@array); + my $count = scalar(@strong); + say 'Array = ' . format_array(@array); + say "Found $count Strong Pairs: " . format_pairs(@strong); } |
