From a9f8285c31f7b7fb2caf114fd0355023f0e52b91 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Mon, 11 Sep 2023 02:42:56 +0100 Subject: - Added solutions by Laurent Rosenfeld. - Added solutions by Macy TY. - Added solutions by Ulrich Rieke. - Added solutions by Robert DiCicco. - Added solutions by Eric Cheung. - Added solutions by Wanderdoc. - Added solutions by Robbie Hatley. - Added solutions by Niels van Dijke. - Added solutions by PokGoPun. - Added solutions by Peter Meszaros. - Added solutions by David Ferrone. - Added solutions by Mark Anderson. - Added solutions by rcmlz. - Added solutions by Luca Ferrari. - Added solutions by W. Luis Mochan. - Added solutions by Thomas Kohler. - Added solutions by Peter Campbell Smith. - Added solutions by E. Choroba. - Added solutions by mauke. - Added solutions by Lubos Kolouch. - Added solutions by Athanasius. - Added solutions by Roger Bell_West. - Added solutions by Stephen G Lynn. - Added solutions by Andrew Shitov. - Added solutions by Steven Wilson. - Added solutions by Dave Jacoby. - Added solutions by Solathian. - Added solutions by Packy Anderson. - Added solutions by Avery Adams. - Added solutions by Humberto Massa. - Added solutions by Matthew Neleigh. - Added solutions by Bob Lied. - Added solutions by Mariano Spadaccini. - Added solutions by Robert Ransbottom. - Added solutions by Jorg Sommrey. - Added solutions by Adam Russell. - Added solutions by Ali Moradi. - Added solutions by Matthias Muth. - Added solutions by Tyler Wardhaugh. - Added solutions by Simon Green. - Added solutions by Cheok-Yin Fung. - Added solutions by Jan Krnavek. - Added solutions by Bruce Gray. - Added solutions by BarrOff. - Added solutions by Marton Polgar. --- challenge-231/massa/raku/ch-01.raku | 66 - challenge-231/massa/raku/ch-02.raku | 59 - challenge-231/massa/raku/ch-1.raku | 66 + challenge-231/massa/raku/ch-2.raku | 59 + challenge-233/eric-cheung/python/ch-1.py | 10 + challenge-233/eric-cheung/python/ch-2.py | 37 + challenge-233/laurent-rosenfeld/blog.txt | 1 + challenge-233/laurent-rosenfeld/blog1.txt | 1 + challenge-233/laurent-rosenfeld/perl/ch-1.pl | 28 + challenge-233/laurent-rosenfeld/raku/ch-1.raku | 14 + challenge-233/laurent-rosenfeld/raku/ch-2.raku | 17 + challenge-233/macy-ty/javascript/ch-1.js | 24 + challenge-233/macy-ty/javascript/ch-2.js | 19 + challenge-233/massa/raku/ch-01.raku | 14 - challenge-233/massa/raku/ch-02.raku | 15 - challenge-233/massa/raku/ch-1.raku | 14 + challenge-233/massa/raku/ch-2.raku | 15 + challenge-233/perlboy1967/ch1.pl | 49 - challenge-233/perlboy1967/ch2.pl | 40 - challenge-233/perlboy1967/perl/ch-1.pl | 49 + challenge-233/perlboy1967/perl/ch-2.pl | 40 + challenge-233/robert-dicicco/julia/ch-1.jl | 52 + challenge-233/robert-dicicco/perl/ch-1.pl | 45 + challenge-233/robert-dicicco/python/ch-1.py | 52 + challenge-233/robert-dicicco/raku/ch-1.raku | 43 + challenge-233/robert-dicicco/ruby/ch-1.rb | 44 + challenge-233/steven-wilson/javascript/ch-01.js | 22 - challenge-233/steven-wilson/javascript/ch-02.js | 13 - challenge-233/steven-wilson/javascript/ch-1.js | 22 + challenge-233/steven-wilson/javascript/ch-2.js | 13 + challenge-233/steven-wilson/python/ch-01.py | 40 - challenge-233/steven-wilson/python/ch-02.py | 23 - challenge-233/steven-wilson/python/ch-1.py | 40 + challenge-233/steven-wilson/python/ch-2.py | 23 + challenge-233/ulrich-rieke/cpp/ch-1.cpp | 71 + challenge-233/ulrich-rieke/cpp/ch-2.cpp | 69 + challenge-233/ulrich-rieke/haskell/ch-1.hs | 27 + challenge-233/ulrich-rieke/haskell/ch-2.hs | 18 + challenge-233/ulrich-rieke/perl/ch-1.pl | 53 + challenge-233/ulrich-rieke/perl/ch-2.pl | 24 + challenge-233/ulrich-rieke/raku/ch-1.raku | 39 + challenge-233/ulrich-rieke/raku/ch-2.raku | 23 + challenge-233/ulrich-rieke/rust/ch-1.rs | 66 + challenge-233/ulrich-rieke/rust/ch-2.rs | 36 + challenge-233/wanderdoc/perl/ch-1.pl | 79 + challenge-233/wanderdoc/perl/ch-2.pl | 33 + members.json | 1 + stats/pwc-challenge-231.json | 847 +++++++++++ stats/pwc-current.json | 753 ++++----- stats/pwc-language-breakdown-summary.json | 80 +- stats/pwc-language-breakdown.json | 1659 ++++++++++---------- stats/pwc-leaders.json | 892 +++++------ stats/pwc-summary-1-30.json | 64 +- stats/pwc-summary-121-150.json | 110 +- stats/pwc-summary-151-180.json | 144 +- stats/pwc-summary-181-210.json | 80 +- stats/pwc-summary-211-240.json | 72 +- stats/pwc-summary-241-270.json | 120 +- stats/pwc-summary-271-300.json | 58 +- stats/pwc-summary-31-60.json | 108 +- stats/pwc-summary-61-90.json | 104 +- stats/pwc-summary-91-120.json | 46 +- stats/pwc-summary.json | 1842 ++++++++++++----------- 63 files changed, 5140 insertions(+), 3447 deletions(-) delete mode 100644 challenge-231/massa/raku/ch-01.raku delete mode 100644 challenge-231/massa/raku/ch-02.raku create mode 100644 challenge-231/massa/raku/ch-1.raku create mode 100644 challenge-231/massa/raku/ch-2.raku create mode 100755 challenge-233/eric-cheung/python/ch-1.py create mode 100755 challenge-233/eric-cheung/python/ch-2.py create mode 100644 challenge-233/laurent-rosenfeld/blog.txt create mode 100644 challenge-233/laurent-rosenfeld/blog1.txt create mode 100644 challenge-233/laurent-rosenfeld/perl/ch-1.pl create mode 100644 challenge-233/laurent-rosenfeld/raku/ch-1.raku create mode 100644 challenge-233/laurent-rosenfeld/raku/ch-2.raku create mode 100644 challenge-233/macy-ty/javascript/ch-1.js create mode 100644 challenge-233/macy-ty/javascript/ch-2.js delete mode 100644 challenge-233/massa/raku/ch-01.raku delete mode 100644 challenge-233/massa/raku/ch-02.raku create mode 100644 challenge-233/massa/raku/ch-1.raku create mode 100644 challenge-233/massa/raku/ch-2.raku delete mode 100755 challenge-233/perlboy1967/ch1.pl delete mode 100755 challenge-233/perlboy1967/ch2.pl create mode 100755 challenge-233/perlboy1967/perl/ch-1.pl create mode 100755 challenge-233/perlboy1967/perl/ch-2.pl create mode 100644 challenge-233/robert-dicicco/julia/ch-1.jl create mode 100644 challenge-233/robert-dicicco/perl/ch-1.pl create mode 100644 challenge-233/robert-dicicco/python/ch-1.py create mode 100644 challenge-233/robert-dicicco/raku/ch-1.raku create mode 100644 challenge-233/robert-dicicco/ruby/ch-1.rb delete mode 100644 challenge-233/steven-wilson/javascript/ch-01.js delete mode 100644 challenge-233/steven-wilson/javascript/ch-02.js create mode 100644 challenge-233/steven-wilson/javascript/ch-1.js create mode 100644 challenge-233/steven-wilson/javascript/ch-2.js delete mode 100644 challenge-233/steven-wilson/python/ch-01.py delete mode 100644 challenge-233/steven-wilson/python/ch-02.py create mode 100644 challenge-233/steven-wilson/python/ch-1.py create mode 100644 challenge-233/steven-wilson/python/ch-2.py create mode 100755 challenge-233/ulrich-rieke/cpp/ch-1.cpp create mode 100755 challenge-233/ulrich-rieke/cpp/ch-2.cpp create mode 100755 challenge-233/ulrich-rieke/haskell/ch-1.hs create mode 100755 challenge-233/ulrich-rieke/haskell/ch-2.hs create mode 100755 challenge-233/ulrich-rieke/perl/ch-1.pl create mode 100755 challenge-233/ulrich-rieke/perl/ch-2.pl create mode 100755 challenge-233/ulrich-rieke/raku/ch-1.raku create mode 100755 challenge-233/ulrich-rieke/raku/ch-2.raku create mode 100755 challenge-233/ulrich-rieke/rust/ch-1.rs create mode 100755 challenge-233/ulrich-rieke/rust/ch-2.rs create mode 100755 challenge-233/wanderdoc/perl/ch-1.pl create mode 100755 challenge-233/wanderdoc/perl/ch-2.pl create mode 100644 stats/pwc-challenge-231.json diff --git a/challenge-231/massa/raku/ch-01.raku b/challenge-231/massa/raku/ch-01.raku deleted file mode 100644 index e4a1b5befb..0000000000 --- a/challenge-231/massa/raku/ch-01.raku +++ /dev/null @@ -1,66 +0,0 @@ -#! /usr/bin/env raku - -# Perl Weekly Challenge -# © 2023 Shimon Bollinger. All rights reserved. -# Last modified: Mon 15 May 2023 09:17:32 PM EDT -# Version 0.0.1 - -=begin pod -=TITLE -=head2 Task 1: Min Max - -=SUBTITLE -=head2 Submitted by massa - -=CHALLENGE -=head2 - -You are given an array of distinct integers. - -Write a script to find all elements that is neither minimum nor maximum. Return -1 if you can’t. - -=head3 Example 1: - -Input: @ints = (3, 2, 1, 4) -Output: (3, 2) - -The minimum is 1 and maximum is 4 in the given array. So (3, 2) is neither min nor max. - -=head3 Example 2: - -Input: @ints = (3, 1) -Output: -1 - -=head3 Example 3: - -Input: @ints = (2, 1, 3) -Output: (2) - -The minimum is 1 and maximum is 3 in the given array. So 2 is neither min nor max. - -=SOLUTION - -=end pod - -# always use the latest version of Raku -use v6.*; - -sub without-min-and-max(@_) { - @_.grep: * ∉ @_.minmax.bounds or -1 -} # end of sub my-sub () - -multi MAIN (Bool :$test!) { - use Test; - - my @tests = [ - %{ input => (3, 2, 1, 4), output => (3, 2), text => 'The minimum is 1 and maximum is 4 in the given array. So (3, 2) is neither min nor max' }, - %{ input => (3, 1), output => -1, text => 'There are only the min and the max in the array' }, - %{ input => (2, 1, 4), output => (2, ), text => 'The minimum is 1 and maximum is 3 in the given array. So 2 is neither min nor max' }, - ]; - - for @tests { - is-deeply without-min-and-max( . ), ., .; - } # end of for @tests -} # end of multi MAIN (Bool :$test!) - - diff --git a/challenge-231/massa/raku/ch-02.raku b/challenge-231/massa/raku/ch-02.raku deleted file mode 100644 index 8587a2e519..0000000000 --- a/challenge-231/massa/raku/ch-02.raku +++ /dev/null @@ -1,59 +0,0 @@ -#! /usr/bin/env raku - -# Perl Weekly Challenge -# © 2023 Shimon Bollinger. All rights reserved. -# Last modified: Mon 15 May 2023 09:17:32 PM EDT -# Version 0.0.1 - -=begin pod -=TITLE -=head2 Task 2: Senior Citizens - -=SUBTITLE -=head2 Submitted by massa - -=CHALLENGE -=head2 - -You are given a list of passenger details in the form “9999999999A1122”, where 9 denotes the phone number, A the sex, 1 the age and 2 the seat number. - -Write a script to return the count of all senior citizens (age >= 60). - -=head3 Example 1: - -Input: @list = ("7868190130M7522","5303914400F9211","9273338290F4010") -Ouput: 2 - -The age of the passengers in the given list are 75, 92 and 40. -So we have only 2 senior citizens. - -=head3 Example 2: - -Input: @list = ("1313579440F2036","2921522980M5644") -Ouput: 0 - -=SOLUTION - -=end pod - -# always use the latest version of Raku -use v6.*; - -sub number-of-senior-citizens(@_) { - @_.grep(/^ \d ** 10 \D (\d\d) \d ** 2 $/).elems -} # end of sub my-sub () - -multi MAIN (Bool :$test!) { - use Test; - - my @tests = [ - %{ input => ("7868190130M7522","5303914400F9211","9273338290F4010"), output => 2, text => 'The age of the passengers in the given list are 75, 92 and 40, so we have only 2 senior citizens' }, - %{ input => ("1313579440F2036","2921522980M5644"), output => 0, text => 'The age of the passengers in the given list are 20 and 56, so we have no senior citizens' }, - ]; - - for @tests { - is-deeply number-of-senior-citizens( . ), ., .; - } # end of for @tests -} # end of multi MAIN (:$test! ) - - diff --git a/challenge-231/massa/raku/ch-1.raku b/challenge-231/massa/raku/ch-1.raku new file mode 100644 index 0000000000..e4a1b5befb --- /dev/null +++ b/challenge-231/massa/raku/ch-1.raku @@ -0,0 +1,66 @@ +#! /usr/bin/env raku + +# Perl Weekly Challenge +# © 2023 Shimon Bollinger. All rights reserved. +# Last modified: Mon 15 May 2023 09:17:32 PM EDT +# Version 0.0.1 + +=begin pod +=TITLE +=head2 Task 1: Min Max + +=SUBTITLE +=head2 Submitted by massa + +=CHALLENGE +=head2 + +You are given an array of distinct integers. + +Write a script to find all elements that is neither minimum nor maximum. Return -1 if you can’t. + +=head3 Example 1: + +Input: @ints = (3, 2, 1, 4) +Output: (3, 2) + +The minimum is 1 and maximum is 4 in the given array. So (3, 2) is neither min nor max. + +=head3 Example 2: + +Input: @ints = (3, 1) +Output: -1 + +=head3 Example 3: + +Input: @ints = (2, 1, 3) +Output: (2) + +The minimum is 1 and maximum is 3 in the given array. So 2 is neither min nor max. + +=SOLUTION + +=end pod + +# always use the latest version of Raku +use v6.*; + +sub without-min-and-max(@_) { + @_.grep: * ∉ @_.minmax.bounds or -1 +} # end of sub my-sub () + +multi MAIN (Bool :$test!) { + use Test; + + my @tests = [ + %{ input => (3, 2, 1, 4), output => (3, 2), text => 'The minimum is 1 and maximum is 4 in the given array. So (3, 2) is neither min nor max' }, + %{ input => (3, 1), output => -1, text => 'There are only the min and the max in the array' }, + %{ input => (2, 1, 4), output => (2, ), text => 'The minimum is 1 and maximum is 3 in the given array. So 2 is neither min nor max' }, + ]; + + for @tests { + is-deeply without-min-and-max( . ), ., .; + } # end of for @tests +} # end of multi MAIN (Bool :$test!) + + diff --git a/challenge-231/massa/raku/ch-2.raku b/challenge-231/massa/raku/ch-2.raku new file mode 100644 index 0000000000..8587a2e519 --- /dev/null +++ b/challenge-231/massa/raku/ch-2.raku @@ -0,0 +1,59 @@ +#! /usr/bin/env raku + +# Perl Weekly Challenge +# © 2023 Shimon Bollinger. All rights reserved. +# Last modified: Mon 15 May 2023 09:17:32 PM EDT +# Version 0.0.1 + +=begin pod +=TITLE +=head2 Task 2: Senior Citizens + +=SUBTITLE +=head2 Submitted by massa + +=CHALLENGE +=head2 + +You are given a list of passenger details in the form “9999999999A1122”, where 9 denotes the phone number, A the sex, 1 the age and 2 the seat number. + +Write a script to return the count of all senior citizens (age >= 60). + +=head3 Example 1: + +Input: @list = ("7868190130M7522","5303914400F9211","9273338290F4010") +Ouput: 2 + +The age of the passengers in the given list are 75, 92 and 40. +So we have only 2 senior citizens. + +=head3 Example 2: + +Input: @list = ("1313579440F2036","2921522980M5644") +Ouput: 0 + +=SOLUTION + +=end pod + +# always use the latest version of Raku +use v6.*; + +sub number-of-senior-citizens(@_) { + @_.grep(/^ \d ** 10 \D (\d\d) \d ** 2 $/).elems +} # end of sub my-sub () + +multi MAIN (Bool :$test!) { + use Test; + + my @tests = [ + %{ input => ("7868190130M7522","5303914400F9211","9273338290F4010"), output => 2, text => 'The age of the passengers in the given list are 75, 92 and 40, so we have only 2 senior citizens' }, + %{ input => ("1313579440F2036","2921522980M5644"), output => 0, text => 'The age of the passengers in the given list are 20 and 56, so we have no senior citizens' }, + ]; + + for @tests { + is-deeply number-of-senior-citizens( . ), ., .; + } # end of for @tests +} # end of multi MAIN (:$test! ) + + diff --git a/challenge-233/eric-cheung/python/ch-1.py b/challenge-233/eric-cheung/python/ch-1.py new file mode 100755 index 0000000000..7123b11bb7 --- /dev/null +++ b/challenge-233/eric-cheung/python/ch-1.py @@ -0,0 +1,10 @@ + +## arrWordList = ["aba", "aabb", "abcd", "bac", "aabc"] ## Example 1 +## arrWordList = ["aabb", "ab", "ba"] ## Example 2 +arrWordList = ["nba", "cba", "dba"] ## Example 3 + +arrCharList = ["".join(sorted(list(set([*strWordLoop])))) for strWordLoop in arrWordList] + +arrOutputList = [1 for nRowLoop in range(0, len(arrCharList) - 1) for nColLoop in range(nRowLoop + 1, len(arrCharList)) if arrCharList[nRowLoop] == arrCharList[nColLoop]] + +print (len(arrOutputList)) diff --git a/challenge-233/eric-cheung/python/ch-2.py b/challenge-233/eric-cheung/python/ch-2.py new file mode 100755 index 0000000000..19ff07aa18 --- /dev/null +++ b/challenge-233/eric-cheung/python/ch-2.py @@ -0,0 +1,37 @@ + +def MySwap(nInput_01, nInput_02): + return [nInput_02, nInput_01] + + +## arrInput = [1, 1, 2, 2, 2, 3] ## Example 1 +## arrInput = [2, 3, 1, 3, 2] ## Example 2 +arrInput = [-1, 1, -6, 4, 5, -6, 1, 4, 1] ## Example 3 + +arrUniq = sorted(list(set(arrInput))) +arrCount = [arrInput.count(nLoop) for nLoop in arrUniq] + +arrOutput = [] + +## print (arrUniq) +## print (arrCount) + +## print ("Before Bubble Sort") + +## Bubble Sort +for nRowLoop in range(0, len(arrCount) - 1): + for nColLoop in range(nRowLoop + 1, len(arrCount)): + if arrCount[nRowLoop] > arrCount[nColLoop]: + [arrCount[nRowLoop], arrCount[nColLoop]] = MySwap(arrCount[nRowLoop], arrCount[nColLoop]) + [arrUniq[nRowLoop], arrUniq[nColLoop]] = MySwap(arrUniq[nRowLoop], arrUniq[nColLoop]) + elif arrCount[nRowLoop] == arrCount[nColLoop] and arrUniq[nRowLoop] < arrUniq[nColLoop]: + [arrUniq[nRowLoop], arrUniq[nColLoop]] = MySwap(arrUniq[nRowLoop], arrUniq[nColLoop]) + +## print ("After Bubble Sort") + +## print (arrUniq) +## print (arrCount) + +for nIndx, nLoop in enumerate(arrUniq): + arrOutput = arrOutput + [nLoop] * arrCount[nIndx] + +print (arrOutput) diff --git a/challenge-233/laurent-rosenfeld/blog.txt b/challenge-233/laurent-rosenfeld/blog.txt new file mode 100644 index 0000000000..13c2c98d38 --- /dev/null +++ b/challenge-233/laurent-rosenfeld/blog.txt @@ -0,0 +1 @@ +https://blogs.perl.org/users/laurent_r/2023/09/perl-weekly-challenge-233-separate-digits.html diff --git a/challenge-233/laurent-rosenfeld/blog1.txt b/challenge-233/laurent-rosenfeld/blog1.txt new file mode 100644 index 0000000000..0ea962df94 --- /dev/null +++ b/challenge-233/laurent-rosenfeld/blog1.txt @@ -0,0 +1 @@ +https://blogs.perl.org/users/laurent_r/2023/09/perl-weekly-challenge-233-frequency-sort.html diff --git a/challenge-233/laurent-rosenfeld/perl/ch-1.pl b/challenge-233/laurent-rosenfeld/perl/ch-1.pl new file mode 100644 index 0000000000..116802ee05 --- /dev/null +++ b/challenge-233/laurent-rosenfeld/perl/ch-1.pl @@ -0,0 +1,28 @@ +use strict; +use warnings; +use feature 'say'; + +sub unique_srt { + my %letters = map { $_ => 0 } split //, $_[0]; + return join "", sort keys %letters; +} +sub fact { + my $num = shift; + my $fact = 1; + $fact *= $_ for 2..$num; + return $fact; +} +sub similar { + my %words; + $words{$_}++ for map { unique_srt $_ } @_; + %words = map { $_ => $words{$_}} grep {$words{$_} > 1} keys %words; + my $count = 0; + $count += (fact $words{$_})/2 for keys %words; + return $count; +} + +for my $test ([], + [], []) { + printf "%-25s => ", "@$test"; + say similar @$test; +} diff --git a/challenge-233/laurent-rosenfeld/raku/ch-1.raku b/challenge-233/laurent-rosenfeld/raku/ch-1.raku new file mode 100644 index 0000000000..d5a494d6df --- /dev/null +++ b/challenge-233/laurent-rosenfeld/raku/ch-1.raku @@ -0,0 +1,14 @@ +sub similar (@in) { + my %words; + %words{$_}++ for map { $_.comb.sort.squish.join("")}, @in; + %words = map { $_ => %words{$_}}, grep {%words{$_} > 1}, %words.keys; + my $count = 0; + $count += ([*] 1..%words{$_})/2 for %words.keys; + return $count; +} + +for , , + -> @test { + printf "%-30s => ", "@test[]"; + say similar @test; +} diff --git a/challenge-233/laurent-rosenfeld/raku/ch-2.raku b/challenge-233/laurent-rosenfeld/raku/ch-2.raku new file mode 100644 index 0000000000..01952f1cda --- /dev/null +++ b/challenge-233/laurent-rosenfeld/raku/ch-2.raku @@ -0,0 +1,17 @@ +sub special-comp { + return $^b <=> $^a if %*histo{$^a} == %*histo{$^b}; + return %*histo{$^a} <=> %*histo{$^b}; +} + +sub freq-sort (@in) { + my %*histo; + %*histo{$_}++ for @in; + my @sorted = sort &special-comp, %*histo.keys; + my @result = map { |($_ xx %*histo{$_})}, @sorted; +} + +for <1 1 2 2 2 3>, <2 3 1 3 2>, + (-1,1,-6,4,5,-6,1,4,1) -> @test { + printf "%-25s => ", "@test[]"; + say freq-sort @test; +} diff --git a/challenge-233/macy-ty/javascript/ch-1.js b/challenge-233/macy-ty/javascript/ch-1.js new file mode 100644 index 0000000000..f4acd36b72 --- /dev/null +++ b/challenge-233/macy-ty/javascript/ch-1.js @@ -0,0 +1,24 @@ +function similarWords(...strArray){ + let newArray = new Array(); + let output = 0; + for (const element of strArray) { + let elem = Array.from(new Set(element)); + elem.sort(); + elem = elem.join(''); + newArray.push(elem); + } + console.log(newArray); + + + for (let i = 0; i < newArray.length; i++) { + for (let j = i+1; j < newArray.length; j++) { + if (newArray[i] == newArray[j]) { + output++; + } + } + } + + console.log(output); +} + +similarWords("aabb", "ab", "ba"); diff --git a/challenge-233/macy-ty/javascript/ch-2.js b/challenge-233/macy-ty/javascript/ch-2.js new file mode 100644 index 0000000000..456234850c --- /dev/null +++ b/challenge-233/macy-ty/javascript/ch-2.js @@ -0,0 +1,19 @@ +const myArray = [-1,1,-6,4,5,-6,1,4,1]; +const elementCounts = {}; +myArray.forEach(element => { + elementCounts[element] = (elementCounts[element] || 0) + 1; +}); + + +const objectArray = Object.entries(elementCounts).sort((a,b) => a[1]-b[1]) + +let newArray = new Array; +objectArray.forEach(element => { + while(element[1] > 0 ){ + newArray.push(element[0]); + element[1] --; + } + +}); + +console.log(newArray); diff --git a/challenge-233/massa/raku/ch-01.raku b/challenge-233/massa/raku/ch-01.raku deleted file mode 100644 index b9bdc1f707..0000000000 --- a/challenge-233/massa/raku/ch-01.raku +++ /dev/null @@ -1,14 +0,0 @@ -#!/usr/bin/env raku - -use v6.d; -use Test; - -plan 3; - -is 2, similar-words ; -is 3, similar-words ; -is 0, similar-words ; - -sub similar-words(@_) { - @_.combinations(2).grep(-> ($a, $b) { $a.comb.Set eqv $b.comb.Set }).elems -} \ No newline at end of file diff --git a/challenge-233/massa/raku/ch-02.raku b/challenge-233/massa/raku/ch-02.raku deleted file mode 100644 index c4d2e678fc..0000000000 --- a/challenge-233/massa/raku/ch-02.raku +++ /dev/null @@ -1,15 +0,0 @@ -#!/usr/bin/env raku - -use v6.d; -use Test; - -plan 3; - -is-deeply <3 1 1 2 2 2>, frequency-sort <1 1 2 2 2 3>; -is-deeply <1 3 3 2 2>, frequency-sort <1 2 2 3 3>; -is-deeply <5 -1 4 4 -6 -6 1 1 1>, frequency-sort <-1 1 -6 4 5 -6 1 4 1>; - -sub frequency-sort(@_) { - my $frequencies = @_.Bag; - @_.sort: { $frequencies{$^a} <=> $frequencies{$^b} || $^b <=> $^a } -} \ No newline at end of file diff --git a/challenge-233/massa/raku/ch-1.raku b/challenge-233/massa/raku/ch-1.raku new file mode 100644 index 0000000000..b9bdc1f707 --- /dev/null +++ b/challenge-233/massa/raku/ch-1.raku @@ -0,0 +1,14 @@ +#!/usr/bin/env raku + +use v6.d; +use Test; + +plan 3; + +is 2, similar-words ; +is 3, similar-words ; +is 0, similar-words ; + +sub similar-words(@_) { + @_.combinations(2).grep(-> ($a, $b) { $a.comb.Set eqv $b.comb.Set }).elems +} \ No newline at end of file diff --git a/challenge-233/massa/raku/ch-2.raku b/challenge-233/massa/raku/ch-2.raku new file mode 100644 index 0000000000..c4d2e678fc --- /dev/null +++ b/challenge-233/massa/raku/ch-2.raku @@ -0,0 +1,15 @@ +#!/usr/bin/env raku + +use v6.d; +use Test; + +plan 3; + +is-deeply <3 1 1 2 2 2>, frequency-sort <1 1 2 2 2 3>; +is-deeply <1 3 3 2 2>, frequency-sort <1 2 2 3 3>; +is-deeply <5 -1 4 4 -6 -6 1 1 1>, frequency-sort <-1 1 -6 4 5 -6 1 4 1>; + +sub frequency-sort(@_) { + my $frequencies = @_.Bag; + @_.sort: { $frequencies{$^a} <=> $frequencies{$^b} || $^b <=> $^a } +} \ No newline at end of file diff --git a/challenge-233/perlboy1967/ch1.pl b/challenge-233/perlboy1967/ch1.pl deleted file mode 100755 index 3b985c029c..0000000000 --- a/challenge-233/perlboy1967/ch1.pl +++ /dev/null @@ -1,49 +0,0 @@ -#!/bin/perl - -=pod - -The Weekly Challenge - 233 -- https://theweeklychallenge.org/blog/perl-weekly-challenge-233 - -Author: Niels 'PerlBoy' van Dijke - -Task 1: Similar Words -Submitted by: Mohammad S Anwar - -You are given an array of words made up of alphabets only. - -Write a script to find the number of pairs of similar words. -Two words are similar if they consist of the same characters. - -=cut - -use v5.16; - -use common::sense; - -use Test::More; - -use List::MoreUtils qw(uniq); -use Algorithm::Combinatorics qw(combinations); - -sub similarWords (@) { - my $n = 0; - my %w; - - # Find and group 'similar' words - push(@{$w{join '',uniq sort split (//,$_)}},$_) for (@_); - - # Do the counting of number of pairs - for (grep { @{$w{$_}} > 1 } keys %w) { - my @c = combinations($w{$_},2); - $n += scalar @c; - } - - return $n; -} - -is(similarWords("aba", "aabb", "abcd", "bac", "aabc"),2); -is(similarWords("aabb", "ab", "ba"),3); -is(similarWords("nba", "cba", "dba"),0); - -done_testing; diff --git a/challenge-233/perlboy1967/ch2.pl b/challenge-233/perlboy1967/ch2.pl deleted file mode 100755 index e341e40365..0000000000 --- a/challenge-233/perlboy1967/ch2.pl +++ /dev/null @@ -1,40 +0,0 @@ -#!/bin/perl - -=pod - -The Weekly Challenge - 233 -- https://theweeklychallenge.org/blog/perl-weekly-challenge-233 - -Author: Niels 'PerlBoy' van Dijke - -Task 2: Frequency Sort -Submitted by: Mohammad S Anwar - -You are given an array of integers. - -Write a script to sort the given array in increasing order based on the -frequency of the values. If multiple values have the same frequency then -sort them in decreasing order. - -=cut - -use v5.16; - -use common::sense; - -use Test::More; -use Test::Deep qw(cmp_deeply); - -sub frequencySort (@) { - my %f; - $f{$_}++ for (@_); - - map {($_) x $f{$_}} sort { $f{$a} <=> $f{$b} or $b <=> $a } keys %f; -} - -cmp_deeply([frequencySort(1,1,2,2,2,3)], - [3,1,1,2,2,2]); -cmp_deeply([frequencySort(2,3,1,3,2)], - [1,3,3,2,2]); - -done_testing; diff --git a/challenge-233/perlboy1967/perl/ch-1.pl b/challenge-233/perlboy1967/perl/ch-1.pl new file mode 100755 index 0000000000..3b985c029c --- /dev/null +++ b/challenge-233/perlboy1967/perl/ch-1.pl @@ -0,0 +1,49 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 233 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-233 + +Author: Niels 'PerlBoy' van Dijke + +Task 1: Similar Words +Submitted by: Mohammad S Anwar + +You are given an array of words made up of alphabets only. + +Write a script to find the number of pairs of similar words. +Two words are similar if they consist of the same characters. + +=cut + +use v5.16; + +use common::sense; + +use Test::More; + +use List::MoreUtils qw(uniq); +use Algorithm::Combinatorics qw(combinations); + +sub similarWords (@) { + my $n = 0; + my %w; + + # Find and group 'similar' words + push(@{$w{join '',uniq sort split (//,$_)}},$_) for (@_); + + # Do the counting of number of pairs + for (grep { @{$w{$_}} > 1 } keys %w) { + my @c = combinations($w{$_},2); + $n += scalar @c; + } + + return $n; +} + +is(similarWords("aba", "aabb", "abcd", "bac", "aabc"),2); +is(similarWords("aabb", "ab", "ba"),3); +is(similarWords("nba", "cba", "dba"),0); + +done_testing; diff --git a/challenge-233/perlboy1967/perl/ch-2.pl b/challenge-233/perlboy1967/perl/ch-2.pl new file mode 100755 index 0000000000..e341e40365 --- /dev/null +++ b/challenge-233/perlboy1967/perl/ch-2.pl @@ -0,0 +1,40 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 233 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-233 + +Author: Niels 'PerlBoy' van Dijke + +Task 2: Frequency Sort +Submitted by: Mohammad S Anwar + +You are given an array of integers. + +Write a script to sort the given array in increasing order based on the +frequency of the values. If multiple values have the same frequency then +sort them in decreasing order. + +=cut + +use v5.16; + +use common::sense; + +use Test::More; +use Test::Deep qw(cmp_deeply); + +sub frequencySort (@) { + my %f; + $f{$_}++ for (@_); + + map {($_) x $f{$_}} sort { $f{$a} <=> $f{$b} or $b <=> $a } keys %f; +} + +cmp_deeply([frequencySort(1,1,2,2,2,3)], + [3,1,1,2,2,2]); +cmp_deeply([frequencySort(2,3,1,3,2)], + [1,3,3,2,2]); + +done_testing; diff --git a/challenge-233/robert-dicicco/julia/ch-1.jl b/challenge-233/robert-dicicco/julia/ch-1.jl new file mode 100644 index 0000000000..8c9bba2ec3 --- /dev/null +++ b/challenge-233/robert-dicicco/julia/ch-1.jl @@ -0,0 +1,52 @@ +#= +AUTHOR: Robert DiCicco +DATE : 2023-09-09 +Challenge 233 Task one Similar Words ( Julia ) +=# + +using Combinatorics +using Printf + +#words = ["aba", "aabb", "abcd", "bac", "aabc"] +words = ["aabb", "ab", "ba"] + +pair = 0 +cnt = 1 + +@printf("Input: @words = %s\n", words) + +for wds in combinations(words,2) + global pair,cnt + #@printf("%s\n",wds[1]) + arr = split(wds[1],"") + srt1 = unique(sort(arr)) + arr = split(wds[2],"") + srt2 = unique(sort(arr)) + srt1 = join(srt1,"") + srt2 = join(srt2, "") + if cmp(srt1,srt2) == 0 + @printf("\tpair%d %s\n",cnt,wds) + pair += 1 + cnt += 1 + end +end +@printf("Output: %d\n",pair) + +#= +SAMPLE OUTPUT +julia .\SimilarWords.jl + +Input: @words = ["aba", "aabb", "abcd", "bac", "aabc"] + pair1 ["aba", "aabb"] + pair2 ["bac", "aabc"] +Output: 2 + +julia .\SimilarWords.jl +Input: @words = ["aabb", "ab", "ba"] + pair1 ["aabb", "ab"] + pair2 ["aabb", "ba"] + pair3 ["ab", "ba"] +Output: 3 +=# + + diff --git a/challenge-233/robert-dicicco/perl/ch-1.pl b/challenge-233/robert-dicicco/perl/ch-1.pl new file mode 100644 index 0000000000..35f3fa7919 --- /dev/null +++ b/challenge-233/robert-dicicco/perl/ch-1.pl @@ -0,0 +1,45 @@ +#!/usr/bin/env perl +=begin comment +AUTHOR: Robert DiCicco +DATE : 2023-09-09 +Challenge 233 Task one Similar Words ( Perl ) +=cut + +use v5.38; +use List::Util qw/uniq/; +use Algorithm::Combinatorics qw/combinations/; + +#my @words = ("aba", "aabb", "abcd", "bac", "aabc"); +my @words = ("aabb", "ab", "ba"); +my $pairs = 0; +my $cnt = 1; + +say "Input: \@words = [@words]"; +my $iter = combinations(\@words, 2); + while (my $c = $iter->next) { + my $str1 = join("",uniq(sort(split("",@$c[0])))); + my $str2 = join("",uniq(sort(split("",@$c[1])))); + if ($str1 eq $str2) { + say "\tpair$cnt \[@$c[0] @$c[1]\]"; + $pairs++; + $cnt++; + } + } +say("Output: $pairs"); + +=begin comment +SAMPLE OUTPUT +perl .\SimilarWords.pl + +Input: @words = [aba aabb abcd bac aabc] + pair1 [aba aabb] + pair2 [bac aabc] +Output: 2 + +perl .\SimilarWords.pl +Input: @words = [aabb ab ba] + pair1 [aabb ab] + pair2 [aabb ba] + pair3 [ab ba] +Output: 3 +=cut diff --git a/challenge-233/robert-dicicco/python/ch-1.py b/challenge-233/robert-dicicco/python/ch-1.py new file mode 100644 index 0000000000..2156c581f8 --- /dev/null +++ b/challenge-233/robert-dicicco/python/ch-1.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python +''' +AUTHOR: Robert DiCicco +DATE : 2023-09-09 +Challenge 233 Task one Similar Words ( Python ) +''' +from itertools import combinations + +#words = ["aba", "aabb", "abcd", "bac", "aabc"] +words = ["aabb", "ab", "ba"] + +pair = 0 +cnt = 1 + +def unique(list1): + unique_list = [] + for x in list1: + if x not in unique_list: + unique_list.append(x) + return unique_list + +print(f"Input: @words = {words}") +for wds in (list(combinations(words, 2))): + arr = [x for x in sorted(wds[0])] + str1 = "".join(unique(arr)) + + arr = [x for x in sorted(wds[1])] + str2 = "".join(unique(arr)) + + if str1 == str2: + print(f"\tpair{cnt} [{wds[0]} {wds[1]}]") + cnt += 1 + pair += 1 + +print(f"Output: {pair}") + +''' +SAMPLE OUTPUT +python .\SimilarWords.py + +Input: @words = ['aba', 'aabb', 'abcd', 'bac', 'aabc'] + pair1 [aba aabb] + pair2 [bac aabc] +Output: 2 + +python .\SimilarWords.py +Input: @words = ['aabb', 'ab', 'ba'] + pair1 [aabb ab] + pair2 [aabb ba] + pair3 [ab ba] +Output: 3 +''' diff --git a/challenge-233/robert-dicicco/raku/ch-1.raku b/challenge-233/robert-dicicco/raku/ch-1.raku new file mode 100644 index 0000000000..299778c808 --- /dev/null +++ b/challenge-233/robert-dicicco/raku/ch-1.raku @@ -0,0 +1,43 @@ +#!/usr/bin/env raku +=begin comment +AUTHOR: Robert DiCicco +DATE : 2023-09-09 +Challenge 233 Task one Similar Words ( Raku ) +=end comment + +#my @words = ("aba", "aabb", "abcd", "bac", "aabc"); +my @words = ("aabb", "ab", "ba"); + +my $pairs = 0; +my $cnt = 1; +say "Input: \@words = ",@words; + +for @words.combinations(2) { # for each combination of size 2 + my @arr = split(" ",$_); + my $srt1 = @arr[0].comb.unique.sort.join; + my $srt2 = @arr[1].comb.unique.sort.join; + if $srt1 eq $srt2 { + say "\tpair$cnt \[@arr[0] @arr[1]\]"; + $pairs++; + $cnt++; + } +} + +say "Output: $pairs"; + +=begin comment +SAMPLE OUTPUT +raku .\SimilarWords.rk + +Input: @words = [aba aabb abcd bac aabc] + pair1 [aba aabb] + pair2 [bac aabc] +Output: 2 + +raku .\SimilarWords.rk +Input: @words = [aabb ab ba] + pair1 [aabb ab] + pair2 [aabb ba] + pair3 [ab ba] +Output: 3 +=end comment diff --git a/challenge-233/robert-dicicco/ruby/ch-1.rb b/challenge-233/robert-dicicco/ruby/ch-1.rb new file mode 100644 index 0000000000..1b4a769c9e --- /dev/null +++ b/challenge-233/robert-dicicco/ruby/ch-1.rb @@ -0,0 +1,44 @@ +#!/usr/bin/env ruby +=begin +AUTHOR: Robert DiCicco +DATE : 2023-09-09 +Challenge 233 Task one Similar Words ( Ruby ) +=end + +words = ["aba", "aabb", "abcd", "bac", "aabc"] +#words = ["aabb", "ab", "ba"] + +ax = words.combination(2).to_a +pairs = 0 +cnt = 1 +puts("Input: @words = #{words}") +ax.each do |res| + str1 = res[0].split(//).sort.uniq.join("") + str2 = res[1].split(//).sort.uniq.join("") + if str1 == str2 + puts("\tpair#{cnt} \[#{res[0]} #{res[1]}\]") + pairs += 1 + cnt += 1 + end +end +puts("Output: #{pairs}") + +=begin +SAMPLE OUTPUT +ruby .\SimilarWords.rb + +Input: @words = ["aabb", "ab", "ba"] + pair1 [aabb ab] + pair2 [aabb ba] + pair3 [ab ba] +Output: 3 + +ruby .\SimilarWords.rb + +Input: @words = ["aba", "aabb", "abcd", "bac", "aabc"] + pair1 [aba aabb] + pair2 [bac aabc] +Output: 2 +=end + + diff --git a/challenge-233/steven-wilson/javascript/ch-01.js b/challenge-233/steven-wilson/javascript/ch-01.js deleted file mode 100644 index dd1b3d3d04..0000000000 --- a/challenge-233/steven-wilson/javascript/ch-01.js +++ /dev/null @@ -1,22 +0,0 @@ -"use strict"; - - -function similarWords(elements) { - let similar = 0; - for (let i = 0; i < elements.length - 1; i++) { - let elem = elements[i]; - for (let j = i + 1; j < elements.length; j++) { - let elemNext = elements[j]; - if (compWords(elem, elemNext)) { - similar++; - } - } - } - return similar; -} - -function compWords(word1, word2) { - let word1s = new Set(word1); - let word2s = new Set(word2); - return word1s.size === word2s.size && [...word1s].every((char) => word2s.has(char)); -} diff --git a/challenge-233/steven-wilson/javascript/ch-02.js b/challenge-233/steven-wilson/javascript/ch-02.js deleted file mode 100644 index fac9e2d0f2..0000000000 --- a/challenge-233/steven-wilson/javascript/ch-02.js +++ /dev/null @@ -1,13 +0,0 @@ -"use strict"; - - -function frequencySort(elements) { - let elemCount = new Map(); - for (let elem of elements){ - elemCount.set( elem, (elemCount.get(elem) ?? 0) + 1 ); - } - elements.sort(function(a,b){ - return elemCount.get(a) - elemCount.get(b) || b - a; - }); - return elements; -} diff --git a/challenge-233/steven-wilson/javascript/ch-1.js b/challenge-233/steven-wilson/javascript/ch-1.js new file mode 100644 index 0000000000..dd1b3d3d04 --- /dev/null +++ b/challenge-233/steven-wilson/javascript/ch-1.js @@ -0,0 +1,22 @@ +"use strict"; + + +function similarWords(elements) { + let similar = 0; + for (let i = 0; i < elements.length - 1; i++) { + let elem = elements[i]; + for (let j = i + 1; j < elements.length; j++) { + let elemNext = elements[j]; + if (compWords(elem, elemNext)) { + similar++; + } + } + } + return similar; +} + +function compWords(word1, word2) { + let word1s = new Set(word1); + let word2s = new Set(word2); + return word1s.size === word2s.size && [...word1s].every((char) => word2s.has(char)); +} diff --git a/challenge-233/steven-wilson/javascript/ch-2.js b/challenge-233/steven-wilson/javascript/ch-2.js new file mode 100644 index 0000000000..fac9e2d0f2 --- /dev/null +++ b/challenge-233/steven-wilson/javascript/ch-2.js @@ -0,0 +1,13 @@ +"use strict"; + + +function frequencySort(elements) { + let elemCount = new Map(); + for (let elem of elements){ + elemCount.set( elem, (elemCount.get(elem) ?? 0) + 1 ); + } + elements.sort(function(a,b){ + return elemCount.get(a) - elemCount.get(b) || b - a; + }); + return elements; +} diff --git a/challenge-233/steven-wilson/python/ch-01.py b/challenge-233/steven-wilson/python/ch-01.py deleted file mode 100644 index 6d599f2e3d..0000000000 --- a/challenge-233/steven-wilson/python/ch-01.py +++ /dev/null @@ -1,40 +0,0 @@ -#!/usr/bin/env python3 - - -def similarWords(*elements): - """Return count of similar words. Two words are similar if they - consist of the same characters. - >>> similarWords( "aba", "aabb", "abcd", "bac", "aabc" ) - 2 - >>> similarWords( "aabb", "ab", "ba" ) - 3 - >>> similarWords( "nba", "cba", "dba" ) - 0 - """ - count = 0 - for i in range(len(elements) - 1): - elem = elements[i] - for j in range(i + 1, len(elements)): - elemNext = elements[j] - if compWords(elem, elemNext): - count += 1 - return count - - -def compWords(word1, word2): - """Return true if two words have same characters - >>> compWords("aba", "aabb") - True - >>> compWords("abc", "aabb") - False - """ - word1s = set([*word1]) - word2s = set([*word2]) - return len(word1s) == len(word2s) and len(word1s.difference(word2s)) == 0 - - -if __name__ == "__main__": - import doctest - - doctest.testmod() - \ No newline at end of file diff --git a/challenge-233/steven-wilson/python/ch-02.py b/challenge-233/steven-wilson/python/ch-02.py deleted file mode 100644 index 7bd69c1e7e..0000000000 --- a/challenge-233/steven-wilson/python/ch-02.py +++ /dev/null @@ -1,23 +0,0 @@ -#!/usr/bin/env python3 - -from collections import Counter - - -def frequencySort(elements): - """Return list sorted by frequency then decending order - >>> frequencySort([1, 1, 2, 2, 2, 3]) - [3, 1, 1, 2, 2, 2] - >>> frequencySort([2, 3, 1, 3, 2]) - [1, 3, 3, 2, 2] - >>> frequencySort([-1, 1, -6, 4, 5, -6, 1, 4, 1]) - [5, -1, 4, 4, -6, -6, 1, 1, 1] - """ - elemCount = Counter(elements) - return sorted(elements, key=lambda x: (elemCount[x], -x)) - - -if __name__ == "__main__": - import doctest - - doctest.testmod() - diff --git a/challenge-233/steven-wilson/python/ch-1.py b/challenge-233/steven-wilson/python/ch-1.py new file mode 100644 index 0000000000..6d599f2e3d --- /dev/null +++ b/challenge-233/steven-wilson/python/ch-1.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python3 + + +def similarWords(*elements): + """Return count of similar words. Two words are similar if they + consist of the same characters. + >>> similarWords( "aba", "aabb", "abcd", "bac", "aabc" ) + 2 + >>> similarWords( "aabb", "ab", "ba" ) + 3 + >>> similarWords( "nba", "cba", "dba" ) + 0 + """ + count = 0 + for i in range(len(elements) - 1): + elem = elements[i] + for j in range(i + 1, len(elements)): + elemNext = elements[j] + if compWords(elem, elemNext): + count += 1 + return count + + +def compWords(word1, word2): + """Return true if two words have same characters + >>> compWords("aba", "aabb") + True + >>> compWords("abc", "aabb") + False + """ + word1s = set([*word1]) + word2s = set([*word2]) + return len(word1s) == len(word2s) and len(word1s.difference(word2s)) == 0 + + +if __name__ == "__main__": + import doctest + + doctest.testmod() + \ No newline at end of file diff --git a/challenge-233/steven-wilson/python/ch-2.py b/challenge-233/steven-wilson/python/ch-2.py new file mode 100644 index 0000000000..7bd69c1e7e --- /dev/null +++ b/challenge-233/steven-wilson/python/ch-2.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python3 + +from collections import Counter + + +def frequencySort(elements): + """Return list sorted by frequency then decending order + >>> frequencySort([1, 1, 2, 2, 2, 3]) + [3, 1, 1, 2, 2, 2] + >>> frequencySort([2, 3, 1, 3, 2]) + [1, 3, 3, 2, 2] + >>> frequencySort([-1, 1, -6, 4, 5, -6, 1, 4, 1]) + [5, -1, 4, 4, -6, -6, 1, 1, 1] + """ + elemCount = Counter(elements) + return sorted(elements, key=lambda x: (elemCount[x], -x)) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() + diff --git a/challenge-233/ulrich-rieke/cpp/ch-1.cpp b/challenge-233/ulrich-rieke/cpp/ch-1.cpp new file mode 100755 index 0000000000..714e45d5df --- /dev/null +++ b/challenge-233/ulrich-rieke/cpp/ch-1.cpp @@ -0,0 +1,71 @@ +#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 ; +} + +int findCombinations( int n ) { + std::vector numers( n ) ; + std::iota( numers.begin( ) , numers.end( ) , 1 ) ; + int numerator = std::accumulate( numers.begin( ) , numers.end( ) , 1 , + std::multiplies( ) ) ; + std::vector denoms( n - 2 ) ; + std::iota( denoms.begin( ) , denoms.end( ) , 1 ) ; + int denominator = std::accumulate( denoms.begin( ) , denoms.end( ) , 1 , + std::multiplies( ) ) ; + return numerator / ( denominator * 2 ) ; +} + +int findPairs( int n ) { + int result = 0 ; + if ( n == 0 || n == 1 ) { + result = 0 ; ; + } + if ( n == 2 ) { + result = 1 ; + } + if ( n > 2 ) { + int c = findCombinations( n ) ; + result = c ; + } + return result ; +} + +int main( ) { + std::cout << "Enter some words, separated by blanks!\n" ; + std::string line ; + std::getline( std::cin , line ) ; + std::vector words( split( line , " " ) ) ; + std::map lettercombis ; + for ( auto s : words ) { + std::set uniques ; + for ( char c : s ) + uniques.insert( c ) ; + std::string letters ; + for ( char c : uniques ) + letters.push_back( c) ; + std::sort( letters.begin( ) , letters.end( ) ) ; + lettercombis[ letters ]++ ; + } + int pairs = 0 ; + for ( auto p : lettercombis ) { + pairs += findPairs( p.second ) ; + } + std::cout << pairs << std::endl ; + return 0 ; +} diff --git a/challenge-233/ulrich-rieke/cpp/ch-2.cpp b/challenge-233/ulrich-rieke/cpp/ch-2.cpp new file mode 100755 index 0000000000..d0590e6bd7 --- /dev/null +++ b/challenge-233/ulrich-rieke/cpp/ch-2.cpp @@ -0,0 +1,69 @@ +#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 ; +} + +bool compare( const std::pair & a , const std::pair + & b ) { + if ( a.second != b.second ) { + return a.second < b.second ; + } + else { + return a.first > b.first ; + } +} + +int main( ) { + std::cout << "Please enter some integers, separated by blanks!\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::map frequencies ; + for ( int i : numbers ) { + if ( frequencies.find( i ) == frequencies.end( ) ) { + frequencies[ i ] = std::count( numbers.begin( ) , numbers.end( ) , + i ) ; + } + } + std::vector> allPairs( frequencies.begin( ) , + frequencies.end( ) ) ; + std::sort( allPairs.begin( ) , allPairs.end( ) , compare ) ; + std::vector result ; + for ( auto p : allPairs ) { + for ( int i = 0 ; i < p.second ; i++ ) { + result.push_back( p.first ) ; + } + } + std::cout << "(" ; + int count = 0 ; + int size = result.size( ) ; + for ( int i : result ) { + std::cout << i ; + count++ ; + if ( count < size ) { + std::cout << " , " ; + } + else { + std::cout << ")\n" ; + } + } + return 0 ; +} diff --git a/challenge-233/ulrich-rieke/haskell/ch-1.hs b/challenge-233/ulrich-rieke/haskell/ch-1.hs new file mode 100755 index 0000000000..f5ac2cebab --- /dev/null +++ b/challenge-233/ulrich-rieke/haskell/ch-1.hs @@ -0,0 +1,27 @@ +module Challenge233 + where +import qualified Data.Set as S +import Data.List ( group , sort ) + +findPairs :: Int -> Int +findPairs n + |n == 0 = 0 + |n == 1 = 0 + |n == 2 = 1 + |n > 2 = myCombis n + where + myCombis :: Int -> Int + myCombis d = div ( product [1..d] ) ( product [1..d - 2] * 2 ) + +findUniqueLetterCombis :: [String] -> [String] +findUniqueLetterCombis = map ( sort . S.toList . S.fromList ) + +solution :: String -> Int +solution s = sum $ map ( findPairs . length ) $ group $ sort $ + findUniqueLetterCombis ( words s ) + +main :: IO ( ) +main = do + putStrLn "Enter some words, separated by spaces!" + allWords <- getLine + print $ solution allWords diff --git a/challenge-233/ulrich-rieke/haskell/ch-2.hs b/challenge-233/ulrich-rieke/haskell/ch-2.hs new file mode 100755 index 0000000000..1f64ad66ac --- /dev/null +++ b/challenge-233/ulrich-rieke/haskell/ch-2.hs @@ -0,0 +1,18 @@ +module Challenge233_2 + where +import Data.List ( sortBy , sort , group ) +import Data.Function ( on ) + +solution :: [Int] -> [Int] +solution = concat .sortBy mySorter . group . sort + where + mySorter :: [Int] -> [Int] -> Ordering + mySorter l1 l2 + |length l1 /= length l2 = ( compare `on` length ) l1 l2 + |otherwise = if ( compare `on` head ) l1 l2 == GT then LT else GT + +main = do + putStrLn "Please enter some integers, separated by blanks!" + numberstrings <- getLine + let numbers = map read $ words numberstrings + print $ solution numbers diff --git a/challenge-233/ulrich-rieke/perl/ch-1.pl b/challenge-233/ulrich-rieke/perl/ch-1.pl new file mode 100755 index 0000000000..0a4e32637f --- /dev/null +++ b/challenge-233/ulrich-rieke/perl/ch-1.pl @@ -0,0 +1,53 @@ +#!/usr/bin/perl ; +use strict ; +use warnings ; +use feature 'say' ; +use List::Util qw ( product ) ; + +#given a number of words with the same numbers, how many pairs can be +#created ? +sub pairnumber { + my $n = shift ; + my $result ; + if ( $n == 0 || $n == 1 ) { + $result = 0 ; + } + elsif ( $n == 2 ) { + $result = 1 ; + } + else { + $result = myCombinations( $n ) ;# compute the number of pairwise combis + } + return $result ; +} + +#if the number of the same sequences of letters is greater than 2 +#the number of pair combinations is given by n! / ((n - 2 )! * 2 ) +sub myCombinations { + my $num = shift ; + my $numerator = product( 1..$num) ; + my $denom = product( 1..($num - 2 ) ) ; + return $numerator / ( $denom * 2 ) ; +} + +say "Enter some words, separated by blanks!" ; +my $line = ; +chomp $line ; +my @words = split( /\s/ , $line ) ; +my %uniques ;#unique letters per entered word +my %lettercombis ; #all different unique letter combinations +for my $word ( @words ) { + my @letters = split( '' , $word ) ; + for my $l ( @letters ) { + $uniques{ $l }++ ;#find out the unique letters + } +#the combination of unique letters per word is sorted , then added +#to the hash with the different letter combinations + $lettercombis{ join( '' , sort keys %uniques ) }++ ; + %uniques = ( ) ;#we must unset the letter hash for the next word +} +my $totalpairs = 0 ; +for my $v( values %lettercombis ) { + $totalpairs += pairnumber( $v ) ; +} +say $totalpairs ; diff --git a/challenge-233/ulrich-rieke/perl/ch-2.pl b/challenge-233/ulrich-rieke/perl/ch-2.pl new file mode 100755 index 0000000000..0cdedd089f --- /dev/null +++ b/challenge-233/ulrich-rieke/perl/ch-2.pl @@ -0,0 +1,24 @@ +#!/usr/bin/perl ; +use strict ; +use warnings ; +use feature 'say' ; + +sub count { + my $array = shift ; + my $element = shift ; + return scalar( grep { $_ == $element } @$array ) ; +} + +say "Enter some integers, separated by blanks!" ; +my $line = ; +chomp $line ; +my @numbers = split( /\s/ , $line ) ; +my %frequencies ; +for my $num ( @numbers ) { + unless ( exists($frequencies{ $num }) ) { + $frequencies{ $num } = count( \@numbers , $num ) ; + } +} +my @sorted = sort { $frequencies{ $a } <=> $frequencies{ $b } || + $b <=> $a } @numbers ; +say "(" . join( ',' , @sorted ) . ")" ; diff --git a/challenge-233/ulrich-rieke/raku/ch-1.raku b/challenge-233/ulrich-rieke/raku/ch-1.raku new file mode 100755 index 0000000000..63d66b9819 --- /dev/null +++ b/challenge-233/ulrich-rieke/raku/ch-1.raku @@ -0,0 +1,39 @@ +use v6 ; + +#how many pairs can be formed if $n is the number of words with the same +#letters ? +sub pairs( $n is copy ) { + my $result ; + if ( $n == 0 || $n == 1 ) { + $result = 0 ; + } + if ( $n == 2 ) { + $result = 1 ; + } + if ( $n > 2 ) { + $result = findCombinations( $n ) ; + } + return $result ; +} + +#if $n is greater than 2 then the number of combinations is +#$n!/ (($n - 2 )! * 2) +sub findCombinations( $num is copy ) { + my $numerator = [*](1..$num) ; + my $denom = [*]( 1..($num - 2)) ; + return $numerator div ( $denom * 2 ) ; +} + +say "Enter some words, separated by blanks!" ; +my $line = $*IN.get ; +my @words = $line.words ; +my @letters_in_word = @words.map( {$_.comb.unique.sort} ) ;#unique letters +my %frequencies ; +for @letters_in_word -> $word { + %frequencies{ $word }++ ;#find the number of unique combination per combi +} +my $pairnumber = 0 ; +for %frequencies.values -> $v { + $pairnumber += pairs( $v ) ; +} +say $pairnumber ; diff --git a/challenge-233/ulrich-rieke/raku/ch-2.raku b/challenge-233/ulrich-rieke/raku/ch-2.raku new file mode 100755 index 0000000000..77f60ca899 --- /dev/null +++ b/challenge-233/ulrich-rieke/raku/ch-2.raku @@ -0,0 +1,23 @@ +use v6 ; + +sub count( @array , $element ) { + return @array.grep( {$_ == $element} ).elems ; +} + +say "Enter some integers, separated by blanks!" ; +my $line = $*IN.get ; +my @numbers = $line.words.map( {.Int} ) ; +my @uniques = @numbers.unique ; +my %frequencies ; +for @uniques -> $num { + %frequencies{ $num } = count( @numbers , $num ) ; +} +my @sorted = @uniques.sort( { %frequencies{$^a} <=> %frequencies{$^b} || + $^b <=> $^a } ) ; +my @result ; +for @sorted -> $num { + for (1..%frequencies{ $num } ) { + @result.push( $num ) ; + } +} +say "(" ~ @result.join( ',' ) ~ ")" ; diff --git a/challenge-233/ulrich-rieke/rust/ch-1.rs b/challenge-233/ulrich-rieke/rust/ch-1.rs new file mode 100755 index 0000000000..338d4baa84 --- /dev/null +++ b/challenge-233/ulrich-rieke/rust/ch-1.rs @@ -0,0 +1,66 @@ +use std::io ; +use std::collections::{HashMap , HashSet} ; + +fn pair_number( number : u32 ) -> u32 { + let pairs : u32 = match number { + inner if inner == 0 => 0 , + inner if inner == 1 => 0 , + inner if inner == 2 => 1 , + inner if inner > 2 => combinations( inner ) , + _ => 0 , + } ; + pairs +} + +fn combinations( num : u32 ) -> u32 { + let mut numbers : Vec = Vec::new( ) ; + for n in 1..=num { + numbers.push( n ) ; + } + let numerator : u32 = numbers.iter( ).product( ) ; + numbers.clear( ) ; + for n in 1..=(num - 2 ) { + numbers.push( n ) ; + } + let denom_fst : u32 = numbers.iter( ).product( ) ; + numerator / ( denom_fst * 2 ) +} + +fn unique_letters( word : &str ) -> String { + let mut letters : HashSet = HashSet::new( ) ; + for c in word.chars( ) { + letters.insert( c ) ; + } + let mut final_string : String = String::new( ) ; + let mut vec_letters : Vec = Vec::new( ) ; + for c in letters { + vec_letters.push( c ) ; + } + vec_letters.sort( ) ; + for c in vec_letters { + final_string.push( c ) ; + } + final_string +} + +fn main() { + println!("Enter some words, separated by blanks!"); + let mut inline : String = String::new( ) ; + io::stdin( ).read_line( &mut inline ).unwrap( ) ; + let entered_line : &str = &*inline ; + let words : Vec<&str> = entered_line.split_whitespace( ).map( | s | + s.trim( ) ).collect( ) ; + let mut strings : Vec = Vec::new( ) ; + words.iter( ).map( | w | unique_letters( & w )).for_each( | res | + strings.push( res ) ) ; + let mut letter_combis : HashMap = HashMap::new( ) ; + for s in &strings { + let value = letter_combis.entry( s.to_string( ) ).or_default( ) ; + *value += 1 ; + } + let mut pairs : u32 = 0 ; + for (_ , v ) in letter_combis { + pairs += pair_number( v ) ; + } + println!("{}" , pairs ) ; +} diff --git a/challenge-233/ulrich-rieke/rust/ch-2.rs b/challenge-233/ulrich-rieke/rust/ch-2.rs new file mode 100755 index 0000000000..31cad09af7 --- /dev/null +++ b/challenge-233/ulrich-rieke/rust/ch-2.rs @@ -0,0 +1,36 @@ +use std::io ; +use std::collections::HashSet ; + +fn main() { + println!("Enter some integers, separated by blanks!"); + let mut inline : String = String::new( ) ; + io::stdin( ).read_line( &mut inline ).unwrap( ) ; + let entered_line : &str = &*inline ; + let numbers : Vec = entered_line.split_whitespace( ).map( | s | + s.trim( ).parse::( ).unwrap( )).collect( ) ; + let mut nums : HashSet = HashSet::new( ) ; + for n in &numbers { + nums.insert( *n ) ; + } + let mut frequencies : Vec<(i32, usize)> = Vec::new( ) ; + for n in &nums { + let ct = numbers.iter( ).filter( | d | *d == n ).count( ) ; + frequencies.push( (*n , ct ) ) ; + } + let freqs = frequencies.as_mut_slice( ) ; + freqs.sort_by( | a , b | { + if a.1 != b.1 { + a.1.cmp( &b.1 ) + } + else { + b.0.cmp( &a.0 ) + } + }) ; + let mut output : Vec = Vec::new( ) ; + for pair in freqs { + for _ in 0..pair.1 { + output.push( pair.0 ) ; + } + } + println!("{:?}" , output ) ; +} diff --git a/challenge-233/wanderdoc/perl/ch-1.pl b/challenge-233/wanderdoc/perl/ch-1.pl new file mode 100755 index 0000000000..4776d68a64 --- /dev/null +++ b/challenge-233/wanderdoc/perl/ch-1.pl @@ -0,0 +1,79 @@ +#!perl +use strict; +use warnings FATAL => qw(all); + +=prompt +You are given an array of words made up of alphabets only. Write a script to find the number of pairs of similar words. Two words are similar if they consist of the same characters. + +Example 1 Input: @words = ("aba", "aabb", "abcd", "bac", "aabc") + +Output: 2 +Pair 1: similar words ("aba", "aabb") +Pair 2: similar words ("bac", "aabc") + +Example 2 + +Input: @words = ("aabb", "ab", "ba") +Output: 3 +Pair 1: similar words ("aabb", "ab") +Pair 2: similar words ("aabb", "ba") +Pair 3: similar words ("ab", "ba") + +Example 3 + +Input: @words = ("nba", "cba", "dba") +Output: 0 + +=cut + +use Algorithm::Combinatorics qw(combinations); +use Test2::V0; + + + + + + + + + + +sub word_letters +{ + my $str = $_[0]; + my %uniq; + my $ltr = join('', sort { $a cmp $b } + grep { ! $uniq{$_}++ } split(//,$str)); + return $ltr; +} + +sub find_pairs +{ + my @wds = @_; + + my %similar; + for my $wd ( @wds ) + { + push @{$similar{ word_letters($wd) }}, $wd; + } + + my @output; + for my $key ( keys %similar ) + { + next if scalar @{$similar{$key}} < 2; + my $iter = combinations(\@{$similar{$key}}, 2); + while (my $p = $iter->next) + { + push @output, $p; # print "@$p$/"; + } + } + @output = sort { $a->[0] cmp $b->[0] } @output; # for Test2::V0 only. + if ( scalar @output > 0 ) { return [@output];} + else { return 0;} +} + +# find_pairs("nba", "cba", "dba"); +is (find_pairs("aba", "aabb", "abcd", "bac", "aabc"), [['aba', 'aabb'], ['bac', 'aabc']], 'Example 1'); +is (find_pairs("aabb", "ab", "ba"), [['aabb', 'ab'], ['aabb', 'ba'], ['ab', 'ba']], 'Example 2'); +is (find_pairs("nba", "cba", "dba"), 0, 'Example 3'); +done_testing(); \ No newline at end of file diff --git a/challenge-233/wanderdoc/perl/ch-2.pl b/challenge-233/wanderdoc/perl/ch-2.pl new file mode 100755 index 0000000000..b9977b29d7 --- /dev/null +++ b/challenge-233/wanderdoc/perl/ch-2.pl @@ -0,0 +1,33 @@ +#!perl +use strict; +use warnings FATAL => qw(all); + +=prompt +You are given an array of integers. Write a script to sort the given array in increasing order based on the frequency of the values. If multiple values have the same frequency then sort them in decreasing order. +Example 1 Input: @ints = (1,1,2,2,2,3) Ouput: (3,1,1,2,2,2) +'3' has a frequency of 1 +'1' has a frequency of 2 +'2' has a frequency of 3 + +Example 2 Input: @ints = (2,3,1,3,2) Ouput: (1,3,3,2,2) + +'2' and '3' both have a frequency of 2, so they are sorted in decreasing order. + +Example 3 Input: @ints = (-1,1,-6,4,5,-6,1,4,1) Ouput: (5,-1,4,4,-6,-6,1,1,1) +=cut + +use Test2::V0; + +sub frequency_sort +{ + my @arr = @_; + my %freq; + $freq{$_}++ for @arr; + @arr = sort { $freq{$a} <=> $freq{$b} || $b <=> $a } @arr; + return @arr; +} + +is([frequency_sort(1,1,2,2,2,3)], [3,1,1,2,2,2], 'Example 1'); +is([frequency_sort(2,3,1,3,2)], [1,3,3,2,2], 'Example 2'); +is([frequency_sort(-1,1,-6,4,5,-6,1,4,1)], [5,-1,4,4,-6,-6,1,1,1], 'Example 3'); +done_testing(); \ No newline at end of file diff --git a/members.json b/members.json index e018ed16e7..45fd40038a 100644 --- a/members.json +++ b/members.json @@ -154,6 +154,7 @@ "luiz-felipe" : "Luiz Felipe", "manfredi" : "Leo Manfredi", "massa" : "Humberto Massa", + "mauke" : "mauke", "miguel-prz" : "Miguel Prz", "lubos-kolouch" : "Lubos Kolouch", "luca-ferrari" : "Luca Ferrari", diff --git a/stats/pwc-challenge-231.json b/stats/pwc-challenge-231.json new file mode 100644 index 0000000000..4bbef37955 --- /dev/null +++ b/stats/pwc-challenge-231.json @@ -0,0 +1,847 @@ +{ + "drilldown" : { + "series" : [ + { + "id" : "Adam Russell", + "name" : "Adam Russell", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 2 + ] + ] + }, + { + "id" : "Ali Moradi", + "name" : "Ali Moradi", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 2 + ], + [ + "Blog", + 1 + ] + ] + }, + { + "name" : "Andreas Voegele", + "id" : "Andreas Voegele", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 1 + ] + ] + }, + { + "data" : [ + [ + "Raku", + 2 + ], + [ + "Blog", + 1 + ] + ], + "id" : "Andrew Shitov", + "name" : "Andrew Shitov" + }, + { + "name" : "Arne Sommer", + "id" : "Arne Sommer", + "data" : [ + [ + "Raku", + 2 + ], + [ + "Blog", + 1 + ] + ] + }, + { + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 2 + ] + ], + "id" : "Athanasius", + "name" : "Athanasius" + }, + { + "name" : "Avery Adams", + "id" : "Avery Adams", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 2 + ] + ] + }, + { + "name" : "BarrOff", + "id" : "BarrOff", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 2 + ] + ] + }, + { + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 1 + ] + ], + "id" : "Bob Lied", + "name" : "Bob Lied" + }, + { + "id" : "Bruce Gray", + "name" : "Bruce Gray", + "data" : [ + [ + "Raku", + 2 + ] + ] + }, + { + "id" : "Cheok-Yin Fung", + "name" : "Cheok-Yin Fung", + "data" : [ + [ + "Perl", + 2 + ] + ] + }, + { + "data" : [ + [ + "Raku", + 2 + ] + ], + "id" : "Clifton Wood", + "name" : "Clifton Wood" + }, + { + "data" : [ + [ + "Perl", + 2 + ], + [ + "Blog", + 1 + ] + ], + "name" : "Dave Jacoby", + "id" : "Dave Jacoby" + }, + { + "id" : "David Ferrone", + "name" : "David Ferrone", + "data" : [ + [ + "Perl", + 2 + ] + ] + }, + { + "data" : [ + [ + "Perl", + 2 + ] + ], + "name" : "E. Choroba", + "id" :