From 5a46973e2f1fed5547e0e4fb7f1adcf3a3ea525e Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Sun, 19 Mar 2023 07:34:59 +0000 Subject: - 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. --- challenge-208/avery-adams/perl/ch-1.pl | 33 + challenge-208/avery-adams/perl/ch-2.pl | 12 + challenge-208/conor-hoekstra/bqn/ch-2.bqn | 4 + challenge-208/conor-hoekstra/ch-02.bqn | 4 - challenge-208/conor-hoekstra/ch-02.py | 9 - challenge-208/conor-hoekstra/python/ch-2.py | 9 + challenge-208/eric-cheung/python/ch-1.py | 23 + challenge-208/eric-cheung/python/ch-2.py | 13 + challenge-208/laurent-rosenfeld/perl/ch-1.pl | 27 + challenge-208/laurent-rosenfeld/perl/ch-2.pl | 25 + challenge-208/laurent-rosenfeld/raku/ch-1.raku | 15 + challenge-208/laurent-rosenfeld/raku/ch-2.raku | 3 + challenge-208/perlboy1967/perl/ch-1.pl | 51 + challenge-208/perlboy1967/perl/ch-2.pl | 51 + challenge-208/perlboy1967/perl/ch1.pl | 51 - challenge-208/perlboy1967/perl/ch2.pl | 51 - challenge-208/robert-dicicco/julia/ch-1.jl | 50 + challenge-208/robert-dicicco/julia/ch-2.jl | 42 + challenge-208/robert-dicicco/perl/ch-1.pl | 57 + challenge-208/robert-dicicco/perl/ch-2.pl | 48 + challenge-208/robert-dicicco/python/ch-1.py | 50 + challenge-208/robert-dicicco/python/ch-2.py | 34 + challenge-208/robert-dicicco/raku/ch-1.raku | 49 + challenge-208/robert-dicicco/raku/ch-2.raku | 40 + challenge-208/robert-dicicco/ruby/ch-1.rb | 55 + challenge-208/robert-dicicco/ruby/ch-2.rb | 45 + challenge-208/tyler-bird/perl/ch-1.pl | 104 ++ challenge-208/tyler-bird/perl/ch-2.pl | 58 + challenge-208/ulrich-rieke/awk/ch-2.awk | 16 + challenge-208/ulrich-rieke/cpp/ch-1.cpp | 73 + challenge-208/ulrich-rieke/cpp/ch-2.cpp | 55 + challenge-208/ulrich-rieke/haskell/ch-1.hs | 43 + challenge-208/ulrich-rieke/haskell/ch-2.hs | 32 + challenge-208/ulrich-rieke/perl/ch-1.pl | 45 + challenge-208/ulrich-rieke/perl/ch-2.pl | 31 + challenge-208/ulrich-rieke/raku/ch-1.raku | 40 + challenge-208/ulrich-rieke/raku/ch-2.raku | 27 + challenge-208/ulrich-rieke/rust/ch-1.rs | 46 + challenge-208/ulrich-rieke/rust/ch-2.rs | 31 + challenge-208/ziameraj16/java/MinimumIndexSum.java | 24 + stats/pwc-challenge-011.json | 495 +++--- stats/pwc-challenge-012.json | 275 +-- stats/pwc-challenge-200.json | 315 ++-- stats/pwc-challenge-201.json | 633 +++---- stats/pwc-challenge-203.json | 533 +++--- stats/pwc-challenge-204.json | 307 ++-- stats/pwc-challenge-205.json | 337 ++-- stats/pwc-challenge-206.json | 577 ++++--- stats/pwc-challenge-207.json | 748 ++++++++ stats/pwc-current.json | 476 ++---- stats/pwc-language-breakdown-summary.json | 62 +- stats/pwc-language-breakdown.json | 1477 ++++++++-------- stats/pwc-leaders.json | 842 ++++----- stats/pwc-summary-1-30.json | 46 +- stats/pwc-summary-121-150.json | 50 +- stats/pwc-summary-151-180.json | 130 +- stats/pwc-summary-181-210.json | 40 +- stats/pwc-summary-211-240.json | 124 +- stats/pwc-summary-241-270.json | 38 +- stats/pwc-summary-271-300.json | 102 +- stats/pwc-summary-31-60.json | 42 +- stats/pwc-summary-61-90.json | 40 +- stats/pwc-summary-91-120.json | 42 +- stats/pwc-summary.json | 1782 ++++++++++---------- 64 files changed, 6432 insertions(+), 4557 deletions(-) create mode 100755 challenge-208/avery-adams/perl/ch-1.pl create mode 100755 challenge-208/avery-adams/perl/ch-2.pl create mode 100644 challenge-208/conor-hoekstra/bqn/ch-2.bqn delete mode 100644 challenge-208/conor-hoekstra/ch-02.bqn delete mode 100644 challenge-208/conor-hoekstra/ch-02.py create mode 100644 challenge-208/conor-hoekstra/python/ch-2.py create mode 100755 challenge-208/eric-cheung/python/ch-1.py create mode 100755 challenge-208/eric-cheung/python/ch-2.py create mode 100644 challenge-208/laurent-rosenfeld/perl/ch-1.pl create mode 100644 challenge-208/laurent-rosenfeld/perl/ch-2.pl create mode 100644 challenge-208/laurent-rosenfeld/raku/ch-1.raku create mode 100644 challenge-208/laurent-rosenfeld/raku/ch-2.raku create mode 100755 challenge-208/perlboy1967/perl/ch-1.pl create mode 100755 challenge-208/perlboy1967/perl/ch-2.pl delete mode 100755 challenge-208/perlboy1967/perl/ch1.pl delete mode 100755 challenge-208/perlboy1967/perl/ch2.pl create mode 100644 challenge-208/robert-dicicco/julia/ch-1.jl create mode 100644 challenge-208/robert-dicicco/julia/ch-2.jl create mode 100644 challenge-208/robert-dicicco/perl/ch-1.pl create mode 100644 challenge-208/robert-dicicco/perl/ch-2.pl create mode 100644 challenge-208/robert-dicicco/python/ch-1.py create mode 100644 challenge-208/robert-dicicco/python/ch-2.py create mode 100644 challenge-208/robert-dicicco/raku/ch-1.raku create mode 100644 challenge-208/robert-dicicco/raku/ch-2.raku create mode 100644 challenge-208/robert-dicicco/ruby/ch-1.rb create mode 100644 challenge-208/robert-dicicco/ruby/ch-2.rb create mode 100755 challenge-208/tyler-bird/perl/ch-1.pl create mode 100755 challenge-208/tyler-bird/perl/ch-2.pl create mode 100644 challenge-208/ulrich-rieke/awk/ch-2.awk create mode 100644 challenge-208/ulrich-rieke/cpp/ch-1.cpp create mode 100644 challenge-208/ulrich-rieke/cpp/ch-2.cpp create mode 100644 challenge-208/ulrich-rieke/haskell/ch-1.hs create mode 100644 challenge-208/ulrich-rieke/haskell/ch-2.hs create mode 100644 challenge-208/ulrich-rieke/perl/ch-1.pl create mode 100644 challenge-208/ulrich-rieke/perl/ch-2.pl create mode 100644 challenge-208/ulrich-rieke/raku/ch-1.raku create mode 100644 challenge-208/ulrich-rieke/raku/ch-2.raku create mode 100644 challenge-208/ulrich-rieke/rust/ch-1.rs create mode 100644 challenge-208/ulrich-rieke/rust/ch-2.rs create mode 100644 challenge-208/ziameraj16/java/MinimumIndexSum.java create mode 100644 stats/pwc-challenge-207.json 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/bqn/ch-2.bqn b/challenge-208/conor-hoekstra/bqn/ch-2.bqn new file mode 100644 index 0000000000..4913670e2e --- /dev/null +++ b/challenge-208/conor-hoekstra/bqn/ch-2.bqn @@ -0,0 +1,4 @@ +FindDuplicate ← {¯1⌈´1+/2=+˝𝕩=⌜1+↕≠𝕩} + +# Tests +FindDuplicate ¨ ⟨1,2,2,4⟩‿⟨1,2,3,4⟩‿⟨1,2,3,3⟩ # ⟨ 1 ¯1 2 ⟩ diff --git a/challenge-208/conor-hoekstra/ch-02.bqn b/challenge-208/conor-hoekstra/ch-02.bqn deleted file mode 100644 index 4913670e2e..0000000000 --- a/challenge-208/conor-hoekstra/ch-02.bqn +++ /dev/null @@ -1,4 +0,0 @@ -FindDuplicate ← {¯1⌈´1+/2=+˝𝕩=⌜1+↕≠𝕩} - -# Tests -FindDuplicate ¨ ⟨1,2,2,4⟩‿⟨1,2,3,4⟩‿⟨1,2,3,3⟩ # ⟨ 1 ¯1 2 ⟩ diff --git a/challenge-208/conor-hoekstra/ch-02.py b/challenge-208/conor-hoekstra/ch-02.py deleted file mode 100644 index fdc6510062..0000000000 --- a/challenge-208/conor-hoekstra/ch-02.py +++ /dev/null @@ -1,9 +0,0 @@ - -from collections import Counter, defaultdict - -def find_duplicate(nums): - return defaultdict(lambda:-1, map(reversed, Counter(nums).items()))[2] - -print(find_duplicate([1,2,2,4])) # 2 -print(find_duplicate([1,2,3,4])) # -1 -print(find_duplicate([1,2,3,3])) # 3 diff --git a/challenge-208/conor-hoekstra/python/ch-2.py b/challenge-208/conor-hoekstra/python/ch-2.py new file mode 100644 index 0000000000..fdc6510062 --- /dev/null +++ b/challenge-208/conor-hoekstra/python/ch-2.py @@ -0,0 +1,9 @@ + +from collections import Counter, defaultdict + +def find_duplicate(nums): + return defaultdict(lambda:-1, map(reversed, Counter(nums).items()))[2] + +print(find_duplicate([1,2,2,4])) # 2 +print(find_duplicate([1,2,3,4])) # -1 +print(find_duplicate([1,2,3,3])) # 3 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 ( [[], []], + [[], []], [[], []] ) { + + 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 (, ), + (, ), (, ) + -> @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/ch-1.pl b/challenge-208/perlboy1967/perl/ch-1.pl new file mode 100755 index 0000000000..9f3f213e53 --- /dev/null +++ b/challenge-208/perlboy1967/perl/ch-1.pl @@ -0,0 +1,51 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 208 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-208 + +Author: Niels 'PerlBoy' van Dijke + +Task 1: Minimum Index Sum +Submitted by: Mohammad S Anwar + +You are given two arrays of strings. + +Write a script to find out all common strings in the given two arrays with minimum +index sum. If no common strings found returns an empty list. + +=cut + +use v5.16; + +use common::sense; + +use List::Util qw(min); +use List::MoreUtils qw(frequency); + +use Test::More; +use Test::Deep qw(cmp_deeply); + +sub mimimumIndexSum(\@\@) { + my %f = frequency(@{$_[0]},@{$_[1]}); + + my %i; + for (@_) { + my $i = 0; + for (@$_) { + $i{$_} += $i++ if ($f{$_} > 1); + } + } + + my $min = min(values %i); + + return [sort grep { $i{$_} == $min } keys %i]; +} + +cmp_deeply(mimimumIndexSum(@{[qw(Perl Raku Love)]},@{[qw(Raku Perl Hate)]}),[qw(Perl Raku)]); +cmp_deeply(mimimumIndexSum(@{[qw(A B C)]},@{[qw(D E F)]}),[]); +cmp_deeply(mimimumIndexSum(@{[qw(A B C)]},@{[qw(C A B)]}),['A']); +cmp_deeply(mimimumIndexSum(@{[qw(Z B C D)]},@{[qw(D C A B)]}),[qw(B C D)]); + +done_testing; diff --git a/challenge-208/perlboy1967/perl/ch-2.pl b/challenge-208/perlboy1967/perl/ch-2.pl new file mode 100755 index 0000000000..4f93b72cd4 --- /dev/null +++ b/challenge-208/perlboy1967/perl/ch-2.pl @@ -0,0 +1,51 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 208 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-208 + +Author: Niels 'PerlBoy' van Dijke + +Task 2: Duplicate and Missing +Submitted by: Mohammad S Anwar + +You are given an array of integers in sequence with one missing and one duplicate. + +Write a script to find the duplicate and missing integer in the given array. Return -1 if none found. + +For the sake of this task, let us assume the array contains no more than one duplicate and missing. + +=cut + +use v5.16; + +use common::sense; + +use List::MoreUtils qw(slide); + +use Test::More; +use Test::Deep qw(cmp_deeply); + +sub duplicateAndMissing(@) { + @_ = sort { $a <=> $b } @_; + + my (@duplicate,@missing); + slide { + if ($a == $b) { push(@duplicate,$a) if ($duplicate[-1] != $b) } + elsif ($b - $a > 1) { push(@missing,$a + 1 .. $b - 1) } + } @_; + + # 'Special' rule to comply to the task: + push(@missing,$_[-1] + 1) + if (scalar(@missing) == 0 && scalar(@duplicate) > 0); + + return [[@duplicate],[@missing]]; +} + +cmp_deeply(duplicateAndMissing(1,2,2,4),[[2],[3]]); +cmp_deeply(duplicateAndMissing(1,2,3,4),[[],[]]); +cmp_deeply(duplicateAndMissing(1,2,3,3),[[3],[4]]); +cmp_deeply(duplicateAndMissing(1,1,1,3,3,5,6,8,8,9),[[1,3,8],[2,4,7]]); + +done_testing; diff --git a/challenge-208/perlboy1967/perl/ch1.pl b/challenge-208/perlboy1967/perl/ch1.pl deleted file mode 100755 index 9f3f213e53..0000000000 --- a/challenge-208/perlboy1967/perl/ch1.pl +++ /dev/null @@ -1,51 +0,0 @@ -#!/bin/perl - -=pod - -The Weekly Challenge - 208 -- https://theweeklychallenge.org/blog/perl-weekly-challenge-208 - -Author: Niels 'PerlBoy' van Dijke - -Task 1: Minimum Index Sum -Submitted by: Mohammad S Anwar - -You are given two arrays of strings. - -Write a script to find out all common strings in the given two arrays with minimum -index sum. If no common strings found returns an empty list. - -=cut - -use v5.16; - -use common::sense; - -use List::Util qw(min); -use List::MoreUtils qw(frequency); - -use Test::More; -use Test::Deep qw(cmp_deeply); - -sub mimimumIndexSum(\@\@) { - my %f = frequency(@{$_[0]},@{$_[1]}); - - my %i; - for (@_) { - my $i = 0; - for (@$_) { - $i{$_} += $i++ if ($f{$_} > 1); - } - } - - my $min = min(values %i); - - return [sort grep { $i{$_} == $min } keys %i]; -} - -cmp_deeply(mimimumIndexSum(@{[qw(Perl Raku Love)]},@{[qw(Raku Perl Hate)]}),[qw(Perl Raku)]); -cmp_deeply(mimimumIndexSum(@{[qw(A B C)]},@{[qw(D E F)]}),[]); -cmp_deeply(mimimumIndexSum(@{[qw(A B C)]},@{[qw(C A B)]}),['A']); -cmp_deeply(mimimumIndexSum(@{[qw(Z B C D)]},@{[qw(D C A B)]}),[qw(B C D)]); - -done_testing; diff --git a/challenge-208/perlboy1967/perl/ch2.pl b/challenge-208/perlboy1967/perl/ch2.pl deleted file mode 100755 index 4f93b72cd4..0000000000 --- a/challenge-208/perlboy1967/perl/ch2.pl +++ /dev/null @@ -1,51 +0,0 @@ -#!/bin/perl - -=pod - -The Weekly Challenge - 208 -- https://theweeklychallenge.org/blog/perl-weekly-challenge-208 - -Author: Niels 'PerlBoy' van Dijke - -Task 2: Duplicate and Missing -Submitted by: Mohammad S Anwar - -You are given an array of integers in sequence with one missing and one duplicate. - -Write a script to find the duplicate and missing integer in the given array. Return -1 if none found. - -For the sake of this task, let us assume the array contains no more than one duplicate and missing. - -=cut - -use v5.16; - -use common::sense; - -use List::MoreUtils qw(slide); - -use Test::More; -use Test::Deep qw(cmp_deeply); - -sub duplicateAndMissing(@) { - @_ = sort { $a <=> $b } @_; - - my (@duplicate,@missing); - slide { - if ($a == $b) { push(@duplicate,$a) if ($duplicate[-1] != $b) } - elsif ($b - $a > 1) { push(@missing,$a + 1 .. $b - 1) } - } @_; - - # 'Special' rule to comply to the task: - push(@missing,$_[-1] + 1) - if (scalar(@missing) == 0 && scalar(@duplicate) > 0); - - return [[@duplicate],[@missing]]; -} - -cmp_deeply(duplicateAndMissing(1,2,2,4),[[2],[3]]); -cmp_deeply(duplicateAndMissing(1,2,3,4),[[],[]]); -cmp_deeply(duplicateAndMissing(1,2,3,3),[[3],[4]]); -cmp_deeply(duplicateAndMissing(1,1,1,3,3,5,6,8,8,9),[[1,3,8],[2,4,7]]); - -done_testing; 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 "Input: \@list1 = $ref1\n"; + print " \@list2 = $ref2\n"; + + print "Output: "; + for (0..2) -> $x { + if $ref1[$x] (elem) $ref2 { + print "$ref1[$x] "; + } + } + say "\n"; +} + +=begin comment +------------------------------------------ +raku .\MinIndexSum.rk +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: + +------------------------------------------ +=end comment diff --git a/challenge-208/robert-dicicco/raku/ch-2.raku b/challenge-208/robert-dicicco/raku/ch-2.raku new file mode 100644 index 0000000000..6a27e8e42e --- /dev/null +++ b/challenge-208/robert-dicicco/raku/ch-2.raku @@ -0,0 +1,40 @@ +#!/usr/bin/env raku +#`{ +---------------------------------------------- +AUTHOR: Robert DiCicco +DATE : 2023-03-15 +Challenge 208 'Duplicate and Missing' ( Raku ) +---------------------------------------------- +} + +my @nums = ([1,2,2,4],[1,2,3,4],[1,2,3,3]); + +for (@nums) -> @a { + my $found = 0; + say "Input: \@nums = ",@a; + my $ln = @a.elems; + for 0..$ln-1 -> $n { + if @a[$n] != $n+1 { + say "Output: (@a[$n],",$n+1,')'; + $found = 1; + } + } + if $found == 0 { + say "Output: -1"; + } + say " "; +} +#`{ +---------------------------------------------- +SAMPLE OUTPUT +raku .\DupMissing.rk +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/ruby/ch-1.rb b/challenge-208/robert-dicicco/ruby/ch-1.rb new file mode 100644 index 0000000000..e69edc4766 --- /dev/null +++ b/challenge-208/robert-dicicco/ruby/ch-1.rb @@ -0,0 +1,55 @@ +#!/usr/bin/env ruby +=begin +------------------------------------------ +AUTHOR: Robert DiCicco +DATE : 2023-03-14 +Challenge 208 Minimum Index Sum ( Ruby ) +------------------------------------------ +=end + +require 'set' + +list1 = [["Perl", "Raku", "Love"],["A","B","C"], ["A", "B", "C"]] +$list2 = [["Raku", "Perl", "Hate"],["C","A","B"], ["D", "E", "F"]] + +ln = list1.length() +$wds = Set + +def create_new_set(i) + $wds = Set.new() + $list2[i].each do |z| + $wds << z + end +end + +(0..ln-1).each do |i| + create_new_set(i) + printf("Input: @list1 = %s\n",list1[i]) + printf(" @list2 = %s\n",$list2[i]) + print("Output: ") + (0..2).each do |x| + if $wds.include?(list1[i][x]) + print("#{list1[i][x]} ") + end + end + print("\n\n") + end + +=begin + ------------------------------------------ + SAMPLE OUTPUT + ruby .\MinIndexSum.rb +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: + + ------------------------------------------ +=end diff --git a/challenge-208/robert-dicicco/ruby/ch-2.rb b/challenge-208/robert-dicicco/ruby/ch-2.rb new file mode 100644 index 0000000000..ef5edb2db0 --- /dev/null +++ b/challenge-208/robert-dicicco/ruby/ch-2.rb @@ -0,0 +1,45 @@ +#!/usr/bin/env ruby +=begin +---------------------------------------------- +AUTHOR: Robert DiCicco +DATE : 2023-03-15 +Challenge 208 'Duplicate and Missing' ( Ruby ) +---------------------------------------------- +=end + +nums = [[1,2,2,4],[1,2,3,4],[1,2,3,3]]; + +nums.each do |a| + found = 0 + puts("Input: @nums = #{a}") + ln = a.length() + (0..ln-1).each do |n| + if a[n] != n+1 + puts("Output: (#{a[n]},#{n+1})") + found = 1 + end + end + if found == 0 + puts("Output: -1") + end + puts(" ") +end + +=begin +---------------------------------------------- +SAMPLE OUTPUT +ruby .\DupMissing.rb +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) +---------------------------------------------- +=end + + + + diff --git a/challenge-208/tyler-bird/perl/ch-1.pl b/challenge-208/tyler-bird/perl/ch-1.pl new file mode 100755 index 0000000000..c31c9a78e8 --- /dev/null +++ b/challenge-208/tyler-bird/perl/ch-1.pl @@ -0,0 +1,104 @@ +package main; + +use strict; +use warnings; + + +sub minimum_index_sums +{ + my($array_ref_1, $array_ref_2) = @_; + + my %in_both = build_hash_with_strings_in_both_arrays($array_ref_1, $array_ref_2); + my %index_sums = (); + my @minimum_index_sums; + + if(scalar(keys %in_both)) { + my %index_sums = build_index_sums(\%in_both); + my %minimum_index_sums; + my $current_minimum; + + foreach my $string1 ( sort { $a cmp $b } keys %index_sums ) { + my $sum = $index_sums{$string1}; + + if(!$current_minimum) { + $current_minimum = $sum; + } + + foreach my $string2 ( sort { $a cmp $b } keys %index_sums) { + my $sum2 = $index_sums{$string2}; + + if($sum2 <= $current_minimum) { + $minimum_index_sums{$string2} = $sum2; + $current_minimum = $sum2; + } else { + delete $minimum_index_sums{$string2}; + } + } + } + + @minimum_index_sums = keys %minimum_index_sums; + } + + return @minimum_index_sums; + +} + +sub build_hash_with_strings_in_both_arrays +{ + my($array_ref_1, $array_ref_2) = @_; + + my %in_both = (); + + for (my $i = 0; $i < scalar( @{$array_ref_1}); $i++) { + my $first_element = $array_ref_1->[$i]; + + for(my $j = 0; $j < scalar( @{$array_ref_2} ); $j++) { + my $second_element = $array_ref_2->[$j]; + + if($first_element eq $second_element) { + if(!exists $in_both{$first_element}) { + $in_both{$first_element} = { + 'indexes' => [] + }; + } + + push(@{$in_both{$first_element}->{'indexes'}}, $i); + push(@{$in_both{$first_element}->{'indexes'}}, $j); + } + } + } + + return %in_both; +} + + +sub build_index_sums +{ + my($in_both_href) = @_; + + my %in_both = %{$in_both_href}; + + my %index_sums; + + foreach my $string ( keys %in_both ) { + my $indexes = $in_both{$string}->{'indexes'}; + my $index_sum = 0; + map { $index_sum += $_ } @{$indexes}; + $index_sums{$string} = $index_sum; + } + return %index_sums; +} + + +print "(" , join(",", map { '"' . $_ . '"' } minimum_index_sums(["Perl", "Raku", "Love"], ["Raku", "Perl", "Hate"]) ) . ")\n"; +print "(" . join(",", map { '"' . $_ . '"' } minimum_index_sums(["A", "B", "C"], ["D", "E", "F"]) ) . ")\n"; +print "(" . join(",", map { '"' . $_ . '"' } minimum_index_sums(["A", "B", "C"], ["C", "A", "B"]) ) . ")\n"; + + + + +#minimum_index_sum(["A", "B", "C"], ["D", "E", "F"]); +#minimum_index_sum(["B", "B", "C"], ["C", "A", "B"]); + + +1; diff --git a/challenge-208/tyler-bird/perl/ch-2.pl b/challenge-208/tyler-bird/perl/ch-2.pl new file mode 100755 index 0000000000..456f67b7ba --- /dev/null +++ b/challenge-208/tyler-bird/perl/ch-2.pl @@ -0,0 +1,58 @@ +package main; +use strict; +use warnings; + + +sub find_duplicate_in_sequence +{ + my($aref) = @_; + + my %hash = (); + + foreach my $element (@{$aref}) { + if(!exists $hash{$element}) { + $hash{$element} = 1; + } else { + return $element; + } + } + + return -1; +} + +sub find_missing_in_sequence +{ + my($aref) = @_; + + my $previous_number = $aref->[0]; + my $first_number = $previous_number; + + foreach my $current_number ( @{$aref} ) { + if($current_number != $first_number) { + if($current_number != $previous_number + 1) { + return $previous_number + 1; + } else { + $previous_number = $current_number; + } + } + } + + return -1; +} + +sub find_missing_and_duplicate_in_sequence +{ + my ($aref) = @_; + + my $duplicate = find_duplicate_in_sequence($aref); + my $missing = find_missing_in_sequence($aref); + + return ($duplicate, $missing); +} + +print "(" . join(",", find_missing_and_duplicate_in_sequence([1,2,2,4])) . ")\n"; +print "(" . join(",", find_missing_and_duplicate_in_sequence([1,2,3,4])) . ")\n"; +print "(" . join(",", find_missing_and_duplicate_in_sequence([1,2,3,3])) . ")\n"; + +1; + diff --git a/challenge-208/ulrich-rieke/awk/ch-2.awk b/challenge-208/ulrich-rieke/awk/ch-2.awk new file mode 100644 index 0000000000..e24ebad4bb --- /dev/null +++ b/challenge-208/ulrich-rieke/awk/ch-2.awk @@ -0,0 +1,16 @@ +#!/usr/bin/awk -f + +{ + for ( i = 2 ; i <= NF ; i++ ) { + if ( $(i - 1) == $i ) { + duplicate = $(i - 1) + if ( i == NF ) { + missing = $(i - 1) + 1 + } + } + if ($i - $(i - 1) > 1 ) { + missing = $(i - 1 ) + 1 + } + } + print "(" duplicate " " missing ")" +} diff --git a/challenge-208/ulrich-rieke/cpp/ch-1.cpp b/challenge-208/ulrich-rieke/cpp/ch-1.cpp new file mode 100644 index 0000000000..69a6590dc4 --- /dev/null +++ b/challenge-208/ulrich-rieke/cpp/ch-1.cpp @@ -0,0 +1,73 @@ +#include +#include +#include +#include +#include +#include +#include + +std::vector split( const std::string & startline , + const std::string & sep ) { + std::vector separated ; + std::string::size_type start { 0 } ; + std::string::size_type pos ; + do { + pos = startline.find_first_of( sep , start ) ; + separated.push_back( startline.substr(start , pos - start )) ; + start = pos + 1 ; + } while ( pos != std::string::npos ) ; + return separated ; +} + +std::map toIndexPairs( + const std::vector & words ) { + std::map values ; + int i = 0 ; + for ( auto s : words ) { + values[s] = i ; + i++ ; + } + return values ; +} + +int main( ) { + std::cout << "Enter some unique strings, separated by blanks!\n" ; + std::string line ; + std::getline( std::cin , line ) ; + std::vector firstWords ( split( line , " " ) ) ; + std::cout << "Enter some more unique strings, separated by blanks!\n" ; + std::getline( std::cin , line ) ; + std::vector secondWords ( split( line , " " ) ) ; + std::map firstMap( toIndexPairs( firstWords ) ) ; + std::map secondMap( toIndexPairs( secondWords ) ) ; + std::vector commonWords ; + std::sort( firstWords.begin( ) , firstWords.end( ) ) ; + std::sort( secondWords.begin( ) , secondWords.end( ) ) ; + std::vector result ; + std::set_intersection( firstWords.begin( ) , firstWords.end( ) , + secondWords.begin( ) , secondWords.end( ) , + std::back_inserter( result ) ) ; + if ( result.size( ) > 0 ) { + std::vector> allIndices ; + for ( auto it = result.begin( ) ; it != result.end( ) ; it++ ) { + allIndices.push_back( std::make_pair( *it , firstMap[*it] + + secondMap[*it] )) ; + } + std::sort( allIndices.begin( ) , allIndices.end( ) , []( const auto p1 , + const auto p2 ){ return p1.second < p2.second ; } ) ; + int minimum = allIndices.begin( )->second ; + std::vector output ; + for ( auto & p : allIndices ) { + if ( p.second == minimum ) + output.push_back( p.first ) ; + } + std::sort( output.begin( ) , output.end( ) ) ; + std::cout << "(" ; + std::copy( output.begin( ) , output.end( ) , + std::ostream_iterator( std::cout , " " )) ; + std::cout << ")\n" ; + } + else + std::cout << "()" << std::endl ; + return 0 ; +} diff --git a/challenge-208/ulrich-rieke/cpp/ch-2.cpp b/challenge-208/ulrich-rieke/cpp/ch-2.cpp new file mode 100644 index 0000000000..4460b717f7 --- /dev/null +++ b/challenge-208/ulrich-rieke/cpp/ch-2.cpp @@ -0,0 +1,55 @@ +#include +#include +#include +#include + +std::vector split( const std::string & startline , + const std::string & sep ) { + std::vector separated ; + std::string::size_type start { 0 } ; + std::string::size_type pos ; + do { + pos = startline.find_first_of( sep , start ) ; + separated.push_back( startline.substr(start , pos - start )) ; + start = pos + 1 ; + } while ( pos != std::string::npos ) ; + return separated ; +} + +int main( ) { + std::cout << "Please enter some integers, separated by blanks!\n" ; + std::cout << "There should be a maximum of one number missing and one duplicate!\n" ; + std::string line ; + std::getline( std::cin , line ) ; + std::vector numberstrings ( split( line , " " ) ) ; + std::vector numbers ; + for ( auto & s : numberstrings ) + numbers.push_back( std::stoi( s ) ) ; + std::sort( numbers.begin( ) , numbers.end( ) ) ; + int len = numbers.size( ) ; + std::vector found ; + for ( int i = 0 ; i < len - 1 ; i++ ) { + if ( numbers[ i + 1 ] == numbers[ i ] ) { //duplicate + found.push_back( numbers[ i ] ) ; + if ( i == len - 2 ) { //duplicate also means missing at the end + found.push_back( numbers[ i ] + 1 ) ; + } + } + if ( numbers[ i + 1 ] - numbers[ i ] > 1 ) { //missing + found.push_back( numbers[ i ] + 1 ) ; + } + } + if ( found.size( ) > 0 ) { + std::cout << '(' ; + for ( int i : found ) { + std::cout << i ; + if ( i != found.back( ) ) + std::cout << ',' ; + } + std::cout << ")\n" ; + } + else { + std::cout << -1 << std::endl ; + } + return 0 ; +} diff --git a/challenge-208/ulrich-rieke/haskell/ch-1.hs b/challenge-208/ulrich-rieke/haskell/ch-1.hs new file mode 100644 index 0000000000..bde86f94f9 --- /dev/null +++ b/challenge-208/ulrich-rieke/haskell/ch-1.hs @@ -0,0 +1,43 @@ +module Challenge208 + where +import Data.List ( findIndex , sortOn) +import Data.Maybe ( fromJust ) +import qualified Data.Set as S + +findCommonWords :: [String] -> [String] -> [String] +findCommonWords firstList secondList = S.toList $ S.intersection +(S.fromList firstList) (S.fromList secondList) + +findStringPositions :: [String] -> [String] -> [(String , Int)] +findStringPositions firstList secondList + |not $ null commonWords = map (\s -> (s, (fromJust $ findIndex (== s) + firstList) + ( fromJust $ findIndex ( == s ) secondList ))) commonWords + |null commonWords = [] + where + commonWords :: [String] + commonWords = findCommonWords firstList secondList + +solution :: [String] -> [String] -> [String] +solution listA listB + |not $ null positions = map fst $ filter ((== minim ) . snd ) sorted + |otherwise = [] + where + positions :: [(String , Int)] + positions = findStringPositions listA listB + sorted :: [(String , Int)] + sorted = sortOn snd positions + minim :: Int + minim = snd $ head sorted + +main :: IO ( ) +main = do + putStrLn "Enter some strings, separated by blanks!" + firstLine <- getLine + putStrLn "Enter some more strings, separated by blanks!" + secondLine <- getLine + let firstWords = words firstLine + secondWords = words secondLine + sol = solution firstWords secondWords + if not $ null sol + then print sol + else print "()" diff --git a/challenge-208/ulrich-rieke/haskell/ch-2.hs b/challenge-208/ulrich-rieke/haskell/ch-2.hs new file mode 100644 index 0000000000..dbdc534d55 --- /dev/null +++ b/challenge-208/ulrich-rieke/haskell/ch-2.hs @@ -0,0 +1,32 @@ +module Challenge208_2 + where +import Data.List ( sort ) +import Data.List.Split ( divvy ) + +reorder :: [Int] -> [(Int , Int)] +reorder list = zip list divvied +where + divvied :: [Int] + divvied = map (\subl -> last subl - head subl ) $ divvy 2 1 list + +solution :: [Int] -> [Int] +solution list = (map fst $ filter ( (== 0) . snd ) $ init reordered) ++ ( map (\p -> fst p ++ 1 ) $ filter ( (> 1 ) . snd ) $ init reordered ) ++ ( checkLast $ last reordered ) +where + reordered :: [(Int , Int)] + reordered = reorder list + checkLast :: (Int , Int ) -> [Int] + checkLast ( p , q ) + |q == 0 = [p , p + 1] + |q == 1 = [] + |q > 1 = [p + 1] + +main :: IO ( ) +main = do + putStrLn "Please enter integers, separated by blanks, with at most one gap and duplicate!" + numberstrings <- getLine + let numbers = map read $ words numberstrings + transformed = solution numbers + if not $ null transformed + then print $ sort transformed + else print "-1" diff --git a/challenge-208/ulrich-rieke/perl/ch-1.pl b/challenge-208/ulrich-rieke/perl/ch-1.pl new file mode 100644 index 0000000000..d9c70ffd60 --- /dev/null +++ b/challenge-208/ulrich-rieke/perl/ch-1.pl @@ -0,0 +1,45 @@ +#!/usr/bin/perl ; +use strict ; +use warnings ; +use feature 'say' ; + +sub findIndex { #index of a word in an array + my $array = shift ; + my $word = shift ; + my $pos = 0 ; + while ( $array->[$pos] ne $word ) { + $pos++ ; + } + return $pos ; +} + +say "Enter some unique strings, separated by blanks!" ; +my $line = ; +chomp $line ; +my @firstWords = split( /\s/ , $line ) ; +say "Enter more unique strings, separated by blanks!" ; +$line = ; +chomp $line ; +my @secondWords = split( /\s/ , $line ) ; +my %wordHash ;#this hash counts the occurrences of each word +map { $wordHash{$_}++ } @firstWords ; +map { $wordHash{ $_ }++ } @secondWords ; +my @selectedWords = grep { $wordHash{ $_ } > 1 } keys %wordHash ; +if ( @selectedWords ) { + my @wordIndices ; #contains a pair of word and the index sum from both arrays + for my $word ( @selectedWords ) { + push @wordIndices , [ $word , findIndex(\@firstWords, $word ) + + findIndex(\@secondWords, $word ) ] ; + } +#sort by index sum, ascending + my @sorted = sort { $a->[1] <=> $b->[1] } @wordIndices ; + my $minimum = $sorted[0]->[1] ;#minimum number of occurrences in both array +#take all those words that occur $minimum times in both arrays combined + my @allWords = map { $_->[0] } grep { $_->[1] == $minimum } @sorted ; + print "(" ; + print join( ',' , sort { $a cmp $b } @allWords ) ; + say ")" ; +} +else { + say "()" ; +} diff --git a/challenge-208/ulrich-rieke/perl/ch-2.pl b/challenge-208/ulrich-rieke/perl/ch-2.pl new file mode 100644 index 0000000000..3ba3a24a40 --- /dev/null +++ b/challenge-208/ulrich-rieke/perl/ch-2.pl @@ -0,0 +1,31 @@ +#!/usr/bin/perl ; +use strict ; +use warnings ; +use feature 'say' ; + +say "Enter some integers, at most one missing and one duplicate, separated by blanks!" ; +my $line = ; +chomp $line ; +my @numbers = split( /\s/ , $line ) ; +my @sorted = sort { $a <=> $b } @numbers ; +my $len = scalar( @sorted ) ; +my @found ; +for my $i ( 0..$len - 2 ) { + if ( $sorted[ $i + 1 ] == $sorted[ $i ] ) { #duplicate + push @found , $sorted[ $i ] ; + if ( $i == $len - 2 ) { #duplicate at the end also means missing + push @found, $sorted[ $i ] + 1 ; + } + } + if ( $sorted[ $i + 1 ] - $sorted[ $i ] > 1 ) { #missing + push @found , $sorted[ $i ] + 1 ; + } +} +if ( @found ) { + print "(" ; + print join( ',' , @found ) ; + say ")" ; +} +else { + say "-1" ; +} diff --git a/challenge-208/ulrich-rieke/raku/ch-1.raku b/challenge-208/ulrich-rieke/raku/ch-1.raku new file mode 100644 index 0000000000..6e8add8079 --- /dev/null +++ b/challenge-208/ulrich-rieke/raku/ch-1.raku @@ -0,0 +1,40 @@ +use v6 ; + +sub findIndex( @array , $word ) { + my $pos = 0 ; + while ( @array[ $pos ] ne $word ) { + $pos++ ; + } + return $pos ; +} + +say "Enter some strings, separated by blanks!" ; +my $line = $*IN.get ; +my @firstWords = $line.words ; +my $firstSet = set( @firstWords ) ; +say "Enter some more strings, separated by blanks!" ; +$line = $*IN.get ; +my @secondWords = $line.words ; +my $secondSet = set( @secondWords ) ; +my $common = $firstSet (&) $secondSet ; +if ( $common.elems > 0 ) { + my @indexlist ; + for $common.keys -> $aWord { + @indexlist.push( [$aWord , findIndex( @firstWords, $aWord ) + findIndex( @secondWords , + $aWord ) ] ) ; + } + my @sorted = @indexlist.sort( {$^a[1] <=> $^b[1] } ) ; + my @allWords ; + my $minimum = @sorted[0][1] ; + for @sorted -> $pair { + if ( $pair[ 1 ] == $minimum ) { + @allWords.push( $pair[ 0 ] ) ; + } + } + print "(" ; + print join( ',' , @allWords ) ; + say ")" ; +} +else { + say "( ) " ; +} diff --git a/challenge-208/ulrich-rieke/raku/ch-2.raku b/challenge-208/ulrich-rieke/raku/ch-2.raku new file mode 100644 index 0000000000..cf2df000b2 --- /dev/null +++ b/challenge-208/ulrich-rieke/raku/ch-2.raku @@ -0,0 +1,27 @@ +use v6 ; + +say "Enter some integers, at most one missing and one duplicate!" ; +say "Separate by blanks!" ; +my $line = $*IN.get ; +my @numbers = $line.words.map( {.Int} ).sort( { $^a <=> $^b } ) ; +my $len = @numbers.elems ; +my @found ; +for (0..$len - 2 ) -> $i { + if ( @numbers[ $i + 1 ] == @numbers[ $i ] ) { #duplicate + @found.push( @numbers[ $i ] ) ; #duplicate number + if ( $i == $len - 2 ) { + @found.push( @numbers[ $i ] + 1 ) ; #missing number , at the end + } + } + if ( @numbers[ $i + 1 ] - @numbers[ $i ] > 1 ) { + @found.push( @numbers[ $i ] + 1 ) ; + } +} +if ( @found ) { + print "(" ; + print @found.join( ',' ) ; + say ")" ; +} +else { + say "-1" ; +} diff --git a/challenge-208/ulrich-rieke/rust/ch-1.rs b/challenge-208/ulrich-rieke/rust/ch-1.rs new file mode 100644 index 0000000000..97869f804a --- /dev/null +++ b/challenge-208/ulrich-rieke/rust/ch-1.rs @@ -0,0 +1,46 @@ +use std::io ; +use std::collections::HashSet ; + +fn find_index( strings : &Vec<&str> , needle : &str ) -> usize { + let mut pos : usize = 0 ; + while strings[ pos ] != needle { + pos += 1 ; + } + pos +} + +fn main() { + println!("Enter some strings, separated by blanks!"); + let mut inline : String = String::new( ) ; + io::stdin( ).read_line( &mut inline ).unwrap( ) ; + let first_line : &str = &*inline ; + let first_words : Vec<&str> = first_line.split_whitespace( ).map( | s | + s.trim( ) ).collect( ) ; + let mut second : String = String::new( ) ; + println!("Enter some other strings, separated by blanks!" ) ; + io::stdin( ).read_line( &mut second ).unwrap( ) ; + let second_line : &str = &*second ; + let second_words : Vec<&str> = second_line.split_whitespace( ).map( | s | + s.trim( ) ).collect( ) ; + let first_set : HashSet<&&str> = first_words.iter( ).collect( ) ; + let second_set : HashSet<&&str> = second_words.iter( ).collect( ) ; + let common : HashSet<&&&str> = first_set.intersection( &second_set).collect( ) ; + if common.len( ) > 0 { + let mut word_indices : Vec<(&str , usize)> = Vec::new( ) ; + let mut set_iter = common.iter( ) ; + while let Some( w ) = set_iter.next( ) { + word_indices.push((**w , find_index( &first_words , **w ) + + find_index( &second_words, **w )) ) ; + } + let indices : &mut[(&str, usize)] = word_indices.as_mut_slice( ) ; + indices.sort_by( | a , b | a.1.cmp(&b.1) ) ; + let minimum : usize = indices[0].1 ; + let mut selected : Vec<&str> = Vec::new( ) ; + indices.iter( ).filter( | p | p.1 == minimum ).for_each( | p | selected.push( + p.0.clone( ) )) ; + println!("{:?}" , selected ) ; + } + else { + println!("()" ) ; + } +} diff --git a/challenge-208/ulrich-rieke/rust/ch-2.rs b/challenge-208/ulrich-rieke/rust/ch-2.rs new file mode 100644 index 0000000000..3a12b84bea --- /dev/null +++ b/challenge-208/ulrich-rieke/rust/ch-2.rs @@ -0,0 +1,31 @@ +use std::io ; + +fn main() { + println!("Please enter some integers, with blanks to separate!"); + let mut inline : String = String::new( ) ; + io::stdin( ).read_line( &mut inline ).unwrap( ) ; + let entered_line : &str = &*inline ; + let mut numbers : Vec = entered_line.split_whitespace( ). + map( | s | s.trim( ).parse::( ).expect( "Could not parse!" )) + .collect( ) ; + numbers.sort( ) ; + let mut found : Vec = Vec::new( ) ; + let len = numbers.len( ) ; + for i in 0..len - 1 { + if numbers[ i ] == numbers[ i + 1 ] { //duplicate , also means missing + found.push( numbers[ i ] ) ; //duplicate + if i == len - 2 { //duplicate here also means missing + found.push( numbers[ i ] + 1 ) ; + } + } + if numbers[ i + 1 ] - numbers[ i ] > 1 { //missing + found.push( numbers[ i ] + 1 ) ; + } + } + if found.len( ) > 0 { + println!("{:?}" , found ) ; + } + else { + println!("-1") ; + } +} diff --git a/challenge-208/ziameraj16/java/MinimumIndexSum.java b/challenge-208/ziameraj16/java/MinimumIndexSum.java new file mode 100644 index 0000000000..a43ab40c6f --- /dev/null +++ b/challenge-208/ziameraj16/java/MinimumIndexSum.java @@ -0,0 +1,24 @@ +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Scanner; + +public class MinimumIndexSum { + + public static void main(String[] args) { + System.out.println("Enter comma separated values of first array"); + List list1 = Arrays.stream(new Scanner(System.in).nextLine().split(",")).toList(); + System.out.println("Enter comma separated values of second array"); + List list2 = Arrays.stream(new Scanner(System.in).nextLine().split(",")).toList(); + int minIndex = Integer.MAX_VALUE; + List values = new ArrayList(); + for (String s : list1) { + if (list2.contains(s) && (list1.indexOf(s) + list2.indexOf(s)) <= minIndex) { + minIndex = list1.indexOf(s) + list2.indexOf(s); + values.add(s); + } + } + System.out.println(values); + } +} + diff --git a/stats/pwc-challenge-011.json b/stats/pwc-challenge-011.json index f1e2b3f746..9bfa04a1a9 100644 --- a/stats/pwc-challenge-011.json +++ b/stats/pwc-challenge-011.json @@ -1,35 +1,185 @@ { - "plotOptions" : { - "series" : { - "dataLabels" : { - "format" : "{point.y}", - "enabled" : 1 - }, - "borderWidth" : 0 + "series" : [ + { + "data" : [ + { + "y" : 2, + "name" : "Aaron Sherman", + "drilldown" : "Aaron Sherman" + }, + { + "name" : "Adam Russell", + "drilldown" : "Adam Russell", + "y" : 3 + }, + { + "drilldown" : "Alicia Bielsa", + "name" : "Alicia Bielsa", + "y" : 2 + }, + { + "name" : "Andrezgz", + "drilldown" : "Andrezgz", + "y" : 2 + }, + { + "name" : "Arne Sommer", + "drilldown" : "Arne Sommer", + "y" : 3 + }, + { + "y" : 3, + "name" : "Athanasius", + "drilldown" : "Athanasius" + }, + { + "y" : 1, + "drilldown" : "Daniel Mantovani", + "name" : "Daniel Mantovani" + }, + { + "y" : 3, + "name" : "Dave Jacoby", + "drilldown" : "Dave Jacoby" + }, + { + "y" : 2, + "name" : "Duncan C. White", + "drilldown" : "Duncan C. White" + }, + { + "y" : 3, + "name" : "E. Choroba", + "drilldown" : "E. Choroba" + }, + { + "y" : 5, + "name" : "Feng Chang", + "drilldown" : "Feng Chang" + }, + { + "y" : 2, + "name" : "Francis Whittle", + "drilldown" : "Francis Whittle" + }, + { + "y" : 2, + "drilldown" : "Gustavo Chaves", + "name" : "Gustavo Chaves" + }, + { + "drilldown" : "Jaldhar H. Vyas", + "name" : "Jaldhar H. Vyas", + "y" : 4 + }, + { + "name" : "Jo Christian Oterhals", + "drilldown" : "Jo Christian Oterhals", + "y" : 3 + }, + { + "y" : 6, + "name" : "Joelle Maslak", + "drilldown" : "Joelle Maslak" + }, + { + "y" : 2, + "name" : "Khalid", + "drilldown" : "Khalid" + }, + { + "y" : 4, + "name" : "Kian-Meng Ang", + "drilldown" : "Kian-Meng Ang" + }, + { + "y" : 2, + "drilldown" : "Kivanc Yazan", + "name" : "Kivanc Yazan" + }, + { + "name" : "Lakpa Tashi Bhutia", + "drilldown" : "Lakpa Tashi Bhutia", + "y" : 2 + }, + { + "name" : "Laurent Rosenfeld", + "drilldown" : "Laurent Rosenfeld", + "y" : 6 + }, + { + "y" : 2, + "name" : "Lubos Kolouch", + "drilldown" : "Lubos Kolouch" + }, + { + "y" : 2, + "name" : "Maxim Nechaev", + "drilldown" : "Maxim Nechaev" + }, + { + "name" : "Ozzy", + "drilldown" : "Ozzy", + "y" : 2 + }, + { + "name" : "Paulo Custodio", + "drilldown" : "Paulo Custodio", + "y" : 2 + }, + { + "y" : 1, + "name" : "Pete Houston", + "drilldown" : "Pete Houston" + }, + { + "y" : 4, + "name" : "Ruben Westerberg", + "drilldown" : "Ruben Westerberg" + }, + { + "name" : "Simon Proctor", + "drilldown" : "Simon Proctor", + "y" : 2 + }, + { + "drilldown" : "Steven Wilson", + "name" : "Steven Wilson", + "y" : 3 + }, + { + "drilldown" : "Stuart Little", + "name" : "Stuart Little", + "y" : 2 + }, + { + "y" : 2, + "drilldown" : "Veesh Goldman", + "name" : "Veesh Goldman" + }, + { + "y" : 3, + "name" : "Yozen Hernandez", + "drilldown" : "Yozen Hernandez" + } + ], + "name" : "The Weekly Challenge - 011", + "colorByPoint" : 1 } - }, - "tooltip" : { - "followPointer" : 1, - "pointFormat" : "{point.name}: {point.y:f}
", - "headerFormat" : "{series.name}
" - }, - "xAxis" : { - "type" : "category" - }, + ], "drilldown" : { "series" : [ { - "name" : "Aaron Sherman", - "id" : "Aaron Sherman", "data" : [ [ "Raku", 2 ] - ] + ], + "id" : "Aaron Sherman", + "name" : "Aaron Sherman" }, { - "id" : "Adam Russell", "data" : [ [ "Perl", @@ -40,27 +190,28 @@ 1 ] ], - "name" : "Adam Russell" + "name" : "Adam Russell", + "id" : "Adam Russell" }, { "name" : "Alicia Bielsa", + "id" : "Alicia Bielsa", "data" : [ [ "Perl", 2 ] - ], - "id" : "Alicia Bielsa" + ] }, { + "name" : "Andrezgz", "id" : "Andrezgz", "data" : [ [ "Perl", 2 ] - ], - "name" : "Andrezgz" + ] }, { "name" : "Arne Sommer", @@ -77,28 +228,26 @@ ] }, { - "name" : "Athanasius", "data" : [ [ "Perl", 3 ] ], + "name" : "Athanasius", "id" : "Athanasius" }, { "name" : "Daniel Mantovani", + "id" : "Daniel Mantovani", "data" : [ [ "Perl", 1 ] - ], - "id" : "Daniel Mantovani" + ] }, { - "name" : "Dave Jacoby", - "id" : "Dave Jacoby", "data" : [ [ "Perl", @@ -108,19 +257,22 @@ "Blog", 1 ] - ] + ], + "name" : "Dave Jacoby", + "id" : "Dave Jacoby" }, { - "name" : "Duncan C. White", - "id" : "Duncan C. White", "data" : [ [ "Perl", 2 ] - ] + ], + "id" : "Duncan C. White", + "name" : "Duncan C. White" }, { + "name" : "E. Choroba", "id" : "E. Choroba", "data" : [ [ @@ -131,11 +283,11 @@ "Blog", 1 ] - ], - "name" : "E. Choroba" + ] }, { "name" : "Feng Chang", + "id" : "Feng Chang", "data" : [ [ "Perl", @@ -145,8 +297,7 @@ "Raku", 3 ] - ], - "id" : "Feng Chang" + ] }, { "data" : [ @@ -159,17 +310,16 @@ "name" : "Francis Whittle" }, { - "name" : "Gustavo Chaves", "data" : [ [ "Perl", 2 ] ], - "id" : "Gustavo Chaves" + "id" : "