diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-08-25 21:08:47 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-08-25 21:08:47 +0100 |
| commit | dc4b31ab417aabe3c4cf880f22f514a7bed1ac2c (patch) | |
| tree | 93493a0bc92fe0eda2e515e9a89749af1d33557a | |
| parent | 7d6e0b05719ae2935b188026d666c56f9a297c03 (diff) | |
| parent | 631ca04a98a675548113509657bb11d9e15eb73c (diff) | |
| download | perlweeklychallenge-club-dc4b31ab417aabe3c4cf880f22f514a7bed1ac2c.tar.gz perlweeklychallenge-club-dc4b31ab417aabe3c4cf880f22f514a7bed1ac2c.tar.bz2 perlweeklychallenge-club-dc4b31ab417aabe3c4cf880f22f514a7bed1ac2c.zip | |
Merge pull request #12577 from PerlBoy1967/branch-for-challenge-336
w336 - Task 1 & 2
| -rwxr-xr-x | challenge-336/perlboy1967/perl/ch1.pl | 48 | ||||
| -rwxr-xr-x | challenge-336/perlboy1967/perl/ch2.pl | 47 |
2 files changed, 95 insertions, 0 deletions
diff --git a/challenge-336/perlboy1967/perl/ch1.pl b/challenge-336/perlboy1967/perl/ch1.pl new file mode 100755 index 0000000000..1a6c36ec02 --- /dev/null +++ b/challenge-336/perlboy1967/perl/ch1.pl @@ -0,0 +1,48 @@ +#!/bin/perl + +=pod + +L<https://theweeklychallenge.org/blog/perl-weekly-challenge-336#TASK1> + +Author: Niels 'PerlBoy' van Dijke + +Task 1: Equal Group +Submitted by: Mohammad Sajid Anwar + +You are given an array of integers. + +Write a script to return true if the given array can be divided into one or more +groups: each group must be of the same size as the others, with at least two +members, and with all members having the same value. + +=cut + +use Test2::V0 qw(-no_srand); +use exact 'v5.32', -signatures; + +use boolean; +use List::Util qw(reduce); +use List::MoreUtils qw(frequency); + +# Euclidean algorithm for GCD +sub gcd { + my ($a, $b) = @_; + while ($b != 0) { + ($a, $b) = ($b, $a % $b); + } + return $a; +} + +sub equalGroup (@ints) { + my @f = values %{{frequency(@ints)}}; + return false if grep { $_ == 1 } @f; + boolean (1 < reduce { gcd($a,$b) } @f); +} + +is(equalGroup(1,1,2,2,2,2),true,'Example 1'); +is(equalGroup(1,1,1,2,2,2,3,3),false,'Example 2'); +is(equalGroup(5,5,5,5,5,5,7,7,7,7,7,7),true,'Example 3'); +is(equalGroup(1,2,3,4),false,'Example 4'); +is(equalGroup(8,8,9,9,10,10,11,11),true,'Example 5'); + +done_testing; diff --git a/challenge-336/perlboy1967/perl/ch2.pl b/challenge-336/perlboy1967/perl/ch2.pl new file mode 100755 index 0000000000..d2af12fb92 --- /dev/null +++ b/challenge-336/perlboy1967/perl/ch2.pl @@ -0,0 +1,47 @@ +#!/bin/perl + +=pod + +L<https://theweeklychallenge.org/blog/perl-weekly-challenge-336#TASK2> + +Author: Niels 'PerlBoy' van Dijke + +Task 2: Final Score +Submitted by: Mohammad Sajid Anwar + +You are given an array of scores by a team. + +Write a script to find the total score of the given team. The score can be any +integer, +, C or D. The + adds the sum of previous two scores. The score C +invalidates the previous score. The score D will double the previous score. + +=cut + +use Test2::V0 qw(-no_srand); +use exact 'v5.32', -signatures; + +use List::Util qw(sum0); + +sub finalScore (@scores) { + my @stack; + for my $score (@scores) { + given($score) { + when('C') { pop(@stack) if (@stack); next } + when('D') { push(@stack,$stack[-1] * 2) if (@stack); next } + when('+') { push(@stack,$stack[-1] + $stack[-2]) if (@stack > 1); next } + default { push(@stack,$_) } + } + } + sum0(@stack); +} + +is(finalScore(qw{5 2 C D +}),30,'Example 1'); +is(finalScore(qw{5 -2 4 C D 9 + +}),27,'Example 2'); +is(finalScore(qw{7 D D C + 3}),45,'Example 3'); +is(finalScore(qw{-5 -10 + D C +}),-55,'Example 4'); +is(finalScore(qw{3 6 + D C 8 + D -2 C +}),128,'Example 5'); +is(finalScore(qw{1 + C D 0}),0,'Own Example 1'); +is(finalScore(qw{0 C D 0}),0,'Own Example 2'); +is(finalScore(qw{C D 0}),0,'Own Example 3'); + +done_testing; |
