diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2023-09-11 02:42:56 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2023-09-11 02:42:56 +0100 |
| commit | a9f8285c31f7b7fb2caf114fd0355023f0e52b91 (patch) | |
| tree | 8b9efd39b9f2272f192263b95d431ef0911c9bcd | |
| parent | 42a5fd82138a8ca1223468318854075eee783248 (diff) | |
| download | perlweeklychallenge-club-a9f8285c31f7b7fb2caf114fd0355023f0e52b91.tar.gz perlweeklychallenge-club-a9f8285c31f7b7fb2caf114fd0355023f0e52b91.tar.bz2 perlweeklychallenge-club-a9f8285c31f7b7fb2caf114fd0355023f0e52b91.zip | |
- 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.
53 files changed, 4253 insertions, 2560 deletions
diff --git a/challenge-231/massa/raku/ch-01.raku b/challenge-231/massa/raku/ch-1.raku index e4a1b5befb..e4a1b5befb 100644 --- a/challenge-231/massa/raku/ch-01.raku +++ b/challenge-231/massa/raku/ch-1.raku diff --git a/challenge-231/massa/raku/ch-02.raku b/challenge-231/massa/raku/ch-2.raku index 8587a2e519..8587a2e519 100644 --- a/challenge-231/massa/raku/ch-02.raku +++ b/challenge-231/massa/raku/ch-2.raku 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 ([<aba aabb abcd bac aabc>], + [<aabb ab ba>], [<nba cba dba>]) { + 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 <aba aabb abcd bac aabc>, <aabb ab ba>, + <nba cba dba> -> @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-1.raku index b9bdc1f707..b9bdc1f707 100644 --- a/challenge-233/massa/raku/ch-01.raku +++ b/challenge-233/massa/raku/ch-1.raku diff --git a/challenge-233/massa/raku/ch-02.raku b/challenge-233/massa/raku/ch-2.raku index c4d2e678fc..c4d2e678fc 100644 --- a/challenge-233/massa/raku/ch-02.raku +++ b/challenge-233/massa/raku/ch-2.raku diff --git a/challenge-233/perlboy1967/ch1.pl b/challenge-233/perlboy1967/perl/ch-1.pl index 3b985c029c..3b985c029c 100755 --- a/challenge-233/perlboy1967/ch1.pl +++ b/challenge-233/perlboy1967/perl/ch-1.pl diff --git a/challenge-233/perlboy1967/ch2.pl b/challenge-233/perlboy1967/perl/ch-2.pl index e341e40365..e341e40365 100755 --- a/challenge-233/perlboy1967/ch2.pl +++ b/challenge-233/perlboy1967/perl/ch-2.pl 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-1.js index dd1b3d3d04..dd1b3d3d04 100644 --- a/challenge-233/steven-wilson/javascript/ch-01.js +++ b/challenge-233/steven-wilson/javascript/ch-1.js diff --git a/challenge-233/steven-wilson/javascript/ch-02.js b/challenge-233/steven-wilson/javascript/ch-2.js index fac9e2d0f2..fac9e2d0f2 100644 --- a/challenge-233/steven-wilson/javascript/ch-02.js +++ b/challenge-233/steven-wilson/javascript/ch-2.js diff --git a/challenge-233/steven-wilson/python/ch-01.py b/challenge-233/steven-wilson/python/ch-1.py index 6d599f2e3d..6d599f2e3d 100644 --- a/challenge-233/steven-wilson/python/ch-01.py +++ b/challenge-233/steven-wilson/python/ch-1.py diff --git a/challenge-233/steven-wilson/python/ch-02.py b/challenge-233/steven-wilson/python/ch-2.py index 7bd69c1e7e..7bd69c1e7e 100644 --- a/challenge-233/steven-wilson/python/ch-02.py +++ b/challenge-233/steven-wilson/python/ch-2.py 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 <vector>
+#include <iostream>
+#include <string>
+#include <map>
+#include <algorithm>
+#include <numeric>
+#include <set>
+
+std::vector<std::string> split( const std::string & startline ,
+ const std::string & sep ) {
+ std::vector<std::string> 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<int> numers( n ) ;
+ std::iota( numers.begin( ) , numers.end( ) , 1 ) ;
+ int numerator = std::accumulate( numers.begin( ) , numers.end( ) , 1 ,
+ std::multiplies<int>( ) ) ;
+ std::vector<int> denoms( n - 2 ) ;
+ std::iota( denoms.begin( ) , denoms.end( ) , 1 ) ;
+ int denominator = std::accumulate( denoms.begin( ) , denoms.end( ) , 1 ,
+ std::multiplies<int>( ) ) ;
+ 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 l |
