diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2023-06-17 09:08:23 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2023-06-17 09:08:23 +0100 |
| commit | feb941721732ebbad6afeddc133b25ea762d9854 (patch) | |
| tree | d7b76fd4b4ccf6756f453df8e3b05eb9e683b8cc | |
| parent | a4958e946480959cf52f795bda20da4ff60b9fbc (diff) | |
| download | perlweeklychallenge-club-feb941721732ebbad6afeddc133b25ea762d9854.tar.gz perlweeklychallenge-club-feb941721732ebbad6afeddc133b25ea762d9854.tar.bz2 perlweeklychallenge-club-feb941721732ebbad6afeddc133b25ea762d9854.zip | |
- Added solutions by Matthias Muth.
- Added solutions by Simon Proctor.
- Added solutions by Laurent Rosenfeld.
- Added solutions by Robert DiCicco.
- Added solutions by Ulrich Rieke.
- Added solutions by Thomas Kohler.
- Added solutions by Mark Anderson.
- Added solutions by E. Choroba.
- Added solutions by David Ferrone.
- Added solutions by W. Luis Mochan.
- Added solutions by Bob Lied.
- Added solutions by Peter Campbell Smith.
- Added solutions by Ali Muradi.
- Added solutions by Robbie Hatley.
- Added solutions by Jorg Sommrey.
- Added solutions by Lubos Kolouch.
- Added solutions by Niels van Dijke.
- Added solutions by Roger Bell_West.
- Added solutions by Arne Sommer.
- Added solutions by Robert Ransbottom.
- Added solutions by Andreas Vogele.
45 files changed, 3914 insertions, 2543 deletions
diff --git a/challenge-221/blog.txt b/challenge-221/bob-lied/blog.txt index 5b69803d6a..5b69803d6a 100644 --- a/challenge-221/blog.txt +++ b/challenge-221/bob-lied/blog.txt diff --git a/challenge-221/perl/ch-1.pl b/challenge-221/bob-lied/perl/ch-1.pl index 1978b64c5a..1978b64c5a 100755 --- a/challenge-221/perl/ch-1.pl +++ b/challenge-221/bob-lied/perl/ch-1.pl diff --git a/challenge-221/perl/ch-2.pl b/challenge-221/bob-lied/perl/ch-2.pl index f9de84d188..f9de84d188 100755 --- a/challenge-221/perl/ch-2.pl +++ b/challenge-221/bob-lied/perl/ch-2.pl diff --git a/challenge-221/eric-cheung/python/ch-1.py b/challenge-221/eric-cheung/python/ch-1.py new file mode 100755 index 0000000000..5e47922272 --- /dev/null +++ b/challenge-221/eric-cheung/python/ch-1.py @@ -0,0 +1,38 @@ +
+## Example 1
+## arrInputWords = ["cat", "bt", "hat", "tree"]
+## strChars = "atach"
+
+## Example 2
+arrInputWords = ["hello", "world", "challenge"]
+strChars = "welldonehopper"
+
+arrUniqChar = list(set(strChars))
+arrUniqCharCount = [strChars.count(charLoop) for charLoop in arrUniqChar]
+
+arrOutputWords = []
+nSumCharLength = 0
+
+for strLoop in arrInputWords:
+ arrCharCount = arrUniqCharCount[:]
+ bIsFound = True
+
+ for charLoop in strLoop:
+ if not charLoop in arrUniqChar:
+ bIsFound = False
+ break
+
+ nIndx = arrUniqChar.index(charLoop)
+
+ if arrCharCount[nIndx] == 0:
+ bIsFound = False
+ break
+
+ arrCharCount[nIndx] = arrCharCount[nIndx] - 1
+
+ if bIsFound:
+ arrOutputWords.append(strLoop)
+ nSumCharLength = nSumCharLength + len(strLoop)
+
+## print (arrOutputWords)
+print (nSumCharLength)
diff --git a/challenge-221/eric-cheung/python/ch-2.py b/challenge-221/eric-cheung/python/ch-2.py new file mode 100755 index 0000000000..0288a402e5 --- /dev/null +++ b/challenge-221/eric-cheung/python/ch-2.py @@ -0,0 +1,47 @@ +
+## Remarks
+## https://www.geeksforgeeks.org/generating-all-possible-subsequences-using-recursion/
+
+## Python3 code to print all possible subsequences for given array using recursion
+## Recursive function to print all possible subsequences for given array
+
+nMaxArrLen = 0
+
+def IsArithmetic (arrListCheck):
+
+ nDiff = arrListCheck[1] - arrListCheck[0]
+ for nIndxLoop in range(2, len(arrListCheck)):
+ if arrListCheck[nIndxLoop] - arrListCheck[nIndxLoop - 1] != nDiff:
+ return False
+
+ return True
+
+def GenSubseqArr (arrFunc, nIndx, subArrFunc):
+
+ global nMaxArrLen
+
+ ## Print the subsequence when reach the leaf of recursion tree
+ if nIndx == len(arrFunc):
+ ## Condition to avoid printing empty subsequence
+ ## if len(subArrFunc) > 0:
+ if len(subArrFunc) > 2 and IsArithmetic(subArrFunc):
+ ## print (subArrFunc)
+ if len(subArrFunc) > nMaxArrLen:
+ nMaxArrLen = len(subArrFunc)
+
+ else:
+ ## Subsequence without including the element at current nIndx
+ GenSubseqArr(arrFunc, nIndx + 1, subArrFunc)
+
+ ## Subsequence including the element at current nIndx
+ GenSubseqArr(arrFunc, nIndx + 1, subArrFunc + [arrFunc[nIndx]])
+
+ return
+
+arrGivenMain = [9, 4, 7, 2, 10] ## Example 1
+## arrGivenMain = [3, 6, 9, 12] ## Example 2
+## arrGivenMain = [20, 1, 15, 3, 10, 5, 8] ## Example 3
+
+GenSubseqArr(arrGivenMain, 0, [])
+
+print (nMaxArrLen)
diff --git a/challenge-221/laurent-rosenfeld/blog.txt b/challenge-221/laurent-rosenfeld/blog.txt new file mode 100644 index 0000000000..76372000c8 --- /dev/null +++ b/challenge-221/laurent-rosenfeld/blog.txt @@ -0,0 +1 @@ +https://blogs.perl.org/users/laurent_r/2023/06/perl-weekly-challenge-221-good-strings.html diff --git a/challenge-221/laurent-rosenfeld/blog1.txt b/challenge-221/laurent-rosenfeld/blog1.txt new file mode 100644 index 0000000000..d1008ecc08 --- /dev/null +++ b/challenge-221/laurent-rosenfeld/blog1.txt @@ -0,0 +1 @@ +https://blogs.perl.org/users/laurent_r/2023/06/perl-weekly-challenge-221-arithmetic-subsequence.html diff --git a/challenge-221/laurent-rosenfeld/perl/ch-1.pl b/challenge-221/laurent-rosenfeld/perl/ch-1.pl new file mode 100644 index 0000000000..dee5b9c37f --- /dev/null +++ b/challenge-221/laurent-rosenfeld/perl/ch-1.pl @@ -0,0 +1,28 @@ +use strict; +use warnings; +use feature 'say'; + +sub find_good { + my ($string, @words) = @_; + # say $string, " - ", "@words"; + my $length = 0; + my %chars; + $chars{$_}++ for split //, $string; + WORD: for my $word (@words) { + my %char_cpy = %chars; + for my $let (split //, $word) { + next WORD unless $char_cpy{$let}; + $char_cpy{$let}--; + } + $length += length $word; + } + return $length +} +for my $test ( + ["atach", [<cat bt hat tree>]], + ["atach", [<cat bt hat tree cata>]], + ["atach", [<cat bt hat tree ataca>]], + ["welldonehopper", [<hello world challenge>]]) { + printf "%-15s - %-22s => ", "@$test[0]", "@{@$test[1]}"; + say find_good @$test[0], @{@$test[1]}; +} diff --git a/challenge-221/laurent-rosenfeld/perl/ch-2.pl b/challenge-221/laurent-rosenfeld/perl/ch-2.pl new file mode 100644 index 0000000000..a577abbfbc --- /dev/null +++ b/challenge-221/laurent-rosenfeld/perl/ch-2.pl @@ -0,0 +1,25 @@ +use strict; +use warnings; +use feature 'say'; + +sub longest_subseq { + my @pairs; + for my $i (0..$#_) { + for my $j ($i+1..$#_) { + push @pairs, [$_[$i], $_[$j]]; + } + } + my %gaps; + $gaps{($_->[1] - $_->[0])}++ for @pairs; + my $max = 0; + for my $k (keys %gaps) { + $max = $gaps{$k} if $gaps{$k} > $max; + } + # For n gaps, we have n + 1 values + return $max + 1; +} + +for my $test ( [<9 4 7 2 10>], [<3 6 9 12>], [<20 1 15 3 10 5 8>] ) { + printf "%-18s => ", "@$test"; + say longest_subseq @$test; +} diff --git a/challenge-221/laurent-rosenfeld/raku/ch-1.raku b/challenge-221/laurent-rosenfeld/raku/ch-1.raku new file mode 100644 index 0000000000..b7d322e00d --- /dev/null +++ b/challenge-221/laurent-rosenfeld/raku/ch-1.raku @@ -0,0 +1,14 @@ +sub find-good ($string, @words) { + my $chars = $string.comb.Bag; + my $length = 0; + for @words -> $word { + $length += $word.chars if $word.comb.Bag ⊆ $chars; + } + return $length +} +for (("atach", <cat bt hat tree atac>), + ("atach", <cat bt hat tree ataca>), + ("welldonehopper", <hello world challenge>)) -> @test { + printf "%-15s - %-22s => ", "@test[0]", "@test[1]"; + say find-good @test[0], @test[1]; +} diff --git a/challenge-221/laurent-rosenfeld/raku/ch-2.raku b/challenge-221/laurent-rosenfeld/raku/ch-2.raku new file mode 100644 index 0000000000..9e11e6f79e --- /dev/null +++ b/challenge-221/laurent-rosenfeld/raku/ch-2.raku @@ -0,0 +1,11 @@ +sub longest-subseq (@in) { + my @pairs = @in.combinations: 2; + my %gaps; + %gaps{~($_[1] - $_[0])}++ for @pairs; + # For n gaps, we have n + 1 values + return %gaps.values.max + 1; +} + +for <9 4 7 2 10>, <3 6 9 12>, <20 1 15 3 10 5 8> -> @test { + printf "%-18s => %d\n", ~(@test), longest-subseq @test; +} diff --git a/challenge-221/perlboy1967/perl/ch1.pl b/challenge-221/perlboy1967/perl/ch-1.pl index a84549d1d4..a84549d1d4 100755 --- a/challenge-221/perlboy1967/perl/ch1.pl +++ b/challenge-221/perlboy1967/perl/ch-1.pl diff --git a/challenge-221/robert-dicicco/julia/ch-1.jl b/challenge-221/robert-dicicco/julia/ch-1.jl new file mode 100644 index 0000000000..0d75c4eebe --- /dev/null +++ b/challenge-221/robert-dicicco/julia/ch-1.jl @@ -0,0 +1,63 @@ +#!/usr/bin/env julia +#= +------------------------------------- +AUTHOR: Robert DiCicco +DATE : 2023-06-13 +Challenge 221 Task 1 Good Strings ( Julia ) +------------------------------------- +=# + +using Printf + +total_score = 0 + +words = [["cat", "bt", "hat", "tree"], ["hello", "world", "challenge"]] +chlist = ["atach","welldonehopper"] +cnt = 1 +listcnt = 1 + +for wds in words + @printf("Input: @words = %s\n",wds) + chars = chlist[cnt] + global total_score = 0 + score = 0 + for w in wds + ln = length(w) + for mycnt in 1:ln + tst = w[mycnt] + if occursin(tst, chlist[listcnt]) + score += 1 + else + break + end + end + if score == ln + @printf("%s\n",w) + total_score += score + score = 0 + end + end + @printf("\tTotal: %d\n", total_score) + println() + global listcnt += 1 +end + +#= +------------------------------------- +SAMPLE OUTPUT +julia .\GoodStrings.jl + +Input: @words = ["cat", "bt", "hat", "tree"] +cat +hat + Total: 6 + +Input: @words = ["hello", "world", "challenge"] +hello +world + Total: 10 +------------------------------------- +=# + + + diff --git a/challenge-221/robert-dicicco/perl/ch-1.pl b/challenge-221/robert-dicicco/perl/ch-1.pl new file mode 100644 index 0000000000..012a057a2f --- /dev/null +++ b/challenge-221/robert-dicicco/perl/ch-1.pl @@ -0,0 +1,60 @@ +#!/usr/bin/env perl +=begin comment +------------------------------------- +AUTHOR: Robert DiCicco +DATE : 2023-06-12 +Challenge 221 Task 1 Good Strings ( Perl ) +------------------------------------- +=cut +use strict; +use warnings; +use feature 'say'; + +my $total_score = 0; + +my @words = (["cat", "bt", "hat", "tree"], ["hello", "world", "challenge"]); +my @chlist = ("atach","welldonehopper"); +my $cnt = 0; +for my $word (@words) { + say "Input: \@words = (@$word)"; + my $chars = $chlist[$cnt]; + $total_score = 0; + for my $w (@$word) { + my $ln = length($w); + my $score = 0; + for (my $cnt = 0; $cnt < $ln; $cnt++){ + my $tst = substr($w,$cnt,1); + if ($chars =~ /$tst/) { + $score++; + } else { + last; + } + + } + if ($score == $ln) { + say $w; + $total_score += $score; + $score = 0; + } + } + say "\tTotal: ", $total_score; + say ""; + $cnt++; +} + +=begin comment +------------------------------------- +SAMPLE OUTPUT +perl .\GoodStrings.pl + +Input: @words = (cat bt hat tree) +cat +hat + Total: 6 + +Input: @words = (hello world challenge) +hello +world + Total: 10 +------------------------------------- +=cut diff --git a/challenge-221/robert-dicicco/python/ch-1.py b/challenge-221/robert-dicicco/python/ch-1.py new file mode 100644 index 0000000000..5433676bd3 --- /dev/null +++ b/challenge-221/robert-dicicco/python/ch-1.py @@ -0,0 +1,51 @@ +#!/usr/bin/env python +# ------------------------------------- +# AUTHOR: Robert DiCicco +# DATE : 2023-06-13 +# Challenge 221 Task 1 Good Strings ( Python ) +# ------------------------------------- + +total_score = 0 + +words = [["cat", "bt", "hat", "tree"], ["hello", "world", "challenge"]] +chlist = ["atach","welldonehopper"] +cnt = 0 +listcnt = 0 + +for wds in words: + print("Input: @words = ",wds) + chars = chlist[cnt] + total_score = 0 + score = 0 + for w in wds: + ln = len(w) + for mycnt in range(0,ln): + tst = w[mycnt] + if tst in chlist[listcnt]: + score += 1 + else: + break + if score == ln: + print(w) + total_score += score + score = 0 + print("\tTotal: ",total_score) + print("") + listcnt += 1 + +# ------------------------------------- +# SAMPLE OUTPUT +# python .\GoodStrings.py + +# Input: @words = ['cat', 'bt', 'hat', 'tree'] +# cat +# hat + # Total: 6 + +# Input: @words = ['hello', 'world', 'challenge'] +# hello +# world + # Total: 10 +# ------------------------------------- + + diff --git a/challenge-221/robert-dicicco/raku/ch-1.raku b/challenge-221/robert-dicicco/raku/ch-1.raku new file mode 100644 index 0000000000..2216add23d --- /dev/null +++ b/challenge-221/robert-dicicco/raku/ch-1.raku @@ -0,0 +1,58 @@ +#!/usr/bin/env raku +=begin comment +------------------------------------- +AUTHOR: Robert DiCicco +DATE : 2023-06-12 +Challenge 221 Task 1 Good Strings ( Raku ) +------------------------------------- +=end comment + +my $total_score = 0; + +my @words = (["cat", "bt", "hat", "tree"], ["hello", "world", "challenge"]); +my @chlist = ("atach","welldonehopper"); +my $cnt = 0; +for (@words) -> @word { + say "Input: \@words = ",@word; + my $chars = @chlist[$cnt]; + $total_score = 0; + for (@word) -> $w { + my $ln = $w.chars; + my $score = 0; + loop (my $cnt = 0; $cnt < $ln; $cnt++){ + my $tst = substr($w,$cnt,1); + if $chars.contains($tst) { + $score++; + } else { + last; + } + } + if ($score == $ln) { + say $w; + $total_score += $score; + $score = 0; + } + } + say "\tTotal: ", $total_score; + say ""; + $cnt++; +} + +=begin comment +------------------------------------- +SAMPLE OUTPUT +raku .\GoodStrings.rk + +Input: @words = [cat bt hat tree] +cat +hat + Total: 6 + +Input: @words = [hello world challenge] +hello +world + Total: 10 +------------------------------------- +=end comment + + diff --git a/challenge-221/robert-dicicco/ruby/ch-1.rb b/challenge-221/robert-dicicco/ruby/ch-1.rb new file mode 100644 index 0000000000..2bb6caae75 --- /dev/null +++ b/challenge-221/robert-dicicco/ruby/ch-1.rb @@ -0,0 +1,62 @@ +#!/usr/bin/env ruby +=begin +------------------------------------- +AUTHOR: Robert DiCicco +DATE : 2023-06-12 +Challenge 221 Task 1 Good Strings ( Ruby ) +------------------------------------- +=end + +total_score = 0 + +words = [["cat", "bt", "hat", "tree"], ["hello", "world", "challenge"]] +chlist = ["atach","welldonehopper"] +cnt = 0 +listcnt = 0 + +words.each do |wds| + puts("Input: @words = #{wds}") + chars = chlist[cnt] + total_score = 0 + wds.each do |w| + ln = w.length + score = 0 + for cnt in 0..ln-1 do + tst = w[cnt,1] + if chlist[listcnt].include? tst + score += 1 + else + break + end + end + if score == ln + puts("#{w}") + total_score += score + score = 0 + end + end + puts("\tTotal: #{total_score}\n\n") + cnt += 1 + listcnt += 1 +end + +=begin +------------------------------------- +SAMPLE OUTPUT +ruby .\GoodStrings.rb +Input: @words = ["cat", "bt", "hat", "tree"] +cat +hat + Total: 6 + +Input: @words = ["hello", "world", "challenge"] +hello +world + Total: 10 +------------------------------------- +=end + + + + + diff --git a/challenge-221/ulrich-rieke/cpp/ch-1.cpp b/challenge-221/ulrich-rieke/cpp/ch-1.cpp new file mode 100644 index 0000000000..71caaed73c --- /dev/null +++ b/challenge-221/ulrich-rieke/cpp/ch-1.cpp @@ -0,0 +1,64 @@ +#include <iostream> +#include <string> +#include <vector> +#include <map> +#include <algorithm> +#include <numeric> + +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 ; +} + +bool condition( const std::string & word , const std::map<char, int> & + provided ) { + std::map<char , int> lettercount ; + for ( char c : word ) + lettercount[c]++ ; + for ( char c : word ) { + if ( ! ( provided.find( c ) != provided.end( ) && + (provided.find( c ))->second >= + (lettercount.find( c ) )->second ) ) + return false ; + } + return true ; +} + + +int main( ) { + std::cout << "Enter some words, separated by blanks!\n" ; + std::string line ; + std::getline( std::cin , line ) ; + std::vector<std::string> words ( split( line, " " ) ) ; + std::cout << "Enter a word to take the characters from!\n" ; + std::string comparator ; + std::cin >> comparator ; + std::map<char , int> compCount ; + for ( char c : comparator ) + compCount[c]++ ; + std::vector<std::string> selected ; + for ( auto s : words ) { + if ( condition( s , compCount ) ) { + selected.push_back( s ) ; + } + } + if ( selected.size( ) > 0 ) { + std::vector<int> lengths ; + for ( auto s : selected ) + lengths.push_back( s.length( ) ) ; + std::cout << std::accumulate( lengths.begin( ) , lengths.end( ) , 0 ) + << std::endl ; + } + else { + std::cout << 0 << std::endl ; + } + return 0 ; +} diff --git a/challenge-221/ulrich-rieke/cpp/ch-2.cpp b/challenge-221/ulrich-rieke/cpp/ch-2.cpp new file mode 100644 index 0000000000..6375a48fa7 --- /dev/null +++ b/challenge-221/ulrich-rieke/cpp/ch-2.cpp @@ -0,0 +1,87 @@ +#include <iostream> +#include <vector> +#include <string> +#include <algorithm> +#include <cmath> + +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 ; +} + +std::string to_binary_string( int n , int len) { + std::string binstr ; + int total = n ; + while ( total != 0 ) { + binstr.append( std::to_string( total % 2 ) ) ; + total /= 2 ; + } + int difference = len - binstr.length( ) ; + if ( difference > 0 ) { + for ( int i = 0 ; i < difference ; i++ ) + binstr.append( std::string( "0" ) ) ; + } + std::reverse( binstr.begin( ) , binstr.end( ) ) ; + return binstr ; +} + +std::vector<int> extract( const std::vector<int> & numbers , + const std::string & binstring ) { + std::vector<int> selection ; + for ( int i = 0 ; i < binstring.length( ) ; i++ ) { + if ( binstring.substr( i , 1 ) == "1" ) + selection.push_back( numbers[ i ] ) ; + } + return selection ; +} + +bool is_arithmetic( const std::vector<int> & aSeq ) { + int len = aSeq.size( ) ; + int start = aSeq[1] - aSeq[0] ; + for ( int i = 1 ; i < len - 1 ; i++ ) { + if ( aSeq[ i + 1 ] - aSeq[ i ] != start ) { + return false ; + } + } + return true ; +} + +int main ( ) { + std::cout << "Enter some integers, separated by blanks!\n" ; + std::string line ; + std::getline( std::cin , line ) ; + std::vector<std::string> numberstrings ( split( line , " " ) ) ; + std::vector<int> numbers ; + for ( auto s : numberstrings ) + numbers.push_back( std::stoi( s ) ) ; + int len = numbers.size( ) ; + if ( is_arithmetic( numbers ) ) + std::cout << len << std::endl ; + else { + int maxlen = 0 ; + int limit = static_cast<int>( std::pow( 2.0 , + static_cast<double>( len )) - 1 ) ; + for ( int i = 0 ; i < limit + 1 ; i++ ) { + std::string binstring ( to_binary_string( i , len ) ) ; + if ( std::count( binstring.begin( ) , binstring.end( ) , + '1' ) > 1 ) { + std::vector<int> selection ( extract( numbers , binstring ) ) ; + if ( is_arithmetic( selection ) ) { + int sublen = selection.size( ) ; + if ( suble |
