From d8ff7a20c8b9a2d659e323c0da9c52e75cb85b08 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Mon, 26 Aug 2024 16:54:51 +0100 Subject: Add Perl solution to challenge 263 --- challenge-263/paulo-custodio/Makefile | 2 ++ challenge-263/paulo-custodio/perl/ch-1.pl | 43 +++++++++++++++++++++++++ challenge-263/paulo-custodio/perl/ch-2.pl | 51 ++++++++++++++++++++++++++++++ challenge-263/paulo-custodio/t/test-1.yaml | 15 +++++++++ challenge-263/paulo-custodio/t/test-2.yaml | 15 +++++++++ 5 files changed, 126 insertions(+) create mode 100644 challenge-263/paulo-custodio/Makefile create mode 100644 challenge-263/paulo-custodio/perl/ch-1.pl create mode 100644 challenge-263/paulo-custodio/perl/ch-2.pl create mode 100644 challenge-263/paulo-custodio/t/test-1.yaml create mode 100644 challenge-263/paulo-custodio/t/test-2.yaml diff --git a/challenge-263/paulo-custodio/Makefile b/challenge-263/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-263/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-263/paulo-custodio/perl/ch-1.pl b/challenge-263/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..228096e9f8 --- /dev/null +++ b/challenge-263/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,43 @@ +#!/usr/bin/env perl + +# Challenge 263 +# +# Task 1: Target Index +# Submitted by: Mohammad Sajid Anwar +# +# You are given an array of integers, @ints and a target element $k. +# +# Write a script to return the list of indices in the sorted array where the +# element is same as the given target element. +# Example 1 +# +# Input: @ints = (1, 5, 3, 2, 4, 2), $k = 2 +# Output: (1, 2) +# +# Sorted array: (1, 2, 2, 3, 4, 5) +# Target indices: (1, 2) as $ints[1] = 2 and $ints[2] = 2 +# +# Example 2 +# +# Input: @ints = (1, 2, 4, 3, 5), $k = 6 +# Output: () +# +# No element in the given array matching the given target. +# +# Example 3 +# +# Input: @ints = (5, 3, 2, 4, 2, 1), $k = 4 +# Output: (4) +# +# Sorted array: (1, 2, 2, 3, 4, 5) +# Target index: (4) as $ints[4] = 4 + +use Modern::Perl; + +my($k, @ints) = @ARGV; +@ints = sort {$a <=> $b} @ints; +my @idx = + map {$_->[0]} + grep {$_->[1] == $k} + map {[$_, $ints[$_]]} 0 .. $#ints; +say @idx ? "@idx" : "()"; diff --git a/challenge-263/paulo-custodio/perl/ch-2.pl b/challenge-263/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..83d7e446ce --- /dev/null +++ b/challenge-263/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,51 @@ +#!/usr/bin/env perl + +# Challenge 263 +# +# Task 2: Merge Items +# Submitted by: Mohammad Sajid Anwar +# +# You are given two 2-D array of positive integers, $items1 and $items2 where +# element is pair of (item_id, item_quantity). +# +# Write a script to return the merged items. +# Example 1 +# +# Input: $items1 = [ [1,1], [2,1], [3,2] ] +# $items2 = [ [2,2], [1,3] ] +# Output: [ [1,4], [2,3], [3,2] ] +# +# Item id (1) appears 2 times: [1,1] and [1,3]. Merged item now (1,4) +# Item id (2) appears 2 times: [2,1] and [2,2]. Merged item now (2,3) +# Item id (3) appears 1 time: [3,2] +# +# Example 2 +# +# Input: $items1 = [ [1,2], [2,3], [1,3], [3,2] ] +# $items2 = [ [3,1], [1,3] ] +# Output: [ [1,8], [2,3], [3,3] ] +# +# Example 3 +# +# Input: $items1 = [ [1,1], [2,2], [3,3] ] +# $items2 = [ [2,3], [2,4] ] +# Output: [ [1,1], [2,9], [3,3] ] + +use Modern::Perl; +use List::Util qw( uniq pairs ); + +my($items1, $items2) = split /,/, "@ARGV"; +my @items1 = (split ' ', $items1); +my @items2 = (split ' ', $items2); + +my %items; +for (pairs(@items1, @items2)) { + my($k, $v) = @$_; + $items{$k} += $v; +} + +my @result; +for my $k (sort {$a <=> $b} keys %items) { + push @result, $k." ".$items{$k}; +} +say join " ", @result; diff --git a/challenge-263/paulo-custodio/t/test-1.yaml b/challenge-263/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..6640790b5a --- /dev/null +++ b/challenge-263/paulo-custodio/t/test-1.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 2 1 5 3 2 4 2 + input: + output: 1 2 +- setup: + cleanup: + args: 6 1 2 4 3 5 + input: + output: () +- setup: + cleanup: + args: 4 5 3 2 4 2 1 + input: + output: 4 diff --git a/challenge-263/paulo-custodio/t/test-2.yaml b/challenge-263/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..b54303afda --- /dev/null +++ b/challenge-263/paulo-custodio/t/test-2.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 1 1 2 1 3 2 , 2 2 1 3 + input: + output: 1 4 2 3 3 2 +- setup: + cleanup: + args: 1 2 2 3 1 3 3 2 , 3 1 1 3 + input: + output: 1 8 2 3 3 3 +- setup: + cleanup: + args: 1 1 2 2 3 3 , 2 3 2 4 + input: + output: 1 1 2 9 3 3 -- cgit