diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2024-04-01 18:17:24 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2024-04-01 18:17:24 +0100 |
| commit | 6e3cd6e8a2eb65b0ba00b4e15e1abeb689a33e8d (patch) | |
| tree | 9651d8a04663c80427d748c4be9886260db3031f /challenge-263 | |
| parent | 0c72fe827b8dcc33de40b2dd35fad3b38fdb06a2 (diff) | |
| download | perlweeklychallenge-club-6e3cd6e8a2eb65b0ba00b4e15e1abeb689a33e8d.tar.gz perlweeklychallenge-club-6e3cd6e8a2eb65b0ba00b4e15e1abeb689a33e8d.tar.bz2 perlweeklychallenge-club-6e3cd6e8a2eb65b0ba00b4e15e1abeb689a33e8d.zip | |
- Added solutions by Mark Anderson.
- Added solutions by Thomas Kohler.
- Added solutions by Feng Chang.
- Added solutions by Peter Meszaros.
- Added solutions by Andrew Shitov.
- Added solutions by Steven Wilson.
- Added solutions by W. Luis Mochan.
- Added solutions by James Smith.
- Added solutions by Wanderdoc.
- Added solutions by Laurent Rosenfeld.
- Added solutions by Eric Cheung.
Diffstat (limited to 'challenge-263')
| -rwxr-xr-x | challenge-263/eric-cheung/python/ch-1.py | 16 | ||||
| -rwxr-xr-x | challenge-263/eric-cheung/python/ch-2.py | 18 | ||||
| -rw-r--r-- | challenge-263/james-smith/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-263/james-smith/perl/ch-1.pl | 5 | ||||
| -rw-r--r-- | challenge-263/james-smith/perl/ch-2.pl | 7 | ||||
| -rw-r--r-- | challenge-263/laurent-rosenfeld/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-263/laurent-rosenfeld/perl/ch-1.pl | 18 | ||||
| -rw-r--r-- | challenge-263/laurent-rosenfeld/raku/ch-1.raku | 13 | ||||
| -rwxr-xr-x | challenge-263/wanderdoc/perl/ch-1.pl | 53 | ||||
| -rwxr-xr-x | challenge-263/wanderdoc/perl/ch-2.pl | 45 |
10 files changed, 177 insertions, 0 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/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/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/wanderdoc/perl/ch-1.pl b/challenge-263/wanderdoc/perl/ch-1.pl new file mode 100755 index 0000000000..97a5973fb5 --- /dev/null +++ b/challenge-263/wanderdoc/perl/ch-1.pl @@ -0,0 +1,53 @@ +#!perl
+use strict;
+use warnings FATAL => qw(all);
+
+=prompt
+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
+=cut
+
+use Sort::Key qw(nkeysort);
+use List::MoreUtils qw(indexes);
+use integer;
+use Test2::V0;
+
+is(target_index([1, 5, 3, 2, 4, 2], 2), [1, 2], 'Example 1');
+is(target_index([1, 2, 4, 3, 5], 6), [], 'Example 2');
+is(target_index([5, 3, 2, 4, 2, 1], 4), [4], 'Example 3');
+done_testing();
+
+sub target_index
+{
+ my ($aref, $elm) = @_;
+ return
+ [indexes { $_ == $elm } nkeysort {$_} @$aref];
+}
+
+
+
+
+
+
+
+# use Data::Dump;
+# dd target_index([1, 5, 3, 2, 4, 2], 2);
+# dd target_index([1, 2, 4, 3, 5], 6);
+# dd target_index([5, 3, 2, 4, 2, 1], 4);
\ No newline at end of file diff --git a/challenge-263/wanderdoc/perl/ch-2.pl b/challenge-263/wanderdoc/perl/ch-2.pl new file mode 100755 index 0000000000..1f3657bb04 --- /dev/null +++ b/challenge-263/wanderdoc/perl/ch-2.pl @@ -0,0 +1,45 @@ +#!perl
+use strict;
+use warnings FATAL => qw(all);
+
+=prompt
+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] ]
+=cut
+
+use Sort::Key qw(nkeysort);
+use Test2::V0;
+
+
+is(merge_items([ [1,1], [2,1], [3,2] ], [ [2,2], [1,3] ]),
+ [ [1,4], [2,3], [3,2] ], 'Example 1');
+is(merge_items([ [1,2], [2,3], [1,3], [3,2] ], [ [3,1], [1,3] ]),
+ [ [1,8], [2,3], [3,3] ], 'Example 2');
+is(merge_items([ [1,1], [2,2], [3,3] ], [ [2,3], [2,4] ]),
+ [ [1,1], [2,9], [3,3] ], 'Example 3');
+ done_testing();
+
+sub merge_items
+{
+ use integer;
+ my @arefs = @_;
+ my %merge;
+ for my $paar ( map { @$_ } @arefs )
+ {
+ $merge{ $paar->[0] } += $paar->[1];
+ }
+ return [ map { [$_, $merge{$_}] } nkeysort{$_} keys %merge ];
+}
|
