From c2f37f15a35dc0146cc7cbd3f13f2f5fabed02a3 Mon Sep 17 00:00:00 2001 From: Luis Mochan Date: Mon, 11 Sep 2023 09:25:07 -0600 Subject: solve pwc234 --- challenge-234/wlmb/blog.txt | 1 + challenge-234/wlmb/perl/ch-1.pl | 27 +++++++++++++++++++++++++++ challenge-234/wlmb/perl/ch-2.pl | 21 +++++++++++++++++++++ 3 files changed, 49 insertions(+) create mode 100644 challenge-234/wlmb/blog.txt create mode 100755 challenge-234/wlmb/perl/ch-1.pl create mode 100755 challenge-234/wlmb/perl/ch-2.pl diff --git a/challenge-234/wlmb/blog.txt b/challenge-234/wlmb/blog.txt new file mode 100644 index 0000000000..38fcd698e6 --- /dev/null +++ b/challenge-234/wlmb/blog.txt @@ -0,0 +1 @@ +https://wlmb.github.io/2023/09/11/PWC234/ diff --git a/challenge-234/wlmb/perl/ch-1.pl b/challenge-234/wlmb/perl/ch-1.pl new file mode 100755 index 0000000000..1b38c6413b --- /dev/null +++ b/challenge-234/wlmb/perl/ch-1.pl @@ -0,0 +1,27 @@ +#!/usr/bin/env perl +# Perl weekly challenge 234 +# Task 1: Common Characters +# +# See https://wlmb.github.io/2023/09/11/PWC234/#task-1-common-characters +use v5.36; +use List::Util qw(min); +die <<~"FIN" unless @ARGV; + Usage: $0 W1 [W2...] + to find the common characters in all words W1, W2... + FIN +my @letters_of_word; +for(@ARGV){ + my %count_of_letter; + $count_of_letter{$_}++ for split ""; + push @letters_of_word, \%count_of_letter; +} +my $number_of_words=@letters_of_word; +my @result=map { + my $letter=$_; + my $repetition=min map { + my $word_number=$_; + $letters_of_word[$word_number]{$letter}//0 + } 0..$number_of_words-1; + ($letter) x $repetition; +} keys %{$letters_of_word[0]}; +say "@ARGV -> @result"; diff --git a/challenge-234/wlmb/perl/ch-2.pl b/challenge-234/wlmb/perl/ch-2.pl new file mode 100755 index 0000000000..1926cc7bd6 --- /dev/null +++ b/challenge-234/wlmb/perl/ch-2.pl @@ -0,0 +1,21 @@ +#!/usr/bin/env perl +# Perl weekly challenge 234 +# Task 2: Unequal Triplets +# +# See https://wlmb.github.io/2023/09/11/PWC234/#task-2-unequal-triplets +use v5.36; +use Algorithm::Combinatorics qw(combinations); +use List::Util qw(sum0 product); +die <<~"FIN" unless @ARGV; + Usage: $0 N1 [N2...] + to count the number of unequal triplets in the set {N1 N2...} + FIN +my %count_for_number; +++$count_for_number{$_} for @ARGV; +my @numbers=keys %count_for_number; +my $result=@numbers<3 + ? 0 + : sum0 map { + product @count_for_number{@$_} +} combinations(\@numbers,3); +say "@ARGV -> $result", -- cgit From ddd380af57253f10aa01735aeb560585e151696a Mon Sep 17 00:00:00 2001 From: Kjetil Skotheim Date: Mon, 11 Sep 2023 17:42:53 +0200 Subject: challenge-233-kjetillll --- challenge-233/kjetillll/perl/ch-1.pl | 51 ++++++++++++++++++++++++++++++++ challenge-233/kjetillll/perl/ch-2.pl | 35 ++++++++++++++++++++++ challenge-233/kjetillll/scala/ch-1.scala | 26 ++++++++++++++++ challenge-233/kjetillll/scala/ch-2.scala | 27 +++++++++++++++++ 4 files changed, 139 insertions(+) create mode 100644 challenge-233/kjetillll/perl/ch-1.pl create mode 100644 challenge-233/kjetillll/perl/ch-2.pl create mode 100644 challenge-233/kjetillll/scala/ch-1.scala create mode 100644 challenge-233/kjetillll/scala/ch-2.scala diff --git a/challenge-233/kjetillll/perl/ch-1.pl b/challenge-233/kjetillll/perl/ch-1.pl new file mode 100644 index 0000000000..58c23dba0f --- /dev/null +++ b/challenge-233/kjetillll/perl/ch-1.pl @@ -0,0 +1,51 @@ +# take words from command line arguments or use the test case here: + +my @words = @ARGV ? @ARGV: ("aba", "aabb", "abcd", "bac", "aabc"); + +print number_of_pairs_of_similar_but_not_equal_words(@words); +print "\n"; + + +sub number_of_pairs_of_similar_but_not_equal_words { + my $count = 0; + "@_" =~ / #regex searching all word pairs in stringified input words + (\b\w+\b) #word $1 + .* + (\b\w+\b) #word $2 + (??{ #means execute and try to match againts regexp-part in last statement + $count++ if similar($1,$2); #increase count if similar + "^"; #keep searching for ^ (beginning, which wont be found) till all pairs done for + }) + /x; #/regex/ modifier x ignores spaces, newlines and #comments + $count; #return count +} + +sub similar { + letters($_[0]) eq letters($_[1]) #similar + && + $_[0] ne $_[1] #but not equal +} + + +#helper function/normalizer +#returns string of distinct letters in alphabetical order +#abba becomes ab and challenge becomes aceghln +sub letters { + my %found; + for my $letter ( shift() =~ /[a-z]/gi ) { + $found{$letter}=1; + } + join '', sort keys %found; +} + +__END__ + +sub what_sane_people_would_do { + my $count = 1; + for my $f ( 0 .. $#_-1 ) { #index first word in pair + for my $s ( $f+1 .. $#_ ) { #index second word in pair + $count++ if similar( $_[$f], $_[$s] ); + } + } + return $count; +} diff --git a/challenge-233/kjetillll/perl/ch-2.pl b/challenge-233/kjetillll/perl/ch-2.pl new file mode 100644 index 0000000000..1a7d6d7c58 --- /dev/null +++ b/challenge-233/kjetillll/perl/ch-2.pl @@ -0,0 +1,35 @@ + +@ARGV ? print join(',', frequency_sort( @ARGV ))."\n" + : run_tests(); + +sub frequency_sort { + my %freq; + $freq{$_}++ for @_; #count the input ints + sort { #return sort of @_ by: + $freq{$a} <=> $freq{$b} #increasing frequency + or #or + $b <=> $a #decreasing int + } + @_; +} + +sub run_tests { + for( + { + Input => [1,1,2,2,2,3], + Output => [3,1,1,2,2,2] + }, + { + Input => [2,3,1,3,2], + Output => [1,3,3,2,2] + }, + { + Input => [-1,1,-6,4,5,-6,1,4,1], + Output => [5,-1,4,4,-6,-6,1,1,1] + } + ){ + my($input,$expected)=@$_{qw(Input Output)}; + my @got = frequency_sort( @$input ); + print "@got" eq "@$expected" ? 'ok' : '***ERROR', "\n"; + } +} diff --git a/challenge-233/kjetillll/scala/ch-1.scala b/challenge-233/kjetillll/scala/ch-1.scala new file mode 100644 index 0000000000..79982fe4dd --- /dev/null +++ b/challenge-233/kjetillll/scala/ch-1.scala @@ -0,0 +1,26 @@ +object happy { + + def main(args:Array[String]):Unit + = println( // use input from command line or else run three tests + ( + if( args.size > 0 ) + Seq( args ) + else + Seq( + Array("aba", "aabb", "abcd", "bac", "aabc"), + Array("nba", "cba", "dba"), + Array("aabb", "ab", "ba") + ) + ).map( similar_pairs_count ) + ) + + def letters(word:String):String + = word.sortWith(_<_).distinct // return string of unique sorted chars in input + + def is_similar(w1:String, w2:String):Boolean + = w1 != w2 && letters(w1) == letters(w2) + + def similar_pairs_count( word: Array[String] ):Int + = ( for( i <- 0 to word.size-2; j <- i+1 to word.size-1 if is_similar( word(i), word(j) ) ) yield 1 ).sum + +} diff --git a/challenge-233/kjetillll/scala/ch-2.scala b/challenge-233/kjetillll/scala/ch-2.scala new file mode 100644 index 0000000000..e3c540be6e --- /dev/null +++ b/challenge-233/kjetillll/scala/ch-2.scala @@ -0,0 +1,27 @@ +object happy { + + def main(args:Array[String]):Unit = + if( args.size > 0 ) + println( frequency_sort( args.toList.map(_.toInt) ) ) + else + run_tests + + def frequency_sort(ar:List[Int]):List[Int] = { + val count = ar.groupBy(identity).view.mapValues(_.size) + ar.sortBy( elem => ( count(elem), -elem ) ) + } + + def run_tests = { + for{ test <- List( + ( List(1,1,2,2,2,3), List(3,1,1,2,2,2) ), + ( List(2,3,1,3,2), List(1,3,3,2,2) ), + ( List(-1,1,-6,4,5,-6,1,4,1), List(5,-1,4,4,-6,-6,1,1,1) ) + )} { + val input = test._1 + val expected = test._2 + val got = frequency_sort(input) + val result = if( got == expected ) "ok" else "***NOT OK" + println( result + " input: "+input+" expected: "+expected+" got: "+got ) + } + } +} -- cgit From 8bedf705aff1de8422eb24e022bc72e24f87ee67 Mon Sep 17 00:00:00 2001 From: Humberto Massa Date: Mon, 11 Sep 2023 14:31:47 -0300 Subject: One-liner solutions --- challenge-234/massa/raku/ch-1.raku | 64 +++++++++++++++++++++++++++++++++++++ challenge-234/massa/raku/ch-2.raku | 65 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 129 insertions(+) create mode 100644 challenge-234/massa/raku/ch-1.raku create mode 100644 challenge-234/massa/raku/ch-2.raku diff --git a/challenge-234/massa/raku/ch-1.raku b/challenge-234/massa/raku/ch-1.raku new file mode 100644 index 0000000000..e81c2851dd --- /dev/null +++ b/challenge-234/massa/raku/ch-1.raku @@ -0,0 +1,64 @@ +#! /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: Common Characters + +=SUBTITLE +=head2 Submitted by massa + +=CHALLENGE +=head2 + +You are given an array of words made up of alphabetic characters only. + +Write a script to return all alphabetic characters that show up in all words including duplicates. + +=head3 Example 1: + +Input: @words = ("java", "javascript", "julia") +Output: ("j", "a") + +The minimum is 1 and maximum is 4 in the given array. So (3, 2) is neither min nor max. + +=head3 Example 2: + +Input: @words = ("bella", "label", "roller") +Output: ("e", "l", "l") + +=head3 Example 3: + +Input: @words = ("cool", "lock", "cook") +Output: ("c", "o") + +=SOLUTION + +=end pod + +# always use the latest version of Raku +use v6.*; + +sub common-characters(@_) { + ([∩] @_».comb».Bag).kv.map({ $^a xx $^b }).flat +} + +multi MAIN (Bool :$test!) { + use Test; + + my @tests = [ + %{ input => , output => }, + %{ input => , output => }, + %{ input => , output => }, + ]; + + for @tests { + common-characters( . ).&is-deeply: ., .; + } # end of for @tests +} # end of multi MAIN (Bool :$test!) + + diff --git a/challenge-234/massa/raku/ch-2.raku b/challenge-234/massa/raku/ch-2.raku new file mode 100644 index 0000000000..6c7b65545b --- /dev/null +++ b/challenge-234/massa/raku/ch-2.raku @@ -0,0 +1,65 @@ +#! /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: Unequal Triplets + +=SUBTITLE +=head2 Submitted by massa + +=CHALLENGE +=head2 + +Input: @ints = (4, 4, 2, 4, 3) +Ouput: 3 + +(0, 2, 4) because 4 != 2 != 3 +(1, 2, 4) because 4 != 2 != 3 +(2, 3, 4) because 2 != 4 != 3 + +=head3 Example 1: + +Input: @ints = (1, 1, 1, 1, 1) +Ouput: 0 + +=head3 Example 2: + +Input: @ints = (4, 7, 1, 10, 7, 4, 1, 1) +Output: 28 + +triplets of 1, 4, 7 = 3x2×2 = 12 combinations +triplets of 1, 4, 10 = 3×2×1 = 6 combinations +triplets of 4, 7, 10 = 2×2×1 = 4 combinations +triplets of 1, 7, 10 = 3x2x1 = 6 combinations + +=SOLUTION + +=end pod + +# always use the latest version of Raku +use v6.*; + +sub unequal-triplets(@_) { + (^+@_).combinations(3).grep({@_[$_].Bag.elems == 3}).elems +} + +multi MAIN (Bool :$test!) { + use Test; + + my @tests = [ + %{ input => (4, 4, 2, 4, 3), output => 3 }, + %{ input => (1, 1, 1, 1, 1), output => 0 }, + %{ input => (4, 7, 1, 10, 7, 4, 1, 1), output => 28 }, + ]; + + for @tests { + unequal-triplets( . ).&is-deeply: ., .; + } # end of for @tests +} # end of multi MAIN (:$test!) + + -- cgit From ec1b0ecad717dcedca568bd9b52da26ac097b172 Mon Sep 17 00:00:00 2001 From: Humberto Massa Date: Mon, 11 Sep 2023 14:38:20 -0300 Subject: There's always an easier way... --- challenge-234/massa/raku/ch-2.raku | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-234/massa/raku/ch-2.raku b/challenge-234/massa/raku/ch-2.raku index 6c7b65545b..d9ce6feb9c 100644 --- a/challenge-234/massa/raku/ch-2.raku +++ b/challenge-234/massa/raku/ch-2.raku @@ -45,7 +45,7 @@ triplets of 1, 7, 10 = 3x2x1 = 6 combinations use v6.*; sub unequal-triplets(@_) { - (^+@_).combinations(3).grep({@_[$_].Bag.elems == 3}).elems + @_.combinations(3).grep(*.Set.elems == 3).elems } multi MAIN (Bool :$test!) { -- cgit From ca1ca215245501d99bd3c88a258383ce4712dbbc Mon Sep 17 00:00:00 2001 From: Thomas Köhler Date: Mon, 11 Sep 2023 20:55:55 +0200 Subject: Add solution 234. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Thomas Köhler --- challenge-234/jeanluc2020/blog-1.txt | 1 + challenge-234/jeanluc2020/blog-2.txt | 1 + challenge-234/jeanluc2020/perl/ch-1.pl | 84 ++++++++++++++++++++++++++++++++++ challenge-234/jeanluc2020/perl/ch-2.pl | 68 +++++++++++++++++++++++++++ 4 files changed, 154 insertions(+) create mode 100644 challenge-234/jeanluc2020/blog-1.txt create mode 100644 challenge-234/jeanluc2020/blog-2.txt create mode 100755 challenge-234/jeanluc2020/perl/ch-1.pl create mode 100755 challenge-234/jeanluc2020/perl/ch-2.pl diff --git a/challenge-234/jeanluc2020/blog-1.txt b/challenge-234/jeanluc2020/blog-1.txt new file mode 100644 index 0000000000..3122817441 --- /dev/null +++ b/challenge-234/jeanluc2020/blog-1.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-234-1.html diff --git a/challenge-234/jeanluc2020/blog-2.txt b/challenge-234/jeanluc2020/blog-2.txt new file mode 100644 index 0000000000..0c129f762b --- /dev/null +++ b/challenge-234/jeanluc2020/blog-2.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-234-2.html diff --git a/challenge-234/jeanluc2020/perl/ch-1.pl b/challenge-234/jeanluc2020/perl/ch-1.pl new file mode 100755 index 0000000000..af76b08810 --- /dev/null +++ b/challenge-234/jeanluc2020/perl/ch-1.pl @@ -0,0 +1,84 @@ +#!/usr/bin/perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-234/#TASK1 +# +# Task 1: Common Characters +# ========================= +# +# You are given an array of words made up of alphabetic characters only. +# +# Write a script to return all alphabetic characters that show up in all words +# including duplicates. +# +## Example 1 +## +## Input: @words = ("java", "javascript", "julia") +## Output: ("j", "a") +# +## Example 2 +## +## Input: @words = ("bella", "label", "roller") +## Output: ("e", "l", "l") +# +## Example 3 +## +## Input: @words = ("cool", "lock", "cook") +## Output: ("c", "o") +# +############################################################ +## +## discussion +## +############################################################ +# +# First, we split each word into its characters, which we count. +# Then, for each character in the first word, we take care the +# value for that character exists for all other words as well, just +# set to 0 if the character doesn't appear in the other word. +# Then we calculate the minimum of times each character is in each +# word, so we can calculate the minimum of occurences for each +# character. +# In the end, we put as many of each character into our result +# set as we found to be the minimum of occurences in each word. + +use strict; +use warnings; +use Data::Dumper; + +common_characters("java", "javascript", "julia"); +common_characters("bella", "label", "roller"); +common_characters("cool", "lock", "cook"); + +sub common_characters { + my @words = @_; + print "Input: (" . join(", ", @words) . ")\n"; + my $data; + foreach my $word (@words) { + my @chars = split //, $word; + foreach my $char (@chars) { + $data->{$word}->{$char}++; + } + } + my $min; + foreach my $char (keys %{$data->{$words[0]}}) { + foreach my $word (@words) { + $data->{$word}->{$char} //= 0; + } + $min->{$char} = min( map { $data->{$_}->{$char} } @words ); + } + my @result; + foreach my $char (keys %{$data->{$words[0]}}) { + foreach my $count (1..$min->{$char}) { + push @result, $char; + } + } + print "Output: (" . join(", ", @result) . ")\n"; +} + +sub min { + my @elems = @_; + my $result = shift @elems; + foreach my $elem (@elems) { + $result = $elem if $elem < $result; + } + return $result; +} diff --git a/challenge-234/jeanluc2020/perl/ch-2.pl b/challenge-234/jeanluc2020/perl/ch-2.pl new file mode 100755 index 0000000000..ef6d45766b --- /dev/null +++ b/challenge-234/jeanluc2020/perl/ch-2.pl @@ -0,0 +1,68 @@ +#!/usr/bin/perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-234/#TASK2 +# +# Task 2: Unequal Triplets +# ======================== +# +# 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]. +# +## Example 1 +## +## Input: @ints = (4, 4, 2, 4, 3) +## Ouput: 3 +## +## (0, 2, 4) because 4 != 2 != 3 +## (1, 2, 4) because 4 != 2 != 3 +## (2, 3, 4) because 2 != 4 != 3 +# +## Example 2 +## +## Input: @ints = (1, 1, 1, 1, 1) +## Ouput: 0 +# +## Example 3 +## +## Input: @ints = (4, 7, 1, 10, 7, 4, 1, 1) +## Output: 28 +## +## triplets of 1, 4, 7 = 3x2×2 = 12 combinations +## triplets of 1, 4, 10 = 3×2×1 = 6 combinations +## triplets of 4, 7, 10 = 2×2×1 = 4 combinations +## triplets of 1, 7, 10 = 3x2x1 = 6 combinations +# +############################################################ +## +## discussion +## +############################################################ +# +# We just walk the array from left to right 3 times inside of +# the previous loop and count the instances in which all 3 +# numbers differ from each other + +use strict; +use warnings; + +unequal_triplets(4, 4, 2, 4, 3); +unequal_triplets(1, 1, 1, 1, 1); +unequal_triplets(4, 7, 1, 10, 7, 4, 1, 1); + +sub unequal_triplets { + my @ints = @_; + print "Input: (" . join(", ", @ints) . ")\n"; + my $result = 0; + foreach my $i (0..$#ints) { + foreach my $j ($i+1..$#ints) { + foreach my $k ($j+1..$#ints) { + if($ints[$i] != $ints[$j] && $ints[$j] != $ints[$k] && $ints[$i] != $ints[$k]) { + $result++; + } + } + } + } + print "Output: $result\n"; +} + -- cgit From d705af9bed193d783669de62dec8fc3a5d4cc1eb Mon Sep 17 00:00:00 2001 From: Packy Anderson Date: Tue, 12 Sep 2023 00:22:11 -0400 Subject: Challenge 234 solutions by Packy Anderson Solutions in: * Perl * Raku * Python One blog post --- challenge-234/packy-anderson/README.md | 106 ++++++++++++++++++++++++++++ challenge-234/packy-anderson/blog.txt | 1 + challenge-234/packy-anderson/perl/ch-1.pl | 75 ++++++++++++++++++++ challenge-234/packy-anderson/perl/ch-2.pl | 42 +++++++++++ challenge-234/packy-anderson/python/ch-1.py | 66 +++++++++++++++++ challenge-234/packy-anderson/python/ch-2.py | 35 +++++++++ challenge-234/packy-anderson/raku/ch-1.raku | 71 +++++++++++++++++++ challenge-234/packy-anderson/raku/ch-2.raku | 41 +++++++++++ 8 files changed, 437 insertions(+) create mode 100644 challenge-234/packy-anderson/README.md create mode 100644 challenge-234/packy-anderson/blog.txt create mode 100755 challenge-234/packy-anderson/perl/ch-1.pl create mode 100755 challenge-234/packy-anderson/perl/ch-2.pl create mode 100755 challenge-234/packy-anderson/python/ch-1.py create mode 100755 challenge-234/packy-anderson/python/ch-2.py create mode 100755 challenge-234/packy-anderson/raku/ch-1.raku create mode 100755 challenge-234/packy-anderson/raku/ch-2.raku diff --git a/challenge-234/packy-anderson/README.md b/challenge-234/packy-anderson/README.md new file mode 100644 index 0000000000..496619a8dc --- /dev/null +++ b/challenge-234/packy-anderson/README.md @@ -0,0 +1,106 @@ +# Solutions by Packy Anderson + +## Perl + +* [Task 1](perl/ch-1.pl) + +Sample output +``` +$ perl/ch-1.pl +Example 1: +Input: @words = ("java", "javascript", "julia") +Output: ("j", "a") + +Example 2: +Input: @words = ("bella", "label", "roller") +Output: ("e", "l", "l") + +Example 3: +Input: @words = ("cool", "lock", "cook") +Output: ("c", "o") +``` + +* [Task 2](perl/ch-2.pl) + +Sample output +``` +$ perl/ch-2.pl +Example 1: +Input: @ints = (4, 4, 2, 4, 3) +Output: 3 + +(0, 2, 4) because 4 != 2 != 3 +(1, 2, 4) because 4 != 2 != 3 +(2, 3, 4) because 2 != 4 != 3 + +Example 2: +Input: @ints = (1, 1, 1, 1, 1) +Output: 0 + +Example 3: +Input: @ints = (4, 7, 1, 10, 7, 4, 1, 1) +Output: 28 + +(0, 1, 2) because 4 != 7 != 1 +(0, 1, 3) because 4 != 7 != 10 +(0, 1, 6) because 4 != 7 != 1 +(0, 1, 7) because 4 != 7 != 1 +(0, 2, 3) because 4 != 1 != 10 +(0, 2, 4) because 4 != 1 != 7 +(0, 3, 4) because 4 != 10 != 7 +(0, 3, 6) because 4 != 10 != 1 +(0, 3, 7) because 4 != 10 != 1 +(0, 4, 6) because 4 != 7 != 1 +(0, 4, 7) because 4 != 7 != 1 +(1, 2, 3) because 7 != 1 != 10 +(1, 2, 5) because 7 != 1 != 4 +(1, 3, 5) because 7 != 10 != 4 +(1, 3, 6) because 7 != 10 != 1 +(1, 3, 7) because 7 != 10 != 1 +(1, 5, 6) because 7 != 4 != 1 +(1, 5, 7) because 7 != 4 != 1 +(2, 3, 4) because 1 != 10 != 7 +(2, 3, 5) because 1 != 10 != 4 +(2, 4, 5) because 1 != 7 != 4 +(3, 4, 5) because 10 != 7 != 4 +(3, 4, 6) because 10 != 7 != 1 +(3, 4, 7) because 10 != 7 != 1 +(3, 5, 6) because 10 != 4 != 1 +(3, 5, 7) because 10 != 4 != 1 +(4, 5, 6) because 7 != 4 != 1 +(4, 5, 7) because 7 != 4 != 1 +``` + +## Raku + +* [Task 1](raku/ch-1.raku) + +Sample output +``` +$ raku/ch-1.raku +Example 1: +Input: @words = ("java", "javascript", "julia") +Output: ("j", "a") + +Example 2: +Input: @words = ("bella", "label", "roller") +Output: ("e", "l", "l") + +Example 3: +Input: @words = ("cool", "lock", "cook") +Output: ("c", "o") +``` + +* [Task 2](raku/ch-2.raku) + +Sample output +``` +``` + +## Guest Language: Python +* [Task 1](python/ch-1.py) +* [Task 2](python/ch-1.py) + +## Blog Post + +[Perl Weekly Challenge: Common, but Unequal, Triplet Characters](https://packy.dardan.com/2023/09/11/perl-weekly-challenge-common-but-unequal-triplet-characters/) \ No newline at end of file diff --git a/challenge-234/packy-anderson/blog.txt b/challenge-234/packy-anderson/blog.txt new file mode 100644 index 0000000000..0475d41383 --- /dev/null +++ b/challenge-234/packy-anderson/blog.txt @@ -0,0 +1 @@ +https://packy.dardan.com/2023/09/11/perl-weekly-challenge-common-but-unequal-triplet-characters/ \ No newline at end of file diff --git a/challenge-234/packy-anderson/perl/ch-1.pl b/challenge-234/packy-anderson/perl/ch-1.pl new file mode 100755 index 0000000000..11f455501b --- /dev/null +++ b/challenge-234/packy-anderson/perl/ch-1.pl @@ -0,0 +1,75 @@ +#!/usr/bin/env perl + +use v5.38; + +use List::Util qw( min ); + +sub charFrequency { + my $word = shift; + my %freq; + foreach my $c ( split //, $word ) { + $freq{$c}++; + } + return \%freq; # return a hash REFERENCE +} + +sub commonCharacters { + my @words = @_; + my @freq = map { charFrequency($_) } @words; + # grab the character frequency map for the first word + my $first = shift @freq; + # now check the characters in the first word against + # the characters in all the subsequent words + foreach my $subsequent ( @freq ) { + foreach my $c ( keys %$first ) { + if (! exists $subsequent->{$c}) { + # this character isn't in subsequent words, + # so let's remove it from the frequency map + # of the first word + delete $first->{$c}; + } + else { + # the character IS in subsequent words, + # so let's set the frequency count to be + # the minimum count found in those words + $first->{$c} = min($first->{$c}, $subsequent->{$c}); + } + } + } + + # now we generate a list of characters in the order they + # appear in the first word + my @output; + # once again, loop over the characters in the first word + foreach my $c ( split //, $words[0] ) { + next unless exists $first->{$c}; + if ($first->{$c} > 1) { + # there's more than one occurence, so let's decrement + # the count for the next time through the loop + $first->{$c}--; + } + else { + # there is only one occurence left, so remove the + # character + delete $first->{$c}; + } + push @output, $c; + } + return @output; +} + +sub solution { + my @words = @_; + say 'Input: @words = ("' . join('", "', @words) . '")'; + my @output = commonCharacters(@words); + say 'Output: ("' . join('", "', @output) . '")'; +} + +say "Example 1:"; +solution("java", "javascript", "julia"); + +say "\nExample 2:"; +solution("bella", "label", "roller"); + +say "\nExample 3:"; +solution("cool", "lock", "cook"); diff --git a/challenge-234/packy-anderson/perl/ch-2.pl b/challenge-234/packy-anderson/perl/ch-2.pl new file mode 100755 index 0000000000..15cdc2cdd0 --- /dev/null +++ b/challenge-234/packy-anderson/perl/ch-2.pl @@ -0,0 +1,42 @@ +#!/usr/bin/env perl + +use v5.38; + +sub findTriplets { + my @ints = @_; + my @solutions; + foreach my $i ( 0 .. $#ints - 2 ) { + foreach my $j ( $i+1 .. $#ints - 1 ) { + foreach my $k ( $j+1 .. $#ints ) { + if ($ints[$i] != $ints[$j] && + $ints[$j] != $ints[$k] && + $ints[$i] != $ints[$k]) { + push @solutions, [$i, $j, $k]; + } + } + } + } + return @solutions; +} + +sub solution { + my @ints = @_; + say 'Input: @ints = (' . join(', ', @ints) . ')'; + my @solutions = findTriplets(@ints); + say 'Output: ' . scalar(@solutions); + say "" if @solutions; + foreach my $triplet ( @solutions ) { + my($i, $j, $k) = @$triplet; + say "($i, $j, $k) because " + . "$ints[$i] != $ints[$j] != $ints[$k]"; + } +} + +say "Example 1:"; +solution(4, 4, 2, 4, 3); + +say "\nExample 2:"; +solution(1, 1, 1, 1, 1); + +say "\nExample 3:"; +solution(4, 7, 1, 10, 7, 4, 1, 1); \ No newline at end of file diff --git a/challenge-234/packy-anderson/python/ch-1.py b/challenge-234/packy-anderson/python/ch-1.py new file mode 100755 index 0000000000..ddfc986b12 --- /dev/null +++ b/challenge-234/packy-anderson/python/ch-1.py @@ -0,0 +1,66 @@ +#!/usr/bin/env python + +from collections import Counter + +def charFrequency(word): + # https://docs.python.org/3/library/collections.html#counter-objects + freq = Counter() + for c in word: + freq[c] += 1 + return freq + +def commonCharacters(words): + # get the character freqencies for each word + freq = list(map(charFrequency, words)) + + # grab the character frequency map for the first word + first = freq.pop(0) + + # make a copy of the dictionary since we'll + # be modifying it in the loop + first_orig = dict(first) + + # now check the characters in the first word against + # the characters in all the subsequent words + for subsequent in freq: + for c in first_orig: + if c not in subsequent: + # this character isn't in subsequent words, + # so let's remove it from the frequency map + # of the first word + first.pop(c) + else: + # the character IS in subsequent words, + # so let's set the frequency count to be + # the minimum count found in those words + first[c] = min(first[c], subsequent[c]) + + # now we generate a list of characters in the order they + # appear in the first word + output = [] + # once again, loop over the characters in the first word + for c in words[0]: + if c not in first: + continue + if first[c] > 1: + first[c] -= 1 + else: + first.pop(c) + output.append(c) + return output + +def solution(words): + quoted = '"' + '", "'.join(words) + '"' + print(f'Input: @words = ({quoted})') + output = commonCharacters(words) + quoted = '"' + '", "'.join(output) + '"' + print(f'Output: ({quoted})') + +print("Example 1:") +solution(["java", "javascript", "julia"]) + +print("\nExample 2:") +solution(["bella", "label", "roller"]) + +print("\nExample 3:") +solution(["cool", "lock", "cook"]) \ No newline at end of file diff --git a/challenge-234/packy-anderson/python/ch-2.py b/challenge-234/packy-anderson/python/ch-2.py new file mode 100755 index 0000000000..9024561157 --- /dev/null +++ b/challenge-234/packy-anderson/python/ch-2.py @@ -0,0 +1,35 @@ +#!/usr/bin/env python + +def findTriplets(ints): + solutions = [] + for i in range(0, len(ints) - 3 ): + for j in range(i + 1, len(ints) - 2): + for k in range(j + 1, len(ints) - 1): + if (ints[i] != ints[j] and + ints[j] != ints[k] and + ints[i] != ints[k]): + solutions.append([i, j, k]) + return solutions + +def solution(ints): + intlist = ", ".join([ str(i) for i in ints ]) + print(f'Input: @ints = ({intlist})') + solutions = findTriplets(ints) + print(f'Output: {len(solutions)}') + if solutions: + print("") + for triplet in solutions: + i, j, k = triplet + print( + f"({i}, {j}, {k}) because " + + f"{ints[i]} != {ints[j]} != {ints[k]}" + ) + +print("Example 1:") +solution([4, 4, 2, 4, 3]) + +print("\nExample 2:") +solution([1, 1, 1, 1, 1]) + +print("\nExample 3:") +solution([4, 7, 1, 10, 7, 4, 1, 1]) \ No newline at end of file diff --git a/challenge-234/packy-anderson/raku/ch-1.raku b/challenge-234/packy-anderson/raku/ch-1.raku new file mode 100755 index 0000000000..5afdb22773 --- /dev/null +++ b/challenge-234/packy-anderson/raku/ch-1.raku @@ -0,0 +1,71 @@ +#!/usr/bin/env raku + +use v6; + +sub charFrequency(Str $word) { + my %freq; + for $word.split('', :skip-empty) -> $c { + %freq{$c}++; + } + return %freq; +} + +sub commonCharacters(*@words where ($_.all ~~ Str)) { + my @freq = @words.map({ charFrequency($_) }); + # grab the character frequency map for the first word + my $first = shift @freq; + # now check the characters in the first word against + # the characters in all the subsequent words + for @freq -> $subsequent { + for $first.keys() -> $c { + if ($subsequent{$c}:!exists) { + # this character isn't in subsequent words, + # so let's remove it from the frequency map + # of the first word + $first{$c}:delete; + } + else { + # the character IS in subsequent words, + # so let's set the frequency count to be + # the minimum count found in those words + $first{$c} = min($first{$c}, $subsequent{$c}); + } + } + } + + # now we generate a list of characters in the order they + # appear in the first word + my @output; + # once again, loop over the characters in the first word + for @words[0].split('', :skip-empty) -> $c { + next unless $first{$c}:exists; + if ($first{$c} > 1) { + # there's more than one occurence, so let's decrement + # the count for the next time through the loop + $first{$c}--; + } + else { + # there is only one occurence left, so remove the + # character + $first{$c}:delete; + } + push @output, $c; + } + return @output; +} + +sub solution { + my @words = @_; + say 'Input: @words = ("' ~ @words.join('", "') ~ '")'; + my @output = commonCharacters(@words); + say 'Output: ("' ~ @output.join('", "') ~ '")'; +} + +say "Example 1:"; +solution("java", "javascript", "julia"); + +say "\nExample 2:"; +solution("bella", "label", "roller"); + +say "\nExample 3:"; +solution("cool", "lock", "cook"); \ No newline at end of file diff --git a/challenge-234/packy-anderson/raku/ch-2.raku b/challenge-234/packy-anderson/raku/ch-2.raku new file mode 100755 index 0000000000..613b9d57d3 --- /dev/null +++ b/challenge-234/packy-anderson/raku/ch-2.raku @@ -0,0 +1,41 @@ +#!/usr/bin/env raku + +use v6; + +sub findTriplets(@ints where ($_.all ~~ Int)) { + my @solutions; + for 0 .. @ints.elems - 3 -> $i { + for $i + 1 .. @ints.elems - 2 -> $j { + for $j + 1 .. @ints.elems - 1 -> $k { + if (@ints[$i] != @ints[$j] && + @ints[$j] != @ints[$k] && + @ints[$i] != @ints[$k]) { + push @solutions, [$i, $j, $k]; + } + } + } + } + return @solutions; +} + +sub solution { + my @ints = @_; + say 'Input: @ints = (' ~ @ints.join(', ') ~ ')'; + my @solutions = findTriplets(@ints); + say 'Output: ' ~ @solutions.elems; + say "" if @solutions; + for @solutions -> @triplet { + my ($i, $j, $k) = @triplet; + say "($i, $j, $k) because " + ~ "@ints[$i] != @ints[$j] != @ints[$k]"; + } +} + +say "Example 1:"; +solution(4, 4, 2, 4, 3); + +say "\nExample 2:"; +solution(1, 1, 1, 1, 1); + +say "\nExample 3:"; +solution(4, 7, 1, 10, 7, 4, 1, 1); \ No newline at end of file -- cgit From 44085208e50a1131cb680414ceb466d62e631c12 Mon Sep 17 00:00:00 2001 From: Packy Anderson Date: Tue, 12 Sep 2023 00:25:24 -0400 Subject: Update README for Raku task 2 output --- challenge-234/packy-anderson/README.md | 45 ++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/challenge-234/packy-anderson/README.md b/challenge-234/packy-anderson/README.md index 496619a8dc..07a82cb6ab 100644 --- a/challenge-234/packy-anderson/README.md +++ b/challenge-234/packy-anderson/README.md @@ -95,6 +95,51 @@ Output: ("c", "o") Sample output ``` +$ raku/ch-2.raku +Example 1: +Input: @ints = (4, 4, 2, 4, 3) +Output: 3 + +(0, 2, 4) because 4 != 2 != 3 +(1, 2, 4) because 4 != 2 != 3 +(2, 3, 4) because 2 != 4 != 3 + +Example 2: +Input: @ints = (1, 1, 1, 1, 1) +Output: 0 + +Example 3: +Input: @ints = (4, 7, 1, 10, 7, 4, 1, 1) +Output: 28 + +(0, 1, 2) because 4 != 7 != 1 +(0, 1, 3) because 4 != 7 != 10 +(0, 1, 6) because 4 != 7 != 1 +(0, 1, 7) because 4 != 7 != 1 +(0, 2, 3) because 4 != 1 != 10 +(0, 2, 4) because 4 != 1 != 7 +(0, 3, 4) because 4 != 10 != 7 +(0, 3, 6) because 4 != 10 != 1 +(0, 3, 7) because 4 != 10 != 1 +(0, 4, 6) because 4 != 7 != 1 +(0, 4, 7) because 4 != 7 != 1 +(1, 2, 3) because 7 != 1 != 10 +(1, 2, 5) because 7 != 1 != 4 +(1, 3, 5) because 7 != 10 != 4 +(1, 3, 6) because 7 != 10 != 1 +(1, 3, 7) because 7 != 10 != 1 +(1, 5, 6) because 7 != 4 != 1 +(1, 5, 7) because 7 != 4 != 1 +(2, 3, 4) because 1 != 10 != 7 +(2, 3, 5) because 1 != 10 != 4 +(2, 4, 5) because 1 != 7 != 4 +(3, 4, 5) because 10 != 7 != 4 +(3, 4, 6) because 10 != 7 != 1 +(3, 4, 7) because 10 != 7 != 1 +(3, 5, 6) because 10 != 4 != 1 +(3, 5, 7) because 10 != 4 != 1 +(4, 5, 6) because 7 != 4 != 1 +(4, 5, 7) because 7 != 4 != 1 ``` ## Guest Language: Python -- cgit From f14045cef30c4f226d9e8dab79005d63dca7515a Mon Sep 17 00:00:00 2001 From: Packy Anderson Date: Tue, 12 Sep 2023 00:27:33 -0400 Subject: Fix blog URL --- challenge-234/packy-anderson/README.md | 2 +- challenge-234/packy-anderson/blog.txt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/challenge-234/packy-anderson/README.md b/challenge-234/packy-anderson/README.md index 07a82cb6ab..445ab48fc5 100644 --- a/challenge-234/packy-anderson/README.md +++ b/challenge-234/packy-anderson/README.md @@ -148,4 +148,4 @@ Output: 28 ## Blog Post -[Perl Weekly Challenge: Common, but Unequal, Triplet Characters](https://packy.dardan.com/2023/09/11/perl-weekly-challenge-common-but-unequal-triplet-characters/) \ No newline at end of file +[Perl Weekly Challenge: Common, but Unequal, Triplet Characters](https://packy.dardan.com/2023/09/12/perl-weekly-challenge-common-but-unequal-triplet-characters/) \ No newline at end of file diff --git a/challenge-234/packy-anderson/blog.txt b/challenge-234/packy-anderson/blog.txt index 0475d41383..7ccaf943dc 100644 --- a/challenge-234/packy-anderson/blog.txt +++ b/challenge-234/packy-anderson/blog.txt @@ -1 +1 @@ -https://packy.dardan.com/2023/09/11/perl-weekly-challenge-common-but-unequal-triplet-characters/ \ No newline at end of file +https://packy.dardan.com/2023/09/12/perl-weekly-challenge-common-but-unequal-triplet-characters/ \ No newline at end of file -- cgit From 380d42f7d744004fb2e3a33ab5271d94ee4816fc Mon Sep 17 00:00:00 2001 From: rcmlz Date: Tue, 12 Sep 2023 11:06:15 +0200 Subject: Raku solutions to 234 --- challenge-234/rcmlz/raku/task-one.rakumod | 14 ++++++++++++++ challenge-234/rcmlz/raku/task-two.rakumod | 14 ++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 challenge-234/rcmlz/raku/task-one.rakumod create mode 100644 challenge-234/rcmlz/raku/task-two.rakumod diff --git a/challenge-234/rcmlz/raku/task-one.rakumod b/challenge-234/rcmlz/raku/task-one.rakumod new file mode 100644 index 0000000000..f44dc14b8c --- /dev/null +++ b/challenge-234/rcmlz/raku/task-one.rakumod @@ -0,0 +1,14 @@ +unit module challenge-nr234::rcmlz::raku::task-one:ver<0.0.1>:auth; + +# 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/task-two.rakumod b/challenge-234/rcmlz/raku/task-two.rakumod new file mode 100644 index 0000000000..0ee6ab4d3e --- /dev/null +++ b/challenge-234/rcmlz/raku/task-two.rakumod @@ -0,0 +1,14 @@ +unit module challenge-nr233::rcmlz::raku::task-two:ver<0.0.1>:auth; + +# 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 +} -- cgit From 817d0e16cfeeb062bcc543cde7ed2f75801e26d6 Mon Sep 17 00:00:00 2001 From: Humberto Massa Date: Tue, 12 Sep 2023 08:10:56 -0300 Subject: A more elegant solution via lizmat --- challenge-234/massa/raku/ch-2.raku | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-234/massa/raku/ch-2.raku b/challenge-234/massa/raku/ch-2.raku index d9ce6feb9c..25c38c73c0 100644 --- a/challenge-234/massa/raku/ch-2.raku +++ b/challenge-234/massa/raku/ch-2.raku @@ -45,7 +45,7 @@ triplets of 1, 7, 10 = 3x2x1 = 6 combinations use v6.*; sub unequal-triplets(@_) { - @_.combinations(3).grep(*.Set.elems == 3).elems + @_.combinations(3).grep(!*.repeated).elems } multi MAIN (Bool :$test!) { -- cgit From 9bbb7fcd2573239b94c8f73c0260f3e1a5ee5405 Mon Sep 17 00:00:00 2001 From: David Ferrone Date: Tue, 12 Sep 2023 10:25:34 -0400 Subject: Week 234 --- challenge-234/zapwai/perl/ch-1.pl | 16 ++++++++++++++++ challenge-234/zapwai/perl/ch-2.pl | 13 +++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 challenge-234/zapwai/perl/ch-1.pl create mode 100644 challenge-234/zapwai/perl/ch-2.pl diff --git a/challenge-234/zapwai/perl/ch-1.pl b/challenge-234/zapwai/perl/ch-1.pl new file mode 100644 index 0000000000..7fdcb33777 --- /dev/null +++ b/challenge-234/zapwai/perl/ch-1.pl @@ -0,0 +1,16 @@ +use v5.30; +no warnings; +my @words = ("java", "javascript", "julia"); +my @letters = split "", $words[0]; +foreach my $word (@words) { + my @l = split "", $word; + foreach my $l (0 .. $#letters) { + # remove from letters if item is not also in word. + splice @letters, $l, 1 unless ($letters[$l] ~~ @l); + } +} +my %ans; +$ans{$_} = 1 for (@letters); +my @ans = keys %ans; +say "Input: \@words = @words"; +say "Output: @ans"; diff --git a/challenge-234/zapwai/perl/ch-2.pl b/challenge-234/zapwai/perl/ch-2.pl new file mode 100644 index 0000000000..13cd1e8930 --- /dev/null +++ b/challenge-234/zapwai/perl/ch-2.pl @@ -0,0 +1,13 @@ +use v5.30; +my @ints = (4, 4, 2, 4, 3); +#@ints = (4, 7, 1, 10, 7, 4, 1, 1); +my $c = 0; +for my $i (0 .. $#ints - 2) { + for my $j ($i .. $#ints - 1) { + for my $k ($j .. $#ints) { + $c++ if ( ($ints[$i] != $ints[$j]) && ($ints[$i] != $ints[$k]) && ($ints[$k] != $ints[$j]) ); + } + } +} +say "Input: \@ints = @ints"; +say "Output: $c"; -- cgit From 9340a553b3b2c46bdf8cae4c2d7335bd83e8d672 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Tue, 12 Sep 2023 15:10:43 +0000 Subject: Challenge 234 Solutions (Raku) --- challenge-234/mark-anderson/raku/ch-1.raku | 11 +++++++++++ challenge-234/mark-anderson/raku/ch-2.raku | 11 +++++++++++ 2 files changed, 22 insertions(+) create mode 100644 challenge-234/mark-anderson/raku/ch-1.raku create mode 100644 challenge-234/mark-anderson/raku/ch-2.raku diff --git a/challenge-234/mark-anderson/raku/ch-1.raku b/challenge-234/mark-anderson/raku/ch-1.raku new file mode 100644 index 0000000000..3fff5386cd --- /dev/null +++ b/challenge-234/mark-anderson/raku/ch-1.raku @@ -0,0 +1,11 @@ +#!/usr/bin/env raku +use Test; + +is-deeply common-character(), ; +is-deeply common-character(), ; +is-deeply common-character(), ; + +sub common-character(@a) +{ + [(&)] @a>>.comb>>.Bag andthen .kxxv.sort +} diff --git a/challenge-234/mark-anderson/raku/ch-2.raku b/challenge-234/mark-anderson/raku/ch-2.raku new file mode 100644 index 0000000000..6a0a11f265 --- /dev/null +++ b/challenge-234/mark-anderson/raku/ch-2.raku @@ -0,0 +1,11 @@ +#!/usr/bin/env raku +use Test; + +is unequal-triplets(<4 4 2 4 3>), 3; +is unequal-triplets(<1 1 1 1 1>), 0; +is unequal-triplets(<4 7 1 10 7 4 1 1>), 28; + +sub unequal-triplets(@a) +{ + sum map { [*] .map(*.values).flat }, @a.Bag.combinations(3) +} -- cgit From 38faa5ed89fb188138aae63e6a5070870e558073 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Tue, 12 Sep 2023 15:13:56 +0000 Subject: Challenge 234 Solutions (Raku) --- challenge-234/mark-anderson/raku/ch-1.raku | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/challenge-234/mark-anderson/raku/ch-1.raku b/challenge-234/mark-anderson/raku/ch-1.raku index 3fff5386cd..253ff1c827 100644 --- a/challenge-234/mark-anderson/raku/ch-1.raku +++ b/challenge-234/mark-anderson/raku/ch-1.raku @@ -1,11 +1,11 @@ #!/usr/bin/env raku use Test; -is-deeply common-character(), ; -is-deeply common-character(), ; -is-deeply common-character(), ; +is-deeply common-characters(), ; +is-deeply common-characters(), ; +is-deeply common-characters(), ; -sub common-character(@a) +sub common-characters(@a) { [(&)] @a>>.comb>>.Bag andthen .kxxv.sort } -- cgit From 3b8138c6cd4986beade0939cbb45f3e1166cec04 Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Tue, 12 Sep 2023 18:26:53 +0200 Subject: feat(challenge-234/lubos-kolouch/perl,raku,python,blog/): Challenge 234 LK Perl Raku Python Blog --- challenge-234/lubos-kolouch/blog.txt | 1 + challenge-234/lubos-kolouch/perl/ch-1.pl | 31 ++++++++++++++++++++++++++++ challenge-234/lubos-kolouch/perl/ch-2.pl | 33 ++++++++++++++++++++++++++++++ challenge-234/lubos-kolouch/python/ch-1.py | 24 ++++++++++++++++++++++ challenge-234/lubos-kolouch/python/ch-2.py | 31 ++++++++++++++++++++++++++++ challenge-234/lubos-kolouch/raku/ch-1.raku | 17 +++++++++++++++ challenge-234/lubos-kolouch/raku/ch-2.raku | 21 +++++++++++++++++++ 7 files changed, 158 insertions(+) create mode 100644 challenge-234/lubos-kolouch/blog.txt create mode 100644 challenge-234/lubos-kolouch/perl/ch-1.pl create mode 100644 challenge-234/lubos-kolouch/perl/ch-2.pl create mode 100644 challenge-234/lubos-kolouch/python/ch-1.py create mode 100644 challenge-234/lubos-kolouch/python/ch-2.py create mode 100644 challenge-234/lubos-kolouch/raku/ch-1.raku create mode 100644 challenge-234/lubos-kolouch/raku/ch-2.raku diff --git a/challenge-234/lubos-kolouch/blog.txt b/challenge-234/lubos-kolouch/blog.txt new file mode 100644 index 0000000000..2aa4127ccc --- /dev/null +++ b/challenge-234/lubos-kolouch/blog.txt @@ -0,0 +1 @@ +https://egroup.kolouch.org/nextcloud/sites/lubos/2023-09-11_Weekly_challenge_234 diff --git a/challenge-234/lubos-kolouch/perl/ch-1.pl b/challenge-234/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..fabc7c05a3 --- /dev/null +++ b/challenge-234/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,31 @@ +use strict; +use warnings; +use List::Util qw(all); + +sub common_characters { + my @words = @_; + + # Count characters for each word + my @char_counts; + for my $word (@words) { + my %count; + $count{$_}++ for split //, $word; + push @char_counts, \%count; + } + + # Find common characters + my @common_chars; + for my $char (keys %{$char_counts[0]}) { + if (all { exists $_->{$char} } @char_counts) { + push @common_chars, $char; + } + } + + return @common_chars; +} + +# Testing the Perl solution +print join(", ", common_characters("java", "javascript", "julia")), "\n"; # Output: j, a +print join(", ", common_characters("bella", "label", "roller")), "\n"; # Output: e, l +print join(", ", common_characters("cool", "lock", "cook")), "\n"; # Output: c, o + diff --git a/challenge-234/lubos-kolouch/perl/ch-2.pl b/challenge-234/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..11cc9003e6 --- /dev/null +++ b/challenge-234/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,33 @@ +use strict; +use warnings; + +sub unequal_triplets { + my @ints = @_; + + # Count the frequency of each number + my %freq; + $freq{$_}++ for @ints; + + my $count = 0; + my @unique_numbers = keys %freq; + + # Manually generate combinations using nested loops + for my $i ( 0 .. $#unique_numbers ) { + for my $j ( $i + 1 .. $#unique_numbers ) { + for my $k ( $j + 1 .. $#unique_numbers ) { + my $product = + $freq{ $unique_numbers[$i] } * + $freq{ $unique_numbers[$j] } * + $freq{ $unique_numbers[$k] }; + $count += $product; + } + } + } + + return $count; +} + +# Test the Perl solution +print unequal_triplets( 4, 4, 2, 4, 3 ), "\n"; # Expected Output: 3 +print unequal_triplets( 1, 1, 1, 1, 1 ), "\n"; # Expected Output: 0 +print unequal_triplets( 4, 7, 1, 10, 7, 4, 1, 1 ), "\n"; # Expected Output: 28 diff --git a/challenge-234/lubos-kolouch/python/ch-1.py b/challenge-234/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..6409c9612e --- /dev/null +++ b/challenge-234/lubos-kolouch/python/ch-1.py @@ -0,0 +1,24 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from typing import List + + +def common_characters(words: List[str]) -> List[str]: + """ + Returns a list of characters that appear in all the given words. + """ + # Start with the set of characters from the first word + common_chars = set(words[0]) + + # Intersect with other words + for word in words[1:]: + common_chars &= set(word) + + return list(common_chars) + + +# Testing the Python solution +print(common_characters(["java", "javascript", "julia"])) # Output: ['j', 'a'] +print(common_characters(["bella", "label", "roller"])) # Output: ['e', 'l'] +print(common_characters(["cool", "lock", "cook"])) # Output: ['c', 'o'] diff --git a/challenge-234/lubos-kolouch/python/ch-2.py b/challenge-234/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..d43b97b13a --- /dev/null +++ b/challenge-234/lubos-kolouch/python/ch-2.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from itertools import combinations +from collections import Counter +from typing import List + + +def unequal_triplets(ints: List[int]) -> int: + """ + Returns the number of unequal triplets from the given list of integers. + """ + # Count the frequency of each number + freq = Counter(ints) + + count = 0 + # Generate all possible combinations of 3 distinct numbers + for combo in combinations(freq.keys(), 3): + # Calculate the number of triplets possible for each combination + product = 1 + for num in combo: + product *= freq[num] + count += product + + return count + + +# Testing the Python solution +print(unequal_triplets([4, 4, 2, 4, 3])) # Output: 3 +print(unequal_triplets([1, 1, 1, 1, 1])) # Output: 0 +print(unequal_triplets([4, 7, 1, 10, 7, 4, 1, 1])) # Output: 28 diff --git a/challenge-234/lubos-kolouch/raku/ch-1.raku b/challenge-234/lubos-kolouch/raku/ch-1.raku new file mode 100644 index 0000000000..1b60d37b67 --- /dev/null +++ b/challenge-234/lubos-kolouch/raku/ch-1.raku @@ -0,0 +1,17 @@ +sub common-characters(@words) { + # Convert the first word into a set of characters + my $common-set = @words[0].comb.Set; + + # Intersect with other words + for @words[1..*] -> $word { + $common-set ∩= $word.comb.Set; + } + + return $common-set.keys; +} + +# Testing the Raku solution +say common-characters(); # Output: j a +say common-characters(); # Output: e l +say common-characters(); # Output: c o + diff --git a/challenge-234/lubos-kolouch/raku/ch-2.raku b/challenge-234/lubos-kolouch/raku/ch-2.raku new file mode 100644 index 0000000000..adb47e3341 --- /dev/null +++ b/challenge-234/lubos-kolouch/raku/ch-2.raku @@ -0,0 +1,21 @@ +sub unequal-triplets(@ints) { + # Count the frequency of each number + my %freq = @ints.Bag; + + my $count = 0; + # Generate all possible combinations of 3 distinct numbers + for %freq.keys.combinations(3) -> @combo { + # Calculate the number of triplets possible for each combination + my $product = 1; + $product *= %freq{$_} for @combo; + $count += $product; + } + + return $count; +} + +# Test the Raku solution +say unequal-triplets([4, 4, 2, 4, 3]); # Output: 3 +say unequal-triplets([1, 1, 1, 1, 1]); # Output: 0 +say unequal-triplets([4, 7, 1, 10, 7, 4, 1, 1]); # Output: 28 + -- cgit From 1bc77d9734fefdda0fc853eb8ad6c57a780d4b4e Mon Sep 17 00:00:00 2001 From: Michael Firkins Date: Tue, 12 Sep 2023 22:09:08 +1000 Subject: pwc234 solution in python --- challenge-234/pokgopun/python/ch-1.py | 82 +++++++++++++++++++++++++++++++++++ challenge-234/pokgopun/python/ch-2.py | 71 ++++++++++++++++++++++++++++++ 2 files changed, 153 insertions(+) create mode 100644 challenge-234/pokgopun/python/ch-1.py create mode 100644 challenge-234/pokgopun/python/ch-2.py diff --git a/challenge-234/pokgopun/python/ch-1.py b/challenge-234/pokgopun/python/ch-1.py new file mode 100644 index 0000000000..be19101dcc --- /dev/null +++ b/challenge-234/pokgopun/python/ch-1.py @@ -0,0 +1,82 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-234/ +""" + +Task 1: Common Characters + +Submitted by: [43]Mohammad S Anwar + __________________________________________________________________ + + You are given an array of words made up of alphabetic characters only. + + Write a script to return all alphabetic characters that show up in all + words including duplicates. + +Example 1 + +Input: @words = ("java", "javascript", "julia") +Output: ("j", "a") + +Example 2 + +Input: @words = ("bella", "label", "roller") +Output: ("e", "l", "l") + +Example 3 + +Input: @words = ("cool", "lock", "cook") +Output: ("c", "o") + +Task 2: Unequal Triplets +""" + +def commonChars(tup): + ### create a ref list of character's count from the first word + #ccLst = [ [tup[0][x],1] for x in range(len(tup[0])) ] + ### change the type of list member from list to tuple + ccLst = [ (tup[0][x],1) for x in range(len(tup[0])) ] + + ### the remaining words will have its characters checked against the ref list + for word in tup[1:]: + + ### create a list of characters from the remaining word + cLst = [word[x] for x in range(len(word))] + + for i in range(len(ccLst)): + + ### get a character from the ref list + c = ccLst[i][0] + + ### check if the remaining word contain the character + if c in word: + + ### increase character's count in the ref list + #ccLst[i][1] += 1 + ### as the ref list has its member type changed from list to tuple, need to recreate the tuple member + ccLst[i] = (ccLst[i][0], ccLst[i][1]+1) + + ### remove the characer that has been counted + cLst.remove(c) + ### updating the remaining word + word = "".join(cLst) + + ### count of common characters, this will use to filter ccList + count = len(tup) + ### filter ccList and then map to create the tuple of common characters + return tuple( + map( + lambda x: x[0], + filter( + lambda x: x[1]==count, + ccLst + ) + ) + ) + +for inpt,otpt in { + ("java", "javascript", "julia"): ("j", "a"), + ("bella", "label", "roller"): ("e", "l", "l"), + ("cool", "lock", "cook"): ("c", "o"), + }.items(): + print(commonChars(inpt)==otpt) + + diff --git a/challenge-234/pokgopun/python/ch-2.py b/challenge-234/pokgopun/python/ch-2.py new file mode 100644 index 0000000000..d7a2b1d06a --- /dev/null +++ b/challenge-234/pokgopun/python/ch-2.py @@ -0,0 +1,71 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-234/ +""" + +Task 2: Unequal Triplets + +Submitted by: [44]Mohammad S Anwar + __________________________________________________________________ + + 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]. + +Example 1 + +Input: @ints = (4, 4, 2, 4, 3) +Ouput: 3 + +(0, 2, 4) because 4 != 2 != 3 +(1, 2, 4) because 4 != 2 != 3 +(2, 3, 4) because 2 != 4 != 3 + +Example 2 + +Input: @ints = (1, 1, 1, 1, 1) +Ouput: 0 + +Example 3 + +Input: @ints = (4, 7, 1, 10, 7, 4, 1, 1) +Output: 28 + +triplets of 1, 4, 7 = 3x2×2 = 12 combinations +triplets of 1, 4, 10 = 3×2×1 = 6 combinations +triplets of 4, 7, 10 = 2×2×1 = 4 combinations +triplets of 1, 7, 10 = 3x2x1 = 6 combinations + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 17th September + 2023. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +""" +from itertools import combinations + +def utCount(tup): + return len( + tuple( + filter( ### set of unequal triplets will have 3 members + lambda x: x==3, + map( ### then count set's member + lambda x: len(x), + map( ### convert list of values to set to remove duplicated values + lambda x: set(x), + map( ### map combo of index (i,j,k) to list of correspoing values + lambda x: ( tup[i] for i in x ), + combinations(range(len(tup)),3) + ) + ) + ) + ) + ) + ) + +for inpt,otpt in { + (4, 4, 2, 4, 3): 3, + (1, 1, 1, 1, 1): 0, + (4, 7, 1, 10, 7, 4, 1, 1): 28, + }.items(): + print(utCount(inpt)==otpt) -- cgit From 4d8d74f952f94a94436c3f0bb3993b38d9bb124c Mon Sep 17 00:00:00 2001 From: Michael Firkins Date: Wed, 13 Sep 2023 03:16:23 +1000 Subject: pwc234 solution in go --- challenge-234/pokgopun/go/ch-1.go | 87 ++++++++++++++++++++++++++++++++++ challenge-234/pokgopun/go/ch-2.go | 98 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 185 insertions(+) create mode 100644 challenge-234/pokgopun/go/ch-1.go create mode 100644 challenge-234/pokgopun/go/ch-2.go diff --git a/challenge-234/pokgopun/go/ch-1.go b/challenge-234/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..ed7c9e0ee5 --- /dev/null +++ b/challenge-234/pokgopun/go/ch-1.go @@ -0,0 +1,87 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-234/ +/*# + +Task 1: Common Characters + +Submitted by: [43]Mohammad S Anwar + __________________________________________________________________ + + You are given an array of words made up of alphabetic characters only. + + Write a script to return all alphabetic characters that show up in all + words including duplicates. + +Example 1 + +Input: @words = ("java", "javascript", "julia") +Output: ("j", "a") + +Example 2 + +Input: @words = ("bella", "label", "roller") +Output: ("e", "l", "l") + +Example 3 + +Input: @words = ("cool", "lock", "cook") +Output: ("c", "o") + +Task 2: Unequal Triplets +#*/ + +package main + +import ( + "fmt" + "slices" + "strings" +) + +func main() { + var mw mwords + for _, data := range []struct { + input []string + output []rune + }{ + {[]string{"java", "javascript", "julia"}, []rune{'j', 'a'}}, + {[]string{"bella", "label", "roller"}, []rune{'e', 'l', 'l'}}, + {[]string{"cool", "lock", "cook"}, []rune{'c', 'o'}}, + } { + mw.words = data.input + fmt.Println(slices.Equal(mw.commonChars(), data.output)) + } +} + +type mchar struct { + chr rune + cnt int +} + +type mwords struct { + words []string + word0 []mchar +} + +func (mw mwords) commonChars() (r []rune) { + for _, c := range mw.words[0] { + mw.word0 = append(mw.word0, mchar{c, 1}) + } + //fmt.Println(mw.word0) + for _, w := range mw.words[1:] { + for i := range mw.word0 { + ri := strings.IndexRune(w, mw.word0[i].chr) + if ri != -1 { + mw.word0[i].cnt++ + w = string(slices.Delete([]rune(w), ri, ri+1)) + } + } + } + //fmt.Println(mc) + l := len(mw.words) + for _, v := range mw.word0 { + if v.cnt == l { + r = append(r, v.chr) + } + } + return r +} diff --git a/challenge-234/pokgopun/go/ch-2.go b/challenge-234/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..6ce69c3cf3 --- /dev/null +++ b/challenge-234/pokgopun/go/ch-2.go @@ -0,0 +1,98 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-234/ +/*# + +Task 2: Unequal Triplets + +Submitted by: [44]Mohammad S Anwar + __________________________________________________________________ + + 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]. + +Example 1 + +Input: @ints = (4, 4, 2, 4, 3) +Ouput: 3 + +(0, 2, 4) because 4 != 2 != 3 +(1, 2, 4) because 4 != 2 != 3 +(2, 3, 4) because 2 != 4 != 3 + +Example 2 + +Input: @ints = (1, 1, 1, 1, 1) +Ouput: 0 + +Example 3 + +Input: @ints = (4, 7, 1, 10, 7, 4, 1, 1) +Output: 28 + +triplets of 1, 4, 7 = 3x2×2 = 12 combinations +triplets of 1, 4, 10 = 3×2×1 = 6 combinations +triplets of 4, 7, 10 = 2×2×1 = 4 combinations +triplets of 1, 7, 10 = 3x2x1 = 6 combinations + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 17th September + 2023. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +*/ +package main + +import ( + "fmt" + "strings" +) + +func main() { + for _, data := range []struct { + inpt []int + otpt int + }{ + {[]int{4, 4, 2, 4, 3}, 3}, + {[]int{1, 1, 1, 1, 1}, 0}, + {[]int{4, 7, 1, 10, 7, 4, 1, 1}, 28}, + } { + fmt.Println(utCount(data.inpt) == data.otpt) + } +} + +func utCount(s []int) (count int) { + l := len(s) + m := make(map[int]int) + var c string + for _, idxs := range strings.Split(combo(3, string(ibytes(l)), "", &c, byte(l)), string(byte(l))) { + for _, v := range []byte(idxs) { + m[s[int(v)]]++ + } + if len(m) == 3 { + count++ + } + clear(m) + } + return count +} +func ibytes(n int) []byte { + b := make([]byte, n) + for i := 0; i < n; i++ { + b[i] = byte(i) + } + return b +} +func combo(r int, e, c string, res *string, sep byte) string { + lc, le := len(c), len(e) + if lc == r || lc+le == r { + *res += string(sep) + (c + e)[:r] + return "" + } else { + for i := 0; i <= lc+le-r; i++ { + combo(r, e[i+1:], c+string(e[i]), res, sep) + } + } + return (*res)[1:] +} -- cgit From d55c8b606ab76cd6626e982ced137e84010ea0b4 Mon Sep 17 00:00:00 2001 From: "E. Choroba" Date: Tue, 12 Sep 2023 19:43:39 +0200 Subject: Solve 234: Common Characters & Unequal Triplets by E. Choroba --- challenge-234/e-choroba/perl/ch-1.pl | 27 ++++++++++++++++ challenge-234/e-choroba/perl/ch-2.pl | 61 ++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100755 challenge-234/e-choroba/perl/ch-1.pl create mode 100755 challenge-234/e-choroba/perl/ch-2.pl diff --git a/challenge-234/e-choroba/perl/ch-1.pl b/challenge-234/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..5492129484 --- /dev/null +++ b/challenge-234/e-choroba/perl/ch-1.pl @@ -0,0 +1,27 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +use List::Util qw{ min }; + +sub common_characters(@words) { + my %seen; + for my $index (0 .. $#words) { + ++$seen{$_}[$index] for split //, $words[$index]; + } + return [map +(($_) x min(map $_ // 0, @{ $seen{$_} }[0 .. $#words])), + keys %seen] +} + +use Test2::V0; +plan 3 + 1; + +is common_characters('java', 'javascript', 'julia'), + bag { item $_ for qw( j a ) }, 'Example 1'; +is common_characters('bella', 'label', 'roller'), + bag { item $_ for qw( e l l ) }, 'Example 2'; +is common_characters('cool', 'lock', 'cook'), + bag { item $_ for qw( c o ) }, 'Example 3'; + +is common_characters('abc', 'xyz', 'abc'), [], 'No common chars'; diff --git a/challenge-234/e-choroba/perl/ch-2.pl b/challenge-234/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..cd5774df59 --- /dev/null +++ b/challenge-234/e-choroba/perl/ch-2.pl @@ -0,0 +1,61 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +use List::Util qw{ product }; + +sub unequal_triplets(@ints) { + my $count = 0; + for my $i (0 .. $#ints - 2) { + for my $j ($i + 1 .. $#ints - 1) { + for my $k ($j + 1 .. $#ints) { + ++$count if $ints[$i] != $ints[$j] + && $ints[$j] != $ints[$k] + && $ints[$k] != $ints[$i] + } + } + } + return $count +} + +sub unequal_triplets_optimized(@ints) { + my %seen; + ++$seen{$_} for @ints; + my @unique = keys %seen; + my $count = 0; + for my $i (0 .. $#unique - 2) { + for my $j ($i + 1 .. $#unique - 1) { + for my $k ($j + 1 .. $#unique) { + $count += product(@seen{ @unique[$i, $j, $k] }); + } + } + } + return $count +} + +use Test::More tests => 3 + 3 + 2; + +is unequal_triplets(4, 4, 2, 4, 3), 3, 'Example 1 naive'; +is unequal_triplets(1, 1, 1, 1, 1), 0, 'Example 2 naive'; +is unequal_triplets(4, 7, 1, 10, 7, 4, 1, 1), 28, 'Example 3 naive'; + +is unequal_triplets_optimized(4, 4, 2, 4, 3), 3, 'Example 1 optimized'; +is unequal_triplets_optimized(1, 1, 1, 1, 1), 0, 'Example 2 optimized'; +is unequal_triplets_optimized(4, 7, 1, 10, 7, 4, 1, 1), 28, + 'Example 3 optimized'; + +my @long = ((1) x 100, (2) x 30, (3) x 20, (4) x 10, 5); +is unequal_triplets(@long), 123100, 'Long naiv