diff options
| author | Matthias Muth <matthias.muth@gmx.de> | 2024-04-06 22:29:23 +0200 |
|---|---|---|
| committer | Matthias Muth <matthias.muth@gmx.de> | 2024-04-06 22:29:23 +0200 |
| commit | c762e1b042a3a5a09816712e6e4f2e43a8e1122e (patch) | |
| tree | 06b0cc06b60263bae13e4a3914472c75004a4bfa /challenge-262 | |
| parent | 5e7b7cfa1f272e562842627843bc71ae40d79260 (diff) | |
| download | perlweeklychallenge-club-c762e1b042a3a5a09816712e6e4f2e43a8e1122e.tar.gz perlweeklychallenge-club-c762e1b042a3a5a09816712e6e4f2e43a8e1122e.tar.bz2 perlweeklychallenge-club-c762e1b042a3a5a09816712e6e4f2e43a8e1122e.zip | |
Challenge 262 Task 1 and 2 solutions in Perl by Matthias Muth
Diffstat (limited to 'challenge-262')
| -rw-r--r-- | challenge-262/matthias-muth/README.md | 9 | ||||
| -rwxr-xr-x | challenge-262/matthias-muth/perl/ch-1.pl | 26 | ||||
| -rwxr-xr-x | challenge-262/matthias-muth/perl/ch-2.pl | 50 | ||||
| -rw-r--r-- | challenge-262/matthias-muth/perl/challenge-262.txt | 63 |
4 files changed, 143 insertions, 5 deletions
diff --git a/challenge-262/matthias-muth/README.md b/challenge-262/matthias-muth/README.md index e5cbc4eea2..1828cea195 100644 --- a/challenge-262/matthias-muth/README.md +++ b/challenge-262/matthias-muth/README.md @@ -1,6 +1,5 @@ -## The Weekly Challenge -## Solutions in Perl by Matthias Muth +**Challenge 262 solutions in Perl by Matthias Muth** +<br/> +(sorry, no blog post this time...) -See [here](perl/#readme) for a blog post describing this week's solutions. - -#### Thank you for the challenge! +**Thank you for the challenge!** diff --git a/challenge-262/matthias-muth/perl/ch-1.pl b/challenge-262/matthias-muth/perl/ch-1.pl new file mode 100755 index 0000000000..04df8207ab --- /dev/null +++ b/challenge-262/matthias-muth/perl/ch-1.pl @@ -0,0 +1,26 @@ +#!/usr/bin/env perl +# +# The Weekly Challenge - Perl & Raku +# (https://theweeklychallenge.org) +# +# Challenge 262 Task 1: Max Positive Negative +# +# Perl solution by Matthias Muth. +# + +use v5.36; + +use List::Util qw( max ); + +sub max_positive_negative( @ints ) { + return max( scalar( grep $_ < 0, @ints ), scalar( grep $_ > 0, @ints ) ); +} + +use Test2::V0 qw( -no_srand ); +is max_positive_negative( -3, 1, 2, -1, 3, -2, 4 ), 4, + 'Example 1: max_positive_negative( -3, 1, 2, -1, 3, -2, 4 ) == 4'; +is max_positive_negative( -1, -2, -3, 1 ), 3, + 'Example 2: max_positive_negative( -1, -2, -3, 1 ) == 3'; +is max_positive_negative( 1, 2 ), 2, + 'Example 3: max_positive_negative( 1, 2 ) == 2'; +done_testing; diff --git a/challenge-262/matthias-muth/perl/ch-2.pl b/challenge-262/matthias-muth/perl/ch-2.pl new file mode 100755 index 0000000000..3615384899 --- /dev/null +++ b/challenge-262/matthias-muth/perl/ch-2.pl @@ -0,0 +1,50 @@ +#!/usr/bin/env perl +# +# The Weekly Challenge - Perl & Raku +# (https://theweeklychallenge.org) +# +# Challenge 262 Task 2: Count Equal Divisible +# +# Perl solution by Matthias Muth. +# + +use v5.36; + +use List::Util qw( product ); + +sub count_equal_divisible( $ints, $k ) { + my %indices; + for my $index ( 0..$ints->$#* ) { + push $indices{ $ints->[$index] }->@*, $index; + } + my $count = 0; + for my $number ( sort { $a <=> $b } keys %indices ) { + say "number $number indices: $indices{$number}->@*"; + next + if $indices{$number}->@* < 2; + my $last_i_index = $indices{$number}->$#* - 1; + my $last_j_index = $indices{$number}->$#*; + say " i: 0..$last_i_index"; + say " j: i+1..$last_j_index"; + for my $i_index ( 0..$last_i_index ) { + my $i = $indices{$number}[$i_index]; + for my $j_index ( $i_index + 1 .. $last_j_index ) { + my $j = $indices{$number}[$j_index]; + my $is_divisible = ( $i * $j ) % $k == 0; + say " ", "checking (i,j) == ($i,$j): ", + $i * $j, " is ", + $is_divisible ? "" : "not ", "divisible by $k"; + ++$count + if $is_divisible; + } + } + } + return $count; +} + +use Test2::V0 qw( -no_srand ); +is count_equal_divisible( [3, 1, 2, 2, 2, 1, 3], 2 ), 4, + 'Example 1: count_equal_divisible( [3, 1, 2, 2, 2, 1, 3], 2 ) == 4'; +is count_equal_divisible( [1, 2, 3], 1 ), 0, + 'Example 2: count_equal_divisible( [1, 2, 3], 1 ) == 0'; +done_testing; diff --git a/challenge-262/matthias-muth/perl/challenge-262.txt b/challenge-262/matthias-muth/perl/challenge-262.txt new file mode 100644 index 0000000000..28ad90d5dd --- /dev/null +++ b/challenge-262/matthias-muth/perl/challenge-262.txt @@ -0,0 +1,63 @@ +The Weekly Challenge - 262 +Monday, Mar 25, 2024 + + +Task 1: Max Positive Negative +Submitted by: Mohammad Sajid Anwar + +You are given an array of integers, @ints. +Write a script to return the maximum number of either positive or negative integers in the given array. +Example 1 + +Input: @ints = (-3, 1, 2, -1, 3, -2, 4) +Output: 4 + +Count of positive integers: 4 +Count of negative integers: 3 +Maximum of count of positive and negative integers: 4 + +Example 2 + +Input: @ints = (-1, -2, -3, 1) +Output: 3 + +Count of positive integers: 1 +Count of negative integers: 3 +Maximum of count of positive and negative integers: 3 + +Example 3 + +Input: @ints = (1,2) +Output: 2 + +Count of positive integers: 2 +Count of negative integers: 0 +Maximum of count of positive and negative integers: 2 + + +Task 2: Count Equal Divisible +Submitted by: Mohammad Sajid Anwar + +You are given an array of integers, @ints and an integer $k. +Write a script to return the number of pairs (i, j) where +a) 0 <= i < j < size of @ints +b) ints[i] == ints[j] +c) i x j is divisible by k + +Example 1 + +Input: @ints = (3,1,2,2,2,1,3) and $k = 2 +Output: 4 + +(0, 6) => ints[0] == ints[6] and 0 x 6 is divisible by 2 +(2, 3) => ints[2] == ints[3] and 2 x 3 is divisible by 2 +(2, 4) => ints[2] == ints[4] and 2 x 4 is divisible by 2 +(3, 4) => ints[3] == ints[4] and 3 x 4 is divisible by 2 + +Example 2 + +Input: @ints = (1,2,3) and $k = 1 +Output: 0 + + +Last date to submit the solution 23:59 (UK Time) Sunday 31st March 2024. |
