diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-08-26 17:12:17 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-08-26 17:12:17 +0100 |
| commit | e6b2d1362da3da633a415e000439aa2ce02d7b27 (patch) | |
| tree | 3afe7a71489a793343cb2c862e133bbc2f80bed0 | |
| parent | b41f573bfd75d0bd374e8cb5fc390bbe5ce29e0b (diff) | |
| parent | d8ff7a20c8b9a2d659e323c0da9c52e75cb85b08 (diff) | |
| download | perlweeklychallenge-club-e6b2d1362da3da633a415e000439aa2ce02d7b27.tar.gz perlweeklychallenge-club-e6b2d1362da3da633a415e000439aa2ce02d7b27.tar.bz2 perlweeklychallenge-club-e6b2d1362da3da633a415e000439aa2ce02d7b27.zip | |
Merge pull request #10706 from pauloscustodio/master
Add Perl solutions
31 files changed, 843 insertions, 1 deletions
diff --git a/challenge-248/paulo-custodio/perl/ch-1.pl b/challenge-248/paulo-custodio/perl/ch-1.pl index 503126e926..021e65777a 100644 --- a/challenge-248/paulo-custodio/perl/ch-1.pl +++ b/challenge-248/paulo-custodio/perl/ch-1.pl @@ -7,7 +7,8 @@ # # 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 the given string such that: +# 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 # the given character in the given string. diff --git a/challenge-259/paulo-custodio/Makefile b/challenge-259/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-259/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-259/paulo-custodio/perl/ch-1.pl b/challenge-259/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..241df88e66 --- /dev/null +++ b/challenge-259/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,62 @@ +#!/usr/bin/env perl + +# Challenge 259 +# +# Task 1: Banking Day Offset +# Submitted by: Lee Johnson +# +# You are given a start date and offset counter. Optionally you also get bank +# holiday date list. +# +# Given a number (of days) and a start date, return the number (of days) +# adjusted to take into account non-banking days. In other words: convert a +# banking day offset to a calendar day offset. +# +# Non-banking days are: +# +# a) Weekends +# b) Bank holidays +# +# Example 1 +# +# Input: $start_date = '2018-06-28', $offset = 3, $bank_holidays = ['2018-07-03'] +# Output: '2018-07-04' +# +# Thursday bumped to Wednesday (3 day offset, with Monday a bank holiday) +# +# Example 2 +# +# Input: $start_date = '2018-06-28', $offset = 3 +# Output: '2018-07-03' + +use Modern::Perl; +use DateTime; + +my $start_date = parse_date(shift @ARGV); +my $offset = shift @ARGV; +my @holidays; +push @holidays, parse_date($_) for @ARGV; + +my $end_date = compute_offset($start_date, $offset, @holidays); +say $end_date->ymd; + +sub compute_offset { + my($start_date, $offset, @holidays) = @_; + my %holidays; $holidays{$_}=1 for @holidays; + + my $date = $start_date; + for (1 .. $offset) { + my $dow; + do { + $date->add(DateTime::Duration->new(days=>1)); + $dow = $date->day_of_week; + } while ($dow==6 || $dow==7 || exists $holidays{$date}); + } + return $date; +} + +sub parse_date { + my($text) = @_; + my($year, $month, $day) = split /-/, $text; + return DateTime->new(year=>$year, month=>$month, day=>$day); +} diff --git a/challenge-259/paulo-custodio/perl/ch-2.pl b/challenge-259/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..46eafd3455 --- /dev/null +++ b/challenge-259/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,115 @@ +#!/usr/bin/env perl + +# Challenge 259 +# +# Task 2: Line Parser +# Submitted by: Gabor Szabo +# +# You are given a line like below: +# +# {% id field1="value1" field2="value2" field3=42 %} +# +# +# Where +# +# a) "id" can be \w+. +# b) There can be 0 or more field-value pairs. +# c) The name of the fields are \w+. +# b) The values are either number in which case we don't need double quotes or +# string in which case we need double quotes around them. +# +# +# The line parser should return structure like below: +# +# { +# name => id, +# fields => { +# field1 => value1, +# field2 => value2, +# field3 => value3, +# } +# } +# +# +# It should be able to parse the following edge cases too: +# +# {% youtube title="Title \"quoted\" done" %} +# +# +# and +# +# {% youtube title="Title with escaped backslash \\" %} +# +# +# BONUS: Extend it to be able to handle multiline tags: +# +# {% id filed1="value1" ... %} +# LINES +# {% endid %} +# +# +# You should expect the following structure from your line parser: +# +# { +# name => id, +# fields => { +# field1 => value1, +# field2 => value2, +# field3 => value3, +# } +# text => LINES +# } + +use Modern::Perl; +use Parse::FSM::Lexer; +use Data::Dump 'dump'; + +my $text = "@ARGV"; +my $data = parse($text); +say dump($data); + +sub parse { + my(@text) = @_; + my $data = {}; + + my $lex = Parse::FSM::Lexer->new; + $lex->from_list($text); + + # start marker + (my $token = $lex->get_token()) or die "start marker missing"; + $token->[0] eq "{" or die "start marker missing, got ", $token->[0]; + ($token = $lex->get_token()) or die "start marker missing"; + $token->[0] eq "%" or die "start marker missing, got ", $token->[0]; + + # name + ($token = $lex->get_token()) or die "name missing"; + $token->[0] eq 'NAME' or die "name expected"; + $data->{name} = $token->[1]; + + # fields + for (;;) { + # field name + ($token = $lex->get_token()) or die "field or end marker missing"; + last if $token->[0] eq '%'; + $token->[0] eq 'NAME' or die "field name expected, got ", $token->[0]; + my $field_name = $token->[1]; + + # = + ($token = $lex->get_token()) or die "'=' expected"; + $token->[0] eq '=' or die "'=' expected, got ", $token->[0]; + + # value + ($token = $lex->get_token()) or die "field value expected"; + ($token->[0] eq 'NUM' || $token->[0] eq 'STR') or die "field value expected, got ", $token->[0]; + my $field_value = $token->[1]; + + $data->{fields}{$field_name} = $field_value; + } + + ($token = $lex->get_token()) or die "end marker missing"; + $token->[0] eq '}' or die "end marker missing, got ", $token->[0]; + + defined($token = $lex->get_token()) and die "extra input, got ", $token->[0]; + + return $data; +} diff --git a/challenge-259/paulo-custodio/t/test-1.yaml b/challenge-259/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..e159d3fdc1 --- /dev/null +++ b/challenge-259/paulo-custodio/t/test-1.yaml @@ -0,0 +1,10 @@ +- setup: + cleanup: + args: 2018-06-28 3 2018-07-03 + input: + output: 2018-07-04 +- setup: + cleanup: + args: 2018-06-28 3 + input: + output: 2018-07-03 diff --git a/challenge-259/paulo-custodio/t/test-2.yaml b/challenge-259/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..ac68f47087 --- /dev/null +++ b/challenge-259/paulo-custodio/t/test-2.yaml @@ -0,0 +1,23 @@ +- setup: + cleanup: + args: '{% id field1=\"value1\" field2=\"value2\" field3=42 %}' + input: + output: | + |{ + | fields => { field1 => "value1", field2 => "value2", field3 => 42 }, + | name => "id", + |} +- setup: + cleanup: + args: '{% youtube title=\"Title \\\"quoted\\\" done\" %}' + input: + output: { fields => { title => "Title \"quoted\" done" }, name => "youtube" } +- setup: + cleanup: + args: '{% youtube title=\"Title with escaped backslash \\\\\" %}' + input: + output: | + |{ + | fields => { title => "Title with escaped backslash \\" }, + | name => "youtube", + |} diff --git a/challenge-260/paulo-custodio/Makefile b/challenge-260/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-260/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-260/paulo-custodio/perl/ch-1.pl b/challenge-260/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..9486b72fc7 --- /dev/null +++ b/challenge-260/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,47 @@ +#!/usr/bin/env perl + +# Challenge 260 +# +# Task 1: Unique Occurrences +# Submitted by: Mohammad Sajid Anwar +# +# You are given an array of integers, @ints. +# +# Write a script to return 1 if the number of occurrences of each value in the +# given array is unique or 0 otherwise. +# Example 1 +# +# Input: @ints = (1,2,2,1,1,3) +# Output: 1 +# +# The number 1 occurred 3 times. +# The number 2 occurred 2 times. +# The number 3 occurred 1 time. +# +# All occurrences are unique, therefore the output is 1. +# +# Example 2 +# +# Input: @ints = (1,2,3) +# Output: 0 +# +# Example 3 +# +# Input: @ints = (-2,0,1,-2,1,1,0,1,-2,9) +# Output: 1 + +use Modern::Perl; + +my @ints = @ARGV; +say uniq_occurences(@ints); + +sub uniq_occurences { + my(@ints) = @_; + my %count; + $count{$_}++ for @ints; + my %uniq; + for (values %count) { + return 0 if $uniq{$_}++; + } + return 1; +} diff --git a/challenge-260/paulo-custodio/perl/ch-2.pl b/challenge-260/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..b256c5e878 --- /dev/null +++ b/challenge-260/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,50 @@ +#!/usr/bin/env perl + +# Challenge 260 +# +# Task 2: Dictionary Rank +# Submitted by: Mark Anderson +# +# You are given a word, $word. +# +# Write a script to compute the dictionary rank of the given word. +# Example 1 +# +# Input: $word = 'CAT' +# Output: 3 +# +# All possible combinations of the letters: +# CAT, CTA, ATC, TCA, ACT, TAC +# +# Arrange them in alphabetical order: +# ACT, ATC, CAT, CTA, TAC, TCA +# +# CAT is the 3rd in the list. +# Therefore the dictionary rank of CAT is 3. +# +# Example 2 +# +# Input: $word = 'GOOGLE' +# Output: 88 +# +# Example 3 +# +# Input: $word = 'SECRET' +# Output: 255 + +use Modern::Perl; +use Algorithm::Combinatorics qw(permutations); +use List::Util 'uniq'; +use List::MoreUtils 'onlyidx'; + +say dictionary_rank(shift // ""); + +sub dictionary_rank { + my($word) = @_; + return + 1+(onlyidx {$_ eq $word} + sort {$a cmp $b} + uniq + map {join '', @$_} + permutations([split //, $word]))[0]; +} diff --git a/challenge-260/paulo-custodio/t/test-1.yaml b/challenge-260/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..afed95bb2a --- /dev/null +++ b/challenge-260/paulo-custodio/t/test-1.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 1 2 2 1 1 3 + input: + output: 1 +- setup: + cleanup: + args: 1 2 3 + input: + output: 0 +- setup: + cleanup: + args: -2 0 1 -2 1 1 0 1 -2 9 + input: + output: 1 diff --git a/challenge-260/paulo-custodio/t/test-2.yaml b/challenge-260/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..035ac3f62a --- /dev/null +++ b/challenge-260/paulo-custodio/t/test-2.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: CAT + input: + output: 3 +- setup: + cleanup: + args: GOOGLE + input: + output: 88 +- setup: + cleanup: + args: SECRET + input: + output: 255 diff --git a/challenge-261/paulo-custodio/Makefile b/challenge-261/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-261/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-261/paulo-custodio/perl/ch-1.pl b/challenge-261/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..aaec18f3ed --- /dev/null +++ b/challenge-261/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,49 @@ +#!/usr/bin/env perl + +# Challenge 261 +# +# Task 1: Element Digit Sum +# Submitted by: Mohammad Sajid Anwar +# +# You are given an array of integers, @ints. +# +# Write a script to evaluate the absolute difference between element and digit +# sum of the given array. +# Example 1 +# +# Input: @ints = (1,2,3,45) +# Output: 36 +# +# Element Sum: 1 + 2 + 3 + 45 = 51 +# Digit Sum: 1 + 2 + 3 + 4 + 5 = 15 +# Absolute Difference: | 51 - 15 | = 36 +# +# Example 2 +# +# Input: @ints = (1,12,3) +# Output: 9 +# +# Element Sum: 1 + 12 + 3 = 16 +# Digit Sum: 1 + 1 + 2 + 3 = 7 +# Absolute Difference: | 16 - 7 | = 9 +# +# Example 3 +# +# Input: @ints = (1,2,3,4) +# Output: 0 +# +# Element Sum: 1 + 2 + 3 + 4 = 10 +# Digit Sum: 1 + 2 + 3 + 4 = 10 +# Absolute Difference: | 10 - 10 | = 0 +# +# Example 4 +# +# Input: @ints = (236, 416, 336, 350) +# Output: 1296 + +use Modern::Perl; +use List::Util 'sum'; + +my @ints = @ARGV; +my @digits = split //, join '', @ints; +say abs(sum(@ints) - sum(@digits)); diff --git a/challenge-261/paulo-custodio/perl/ch-2.pl b/challenge-261/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..289fc37213 --- /dev/null +++ b/challenge-261/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,56 @@ +#!/usr/bin/env perl + +# Challenge 261 +# +# Task 2: Multiply by Two +# Submitted by: Mohammad Sajid Anwar +# +# You are given an array of integers, @ints and an integer $start.. +# +# Write a script to do the followings: +# +# a) Look for $start in the array @ints, if found multiply the number by 2 +# b) If not found stop the process otherwise repeat +# +# In the end return the final value. +# Example 1 +# +# Input: @ints = (5,3,6,1,12) and $start = 3 +# Output: 24 +# +# Step 1: 3 is in the array so 3 x 2 = 6 +# Step 2: 6 is in the array so 6 x 2 = 12 +# Step 3: 12 is in the array so 12 x 2 = 24 +# +# 24 is not found in the array so return 24. +# +# Example 2 +# +# Input: @ints = (1,2,4,3) and $start = 1 +# Output: 8 +# +# Step 1: 1 is in the array so 1 x 2 = 2 +# Step 2: 2 is in the array so 2 x 2 = 4 +# Step 3: 4 is in the array so 4 x 2 = 8 +# +# 8 is not found in the array so return 8. +# +# Example 3 +# +# Input: @ints = (5,6,7) and $start = 2 +# Output: 2 +# +# 2 is not found in the array so return 2. + +use Modern::Perl; + +my($start, @ints) = @ARGV; +say mult_two($start, @ints); + +sub mult_two { + my($n, @ints) = @_; + while (grep {$_ == $n} @ints) { + $n *= 2; + } + return $n; +} diff --git a/challenge-261/paulo-custodio/t/test-1.yaml b/challenge-261/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..26fe36e663 --- /dev/null +++ b/challenge-261/paulo-custodio/t/test-1.yaml @@ -0,0 +1,20 @@ +- setup: + cleanup: + args: 1 2 3 45 + input: + output: 36 +- setup: + cleanup: + args: 1 12 3 + input: + output: 9 +- setup: + cleanup: + args: 1 2 3 4 + input: + output: 0 +- setup: + cleanup: + args: 236 416 336 350 + input: + output: 1296 diff --git a/challenge-261/paulo-custodio/t/test-2.yaml b/challenge-261/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..b1d5ba5d6c --- /dev/null +++ b/challenge-261/paulo-custodio/t/test-2.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 3 5 3 6 1 12 + input: + output: 24 +- setup: + cleanup: + args: 1 1 2 4 3 + input: + output: 8 +- setup: + cleanup: + args: 2 5 6 7 + input: + output: 2 diff --git a/challenge-262/paulo-custodio/Makefile b/challenge-262/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-262/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-262/paulo-custodio/perl/ch-1.pl b/challenge-262/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..de3a9361db --- /dev/null +++ b/challenge-262/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,43 @@ +#!/usr/bin/env perl + +# Challenge 262 +# +# Task 1: Max Positive Negative +# Submitted by: Mohammad Sajid Anwar +# +# You are given an array of integers, @ints. +# +# Write a script to return the maximum number of either positive or negative +# integers in the given array. +# Example 1 +# +# Input: @ints = (-3, 1, 2, -1, 3, -2, 4) +# Output: 4 +# +# Count of positive integers: 4 +# Count of negative integers: 3 +# Maximum of count of positive and negative integers: 4 +# +# Example 2 +# +# Input: @ints = (-1, -2, -3, 1) +# Output: 3 +# +# Count of positive integers: 1 +# Count of negative integers: 3 +# Maximum of count of positive and negative integers: 3 +# +# Example 3 +# +# Input: @ints = (1,2) +# Output: 2 +# +# Count of positive integers: 2 +# Count of negative integers: 0 +# Maximum of count of positive and negative integers: 2 + +use Modern::Perl; +use List::Util 'max'; + +my @ints = @ARGV; +say max(scalar(grep {$_>0} @ints), scalar(grep {$_<0} @ints)); diff --git a/challenge-262/paulo-custodio/perl/ch-2.pl b/challenge-262/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..4977ea41d6 --- /dev/null +++ b/challenge-262/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,45 @@ +#!/usr/bin/env perl + +# Challenge 262 +# +# Task 2: Count Equal Divisible +# Submitted by: Mohammad Sajid Anwar +# +# You are given an array of integers, @ints and an integer $k. +# +# Write a script to return the number of pairs (i, j) where +# +# a) 0 <= i < j < size of @ints +# b) ints[i] == ints[j] +# c) i x j is divisible by k +# +# Example 1 +# +# Input: @ints = (3,1,2,2,2,1,3) and $k = 2 +# Output: 4 +# +# (0, 6) => ints[0] == ints[6] and 0 x 6 is divisible by 2 +# (2, 3) => ints[2] == ints[3] and 2 x 3 is divisible by 2 +# (2, 4) => ints[2] == ints[4] and 2 x 4 is divisible by 2 +# (3, 4) => ints[3] == ints[4] and 3 x 4 is divisible by 2 +# +# Example 2 +# +# Input: @ints = (1,2,3) and $k = 1 +# Output: 0 + +use Modern::Perl; + +my($k, @ints) = @ARGV; +say count_pairs($k, @ints); + +sub count_pairs { + my($k, @ints) = @_; + my $count = 0; + for my $i (0 .. $#ints-1) { + for my $j ($i+1 .. $#ints) { + $count++ if $ints[$i] == $ints[$j] && ($i*$j) % $k == 0; + } + } + return $count; +} diff --git a/challenge-262/paulo-custodio/t/test-1.yaml b/challenge-262/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..0c642bb9bd --- /dev/null +++ b/challenge-262/paulo-custodio/t/test-1.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: -3 1 2 -1 3 -2 4 + input: + output: 4 +- setup: + cleanup: + args: -1 -2 -3 1 + input: + output: 3 +- setup: + cleanup: + args: 1 2 + input: + output: 2 diff --git a/challenge-262/paulo-custodio/t/test-2.yaml b/challenge-262/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..ce1c8bff67 --- /dev/null +++ b/challenge-262/paulo-custodio/t/test-2.yaml @@ -0,0 +1,10 @@ +- setup: + cleanup: + args: 2 3 1 2 2 2 1 3 + input: + output: 4 +- setup: + cleanup: + args: 1 1 2 3 + input: + output: 0 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 diff --git a/challenge-284/paulo-custodio/Makefile b/challenge-284/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3 |
