diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-07-02 12:57:43 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-07-02 12:57:43 +0100 |
| commit | 4f41284bb307dda2b2dab0c26bdefe6723c1755c (patch) | |
| tree | 7b9adcee75535b226b102ad7b6b5f9d948d213a5 | |
| parent | c6b28699f8037caa3d1c32b26d97d96997fac349 (diff) | |
| parent | 3cf1e09eae6aeea8049a73710ed3c20a9bf872c4 (diff) | |
| download | perlweeklychallenge-club-4f41284bb307dda2b2dab0c26bdefe6723c1755c.tar.gz perlweeklychallenge-club-4f41284bb307dda2b2dab0c26bdefe6723c1755c.tar.bz2 perlweeklychallenge-club-4f41284bb307dda2b2dab0c26bdefe6723c1755c.zip | |
Merge pull request #10357 from mgo1977/new-branch
mgo1977 - PWC 276
| -rw-r--r-- | challenge-276/mgo1977/README | 2 | ||||
| -rw-r--r-- | challenge-276/mgo1977/perl/ch1.pl | 74 | ||||
| -rw-r--r-- | challenge-276/mgo1977/perl/ch2.pl | 74 |
3 files changed, 150 insertions, 0 deletions
diff --git a/challenge-276/mgo1977/README b/challenge-276/mgo1977/README new file mode 100644 index 0000000000..e69b97876a --- /dev/null +++ b/challenge-276/mgo1977/README @@ -0,0 +1,2 @@ +Solution by Mariano Ortega + diff --git a/challenge-276/mgo1977/perl/ch1.pl b/challenge-276/mgo1977/perl/ch1.pl new file mode 100644 index 0000000000..7fbb808b0b --- /dev/null +++ b/challenge-276/mgo1977/perl/ch1.pl @@ -0,0 +1,74 @@ +#!/bin/perl -w + +# You are given an array of integers, @hours. +# Write a script to return the number of pairs that forms a complete day. +# A complete day is defined as a time duration that is an exact multiple of 24 hours. + +# Example 1 +# Input: @hours = (12, 12, 30, 24, 24) +# Output: 2 +# +# Pair 1: (12, 12) +# Pair 2: (24, 24) + +# Example 2 +# Input: @hours = (72, 48, 24, 5) +# Output: 3 + +# Pair 1: (72, 48) +# Pair 2: (72, 24) +# Pair 3: (48, 24) + +# Example 3 +# Input: @hours = (12, 18, 24) +# Output: 0 + +testMe(\&process, 'Example1', [12, 12, 30, 24, 24], 2); +testMe(\&process, 'Example2', [72, 48, 24, 5], 3); +testMe(\&process, 'Example3', [12, 18, 24], 0); + +sub testMe { + my $processor = shift; + my $name = shift; + my $input = shift; + my $expectedValue = shift; + + my $got = &$processor(@$input); + + if ( $got==$expectedValue ) { + print "[OK ] $name\n"; + } + else { + print "[ERR] $name :: got=$got, expectedValue=$expectedValue\n"; + } + +} + + +sub process { + my @input = @_; + + my $len = @input; + + my $numberOfPairs = 0; + + for(my $i=0; $i < $len; ++$i) { + + my $v1 = $input[$i]; + + for(my $j=$i+1; $j < $len; ++$j) { + + my $v2 = $input[$j]; + + if ( ($v1+$v2)%24==0 ) { + ++$numberOfPairs; + } + + } + + } + + return $numberOfPairs; +} + + diff --git a/challenge-276/mgo1977/perl/ch2.pl b/challenge-276/mgo1977/perl/ch2.pl new file mode 100644 index 0000000000..136f80de1a --- /dev/null +++ b/challenge-276/mgo1977/perl/ch2.pl @@ -0,0 +1,74 @@ +#!/bin/perl -w + +use feature 'signatures'; + +# You are given an array of positive integers, @ints. +# Write a script to return the total number of elements in the given array which have the highest frequency. + +# Example 1 +# Input: @ints = (1, 2, 2, 4, 1, 5) +# Ouput: 4 + +# The maximum frequency is 2. +# The elements 1 and 2 has the maximum frequency. + + +# Example 2 +# Input: @ints = (1, 2, 3, 4, 5) +# Ouput: 5 + +# The maximum frequency is 1. +# The elements 1, 2, 3, 4 and 5 has the maximum frequency. + + +testMe(\&process, 'Example1', [1, 2, 2, 4, 1, 5], 4); +testMe(\&process, 'Example2', [1, 2, 3, 4, 5], 5); +testMe(\&process, 'Example3', [1, 2, 2, 3, 2, 4, 6, 5, 5], 3); + +sub testMe { + my $processor = shift; + my $name = shift; + my $input = shift; + my $expectedValue = shift; + + my $got = &$processor(@$input); + + if ( $got==$expectedValue ) { + print "[OK ] $name\n"; + } + else { + print "[ERR] $name :: got=$got, expectedValue=$expectedValue\n"; + } + +} + +sub process { + my @input = @_; + + my $len = @input; + + my $result = 0; + + my $map = {}; + + for(my $i=0; $i < $len; ++$i) { + $map->{$input[$i]}++; + } + + my $maxValue = (sort { $b <=> $a } values %$map)[0]; + + foreach my $key (@input) { + + my $value = $map->{$key}; + + if ( $value == $maxValue ) { + ++$result; + } + + } + + return $result; +} + + + |
