diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2023-09-17 23:51:03 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2023-09-17 23:51:03 +0100 |
| commit | d70c6af225f654081884e720449f92c0f52514b1 (patch) | |
| tree | dbf6a98e554dba8490951d474ad38db0ff61f4d2 | |
| parent | 331c5346d87f3131e7480194e98b570d7ff3d6de (diff) | |
| download | perlweeklychallenge-club-d70c6af225f654081884e720449f92c0f52514b1.tar.gz perlweeklychallenge-club-d70c6af225f654081884e720449f92c0f52514b1.tar.bz2 perlweeklychallenge-club-d70c6af225f654081884e720449f92c0f52514b1.zip | |
- Added solutions by Laurent Rosenfeld.
- Added solutions by Ulrich Rieke.
- Added solutions by Robert DiCicco.
- Added solutions by W. Luis Mochan.
- Added solutions by Kjetil Skotheim.
- Added solutions by Humberto Massa.
- Added solutions by Thomas Kohler.
- Added solutions by Packy Anderson.
- Added solutions by rcmlz.
- Added solutions by PokGoPun.
- Added solutions by David Ferrone.
- Added solutions by Mark Anderson.
- Added solutions by Lubos Kolouch.
- Added solutions by E. Choroba.
- Added solutions by Robbie Hatley.
- Added solutions by Peter Meszaros.
- Added solutions by Peter Campbell Smith.
- Added solutions by Dave Jacoby.
- Added solutions by Avery Adams.
- Added solutions by Steven Wilson.
- Added solutions by Ali Moradi.
- Added solutions by Jorg Sommrey.
- Added solutions by Bob Lied.
- Added solutions by Matthew Neleigh.
- Added solutions by Matthias Muth.
- Added solutions by Stephen G. Lynn.
- Added solutions by Bruce Gray.
- Added solutions by Cheok-Yin Fung.
- Added solutions by Jan Krnavek.
- Added solutions by Arne Sommer.
- Added solutions by Athanasius.
- Added solutions by Simon Green.
- Added solutions by Luca Ferrari.
- Added solutions by Solathian.
- Added solutions by BarrOff.
- Added solutions by Robert Ransbottom.
47 files changed, 4551 insertions, 2958 deletions
diff --git a/challenge-234/avery-adams/blog.txt b/challenge-234/avery-adams/blog.txt new file mode 100644 index 0000000000..41ec11c849 --- /dev/null +++ b/challenge-234/avery-adams/blog.txt @@ -0,0 +1 @@ +https://dev.to/oldtechaa/perl-weekly-challenge-234-sharing-is-caring-98k diff --git a/challenge-234/avery-adams/blogs.txt b/challenge-234/avery-adams/blog1.txt index 3d2b572d46..acc1f85cac 100644 --- a/challenge-234/avery-adams/blogs.txt +++ b/challenge-234/avery-adams/blog1.txt @@ -1,2 +1 @@ -https://dev.to/oldtechaa/perl-weekly-challenge-234-sharing-is-caring-98k https://blogs.perl.org/users/oldtechaa/2023/09/perl-weekly-challenge-234---sharing-is-caring.html diff --git a/challenge-234/eric-cheung/python/ch-1.py b/challenge-234/eric-cheung/python/ch-1.py new file mode 100755 index 0000000000..b426840cc6 --- /dev/null +++ b/challenge-234/eric-cheung/python/ch-1.py @@ -0,0 +1,23 @@ +
+arrWords = ["java", "javascript", "julia"] ## Example 1
+## arrWords = ["bella", "label", "roller"] ## Example 2
+## arrWords = ["cool", "lock", "cook"] ## Example 3
+
+arrUniqChar = list(set([*arrWords[0]]))
+arrOutput = []
+
+for charLoop in arrUniqChar:
+ bExist = True
+ nNumCount = arrWords[0].count(charLoop)
+
+ for wordLoop in arrWords[1:]:
+ if charLoop in wordLoop:
+ nNumCount = min(nNumCount, wordLoop.count(charLoop))
+ else:
+ bExist = False
+ break
+
+ if bExist:
+ arrOutput = arrOutput + [charLoop] * nNumCount
+
+print (arrOutput)
diff --git a/challenge-234/eric-cheung/python/ch-2.py b/challenge-234/eric-cheung/python/ch-2.py new file mode 100755 index 0000000000..356ebee5c4 --- /dev/null +++ b/challenge-234/eric-cheung/python/ch-2.py @@ -0,0 +1,28 @@ +
+import sys
+from itertools import combinations
+import numpy as np
+
+nListCount = 3
+
+arrInput = [4, 4, 2, 4, 3] ## Example 1
+## arrInput = [1, 1, 1, 1, 1] ## Example 2
+## arrInput = [4, 7, 1, 10, 7, 4, 1, 1] ## Example 3
+
+arrUniq = list(set(arrInput))
+
+## print (arrUniq)
+
+if len(arrUniq) < nListCount:
+ print (0)
+ sys.exit()
+
+nNumCount = 0
+
+arrCombList = combinations(arrUniq, nListCount)
+
+for arrCombLoop in list(arrCombList):
+ nProduct = np.prod([arrInput.count(arrCombLoop[nLoop]) for nLoop in range(nListCount)])
+ nNumCount = nNumCount + nProduct
+
+print (nNumCount)
diff --git a/challenge-234/laurent-rosenfeld/blog.txt b/challenge-234/laurent-rosenfeld/blog.txt new file mode 100644 index 0000000000..453be158f0 --- /dev/null +++ b/challenge-234/laurent-rosenfeld/blog.txt @@ -0,0 +1 @@ +https://blogs.perl.org/users/laurent_r/2023/09/perl-weekly-challenge-234-common-characters.html diff --git a/challenge-234/laurent-rosenfeld/perl/ch-1.pl b/challenge-234/laurent-rosenfeld/perl/ch-1.pl new file mode 100644 index 0000000000..9fc9c07266 --- /dev/null +++ b/challenge-234/laurent-rosenfeld/perl/ch-1.pl @@ -0,0 +1,21 @@ +use strict; +use warnings; +use feature 'say'; + +sub common_chars { + my $count = scalar @_; + my @letters = map { [ split // ] } @_; + my %histo; # letter histogram + for my $w_ref (@letters) { + my %unique = map { $_ => 1 } @$w_ref; + $histo{$_}++ for keys %unique; + } + my @result = grep { $histo{$_} == $count } keys %histo; + return "@result"; +} + +for my $test ([<java javascript julia>], + [<bella label roller>], [<cool lock cook>]) { + printf "%-25s => ", "@$test"; + say common_chars @$test; +} diff --git a/challenge-234/laurent-rosenfeld/raku/ch-1.raku b/challenge-234/laurent-rosenfeld/raku/ch-1.raku new file mode 100644 index 0000000000..7eea072454 --- /dev/null +++ b/challenge-234/laurent-rosenfeld/raku/ch-1.raku @@ -0,0 +1,10 @@ +sub common-chars (@in) { + my @letters = map { .comb }, @in; + return ~ ∩ @letters; +} + +for <java javascript julia>, <bella label roller>, + <cool lock cook> -> @test { + printf "%-25s => ", "@test[]"; + say common-chars @test; +} diff --git a/challenge-234/rcmlz/raku/ch-1.raku b/challenge-234/rcmlz/raku/ch-1.raku new file mode 100644 index 0000000000..f44dc14b8c --- /dev/null +++ b/challenge-234/rcmlz/raku/ch-1.raku @@ -0,0 +1,14 @@ +unit module challenge-nr234::rcmlz::raku::task-one:ver<0.0.1>:auth<rcmlz@github.com)>; + +# run in terminal: raku --optimize=3 -I challenge-nr234/rcmlz/raku/ -- test/challenge-nr234/raku/task-one.rakutest +# raku --optimize=3 -I challenge-nr234 -- test/benchmark-scalabiity.raku --task=task-one --user=rcmlz --max-run-times=1,5,7 --test-before-benchmark=True --out-folder=/tmp nr234; cat /tmp/nr234_task-one.csv + +#|[ +You are given an array of words made up of characters only. + +- Write a script to return all alphabetic characters that show up in all words +- including duplicates. +] +our sub solution(@input) is export { + @input.map( *.comb.Bag ).reduce(&infix:<∩>).kxxv; +}
\ No newline at end of file diff --git a/challenge-234/rcmlz/raku/ch-2.raku b/challenge-234/rcmlz/raku/ch-2.raku new file mode 100644 index 0000000000..0ee6ab4d3e --- /dev/null +++ b/challenge-234/rcmlz/raku/ch-2.raku @@ -0,0 +1,14 @@ +unit module challenge-nr233::rcmlz::raku::task-two:ver<0.0.1>:auth<rcmlz@github.com)>; + +# run in terminal: raku --optimize=3 -I challenge-nr234/rcmlz/raku/ -- test/challenge-nr234/raku/task-two.rakutest +# raku --optimize=3 -I challenge-nr234 -- test/benchmark-scalabiity.raku --task=task-two --user=rcmlz --max-run-times=1,5,7 --test-before-benchmark=True --out-folder=/tmp nr234; cat /tmp/nr234_task-two.csv + +#|[ +You are given an array of positive integers. + +- Write a script to find the number of triplets (i, j, k) that satisfies +- num[i] != num[j], num[j] != num[k] and num[k] != num[i]. +] +our sub solution(@input) is export { + @input.Bag.combinations(3).map( *.map( *.value ).reduce(&infix:<*>)).sum +} diff --git a/challenge-234/robert-dicicco/julia/ch-1.jl b/challenge-234/robert-dicicco/julia/ch-1.jl new file mode 100644 index 0000000000..d1649c1402 --- /dev/null +++ b/challenge-234/robert-dicicco/julia/ch-1.jl @@ -0,0 +1,37 @@ +#!/usr/bin/env julia +#= +------------------------------------------------ +AUTHOR: Robert DiCicco +DATE : 2023-09-13 +Challenge 234 Task 1 Common Characters ( Julia ) +------------------------------------------------ +=# + +using Printf + +wordlist = [["java", "javascript", "julia"],["bella", "label", "roller"],["cool", "lock", "cook"]] + +function CreateSeen(w) + wordnum = length(w) + charset = join(w) + seen = Dict(map(k -> (k,0), 'a':'z')) + for x in charset + seen[x] += 1 + end + + @printf("Output: (") + for (key, val) in seen + if (val >= wordnum && val < (2*wordnum)) + @printf("%s ",key) + elseif (val == (2*wordnum)) + @printf("%s %s ",key,key) + end + end + println(")\n") +end + +for wds in wordlist + @printf("Input: %s\n",wds) + charset = "" + CreateSeen(wds) +end diff --git a/challenge-234/robert-dicicco/julia/ch-2.jl b/challenge-234/robert-dicicco/julia/ch-2.jl new file mode 100644 index 0000000000..3d453fb3c9 --- /dev/null +++ b/challenge-234/robert-dicicco/julia/ch-2.jl @@ -0,0 +1,42 @@ +#!/usr/bin/env julia +#= +----------------------------------- +AUTHOR: Robert DiCicco +DATE : 2023-09-14 +Challenge 234 Task 2 Unequal Triplets ( Julia ) +----------------------------------- +=# + +using Printf +using Combinatorics + +myints = [[4, 4, 2, 4, 3],[1, 1, 1, 1, 1],[4, 7, 1, 10, 7, 4, 1, 1]] + +for mints in myints + cnt = 0 + @printf("Input: @ints = %s\n",mints) + for res in combinations(mints, 3) + if ((res[1] != res[2]) && (res[2] != res[3]) && (res[1] != res[3])) + cnt += 1 + end + end + @printf("Output: %s\n\n",cnt) +end + +#= +----------------------------------- +SAMPLE OUTPUT +julia .\UnequalTriplets.jl + +Input: @ints = [4, 4, 2, 4, 3] +Output: 3 + +Input: @ints = [1, 1, 1, 1, 1] +Output: 0 + +Input: @ints = [4, 7, 1, 10, 7, 4, 1, 1] +Output: 28 +----------------------------------- +=# + + diff --git a/challenge-234/robert-dicicco/perl/ch-1.pl b/challenge-234/robert-dicicco/perl/ch-1.pl new file mode 100644 index 0000000000..ac4d392845 --- /dev/null +++ b/challenge-234/robert-dicicco/perl/ch-1.pl @@ -0,0 +1,58 @@ +#!/usr/bin/env perl +=begin comment +------------------------------------------------ +AUTHOR: Robert DiCicco +DATE : 2023-09-13 +Challenge 234 Task 1 Common Characters ( Perl ) +------------------------------------------------ +=cut +use v5.38; + +my @wordlist = (["java", "javascript", "julia"],["bella", "label", "roller"],["cool", "lock", "cook"]); + +for my $wds ( @wordlist) { + my $wordnum = scalar @$wds; + say "wordnum = $wordnum"; + my %seen = (); + my $charlist = ""; + say "Input: \@words = (@$wds)"; + my $cnt = 0; + while($cnt < 3) { + $charlist .= @$wds[$cnt]; + $cnt++; + } + my @arr = split("",$charlist); + for my $c ( @arr) { + $seen{$c} += 1; + } + print "Output: ("; + for my $key (keys %seen) { + if ($seen{$key} == ($wordnum * 2)) { + print "$key $key "; + } elsif (( $seen{$key} >= $wordnum) && ( $seen{$key} < ($wordnum * 2))) { + print "$key "; + } + } + print ")\n\n"; +} + +=begin comment +------------------------------------------------ +SAMPLE OUTPUT +perl .\CommonCharacters.pl + +wordnum = 3 +Input: @words = (java javascript julia) +Output: (a j ) + +wordnum = 3 +Input: @words = (bella label roller) +Output: (e l l ) + +wordnum = 3 +Input: @words = (cool lock cook) +Output: (o c ) +------------------------------------------------ +=cut + + diff --git a/challenge-234/robert-dicicco/perl/ch-2.pl b/challenge-234/robert-dicicco/perl/ch-2.pl new file mode 100644 index 0000000000..c1e11ed515 --- /dev/null +++ b/challenge-234/robert-dicicco/perl/ch-2.pl @@ -0,0 +1,43 @@ +#!/usr/bin/env perl +=begin comment +----------------------------------- +AUTHOR: Robert DiCicco +DATE : 2023-09-14 +Challenge 234 Task 2 Unequal Triplets ( Perl ) +----------------------------------- +=cut + +use v5.38; +use Algorithm::Combinatorics qw(permutations combinations); + +my @myints = ([4, 4, 2, 4, 3],[1, 1, 1, 1, 1],[4, 7, 1, 10, 7, 4, 1, 1]); + +foreach my $i (@myints) { + say "Input: \@ints = [@$i]"; + my $cnt = 0; + my $iter = combinations($i, 3); + while (my $c = $iter->next) { + if ((@$c[0] != @$c[1]) && (@$c[1] != @$c[2]) && (@$c[0] != @$c[2])){ + $cnt++; + } + } + say "Output: $cnt\n"; +} + +=begin comment +----------------------------------- +SAMPLE OUTPUT +perl .\UnequalTriplets.pl + +Input: @ints = [4 4 2 4 3] +Output: 3 + +Input: @ints = [1 1 1 1 1] +Output: 0 + +Input: @ints = [4 7 1 10 7 4 1 1] +Output: 28 +----------------------------------- +=cut + + diff --git a/challenge-234/robert-dicicco/python/ch-1.py b/challenge-234/robert-dicicco/python/ch-1.py new file mode 100644 index 0000000000..58459f254a --- /dev/null +++ b/challenge-234/robert-dicicco/python/ch-1.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python +''' +------------------------------------------------ +AUTHOR: Robert DiCicco +DATE : 2023-09-13 +Challenge 234 Task 1 Common Characters ( Python ) +------------------------------------------------ +''' + +wordlist = [["java", "javascript", "julia"],["bella", "label", "roller"],["cool", "lock", "cook"]] + +def CreateSeen (w): + seen = dict() + charset = "".join(w) + for x in charset: + seen[x] = 0 + for x in charset: + seen[x] += 1 + + print("Output: (", end="") + for key, value in seen.items(): + if value >= 3 and value < 6: + print(f"{key} ",end="") + elif value == 6: + print(f"{key} ", end="") + print(f"{key} ", end="") + print(")\n") + +for wds in wordlist: + print("Input: @wordlist = ",wds) + charset = "" + + wordnum = len(wds) + CreateSeen(wds) + +''' +------------------------------------------------ +SAMPLE OUTPUT +python .\CommonCharacters.py + +Input: @wordlist = ['java', 'javascript', 'julia'] +Output: (j a ) + +Input: @wordlist = ['bella', 'label', 'roller'] +Output: (e l l ) + +Input: @wordlist = ['cool', 'lock', 'cook'] +Output: (c o ) +------------------------------------------------ +''' + + + diff --git a/challenge-234/robert-dicicco/python/ch-2.py b/challenge-234/robert-dicicco/python/ch-2.py new file mode 100644 index 0000000000..de348ce1bf --- /dev/null +++ b/challenge-234/robert-dicicco/python/ch-2.py @@ -0,0 +1,35 @@ +#!/usr/bin/env python +''' +----------------------------------- +AUTHOR: Robert DiCicco +DATE : 2023-09-14 +Challenge 234 Task 2 Unequal Triplets ( Python ) +----------------------------------- +''' + +from itertools import combinations + +myints = [[4, 4, 2, 4, 3],[1, 1, 1, 1, 1],[4, 7, 1, 10, 7, 4, 1, 1]] + +for mints in myints: + cnt = 0 + print(f"Inputs: @ints = {mints}") + for res in (list(combinations(mints, 3))): + if ((res[0] != res[1]) and (res[1] != res[2]) and (res[0] != res[2])): + cnt += 1 + print(f"Output: {cnt}\n") + +# ----------------------------------- +# SAMPLE OUTPUT +# python .\UnequalTriplets.py + +# Inputs: @ints = [4, 4, 2, 4, 3] +# Output: 3 + +# Inputs: @ints = [1, 1, 1, 1, 1] +# Output: 0 + +# Inputs: @ints = [4, 7, 1, 10, 7, 4, 1, 1] +# Output: 28 +# ---------------------------------- + diff --git a/challenge-234/robert-dicicco/raku/ch-1.raku b/challenge-234/robert-dicicco/raku/ch-1.raku new file mode 100644 index 0000000000..6006032c51 --- /dev/null +++ b/challenge-234/robert-dicicco/raku/ch-1.raku @@ -0,0 +1,57 @@ +#!/usr/bin/env raku +=begin comment +------------------------------------------------ +AUTHOR: Robert DiCicco +DATE : 2023-09-13 +Challenge 234 Task 1 Common Characters ( Raku ) +------------------------------------------------ +=end comment + +use v6; + +my @wordlist = (["java", "javascript", "julia"],["bella", "label", "roller"],["cool", "lock", "cook"]); + +for (@wordlist) -> @wds { + my $wordnum = @wds.elems; + my %seen = (); + my $charlist = ""; + say "Input: \@words = ",@wds; + my $cnt = 0; + while $cnt < 3 { + $charlist ~= @wds[$cnt]; + $cnt++; + } + my @arr = split("",$charlist); + for ( @arr) -> $c { + %seen{$c} += 1; + } + + print "Output: ("; + for %seen.keys -> $k { + if $k ne ' ' { + if %seen{$k} >= $wordnum && %seen{$k} < (2 * $wordnum) { + print "$k "; + } elsif %seen{$k} == (2 * $wordnum) { + print "$k $k " + } + } + } + print ")\n\n"; +} + +=begin comment +------------------------------------------------ +SAMPLE OUTPUT +raku .\CommonCharacters.rk +Input: @words = [java javascript julia] +Output: (a j ) + +Input: @words = [bella label roller] +Output: (e l l ) + +Input: @words = [cool lock cook] +Output: (o c ) +------------------------------------------------ +=end comment + + diff --git a/challenge-234/robert-dicicco/raku/ch-2.raku b/challenge-234/robert-dicicco/raku/ch-2.raku new file mode 100644 index 0000000000..d6021f4e38 --- /dev/null +++ b/challenge-234/robert-dicicco/raku/ch-2.raku @@ -0,0 +1,39 @@ +#!/usr/bin/env raku +=begin comment +----------------------------------- +AUTHOR: Robert DiCicco +DATE : 2023-09-14 +Challenge 234 Task 2 Unequal Triplets ( Raku ) +----------------------------------- +=end comment + +my @myints = ([4, 4, 2, 4, 3],[1, 1, 1, 1, 1],[4, 7, 1, 10, 7, 4, 1, 1]); +for (@myints) -> @ints { + my $cnt = 0; + say "Input: \@ints = ",@ints; + for @ints.combinations(3) -> @i { + if ((@i[0] != @i[1]) && (@i[1] != @i[2]) && (@i[0] != @i[2])) { + $cnt++; + } + } + say "Output: $cnt\n"; +} + +=begin comment +----------------------------------- +SAMPLE OUTPUT +raku .\UnequalTriplets.rk + +Input: @ints = [4 4 2 4 3] +Output: 3 + +Input: @ints = [1 1 1 1 1] +Output: 0 + +Input: @ints = [4 7 1 10 7 4 1 1] +Output: 28 +----------------------------------- +=end comment + + + diff --git a/challenge-234/robert-dicicco/ruby/ch-1.rb b/challenge-234/robert-dicicco/ruby/ch-1.rb new file mode 100644 index 0000000000..22bb18da5a --- /dev/null +++ b/challenge-234/robert-dicicco/ruby/ch-1.rb @@ -0,0 +1,55 @@ +#!/usr/bin/env ruby +=begin +------------------------------------------------ +AUTHOR: Robert DiCicco +DATE : 2023-09-13 +Challenge 234 Task 1 Common Characters ( Ruby ) +------------------------------------------------ +=end + +wordlist = [["java", "javascript", "julia"],["bella", "label", "roller"],["cool", "lock", "cook"]] + +wordlist.each do |wds| + charset = "" + seen = Hash.new + ("a".."z").each do |ch| + seen[ch] = 0 + end + puts("Input: @wordlist = #{wds}") + wordnum = wds.length + cnt = 0 + wds.each do |w| + charset += w + end + arr = charset.split("") + arr.each do |i| + seen[i] += 1 + end + + print("Output: (") + seen.each do |key, val| + if val >= wordnum && val < (2 * wordnum) + print("#{key} ") + elsif val == (2 * wordnum) + print("#{key} #{key} ") + end + end + puts(")\n\n") +end + +=begin +------------------------------------------------ +SAMPLE OUTPUT +ruby .\CommonCharacters.rb +Input: @wordlist = ["java", "javascript", "julia"] +Output: (a j ) + +Input: @wordlist = ["bella", "label", "roller"] +Output: (e l l ) + +Input: @wordlist = ["cool", "lock", "cook"] +Output: (c o ) +------------------------------------------------ +=end + + diff --git a/challenge-234/robert-dicicco/ruby/ch-2.rb b/challenge-234/robert-dicicco/ruby/ch-2.rb new file mode 100644 index 0000000000..be81b632d5 --- /dev/null +++ b/challenge-234/robert-dicicco/ruby/ch-2.rb @@ -0,0 +1,39 @@ +#!/usr/bin/env ruby +=begin +----------------------------------- +AUTHOR: Robert DiCicco +DATE : 2023-09-14 +Challenge 234 Task 2 Unequal Triplets ( Ruby ) +----------------------------------- +=end + +myints = [[4, 4, 2, 4, 3],[1, 1, 1, 1, 1],[4, 7, 1, 10, 7, 4, 1, 1]] + +myints.each do |mints| + cnt = 0 + puts("input: @ints = #{mints}") + ax = mints.combination(3).to_a + ax.each do |res| + if ((res[0] != res[1]) && (res[1] != res[2]) && (res[0] != res[2])) + cnt += 1 + end + end + puts("#{cnt}\n\n") +end + +=begin +----------------------------------- +SAMPLE OUTPUT +ruby .\UnequalTriplets.rb +input: @ints = [4, 4, 2, 4, 3] +3 + +input: @ints = [1, 1, 1, 1, 1] +0 + +input: @ints = [4, 7, 1, 10, 7, 4, 1, 1] +28 +----------------------------------- +=end + + diff --git a/challenge-234/robert-dicicco/tcl/ch-2.tcl b/challenge-234/robert-dicicco/tcl/ch-2.tcl new file mode 100644 index 0000000000..5773875c49 --- /dev/null +++ b/challenge-234/robert-dicicco/tcl/ch-2.tcl @@ -0,0 +1,60 @@ +#!/usr/bin/env tclsh +set comment { +----------------------------------- |
