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 /challenge-216 | |
| 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.
Diffstat (limited to 'challenge-216')
| -rw-r--r-- | challenge-216/ulrich-rieke/cpp/ch-1.cpp | 55 | ||||
| -rw-r--r-- | challenge-216/ulrich-rieke/haskell/ch-1.hs | 20 | ||||
| -rw-r--r-- | challenge-216/ulrich-rieke/perl/ch-1.pl | 24 | ||||
| -rw-r--r-- | challenge-216/ulrich-rieke/perl/ch-2.pl | 127 | ||||
| -rw-r--r-- | challenge-216/ulrich-rieke/raku/ch-1.raku | 12 | ||||
| -rw-r--r-- | challenge-216/ulrich-rieke/rust/ch-1.rs | 28 |
6 files changed, 266 insertions, 0 deletions
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 ) ; +} |
