diff options
| author | Andrew Shitov <mail@andreyshitov.com> | 2024-04-02 08:55:46 +0200 |
|---|---|---|
| committer | Andrew Shitov <mail@andreyshitov.com> | 2024-04-02 08:55:46 +0200 |
| commit | 58863a39e4fb0cf8abb304d475ea35f2d25800c8 (patch) | |
| tree | 4642a321cf9eecd993babd7e4345ce84a8148c0c | |
| parent | 6ccd059d128a87bfcc67d42046bb4cf67a1a40af (diff) | |
| parent | 6e3cd6e8a2eb65b0ba00b4e15e1abeb689a33e8d (diff) | |
| download | perlweeklychallenge-club-58863a39e4fb0cf8abb304d475ea35f2d25800c8.tar.gz perlweeklychallenge-club-58863a39e4fb0cf8abb304d475ea35f2d25800c8.tar.bz2 perlweeklychallenge-club-58863a39e4fb0cf8abb304d475ea35f2d25800c8.zip | |
Merge remote-tracking branch 'upstream/master' into ash-263
46 files changed, 3857 insertions, 2875 deletions
diff --git a/challenge-263/eric-cheung/python/ch-1.py b/challenge-263/eric-cheung/python/ch-1.py new file mode 100755 index 0000000000..7c330e0eda --- /dev/null +++ b/challenge-263/eric-cheung/python/ch-1.py @@ -0,0 +1,16 @@ +
+## Example 1
+## arrInt = [1, 5, 3, 2, 4, 2]
+## nK = 2
+
+## Example 2
+## arrInt = [1, 2, 4, 3, 5]
+## nK = 6
+
+## Example 3
+arrInt = [5, 3, 2, 4, 2, 1]
+nK = 4
+
+arrOutput = [nIndxLoop for nIndxLoop, elemLoop in enumerate(sorted(arrInt)) if elemLoop == nK]
+
+print (arrOutput)
diff --git a/challenge-263/eric-cheung/python/ch-2.py b/challenge-263/eric-cheung/python/ch-2.py new file mode 100755 index 0000000000..b50b2084d7 --- /dev/null +++ b/challenge-263/eric-cheung/python/ch-2.py @@ -0,0 +1,18 @@ +
+## Example 1
+## arrItem_01 = [[1, 1], [2, 1], [3, 2]]
+## arrItem_02 = [[2, 2], [1, 3]]
+
+## Example 2
+## arrItem_01 = [[1, 2], [2, 3], [1, 3], [3, 2]]
+## arrItem_02 = [[3, 1], [1, 3]]
+
+## Example 3
+arrItem_01 = [[1, 1], [2, 2], [3, 3]]
+arrItem_02 = [[2, 3], [2, 4]]
+
+arrCombine = arrItem_01 + arrItem_02
+arrUniq = set([arrLoop[0] for arrLoop in arrCombine])
+arrOutput = [[elemLoop, sum([arrLoop[1] for arrLoop in arrCombine if arrLoop[0] == elemLoop])] for elemLoop in arrUniq]
+
+print (arrOutput)
diff --git a/challenge-263/feng-chang/raku/ch-1.raku b/challenge-263/feng-chang/raku/ch-1.raku new file mode 100755 index 0000000000..7f02543374 --- /dev/null +++ b/challenge-263/feng-chang/raku/ch-1.raku @@ -0,0 +1,6 @@ +#!/bin/env raku + +unit sub MAIN(*@ints); + +my \k = @ints.pop; +put @ints.sort.grep(k, :k).gist; diff --git a/challenge-263/feng-chang/raku/ch-2.raku b/challenge-263/feng-chang/raku/ch-2.raku new file mode 100755 index 0000000000..aa93ae7c2a --- /dev/null +++ b/challenge-263/feng-chang/raku/ch-2.raku @@ -0,0 +1,10 @@ +#!/bin/env raku + +unit sub MAIN(Str:D $s1, Str:D $s2); + +use MONKEY-SEE-NO-EVAL; + +my @items = EVAL "$s1, $s2"; +my %merged; +%merged{$_[0]} += $_[1] for @items; +put %merged.keys.sort.map({ [ +$_, %merged{$_} ] }).Array.raku; diff --git a/challenge-263/feng-chang/raku/ch-2a.raku b/challenge-263/feng-chang/raku/ch-2a.raku new file mode 100755 index 0000000000..1b8f481323 --- /dev/null +++ b/challenge-263/feng-chang/raku/ch-2a.raku @@ -0,0 +1,7 @@ +#!/bin/env raku + +unit sub MAIN(Str:D $s1, Str:D $s2); + +use MONKEY-SEE-NO-EVAL; + +put (EVAL "$s1, $s2").map({ $_[0] xx $_[1] }).Bag.sort(+*.key).map({ [+.key, .value] }).Array.raku; diff --git a/challenge-263/feng-chang/raku/test.raku b/challenge-263/feng-chang/raku/test.raku new file mode 100755 index 0000000000..c35a6af606 --- /dev/null +++ b/challenge-263/feng-chang/raku/test.raku @@ -0,0 +1,34 @@ +#!/bin/env raku + +# The Weekly Challenge 263 +use Test; + +sub pwc-test(Str:D $script, Bool :$deeply? = False, *@input) { + my ($expect, $assertion) = @input.splice(*-2, 2); + my $p = run $script, |@input, :out; + if $deeply { + is-deeply $p.out.slurp(:close).chomp.words.Bag, $expect, $assertion; + } else { + is $p.out.slurp(:close).chomp, $expect, $assertion; + } +} + +# Task 1, Target Index +pwc-test './ch-1.raku', <1 5 3 2 4 2>, 2, '(1 2)', 'Target Index: @ints = (1,5,3,2,4,2), $k=2 => (1, 2)'; +pwc-test './ch-1.raku', <1 2 4 3 5>, 6, '()', 'Target Index: @ints = (1,2,4,3,5), $k=6 => ()'; +pwc-test './ch-1.raku', <5 3 2 4 2 1>, 4, '(4)', 'Target Index: @ints = (5,3,2,4,2,1), $k=4 => (4)'; + +# Task 2, Merge Items +pwc-test './ch-2.raku', '[1,1],[2,1],[3,2]', '[2,2],[1,3]', '[[1, 4], [2, 3], [3, 2]]', + 'Merge Items: $items1 = [[1,1],[2,1],[3,2]], $items2 = [[2,2],[1,3]] => [[1,4],[2,3],[3,2]]'; +pwc-test './ch-2.raku', '[1,2],[2,3],[1,3],[3,2]', '[3,1],[1,3]', '[[1, 8], [2, 3], [3, 3]]', + 'Merge Items: $items1 = [[1,2],[2,3],[1,3],[3,2]], $items2 = [[3,1],[1,3]] => [[1,8],[2,3],[3,3]]'; +pwc-test './ch-2.raku', '[1,1],[2,2],[3,3]', '[2,3],[2,4]', '[[1, 1], [2, 9], [3, 3]]', + 'Merge Items: $items1 = [[1,1],[2,2],[3,3]], $items2 = [[2,3],[2,4]] => [[1, 1], [2, 9], [3, 3]]'; + +pwc-test './ch-2a.raku', '[1,1],[2,1],[3,2]', '[2,2],[1,3]', '[[1, 4], [2, 3], [3, 2]]', + 'Merge Items: $items1 = [[1,1],[2,1],[3,2]], $items2 = [[2,2],[1,3]] => [[1,4],[2,3],[3,2]]'; +pwc-test './ch-2a.raku', '[1,2],[2,3],[1,3],[3,2]', '[3,1],[1,3]', '[[1, 8], [2, 3], [3, 3]]', + 'Merge Items: $items1 = [[1,2],[2,3],[1,3],[3,2]], $items2 = [[3,1],[1,3]] => [[1,8],[2,3],[3,3]]'; +pwc-test './ch-2a.raku', '[1,1],[2,2],[3,3]', '[2,3],[2,4]', '[[1, 1], [2, 9], [3, 3]]', + 'Merge Items: $items1 = [[1,1],[2,2],[3,3]], $items2 = [[2,3],[2,4]] => [[1, 1], [2, 9], [3, 3]]'; diff --git a/challenge-263/james-smith/blog.txt b/challenge-263/james-smith/blog.txt new file mode 100644 index 0000000000..e69ad27da2 --- /dev/null +++ b/challenge-263/james-smith/blog.txt @@ -0,0 +1 @@ +https://challenges.jamessmith.me.uk/weekly/weekly-challenge-263 diff --git a/challenge-263/james-smith/perl/ch-1.pl b/challenge-263/james-smith/perl/ch-1.pl new file mode 100644 index 0000000000..7534c1232c --- /dev/null +++ b/challenge-263/james-smith/perl/ch-1.pl @@ -0,0 +1,5 @@ +sub target_index { + my( $k, @c ) = ( pop, 0, 0, 0 ); + $c[ $_ <=> $k ]++ for @_; + $c[2] .. $c[2] + $c[0] - 1 +} diff --git a/challenge-263/james-smith/perl/ch-2.pl b/challenge-263/james-smith/perl/ch-2.pl new file mode 100644 index 0000000000..784e4ca62c --- /dev/null +++ b/challenge-263/james-smith/perl/ch-2.pl @@ -0,0 +1,7 @@ +sub merge_items { + my %c; + for( @_ ) { + $c{ $_->[0] } += $_->[1] for @{$_} + } + map { [ 0 + $_ => $c{$_} ] } keys %c +} diff --git a/challenge-263/jeanluc2020/blog-1.txt b/challenge-263/jeanluc2020/blog-1.txt new file mode 100644 index 0000000000..03c12db1f2 --- /dev/null +++ b/challenge-263/jeanluc2020/blog-1.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-263-1.html diff --git a/challenge-263/jeanluc2020/blog-2.txt b/challenge-263/jeanluc2020/blog-2.txt new file mode 100644 index 0000000000..53ce1ab25b --- /dev/null +++ b/challenge-263/jeanluc2020/blog-2.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-263-2.html diff --git a/challenge-263/jeanluc2020/perl/ch-1.pl b/challenge-263/jeanluc2020/perl/ch-1.pl new file mode 100755 index 0000000000..6953f7a2ae --- /dev/null +++ b/challenge-263/jeanluc2020/perl/ch-1.pl @@ -0,0 +1,62 @@ +#!/usr/bin/env perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-263/#TASK1 +# +# Task 1: Target Index +# ==================== +# +# 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 $k[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 +# +############################################################ +## +## discussion +## +############################################################ +# +# Straight forward steps: sort the input array, use an index to +# walk the array and remember the instances where $ints[$i] == $k. + +use strict; +use warnings; + +target_index( [1, 5, 3, 2, 4, 2], 2); +target_index( [1, 2, 4, 3, 5], 6); +target_index( [5, 3, 2, 4, 2, 1], 4); + +sub target_index { + my ($ints, $k) = @_; + print "Input: (" . join(", ", @$ints) . ")\n"; + my @result = (); + my @sorted = sort @$ints; + foreach my $i (0..$#sorted) { + if($sorted[$i] == $k) { + push @result, $i; + } + } + print "Output: (" . join(", ", @result) . ")\n"; +} diff --git a/challenge-263/jeanluc2020/perl/ch-2.pl b/challenge-263/jeanluc2020/perl/ch-2.pl new file mode 100755 index 0000000000..7ab660b370 --- /dev/null +++ b/challenge-263/jeanluc2020/perl/ch-2.pl @@ -0,0 +1,77 @@ +#!/usr/bin/env perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-263/#TASK2 +# +# Task 2: Merge Items +# =================== +# +# 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] ] +# +############################################################ +## +## discussion +## +############################################################ +# +# For this one, actually printing input and output is a little +# more tricky than the actual algorithm: Simply walk the combination +# of both arrays, and for each entry use the first element of that +# as the key of a result hash and the second element as what to add +# to the value of that entry in the hash. Print the result sorted +# by the hash keys. + +use strict; +use warnings; + +merge_items( [ [1,1], [2,1], [3,2] ], [ [2,2], [1,3] ] ); +merge_items( [ [1,2], [2,3], [1,3], [3,2] ], [ [3,1], [1,3] ] ); +merge_items( [ [1,1], [2,2], [3,3] ], [ [2,3], [2,4] ] ); + +sub merge_items { + my ($items1, $items2) = @_; + my $result; + print "Input: item1: ["; + foreach my $elem (@$items1) { + print "[$elem->[0], $elem->[1]],"; + } + print "],\n"; + print " item2: ["; + foreach my $elem (@$items2) { + print "[$elem->[0], $elem->[1]],"; + } + print "]\n"; + + foreach my $elem( (@$items1, @$items2) ) { + $result->{$elem->[0]} += $elem->[1]; + } + + print "Output: ["; + foreach my $elem (sort {$a <=> $b} keys %$result) { + print "[$elem, $result->{$elem}],"; + } + print "]\n"; +} diff --git a/challenge-263/jeanluc2020/python/ch-1.py b/challenge-263/jeanluc2020/python/ch-1.py new file mode 100755 index 0000000000..ebfafa7558 --- /dev/null +++ b/challenge-263/jeanluc2020/python/ch-1.py @@ -0,0 +1,56 @@ +#!/usr/bin/python3 +# https://theweeklychallenge.org/blog/perl-weekly-challenge-263/#TASK1 +# +# Task 1: Target Index +# ==================== +# +# 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 $k[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 +# +############################################################ +## +## discussion +## +############################################################ +# +# Straight forward steps: sort the input array, use an index to +# walk the array and remember the instances where $ints[$i] == $k. + +def target_index(ints: list, k: int) -> None: + print("Input: (", ", ".join(str(x) for x in ints), ")", sep="") + result = [] + sorted_ints = sorted(ints) + for i in range(len(sorted_ints)): + if sorted_ints[i] == k: + result.append(i) + print("Output: (", ", ".join(str(x) for x in result), ")", sep="") + +target_index( [1, 5, 3, 2, 4, 2], 2); +target_index( [1, 2, 4, 3, 5], 6); +target_index( [5, 3, 2, 4, 2, 1], 4); + diff --git a/challenge-263/jeanluc2020/python/ch-2.py b/challenge-263/jeanluc2020/python/ch-2.py new file mode 100755 index 0000000000..1944346edb --- /dev/null +++ b/challenge-263/jeanluc2020/python/ch-2.py @@ -0,0 +1,75 @@ +#!/usr/bin/python3 +# https://theweeklychallenge.org/blog/perl-weekly-challenge-263/#TASK2 +# +# Task 2: Merge Items +# =================== +# +# 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] ] +# +############################################################ +## +## discussion +## +############################################################ +# +# For this one, actually printing input and output is a little +# more tricky than the actual algorithm: Simply walk the combination +# of both arrays, and for each entry use the first element of that +# as the key of a result hash and the second element as what to add +# to the value of that entry in the hash. Print the result sorted +# by the hash keys. + +def merge_items(items1: list, items2: list) -> None: + result = {} + print("Input: items1 = [ ", end="") + for elem in items1: + print("[", elem[0], ", ", elem[1], "], ", sep="", end="") + print("],") + print(" items2 = [", end="") + for elem in items1: + print("[", elem[0], ", ", elem[1], "], ", sep="", end="") + print("]") + + all = items1 + for x in items2: + all.append(x) + for elem in all: + if elem[0] in result: + result[elem[0]] += elem[1] + else: + result[elem[0]] = elem[1] + + print("Output: [", end="") + for elem in sorted(result): + print("[", elem, ", ", result[elem], "], ", sep="", end="") + print("]") + +merge_items( [ [1,1], [2,1], [3,2] ], [ [2,2], [1,3] ] ); +merge_items( [ [1,2], [2,3], [1,3], [3,2] ], [ [3,1], [1,3] ] ); +merge_items( [ [1,1], [2,2], [3,3] ], [ [2,3], [2,4] ] ); + diff --git a/challenge-263/laurent-rosenfeld/blog.txt b/challenge-263/laurent-rosenfeld/blog.txt new file mode 100644 index 0000000000..90950b58da --- /dev/null +++ b/challenge-263/laurent-rosenfeld/blog.txt @@ -0,0 +1 @@ +https://blogs.perl.org/users/laurent_r/2024/04/perl-weekly-challenge-263-target-index.html diff --git a/challenge-263/laurent-rosenfeld/perl/ch-1.pl b/challenge-263/laurent-rosenfeld/perl/ch-1.pl new file mode 100644 index 0000000000..674699715d --- /dev/null +++ b/challenge-263/laurent-rosenfeld/perl/ch-1.pl @@ -0,0 +1,18 @@ +use strict; +use warnings; +use feature 'say'; + +sub find_index { + my $target = shift; + my @sorted = sort { $a <=> $b } @_; + my @out = grep {$sorted[$_] == $target} 0..$#sorted; + return "@out" || "()"; +} + +my @tests = ( [2, [1, 5, 3, 2, 4, 2]], + [6, [1, 2, 4, 3, 5]], + [4, [5, 3, 2, 4, 2, 1]] ); +for my $test (@tests) { + printf "%d - %-15s => ", $test->[0], "@{$test->[1]}"; + say find_index @$test[0], @{$test->[1]}; +} diff --git a/challenge-263/laurent-rosenfeld/raku/ch-1.raku b/challenge-263/laurent-rosenfeld/raku/ch-1.raku new file mode 100644 index 0000000000..35b95147f4 --- /dev/null +++ b/challenge-263/laurent-rosenfeld/raku/ch-1.raku @@ -0,0 +1,13 @@ +sub find-index ($target, @in) { + my @sorted = @in.sort; + my @out = grep {@sorted[$_] == $target}, 0..@sorted.end; + return @out; +} + +my @tests = (2, (1, 5, 3, 2, 4, 2)), + (6, (1, 2, 4, 3, 5)), + (4, (5, 3, 2, 4, 2, 1)); +for @tests -> @test { + printf "%d - %-15s => ", @test[0], "@test[1]"; + say find-index @test[0], @test[1]; +} diff --git a/challenge-263/mark-anderson/raku/ch-1.raku b/challenge-263/mark-anderson/raku/ch-1.raku new file mode 100644 index 0000000000..32b97979a5 --- /dev/null +++ b/challenge-263/mark-anderson/raku/ch-1.raku @@ -0,0 +1,11 @@ +#!/usr/bin/env raku +use Test; + +is-deeply target-index((1,5,3,2,4,2), 2), (1,2); +is-deeply target-index((1,2,4,3,5), 6), (); +is-deeply target-index((5,3,2,4,2,1), 4), (4,); + +sub target-index(@ints, $k) +{ + @ints.sort.grep($k, :k) +} diff --git a/challenge-263/mark-anderson/raku/ch-2.raku b/challenge-263/mark-anderson/raku/ch-2.raku new file mode 100644 index 0000000000..e303e361f3 --- /dev/null +++ b/challenge-263/mark-anderson/raku/ch-2.raku @@ -0,0 +1,14 @@ +#!/usr/bin/env raku +use Test; + +is-deeply merge-items([[1,1],[2,1],[3,2]], [[2,2],[1,3]]), ((1,4),(2,3),(3,2)); +is-deeply merge-items([[1,2],[2,3],[1,3],[3,2]], [[3,1],[1,3]]), ((1,8),(2,3),(3,3)); +is-deeply merge-items([[1,1],[2,2],[3,3]], [[2,3],[2,4]]), ((1,1),(2,9),(3,3)); + +sub merge-items($i1, $i2) +{ + given (|$i1, |$i2).classify({.head}, :as{.tail}) + { + sort .keys Z .values>>.sum + } +} diff --git a/challenge-263/peter-meszaros/perl/ch-1.pl b/challenge-263/peter-meszaros/perl/ch-1.pl new file mode 100755 index 0000000000..1b2128d8b0 --- /dev/null +++ b/challenge-263/peter-meszaros/perl/ch-1.pl @@ -0,0 +1,67 @@ +#!/usr/bin/env perl +# +# 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 $k[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 strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + [[[1, 5, 3, 2, 4, 2], 2], [1, 2], 'Example 1'], + [[[1, 2, 4, 3, 5], 6], [], 'Example 2'], + [[[5, 3, 2, 4, 2, 1], 4], [4], 'Example 3'], +]; + +sub target_index +{ + my $l = $_->[0]->[0]; + my $k = $_->[0]->[1]; + + my @l = sort {$a <=> $b} @$l; + my @res; + + my $found; + for (0..$#l) { + if ($l[$_] == $k) { + push @res, $_; + $found = 1; + } elsif ($found) { + last; + } + } + return \@res; +} + +for (@$cases) { + is(target_index($_->[0]), $_->[1], $_->[2]); +} +done_testing(); + +exit 0; diff --git a/challenge-263/peter-meszaros/perl/ch-2.pl b/challenge-263/peter-meszaros/perl/ch-2.pl new file mode 100755 index 0000000000..4b3cad77ac --- /dev/null +++ b/challenge-263/peter-meszaros/perl/ch-2.pl @@ -0,0 +1,69 @@ +#!/usr/bin/env perl +# +# 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 strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + [[[[1,1], [2,1], [3,2]], [[2,2], [1,3]]], + [[1,4], [2,3], [3,2]], 'Example 1'], + [[[[1,2], [2,3], [1,3], [3,2]], [[3,1], [1,3]]], + [[1,8], [2,3], [3,3]], 'Example 2'], + [[[[1,1], [2,2], [3,3]], [[2,3], [2,4]]], + [[1,1], [2,9], [3,3]], 'Example 3'], +]; + +sub merge_items +{ + my $l1 = $_->[0]->[0]; + my $l2 = $_->[0]->[1]; + + my %l; + + for my $i (@$l1, @$l2) { + $l{$i->[0]} += $i->[1]; + } + + my @res; + for my $k (sort {$a <=> $b} keys %l) { |
