diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-04-01 17:49:11 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-04-01 17:49:11 +0100 |
| commit | 43ee70dd2fa244621fcf00514486778a52e85ef6 (patch) | |
| tree | d33e5478a916eb4303f7fb25c165ba9f7d4ea099 | |
| parent | 242718252ccc4fe4a21d5937ceae7dc2a66075a0 (diff) | |
| parent | 7da47ab176462dbcf92ac01c7931a7da1136757c (diff) | |
| download | perlweeklychallenge-club-43ee70dd2fa244621fcf00514486778a52e85ef6.tar.gz perlweeklychallenge-club-43ee70dd2fa244621fcf00514486778a52e85ef6.tar.bz2 perlweeklychallenge-club-43ee70dd2fa244621fcf00514486778a52e85ef6.zip | |
Merge pull request #9846 from jeanluc2020/jeanluc-263
Add solution 263
| -rw-r--r-- | challenge-263/jeanluc2020/blog-1.txt | 1 | ||||
| -rw-r--r-- | challenge-263/jeanluc2020/blog-2.txt | 1 | ||||
| -rwxr-xr-x | challenge-263/jeanluc2020/perl/ch-1.pl | 62 | ||||
| -rwxr-xr-x | challenge-263/jeanluc2020/perl/ch-2.pl | 77 | ||||
| -rwxr-xr-x | challenge-263/jeanluc2020/python/ch-1.py | 56 | ||||
| -rwxr-xr-x | challenge-263/jeanluc2020/python/ch-2.py | 75 |
6 files changed, 272 insertions, 0 deletions
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] ] ); + |
