diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2023-03-19 07:34:59 +0000 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2023-03-19 07:34:59 +0000 |
| commit | 5a46973e2f1fed5547e0e4fb7f1adcf3a3ea525e (patch) | |
| tree | 2336cf3bf456b60630455f0b94c0e8b2044a1b8c | |
| parent | 87e50ae17dae2c587957134a5ec50ecd3a3c6fce (diff) | |
| download | perlweeklychallenge-club-5a46973e2f1fed5547e0e4fb7f1adcf3a3ea525e.tar.gz perlweeklychallenge-club-5a46973e2f1fed5547e0e4fb7f1adcf3a3ea525e.tar.bz2 perlweeklychallenge-club-5a46973e2f1fed5547e0e4fb7f1adcf3a3ea525e.zip | |
- Added solutions by Mark Anderson.
- Added solutions by W. Luis Mochan.
- Added solutions by E. Choroba.
- Added solutions by Feng Chang.
- Added solutions by Lubos Kolouch.
- Added solutions by Thomas Kohler.
- Added solutions by David Ferrone.
- Added solutions by Mariano Spadaccini.
- Added solutions by Peter Campbell Smith.
- Added solutions by Roger Bell_West.
- Added solutions by Robbie Hatley.
- Added solutions by Paulo Custodio.
- Added solutions by Dave Jacoby.
- Added solutions by Bob Lied.
- Added solutions by Luca Ferrari.
- Added solutions by Jorg Sommrey.
- Added solutions by Niels van Dijke.
- Added solutions by Matthew Neleigh.
- Added solutions by James Smith.
- Added solutions by Arne Sommer.
- Added solutions by Carlos Oliveira.
- Added solutions by Ulrich Rieke.
- Added solutions by Avery Adams.
- Added solutions by Robert DiCicco.
- Added solutions by Tyler Bird.
60 files changed, 5772 insertions, 3897 deletions
diff --git a/challenge-208/avery-adams/perl/ch-1.pl b/challenge-208/avery-adams/perl/ch-1.pl new file mode 100755 index 0000000000..7a87b2246e --- /dev/null +++ b/challenge-208/avery-adams/perl/ch-1.pl @@ -0,0 +1,33 @@ +use strict; +use v5.10; + +my @list1 = ("Perl", "Raku", "Love"); +my @list2 = ("Raku", "Perl", "Hate"); + +# my @list1 = ("A", "B", "C"); +# my @list2 = ("D", "E", "F"); + +# my @list1 = ("A", "B", "C"); +# my @list2 = ("C", "A", "B"); + +my $minindex; +my @results; +for (my $index1 = 0; $index1 < scalar @list1; $index1++) { + for (my $index2 = 0; $index2 < scalar @list2; $index2++) { + if ($list1[$index1] eq $list2[$index2] && defined($minindex)) { + if ($index1 + $index2 < $minindex) { + @results = ($list1[$index1]); + $minindex = $index1 + $index2; + } elsif ($index1 + $index2 == $minindex) { + push (@results, $list1[$index1]); + } + } elsif ($list1[$index1] eq $list2[$index2] && !defined($minindex)) { + @results = ($list1[$index1]); + $minindex = $index1 + $index2; + } + } +} +if (scalar @results == 0) {exit} +foreach (@results) { + say $_; +}
\ No newline at end of file diff --git a/challenge-208/avery-adams/perl/ch-2.pl b/challenge-208/avery-adams/perl/ch-2.pl new file mode 100755 index 0000000000..5f5b088879 --- /dev/null +++ b/challenge-208/avery-adams/perl/ch-2.pl @@ -0,0 +1,12 @@ +use strict; +use v5.10; + +my $index; +my ($duplicate, $missing); +foreach (@ARGV) { + if (!defined($index)) {$index = 1 and next} + if ($_ == $ARGV[$index - 1]) {$duplicate = $_} + if ($_ != $ARGV[$index - 1] + 1) {$missing = $ARGV[$index - 1] + 1} + $index++; +} +defined($duplicate) && defined($missing) ? say ("Duplicate is $duplicate", "\n", "Missing is $missing") : say -1; diff --git a/challenge-208/conor-hoekstra/ch-02.bqn b/challenge-208/conor-hoekstra/bqn/ch-2.bqn index 4913670e2e..4913670e2e 100644 --- a/challenge-208/conor-hoekstra/ch-02.bqn +++ b/challenge-208/conor-hoekstra/bqn/ch-2.bqn diff --git a/challenge-208/conor-hoekstra/ch-02.py b/challenge-208/conor-hoekstra/python/ch-2.py index fdc6510062..fdc6510062 100644 --- a/challenge-208/conor-hoekstra/ch-02.py +++ b/challenge-208/conor-hoekstra/python/ch-2.py diff --git a/challenge-208/eric-cheung/python/ch-1.py b/challenge-208/eric-cheung/python/ch-1.py new file mode 100755 index 0000000000..15a03253aa --- /dev/null +++ b/challenge-208/eric-cheung/python/ch-1.py @@ -0,0 +1,23 @@ +
+## Example 1
+## arrList_01 = ["Perl", "Raku", "Love"]
+## arrList_02 = ["Raku", "Perl", "Hate"]
+
+## Example 2
+## arrList_01 = ["A", "B", "C"]
+## arrList_02 = ["D", "E", "F"]
+
+## Example 3
+arrList_01 = ["A", "B", "C"]
+arrList_02 = ["C", "A", "B"]
+
+arrCommonList = [strLoop for strLoop in arrList_01 if strLoop in arrList_02]
+
+if len(arrCommonList) > 0:
+ arrCommonIndxSum = [arrList_01.index(strLoop) + arrList_02.index(strLoop) for strLoop in arrCommonList]
+ arrMinIndx = [nIndx for nIndx, nLoop in enumerate(arrCommonIndxSum) if nLoop == min(arrCommonIndxSum)]
+ arrMinList = [arrCommonList[nIndx] for nIndx in arrMinIndx]
+
+ print (arrMinList)
+else:
+ print ([])
diff --git a/challenge-208/eric-cheung/python/ch-2.py b/challenge-208/eric-cheung/python/ch-2.py new file mode 100755 index 0000000000..951f8a03a0 --- /dev/null +++ b/challenge-208/eric-cheung/python/ch-2.py @@ -0,0 +1,13 @@ +
+## arrNum = [1, 2, 2, 4] ## Example 1
+## arrNum = [1, 2, 3, 4] ## Example 2
+arrNum = [1, 2, 3, 3] ## Example 3
+
+arrList = range(1, len(arrNum) + 1)
+
+arrDupMiss = list(set([nElemLoop for nElemLoop in arrNum if arrNum.count(nElemLoop) > 1])) + list(set(arrList).difference(set(arrNum)))
+
+if len(arrDupMiss) > 0:
+ print (arrDupMiss)
+else:
+ print (-1)
diff --git a/challenge-208/laurent-rosenfeld/perl/ch-1.pl b/challenge-208/laurent-rosenfeld/perl/ch-1.pl new file mode 100644 index 0000000000..eac62aa3b7 --- /dev/null +++ b/challenge-208/laurent-rosenfeld/perl/ch-1.pl @@ -0,0 +1,27 @@ +use strict; +use warnings; +use feature "say"; + +sub min_sum_idx { + my @s1 = @{$_[0]}; + my @s2 = @{$_[1]}; + + my %h1 = map {$s1[$_] => $_ } 0..$#s1; + my %h2 = map {$s2[$_] => $_ } 0..$#s2; + my @result = grep { exists $h1{$_} } @s2; + return "()" unless @result; + my %res = map { $_ => $h1{$_} + $h2{$_} } @result; + my $min = $res{$result[0]}; + for my $k (keys %res) { + $min = $res{$k} if $res{$k} < $min; + } + return grep {$res{$_} == $min} @result; +} + +for my $test ( [[<Perl Raku Love>], [<Raku Perl Hate>]], + [[<A B C>], [<D E F>]], [[<A B C>], [<C A B>]] ) { + + printf "%-14s - %-16s => ", + "@{$test->[0]}", "@{$test->[1]}"; + say join " ", min_sum_idx @$test; +} diff --git a/challenge-208/laurent-rosenfeld/perl/ch-2.pl b/challenge-208/laurent-rosenfeld/perl/ch-2.pl new file mode 100644 index 0000000000..e2245072f3 --- /dev/null +++ b/challenge-208/laurent-rosenfeld/perl/ch-2.pl @@ -0,0 +1,25 @@ +use strict; +use warnings; +use feature "say"; + +sub dupe_and_missing { + my @nums = @_; + my ($dupe, $missing); + for my $i (1..$#nums) { + if ($nums[$i] == $nums[$i-1]) { + $dupe = $nums[$i]; + } elsif ($nums[$i] - $nums[$i-1] != 1) { + $missing = $nums[$i-1] + 1; + } + } + return "($dupe, $missing)" if $dupe and $missing; + return "-1" unless $dupe or $missing; + return "($dupe, -)" if $dupe; + return "(-, $missing)"; +} + +for my $test ([<1 2 2 4>], [<1 2 3 4>], [<1 2 3 3>], + [<1 2 4 5>], [<1 1 3 4>], [<1 3 4 5>], [<1 2 2 3 5>]) { + printf "%-12s => ", "@$test"; + say dupe_and_missing @$test; +} diff --git a/challenge-208/laurent-rosenfeld/raku/ch-1.raku b/challenge-208/laurent-rosenfeld/raku/ch-1.raku new file mode 100644 index 0000000000..e8822a345d --- /dev/null +++ b/challenge-208/laurent-rosenfeld/raku/ch-1.raku @@ -0,0 +1,15 @@ +sub min-sum-idx (@s1, @s2) { + my $b1 = (map {@s1[$_] => $_ + 1}, 0..@s1.end).Bag; + my $b2 = (map {@s2[$_] => $_ + 1}, 0..@s2.end).Bag; + my $result = (map { $_ => $b1{$_} + $b2{$_}}, + ($b1 ∩ $b2).keys).Bag; + my $min = $result.values.min; + return grep {$result{$_} == $min}, $result.keys; +} + +for (<Perl Raku Love>, <Raku Perl Hate>), + (<A B C>, <D E F>), (<A B C>, <C A B>) + -> @test { + say "@test[0] - @test[1]".fmt("%-35s => "), + min-sum-idx |@test; +} diff --git a/challenge-208/laurent-rosenfeld/raku/ch-2.raku b/challenge-208/laurent-rosenfeld/raku/ch-2.raku new file mode 100644 index 0000000000..b0230ec78e --- /dev/null +++ b/challenge-208/laurent-rosenfeld/raku/ch-2.raku @@ -0,0 +1,3 @@ +sub dupe-and-missing (@nums) { my ($dupe, $missing); for 1..@nums.end -> $i { if @nums[$i] == @nums[$i-1] { $dupe = @nums[$i]; } elsif @nums[$i] - @nums[$i-1] != 1 { $missing = @nums[$i-1] + 1; } } return “($dupe, $missing)” if $dupe and $missing; return “-1” unless $dupe or $missing; return “($dupe, -)” if $dupe; # no missing item return “(-, $missing)”; # no dupe } + +for <1 2 2 4>, <1 2 3 4>, <1 2 3 3>, <1 2 4 5>, <1 1 3 4>, <1 3 4 5>, <1 2 2 3 5> -> @test { say “@test[]”.fmt(“%-12s => “), dupe-and-missing @test; } diff --git a/challenge-208/perlboy1967/perl/ch1.pl b/challenge-208/perlboy1967/perl/ch-1.pl index 9f3f213e53..9f3f213e53 100755 --- a/challenge-208/perlboy1967/perl/ch1.pl +++ b/challenge-208/perlboy1967/perl/ch-1.pl diff --git a/challenge-208/perlboy1967/perl/ch2.pl b/challenge-208/perlboy1967/perl/ch-2.pl index 4f93b72cd4..4f93b72cd4 100755 --- a/challenge-208/perlboy1967/perl/ch2.pl +++ b/challenge-208/perlboy1967/perl/ch-2.pl diff --git a/challenge-208/robert-dicicco/julia/ch-1.jl b/challenge-208/robert-dicicco/julia/ch-1.jl new file mode 100644 index 0000000000..54724ddc13 --- /dev/null +++ b/challenge-208/robert-dicicco/julia/ch-1.jl @@ -0,0 +1,50 @@ +#!/usr/bin/env julia +#= +------------------------------------------ +AUTHOR: Robert DiCicco +DATE : 2023-03-14 +Challenge 208 Minimum Index Sum ( Julia ) +------------------------------------------ +=# +using Printf + +list1 = [["Perl", "Raku", "Love"],["A","B","C"], ["A", "B", "C"]] +list2 = [["Raku", "Perl", "Hate"],["C","A","B"], ["D", "E", "F"]] + +set1 = Set() +set2 = Set() + +ln = length(list1) + +for j in 1:ln + println("Input: @list1 = ",list1[j]) + println(" @list2 = ",list2[j]) + global set1,set2 + set1 = list1[j] + set2 = list2[j] + println("Output: ",intersect(set1, set2)) + println("\n") +end + +#= +------------------------------------------ +SAMPLE OUTPUT +julia .\MinIndexSum.jl +Input: @list1 = ["Perl", "Raku", "Love"] + @list2 = ["Raku", "Perl", "Hate"] +Output: ["Perl", "Raku"] + + +Input: @list1 = ["A", "B", "C"] + @list2 = ["C", "A", "B"] +Output: ["A", "B", "C"] + + +Input: @list1 = ["A", "B", "C"] + @list2 = ["D", "E", "F"] +Output: String[] + +------------------------------------------ +=# + + diff --git a/challenge-208/robert-dicicco/julia/ch-2.jl b/challenge-208/robert-dicicco/julia/ch-2.jl new file mode 100644 index 0000000000..285fbc1d3d --- /dev/null +++ b/challenge-208/robert-dicicco/julia/ch-2.jl @@ -0,0 +1,42 @@ +#!/usr/bin/env julia +#= +---------------------------------------------- +AUTHOR: Robert DiCicco +DATE : 2023-03-15 +Challenge 208 'Duplicate and Missing' ( Julia ) +---------------------------------------------- +=# +using Printf + +nums = [[1,2,2,4],[1,2,3,4],[1,2,3,3]];\ + +for a in nums + found = 0 + @printf("Input: @nums = %s\n",a) + ln = length(a) + for n in 1:ln + if (a[n] != n) + @printf("Output: (%d,%d)\n",a[n],n) + found = 1 + end + end + if found == 0 + @printf("Output: -1\n") + end + println(" ") +end +#= +---------------------------------------------- +SAMPLE OUTPUT +julia .\DupMissing.jl +Input: @nums = [1, 2, 2, 4] +Output: (2,3) + +Input: @nums = [1, 2, 3, 4] +Output: -1 + +Input: @nums = [1, 2, 3, 3] +Output: (3,4) +---------------------------------------------- +=# + diff --git a/challenge-208/robert-dicicco/perl/ch-1.pl b/challenge-208/robert-dicicco/perl/ch-1.pl new file mode 100644 index 0000000000..d59450ae32 --- /dev/null +++ b/challenge-208/robert-dicicco/perl/ch-1.pl @@ -0,0 +1,57 @@ +#!/usr/bin/env perl +=begin comment +------------------------------------------ +AUTHOR: Robert DiCicco +DATE : 2023-03-14 +Challenge 208 Minimum Index Sum ( Perl ) +------------------------------------------ +=cut + +use strict; +use warnings; +use feature 'say'; + +my @list1 = (["Perl", "Raku", "Love"],["A","B","C"], ["A", "B", "C"]); +my @list2 = (["Raku", "Perl", "Hate"],["C","A","B"], ["D", "E", "F"]); + +my $ln = scalar @list1; + +for (0..$ln - 1) { + my @isect = (); + my $ref1 = $list1[$_]; + my $ref2 = $list2[$_]; + + print "Input: \@list1 = @$ref1\n"; + print " \@list2 = @$ref2\n"; + + foreach my $item (@$ref1) { + push @isect, $item if grep { $item eq $_ } @$ref2; + } + + print "Output : ("; + for my $item (@isect) { + + print "'",$item,"' "; + } + say ")\n"; +} + +=begin comment +------------------------------------------ +perl .\MinIndexSum.pl +Input: @list1 = Perl Raku Love + @list2 = Raku Perl Hate +Output : ('Perl' 'Raku' ) + +Input: @list1 = A B C + @list2 = C A B +Output : ('A' 'B' 'C' ) + +Input: @list1 = A B C + @list2 = D E F +Output : () +------------------------------------------ + + + +=cut diff --git a/challenge-208/robert-dicicco/perl/ch-2.pl b/challenge-208/robert-dicicco/perl/ch-2.pl new file mode 100644 index 0000000000..e699e96581 --- /dev/null +++ b/challenge-208/robert-dicicco/perl/ch-2.pl @@ -0,0 +1,48 @@ +#!/usr/bin/env perl +=begin comment +---------------------------------------------- +AUTHOR: Robert DiCicco +DATE : 2023-03-15 +Challenge 208 'Duplicate and Missing' ( Perl ) +---------------------------------------------- +=cut +use strict; +use warnings; +use feature 'say'; + +my @nums = ([1,2,2,4],[1,2,3,4],[1,2,3,3]); + +for (@nums) { + my $found = 0; + my @a = @{$_}; + say "Input: \@nums = (@a)"; + my $ln = scalar @a; + for my $n (0..$ln-1) { + if ( $a[$n] != $n+1) { + say "Output: ($a[$n],",$n+1,')'; + $found = 1; + } + } + if ($found == 0 ) { + say "Output: -1"; + } + say " "; +} + +=begin comment +---------------------------------------------- +SAMPLE OUTPUT +perl .\DupMissing.pl +Input: @nums = (1 2 2 4) +Output: (2,3) + +Input: @nums = (1 2 3 4) +Output: -1 + +Input: @nums = (1 2 3 3) +Output: (3,4) +---------------------------------------------- +=cut + + + diff --git a/challenge-208/robert-dicicco/python/ch-1.py b/challenge-208/robert-dicicco/python/ch-1.py new file mode 100644 index 0000000000..057c8aea5b --- /dev/null +++ b/challenge-208/robert-dicicco/python/ch-1.py @@ -0,0 +1,50 @@ +#!/usr/binenv python +''' +------------------------------------------ +AUTHOR: Robert DiCicco +DATE : 2023-03-14 +Challenge 208 Minimum Index Sum ( Python ) +------------------------------------------ +''' + +list1 = [["Perl", "Raku", "Love"],["A","B","C"], ["A", "B", "C"]] +list2 = [["Raku", "Perl", "Hate"],["C","A","B"], ["D", "E", "F"]] + +for x in range(0,3): + set1 = set() + set2 = set() + for wds in list1[x]: + set1.add(wds) + + print("Input: @list1 = ",set1) + + for wds in list2[x]: + set2.add(wds) + + print(" @list2 = ",set2) + + print('Output: ', sorted(set1 & set2)) + print("\n") + +''' + ------------------------------------------ + SAMPLE OUTPUT + python .\MinIndexSum.py +Input: @list1 = {'Raku', 'Perl', 'Love'} + @list2 = {'Raku', 'Perl', 'Hate'} +Output: ['Perl', 'Raku'] + + +Input: @list1 = {'A', 'B', 'C'} + @list2 = {'A', 'C', 'B'} +Output: ['A', 'B', 'C'] + + +Input: @list1 = {'A', 'B', 'C'} + @list2 = {'D', 'E', 'F'} +Output: [] + + ------------------------------------------ +''' + + diff --git a/challenge-208/robert-dicicco/python/ch-2.py b/challenge-208/robert-dicicco/python/ch-2.py new file mode 100644 index 0000000000..43b2f2b6fc --- /dev/null +++ b/challenge-208/robert-dicicco/python/ch-2.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python +''' +---------------------------------------------- +AUTHOR: Robert DiCicco +DATE : 2023-03-15 +Challenge 208 'Duplicate and Missing' ( Python ) +---------------------------------------------- +''' +nums = [[1,2,2,4],[1,2,3,4],[1,2,3,3]] + +for a in nums: + found = 0 + print("Input: @nums = ",a) + ln = len(a) + for n in range(ln): + if a[n] != n+1: + print("Output: (",a[n],',',n+1,")\n") + found = 1 + if found == 0: + print("Output: -1\n") +''' +---------------------------------------------- +SAMPLE OUTPUT +python .\DupMissing.py +Input: @nums = [1, 2, 2, 4] +Output: ( 2 , 3 ) + +Input: @nums = [1, 2, 3, 4] +Output: -1 + +Input: @nums = [1, 2, 3, 3] +Output: ( 3 , 4 ) +---------------------------------------------- +''' diff --git a/challenge-208/robert-dicicco/raku/ch-1.raku b/challenge-208/robert-dicicco/raku/ch-1.raku new file mode 100644 index 0000000000..5b8104a781 --- /dev/null +++ b/challenge-208/robert-dicicco/raku/ch-1.raku @@ -0,0 +1,49 @@ +#!/usr/bin/env raku +=begin comment +------------------------------------------ +AUTHOR: Robert DiCicco +DATE : 2023-03-14 +Challenge 208 Minimum Index Sum ( Raku ) +------------------------------------------ +=end comment +use v6; + +my @list1 = (["Perl", "Raku", "Love"],["A","B","C"], ["A", "B", "C"]); +my @list2 = (["Raku", "Perl", "Hate"],["C","A","B"], ["D", "E", "F"]); + +my $ln = @list1.elems; + +for (0..$ln-1) -> $i { + my @isect = (); + my $ref1 = @list1[$i]; + my $ref2 = @list2[$i]; + + print |
