diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2023-05-11 23:29:02 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2023-05-11 23:29:02 +0100 |
| commit | 6129c5d5a28972223b3b9df7d90179b9cd35493d (patch) | |
| tree | de26b1e506e87a801c2209e93f6804074427a382 | |
| parent | d56749df940d5687c02afd8743c56172fa997a13 (diff) | |
| download | perlweeklychallenge-club-6129c5d5a28972223b3b9df7d90179b9cd35493d.tar.gz perlweeklychallenge-club-6129c5d5a28972223b3b9df7d90179b9cd35493d.tar.bz2 perlweeklychallenge-club-6129c5d5a28972223b3b9df7d90179b9cd35493d.zip | |
- Added solutions by Ulrich Rieke.
- Added solutions by Roger Bell_West.
- Added solutions by David Ferrone.
- Added solutions by Peter Campbell Smith.
- Added solutions by Thomas Kohler.
- Added solutions by Arne Sommer.
- Added solutions by Jaldhar H. Vyas.
26 files changed, 3075 insertions, 2695 deletions
diff --git a/challenge-016/jaldhar-h-vyas/perl5/ch-1.pl b/challenge-016/jaldhar-h-vyas/perl/ch-1.pl index 36b54c0b35..36b54c0b35 100755 --- a/challenge-016/jaldhar-h-vyas/perl5/ch-1.pl +++ b/challenge-016/jaldhar-h-vyas/perl/ch-1.pl diff --git a/challenge-016/jaldhar-h-vyas/perl5/ch-2.pl b/challenge-016/jaldhar-h-vyas/perl/ch-2.pl index f83ec5c488..f83ec5c488 100755 --- a/challenge-016/jaldhar-h-vyas/perl5/ch-2.pl +++ b/challenge-016/jaldhar-h-vyas/perl/ch-2.pl diff --git a/challenge-016/jaldhar-h-vyas/perl6/ch-1.p6 b/challenge-016/jaldhar-h-vyas/raku/ch-1.p6 index 1dd07a146c..1dd07a146c 100755 --- a/challenge-016/jaldhar-h-vyas/perl6/ch-1.p6 +++ b/challenge-016/jaldhar-h-vyas/raku/ch-1.p6 diff --git a/challenge-016/jaldhar-h-vyas/perl6/ch-2.p6 b/challenge-016/jaldhar-h-vyas/raku/ch-2.p6 index 8a10ee17f7..8a10ee17f7 100755 --- a/challenge-016/jaldhar-h-vyas/perl6/ch-2.p6 +++ b/challenge-016/jaldhar-h-vyas/raku/ch-2.p6 diff --git a/challenge-216/ulrich-rieke/cpp/ch-1.cpp b/challenge-216/ulrich-rieke/cpp/ch-1.cpp new file mode 100644 index 0000000000..e51a101c2a --- /dev/null +++ b/challenge-216/ulrich-rieke/cpp/ch-1.cpp @@ -0,0 +1,55 @@ +#include <iostream> +#include <vector> +#include <string> +#include <cctype> + +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 & regi , const std::string & word ) { + for ( std::string::size_type i = 0 ; i < regi.length( ) ; i++ ) { + if ( word.find( regi.substr( i , 1 ) ) == std::string::npos ) { + return false ; + } + } + return true ; +} + +int main( ) { + std::cout << "Please 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 , separated by blanks, 2 parts of a registration number!\n" ; + std::getline( std::cin , line ) ; + std::vector<std::string> regiparts( split( line , " " ) ) ; + std::string registration ( regiparts[ 0 ] + regiparts[ 1 ] ) ; + std::string relevant_word ; + for ( auto c : registration ) { + if ( std::isalpha( c ) ) + relevant_word.push_back( static_cast<char>( tolower( c ) ) ) ; + } + std::vector<std::string> selected ; + for ( auto word : words ) + if ( condition ( relevant_word , word ) ) + selected.push_back( word ) ; + std::cout << "(" ; + for ( auto word : selected ) { + std::cout << word ; + if ( word != selected.back( ) ) + std::cout << " , " ; + else + std::cout << ")\n" ; + } + return 0 ; +} diff --git a/challenge-216/ulrich-rieke/haskell/ch-1.hs b/challenge-216/ulrich-rieke/haskell/ch-1.hs new file mode 100644 index 0000000000..b0269d8ec8 --- /dev/null +++ b/challenge-216/ulrich-rieke/haskell/ch-1.hs @@ -0,0 +1,20 @@ +module Challenge216 + where +import Data.Char ( toLower , isAlpha) +import qualified Data.Set as S + +solution :: [String] -> String -> [String] +solution theWords registration = filter (\w -> regiset `S.isSubsetOf` (S.fromList +w )) theWords +where + regiset = S.fromList registration + +main :: IO ( ) +main = do + putStrLn "Please enter some words, separated by blanks!" + theWords <- getLine + putStrLn "Please enter a registration number in 2 parts!" + regiline <- getLine + let allWords = words theWords + relevant_word = map toLower $ filter isAlpha $ foldl1 ( ++ ) $ words regiline + print $ solution allWords relevant_word diff --git a/challenge-216/ulrich-rieke/perl/ch-1.pl b/challenge-216/ulrich-rieke/perl/ch-1.pl new file mode 100644 index 0000000000..340f20fc1f --- /dev/null +++ b/challenge-216/ulrich-rieke/perl/ch-1.pl @@ -0,0 +1,24 @@ +#!/usr/bin/perl ; +use strict ; +use warnings ; +use feature 'say' ; +use List::Util qw ( all ) ; + +sub condition { + my $regi = shift ; + my $word = shift ; + return all { index( $word , $_ ) != -1 } split( // , $regi ) ; +} + +say "Please enter some words, separated by blanks!" ; +my $line = <STDIN> ; +chomp $line ; +my @words = split( /\s/ , $line ) ; +say "Please enter a registration number with 2 parts, separated by blanks!" ; +$line = <STDIN> ; +chomp $line ; +my @regiparts = split( /\s/ , $line ) ; +my $registration = $regiparts[ 0 ] . $regiparts[ 1 ] ; +my $relevant_word = join ( '' , grep { $_ =~ /[a-z]/ } split( // , +lc $registration )) ; +say "(" . join( ',' , grep { condition( $relevant_word , $_ ) } @words ) . ")" ; diff --git a/challenge-216/ulrich-rieke/perl/ch-2.pl b/challenge-216/ulrich-rieke/perl/ch-2.pl new file mode 100644 index 0000000000..94ad1ce19d --- /dev/null +++ b/challenge-216/ulrich-rieke/perl/ch-2.pl @@ -0,0 +1,127 @@ +#!/usr/bin/perl ; +use strict ; +use warnings ; +use feature 'say' ; +use List::Util qw ( any all max ) ; + +sub find_intersection { + my $firstArray = shift ; + my $secondArray = shift ; + my %firstHash ; + my %secondHash ; + for my $letter( @{$firstArray} ) { + $firstHash{ $letter }++ ; + } + for my $letter( @{$secondArray} ) { + $secondHash{ $letter }++ ; + } + return grep { exists( $secondHash{ $_ } ) } keys %firstHash ; +} + +sub find_most_frequent { + my $array = shift ; + my %vals ; + for my $num ( @{$array} ) { + $vals{ $num }++ ; + } + my $maximum = max ( values %vals ) ; + my @nums = grep { $vals{ $_ } == $maximum } keys %vals ; + return $nums[ 0 ] ; +} + +#which letters are provided by which sticker ? +sub find_letter_distribution { + my $stickerArray = shift ; + my %letterDistri ; + for my $i ( 0 .. scalar ( @{$stickerArray} - 1 ) ) { + for my $letter ( split ( // , $stickerArray->[ $i ] ) ) { + push @{$letterDistri{ $letter }} , $i ; + } + } + return %letterDistri ; +} + +say "Please enter some sticker words, separated by blanks!" ; +my $line = <STDIN> ; +chomp $line ; +my @stickers = split( /\s/ , $line ) ; +say "Please enter a word!" ; +my $word = <STDIN> ; +chomp $word ; +my %letters_in_words ;#in which words do I find a given letter ? +my $multistickers = 0 ;#how many stickers have to be used more than once? +my %supplied ; #how many letters are offered ? +my %needed ; #how many letters are needed ? +#which letters are supplied ? for every letter, fill a hash showing +#in which word the letter was found +%letters_in_words = find_letter_distribution( \@stickers ) ; +#tabulate how many letters are needed +for my $letter ( split( // , $word ) ) { + $needed{ $letter }++ ; +} +for my $w ( @stickers ) { + for my $let ( split // , $w ) { + $supplied{ $let }++ ; + } +} +my @supplied_letters = keys %supplied ; +my @needed_letters = keys %needed ; +#is there any needed character which is not supplied ? +if ( any { not exists( $supplied{ $_ } ) } @needed_letters ) { + say 0 ; +} +#we supply enough characters +elsif ( all { $supplied{ $_ } >= $needed{ $_ } } @needed_letters ) { + my %uniques ; #see in which stickers you found letters + my @sorted = sort { $a cmp $b } split ( // , $word ) ; + my @found ; + for my $letter ( @sorted ) { +#look up in which sticker the character needed was found. Select the word +#where it was found most often + push @found , find_most_frequent( \@{$letters_in_words{ $letter }} ) ; + } + map { $uniques{ $_ }++ } @found ; + say scalar( keys %uniques ) ; +} +else { #we have at least one supplied letter for every letter we need, but +#we have to draw more than one sticker of the same word to fulfill our +#needs. We draw more than one from the words that supply the needed letter, +#in this order : 1)multiply the word that provides the letter needed +#2)if there are more than 1 word that provide letters find out which word +#contributes most letters to the word + my @missing = grep { $supplied{ $_ } < $needed{ $_ } } @needed_letters ; + for my $letter ( @missing ) { + if ( all { $_ == ${$letters_in_words{ $letter }}[0] } + @{$letters_in_words{ $letter }} ) { + $multistickers = $needed{ $letter } - $supplied{ $letter } ; + my $num = ${$letters_in_words{ $letter }}[0] ; + $stickers[ $num ] = $stickers[ $num ] . ($stickers[ $num ] x + $multistickers) ; + } + else { #we find the missing letters in more than 1 word +#which of the stickers has the greatest intersection with our target word ? +#which word of @stickers contributes most ? + my %places_found ; + for my $num ( @{$letters_in_words{ $letter }} ) { + $places_found{ $num }++ ; + } + my @sorted = sort { $places_found{$b} <=> $places_found{$a} } keys + %places_found ; + $multistickers = $needed{ $letter } - $supplied{ $letter } - + $sorted[ 0 ] ; + my $selected = grep { $places_found{ $_ } == $sorted[ 0 ] } keys + %places_found ; + $stickers[ $selected ] = $stickers[ $selected ] . ( + $stickers[ $selected ] x $multistickers ) ; + } + } + %letters_in_words = find_letter_distribution(\@stickers) ; + my %uniqes ; + my @sorted = sort { $a cmp $b } split( // , $word ) ; + my @found ; + for my $letter ( @sorted ) { + push @found , find_most_frequent( \@{$letters_in_words{ $letter }} ) ; + } + map { $uniqes{ $_ }++ } @found ; + say ( scalar( keys %uniqes ) + $multistickers ) ; +} diff --git a/challenge-216/ulrich-rieke/raku/ch-1.raku b/challenge-216/ulrich-rieke/raku/ch-1.raku new file mode 100644 index 0000000000..65b4b25a71 --- /dev/null +++ b/challenge-216/ulrich-rieke/raku/ch-1.raku @@ -0,0 +1,12 @@ +use v6 ; + +say "Enter some words , separated by blanks!" ; +my $line = $*IN.get ; +my @words = $line.words ; +say "Enter the 2 parts of a registration number!" ; +$line = $*IN.get ; +my @registrationline = $line.words ; +my $registration = @registrationline[0] ~ @registrationline[1] ; +my $comp = set($registration.lc.comb.grep( { $_ ~~ / <alpha> / } )) ; +my @selected = @words.grep( { $comp (<=) set( $_.comb) } ) ; +say "(" ~ @selected.join( ',' ) ~ ")" ; diff --git a/challenge-216/ulrich-rieke/rust/ch-1.rs b/challenge-216/ulrich-rieke/rust/ch-1.rs new file mode 100644 index 0000000000..7743114c8a --- /dev/null +++ b/challenge-216/ulrich-rieke/rust/ch-1.rs @@ -0,0 +1,28 @@ +use std::io ; + +fn main( ) { + println!("Enter some words, separated by blanks!" ) ; + let mut inline : String = String::new( ) ; + io::stdin( ).read_line( &mut inline ).unwrap( ) ; + let mut regiline : String = String::new( ) ; + println!("Enter a registration numbers, separated by blanks!" ) ; + io::stdin( ).read_line( &mut regiline ).unwrap( ) ; + let entered_line : &str = &*inline ; + let words : Vec<&str> = entered_line.split_whitespace( ).map( | s | + s.trim( ) ).collect( ) ; + let regi : &str = &*regiline ; + let registration : Vec<&str> = regi.split_whitespace( ).map( | s | + s.trim( ) ).collect( ) ; + let mut all_lowers : String = String::new( ) ; + for w in registration { + for c in w.chars( ) { + if c.is_alphabetic( ) { + all_lowers.push( c.to_ascii_lowercase( ) ) ; + } + } + } + let reg : &str = all_lowers.as_str( ) ; + let selected : Vec<&&str> = words.iter( ).filter( | &s | + reg.chars( ).all( | l | s.contains( *&l ))).collect( ) ; + println!("{:?}" , selected ) ; +} diff --git a/stats/pwc-challenge-016.json b/stats/pwc-challenge-016.json index d2c7ff6ace..f6d1b2d6a8 100644 --- a/stats/pwc-challenge-016.json +++ b/stats/pwc-challenge-016.json @@ -1,25 +1,36 @@ { - "xAxis" : { - "type" : "category" + "subtitle" : { + "text" : "[Champions: 31] Last updated at 2023-05-11 22:21:41 GMT" }, "title" : { "text" : "The Weekly Challenge - 016" }, + "chart" : { + "type" : "column" + }, + "tooltip" : { + "pointFormat" : "<span style='color:{point.color}'>{point.name}</span>: <b>{point.y:f}</b><br/>", + "headerFormat" : "<span style='font-size:11px'>{series.name}</span><br/>", + "followPointer" : 1 + }, + "yAxis" : { + "title" : { + "text" : "Total Solutions" + } + }, "plotOptions" : { "series" : { + "borderWidth" : 0, "dataLabels" : { "enabled" : 1, "format" : "{point.y}" - }, - "borderWidth" : 0 + } } }, - "legend" : { - "enabled" : 0 - }, "drilldown" : { "series" : [ { + "name" : "Adam Russell", "data" : [ [ "Perl", @@ -30,21 +41,19 @@ 1 ] ], - "name" : "Adam Russell", "id" : "Adam Russell" }, { + "id" : "Andrezgz", "data" : [ [ "Perl", 1 ] ], - "name" : "Andrezgz", - "id" : "Andrezgz" + "name" : "Andrezgz" }, { - "id" : "Arne Sommer", "name" : "Arne Sommer", "data" : [ [ @@ -55,7 +64,8 @@ "Blog", 1 ] - ] + ], + "id" : "Arne Sommer" }, { "name" : "Athanasius", @@ -76,14 +86,14 @@ ] }, { - "id" : "Daniel Mantovani", "name" : "Daniel Mantovani", "data" : [ [ "Perl", 2 ] - ] + ], + "id" : "Daniel Mantovani" }, { "name" : "Dave Jacoby", @@ -96,28 +106,27 @@ ] }, { - "name" : "Duane Powell", "id" : "Duane Powell", "data" : [ [ "Perl", 1 ] - ] + ], + "name" : "Duane Powell" }, { + "name" : "Duncan C. White", + "id" : "Duncan C. White", "data" : [ [ "Perl", 2 ] - ], - "id" : "Duncan C. White", - "name" : "Duncan C. White" + ] }, { "name" : "E. Choroba", - "id" : "E. Choroba", "data" : [ [ "Perl", @@ -127,10 +136,10 @@ "Blog", 1 ] - ] + ], + "id" : "E. Choroba" }, { - "id" : "Feng Chang", "name" : "Feng Chang", "data" : [ [ @@ -141,44 +150,48 @@ "Raku", 2 ] - ] + ], + "id" : "Feng Chang" }, { + "name" : "Francis Whittle", "data" : [ [ "Raku", 2 ] ], - "name" : "Francis Whittle", "id" : "Francis Whittle" }, { "id" : "Gustavo Chaves", - "name" : "Gustavo Chaves", "data" : [ [ "Perl", 1 ] - ] + ], + "name" : "Gustavo Chaves" }, { + "name" : "Jaldhar H. Vyas", "data" : [ [ "Perl", - 1 + 2 ], [ "Raku", + 2 + ], + [ + "Blog", 1 ] ], - "name" : "Jaldhar H. Vyas", "id" : "Jaldhar H. Vyas" }, { - "name" : "Joelle Maslak", "id" : "Joelle Maslak", "data" : [ [ @@ -189,31 +202,31 @@ "Raku", 2 ] - ] + ], + "name" : "Joelle Maslak" }, { - "name" : "Jorg Sommrey", "id" : "Jorg Sommrey", "data" : [ [ "Perl", 1 ] - ] + ], + "name" : "Jorg Sommrey" }, { - "name" : "Kevin Colyer", - "id" : "Kevin Colyer", "data" : [ [ "Raku", 2 ] - ] + ], + "id" : "Kevin Colyer", + "name" : "Kevin Colyer" }, { "id" : "Kian-Meng Ang", - "name" : "Kian-Meng Ang", "data" : [ [ "Perl", @@ -223,19 +236,22 @@ "Blog", 1 ] - ] + ], + "name" : "Kian-Meng Ang" }, { + "id" : "Lakpa Tashi Bhutia", "data" : [ [ "Perl", 1 ] ], - "name" : "Lakpa Tashi Bhutia", - "id" : "Lakpa Tashi Bhutia" + "name" : "Lakpa Tashi Bhutia" }, { + "name" : "Laurent Rosenfeld", + "id" : "Laurent Rosenfeld", "data" : [ [ "Perl", @@ -249,39 +265,37 @@ "Blog", 3 ] - ], - "id" : "Laurent Rosenfeld", - "name" : "Laurent Rosenfeld" + ] }, { - "id" : "Lubos Kolouch", - "name" : "Lubos Kolouch", "data" : [ [ "Perl", 2 ] - ] + ], + "id" : "Lubos Kolouch", + "name" : "Lubos Kolouch" }, { + "name" : "Michael Hamlin", + "id" : "Michael Hamlin", "data" : [ [ "Perl", 1 ] - ], - "id" : "Michael Hamlin", - "name" : "Michael Hamlin" + ] }, { + "id" : "Noud Aldenhoven", "data" : [ [ "Raku", 2 ] ], - "name" : "Noud Aldenhoven", - "id" : "Noud Aldenhoven" + "name" : "Noud Aldenhoven" }, { "data" : [ @@ -290,8 +304,8 @@ 1 ] ], - "name" : "Ozzy", - "id" : "Ozzy" + "id" : "Ozzy", + "name" : "Ozzy" }, { "data" : [ @@ -300,12 +314,10 @@ 2 ] ], - "name" : "Paulo Custodio", - "id" : "Paulo Custodio" + "id" : "Paulo Custodio", + "name" : "Paulo Custodio" }, { - "name" : "Roger Bell_West", - "id" : "Roger Bell_West", "data" : [ [ "Perl", @@ -315,9 +327,12 @@ "Blog", 1 ] - ] + ], + "id" : "Roger Bell_West", + "name" : "Roger Bell_West" }, { + "id" : "Ruben Westerberg", "data" : [ [ "Perl", @@ -328,38 +343,37 @@ 2 ] ], - "id" : "Ruben Westerberg", "name" : "Ruben Westerberg" }, { "name" : "Simon Proctor", - "id" : "Simon Proctor", "data" : [ [ "Raku", 1 ] - ] + ], + "id" : "Simon Proctor" }, { - "name" : "Steven Wilson", "id" : "Steven Wilson", "data" : [ [ "Perl", 2 ] - ] + ], + "name" : "Steven Wilson" }, { + "id" : "Stuart Little", "data" : [ [ "Raku", 2 ] ], - "name" : "Stuart Little", - "id" : "Stuart Little" + "name" : "Stuart Little" }, { "data" : [ @@ -368,12 +382,10 @@ 1 ] ], - "name" : "Veesh Goldman", - "id" : "Veesh Goldman" + "id" : "Veesh Goldman", + "name" : "Veesh Goldman" }, { - "id" : "Yozen Hernandez", - "name" : "Yozen Hernandez", "data" : [ [ "Perl", @@ -383,81 +395,87 @@ "Blog", 2 ] - ] + ], + "id" : "Yozen Hernandez", + "name" : "Yozen Hernandez" } ] }, + "xAxis" : { + "type" : "category" + }, "series" : [ { + "colorByPoint" : 1, "data" : [ { "name" : "Adam Russell", - "y" : 3, - "drilldown" : "Adam Russell" + "drilldown" : "Adam Russell", + "y" : 3 }, { "drilldown" : "Andrezgz", - "y" : 1, - "name" : "Andrezgz" + "name" : "Andrezgz", + "y" : 1 }, { - "drilldown" : "Arne Sommer", + "y" : 3, "name" : "Arne Sommer", - "y" : 3 + "drilldown" : "Arne Sommer" }, { + "name" : "Athanasius", "drilldown" : "Athanasius", - "y" : 5, - "name" : "Athanasius" + "y" : 5 }, { - "name" : "Daniel Mantovani", "y" : 2, - "drilldown" : "Daniel Mantovani" + "drilldown" : "Daniel Mantovani", + "name" : "Daniel Mantovani" }, { + "name" : "Dave Jacoby", "drilldown" : "Dave Jacoby", - "y" : 1, - "name" : "Dave Jacoby" + "y" : 1 }, { - "name" : "Duane Powell", "y" : 1, - "drilldown" : "Duane Powell" + "drilldown" : "Duane Powell", + "name" : "Duane Powell" }, { - "y" : 2, + "drilldown" : "Duncan C. White", "name" : "Duncan C. White", - "drilldown" : "Duncan C. White" + "y" : 2 }, { + "y" : 3, "drilldown" : "E. Choroba", - "name" : "E. Choroba", - "y" : 3 + "name" : "E. Choroba" }, { + "y" : 4, "drilldown" : "Feng Chang", - "name" : "Feng Chang", - "y" : 4 + "name" : "Feng Chang" }, { - "drilldown" : "Francis Whittle", "name" : "Francis Whittle", + "drilldown" : "Francis Whittle", "y" : 2 }, { - "y" : 1, + "drilldown" : "Gustavo Chaves", "name" : "Gustavo Chaves", - "drilldown" : "Gustavo Chaves" + "y" : 1 }, { "drilldown" : "Jaldhar H. Vyas", "name" : "Jaldhar H. Vyas", - "y" : 2 + "y" : 5 }, { - "drilldown" : "Joelle Maslak", "name" : "Joelle Maslak", + "drilldown" : "Joelle Maslak", "y" : 4 }, { @@ -466,24 +484,24 @@ "y" : 1 }, { - "y" : 2, + "drilldown" : "Kevin Colyer", "name" : "Kevin Colyer", - "drilldown" : "Kevin Colyer" + "y" : 2 }, { "name" : "Kian-Meng Ang", - "y" : 2, - "drilldown" : "Kian-Meng Ang" + "drilldown" : "Kian-Meng Ang", + "y" : 2 }, { "y" : 1, - "name" : "Lakpa Tashi Bhutia", - "drilldown" : "Lakpa Tashi Bhutia" + "drilldown" : "Lakpa Tashi Bhutia", + "name" : "Lakpa Tashi Bhutia" }, { - "name" : "Laurent Rosenfeld", "y" : 7, - "drilldown" : "Laurent Rosenfeld" + "drilldown" : "Laurent Rosenfeld", + "name" : "Laurent Rosenfeld" }, |
