From 7e7689b2bc6ea77f92d427c91e194caebe1d10a7 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Mon, 26 Aug 2024 10:51:57 +0100 Subject: Add Perl solution to challenge 284 --- challenge-248/paulo-custodio/perl/ch-1.pl | 2 +- challenge-284/paulo-custodio/Makefile | 2 ++ challenge-284/paulo-custodio/README | 1 + challenge-284/paulo-custodio/perl/ch-1.pl | 39 +++++++++++++++++++++++++ challenge-284/paulo-custodio/perl/ch-2.pl | 46 ++++++++++++++++++++++++++++++ challenge-284/paulo-custodio/t/test-1.yaml | 15 ++++++++++ challenge-284/paulo-custodio/t/test-2.yaml | 15 ++++++++++ 7 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 challenge-284/paulo-custodio/Makefile create mode 100644 challenge-284/paulo-custodio/README create mode 100644 challenge-284/paulo-custodio/perl/ch-1.pl create mode 100644 challenge-284/paulo-custodio/perl/ch-2.pl create mode 100644 challenge-284/paulo-custodio/t/test-1.yaml create mode 100644 challenge-284/paulo-custodio/t/test-2.yaml diff --git a/challenge-248/paulo-custodio/perl/ch-1.pl b/challenge-248/paulo-custodio/perl/ch-1.pl index b99dce5818..021e65777a 100644 --- a/challenge-248/paulo-custodio/perl/ch-1.pl +++ b/challenge-248/paulo-custodio/perl/ch-1.pl @@ -7,7 +7,7 @@ # # You are given a string and a character in the given string. # -# Write a script to return an array of integers of size same as length of +# Write a script to return an array of integers of size same as length of # the given string such that: # # distance[i] is the distance from index i to the closest occurence of diff --git a/challenge-284/paulo-custodio/Makefile b/challenge-284/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-284/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-284/paulo-custodio/README b/challenge-284/paulo-custodio/README new file mode 100644 index 0000000000..87dc0b2fbd --- /dev/null +++ b/challenge-284/paulo-custodio/README @@ -0,0 +1 @@ +Solution by Paulo Custodio diff --git a/challenge-284/paulo-custodio/perl/ch-1.pl b/challenge-284/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..09f0564364 --- /dev/null +++ b/challenge-284/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,39 @@ +#!/usr/bin/env perl + +# Challenge 284 +# +# Task 1: Lucky Integer +# Submitted by: Mohammad Sajid Anwar +# +# You are given an array of integers, @ints. +# +# Write a script to find the lucky integer if found otherwise return -1. If +# there are more than one then return the largest. +# +# A lucky integer is an integer that has a frequency in the array equal to +# its value. +# +# Example 1 +# +# Input: @ints = (2, 2, 3, 4) +# Output: 2 +# +# Example 2 +# +# Input: @ints = (1, 2, 2, 3, 3, 3) +# Output: 3 +# +# Example 3 +# +# Input: @ints = (1, 1, 1, 3) +# Output: -1 + +use Modern::Perl; + +my @ints = @ARGV; +my %count; +$count{$_}++ for @ints; +my @lucky = + sort {$b <=> $a} + grep {$_ == $count{$_}} @ints; +say !@lucky ? -1 : $lucky[0]; diff --git a/challenge-284/paulo-custodio/perl/ch-2.pl b/challenge-284/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..6bb9662a88 --- /dev/null +++ b/challenge-284/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,46 @@ +#!/usr/bin/env perl + +# Challenge 284 +# +# Task 2: Relative Sort +# Submitted by: Mohammad Sajid Anwar +# +# You are given two list of integers, @list1 and @list2. The elements in the +# @list2 are distinct and also in the @list1. +# +# Write a script to sort the elements in the @list1 such that the relative +# order of items in @list1 is same as in the @list2. Elements that is missing +# in @list2 should be placed at the end of @list1 in ascending order. +# Example 1 +# +# Input: @list1 = (2, 3, 9, 3, 1, 4, 6, 7, 2, 8, 5) +# @list2 = (2, 1, 4, 3, 5, 6) +# Ouput: (2, 2, 1, 4, 3, 3, 5, 6, 7, 8, 9) +# +# Example 2 +# +# Input: @list1 = (3, 3, 4, 6, 2, 4, 2, 1, 3) +# @list2 = (1, 3, 2) +# Ouput: (1, 3, 3, 3, 2, 2, 4, 4, 6) +# +# Example 3 +# +# Input: @list1 = (3, 0, 5, 0, 2, 1, 4, 1, 1) +# @list2 = (1, 0, 3, 2) +# Ouput: (1, 1, 1, 0, 0, 3, 2, 4, 5) + +use Modern::Perl; +my $input = "@ARGV"; +my($list1, $list2) = split /,/, $input; +my @list1 = split ' ', $list1; +my @list2 = split ' ', $list2; + +my @output; +while (@list2) { + my $n = shift @list2; + push @output, grep {$_ == $n} @list1; + @list1 = grep {$_ != $n} @list1; +} +push @output, sort {$a <=> $b} @list1; + +say "@output"; diff --git a/challenge-284/paulo-custodio/t/test-1.yaml b/challenge-284/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..5feba9c238 --- /dev/null +++ b/challenge-284/paulo-custodio/t/test-1.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 2 2 3 4 + input: + output: 2 +- setup: + cleanup: + args: 1 2 2 3 3 3 + input: + output: 3 +- setup: + cleanup: + args: 1 1 1 3 + input: + output: -1 diff --git a/challenge-284/paulo-custodio/t/test-2.yaml b/challenge-284/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..f4d2db7f63 --- /dev/null +++ b/challenge-284/paulo-custodio/t/test-2.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 2 3 9 3 1 4 6 7 2 8 5 , 2 1 4 3 5 6 + input: + output: 2 2 1 4 3 3 5 6 7 8 9 +- setup: + cleanup: + args: 3 3 4 6 2 4 2 1 3 , 1 3 2 + input: + output: 1 3 3 3 2 2 4 4 6 +- setup: + cleanup: + args: 3 0 5 0 2 1 4 1 1 , 1 0 3 2 + input: + output: 1 1 1 0 0 3 2 4 5 -- cgit