From 6c7dbe6403cd700b6d9ffc7cfff4e03dbe828091 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Tue, 27 Aug 2024 21:58:20 +0100 Subject: Add Perl solution to challenge 276 --- challenge-276/paulo-custodio/Makefile | 2 ++ challenge-276/paulo-custodio/perl/ch-1.pl | 43 ++++++++++++++++++++++++++++++ challenge-276/paulo-custodio/perl/ch-2.pl | 35 ++++++++++++++++++++++++ challenge-276/paulo-custodio/t/test-1.yaml | 15 +++++++++++ challenge-276/paulo-custodio/t/test-2.yaml | 10 +++++++ 5 files changed, 105 insertions(+) create mode 100644 challenge-276/paulo-custodio/Makefile create mode 100644 challenge-276/paulo-custodio/perl/ch-1.pl create mode 100644 challenge-276/paulo-custodio/perl/ch-2.pl create mode 100644 challenge-276/paulo-custodio/t/test-1.yaml create mode 100644 challenge-276/paulo-custodio/t/test-2.yaml diff --git a/challenge-276/paulo-custodio/Makefile b/challenge-276/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-276/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-276/paulo-custodio/perl/ch-1.pl b/challenge-276/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..aedd6c5efc --- /dev/null +++ b/challenge-276/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,43 @@ +#!/usr/bin/env perl + +# Challenge 276 +# +# Task 1: Complete Day +# Submitted by: Mohammad Sajid Anwar +# 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 + +use Modern::Perl; + +say complete_days(@ARGV); + +sub complete_days { + my(@hours) = @_; + my $pairs = 0; + for my $i (0 .. $#hours-1) { + for my $j ($i+1 .. $#hours) { + $pairs++ if ($hours[$i]+$hours[$j]) % 24 == 0; + } + } + return $pairs; +} diff --git a/challenge-276/paulo-custodio/perl/ch-2.pl b/challenge-276/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..c9b50fc0b1 --- /dev/null +++ b/challenge-276/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,35 @@ +#!/usr/bin/env perl + +# Challenge 276 +# +# Task 2: Maximum Frequency +# Submitted by: Mohammad Sajid Anwar +# 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. + +use Modern::Perl; +use List::Util 'max'; + +say max_freq(@ARGV); + +sub max_freq { + my(@ints) = @_; + my %count; $count{$_}++ for @ints; + my $max_count = max(values %count); + my $max_freq = scalar grep {$count{$_} == $max_count} keys %count; + return $max_freq; +} diff --git a/challenge-276/paulo-custodio/t/test-1.yaml b/challenge-276/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..3b3fa1ea4b --- /dev/null +++ b/challenge-276/paulo-custodio/t/test-1.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 12 12 30 24 24 + input: + output: 2 +- setup: + cleanup: + args: 72 48 24 5 + input: + output: 3 +- setup: + cleanup: + args: 12 18 24 + input: + output: 0 diff --git a/challenge-276/paulo-custodio/t/test-2.yaml b/challenge-276/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..8da34089b6 --- /dev/null +++ b/challenge-276/paulo-custodio/t/test-2.yaml @@ -0,0 +1,10 @@ +- setup: + cleanup: + args: 1 2 2 4 1 5 + input: + output: 2 +- setup: + cleanup: + args: 1 2 3 4 5 + input: + output: 5 -- cgit