From 274bf4a39a2a89b95ebbc89cea4d775f2c8eaead Mon Sep 17 00:00:00 2001 From: Mohammad Sajid Anwar Date: Mon, 22 Jul 2024 13:50:28 +0100 Subject: - Added solutions by Ulrich Rieke. --- challenge-279/ulrich-rieke/cpp/ch-1.cpp | 38 ++++++++++++++++++++++++++++++ challenge-279/ulrich-rieke/cpp/ch-2.cpp | 14 +++++++++++ challenge-279/ulrich-rieke/haskell/ch-1.hs | 16 +++++++++++++ challenge-279/ulrich-rieke/haskell/ch-2.hs | 12 ++++++++++ challenge-279/ulrich-rieke/perl/ch-1.pl | 17 +++++++++++++ challenge-279/ulrich-rieke/perl/ch-2.pl | 18 ++++++++++++++ challenge-279/ulrich-rieke/raku/ch-1.raku | 11 +++++++++ challenge-279/ulrich-rieke/raku/ch-2.raku | 6 +++++ challenge-279/ulrich-rieke/rust/ch-1.rs | 33 ++++++++++++++++++++++++++ challenge-279/ulrich-rieke/rust/ch-2.rs | 18 ++++++++++++++ 10 files changed, 183 insertions(+) create mode 100755 challenge-279/ulrich-rieke/cpp/ch-1.cpp create mode 100755 challenge-279/ulrich-rieke/cpp/ch-2.cpp create mode 100755 challenge-279/ulrich-rieke/haskell/ch-1.hs create mode 100755 challenge-279/ulrich-rieke/haskell/ch-2.hs create mode 100755 challenge-279/ulrich-rieke/perl/ch-1.pl create mode 100755 challenge-279/ulrich-rieke/perl/ch-2.pl create mode 100755 challenge-279/ulrich-rieke/raku/ch-1.raku create mode 100755 challenge-279/ulrich-rieke/raku/ch-2.raku create mode 100755 challenge-279/ulrich-rieke/rust/ch-1.rs create mode 100755 challenge-279/ulrich-rieke/rust/ch-2.rs (limited to 'challenge-279') diff --git a/challenge-279/ulrich-rieke/cpp/ch-1.cpp b/challenge-279/ulrich-rieke/cpp/ch-1.cpp new file mode 100755 index 0000000000..8d6d6444c8 --- /dev/null +++ b/challenge-279/ulrich-rieke/cpp/ch-1.cpp @@ -0,0 +1,38 @@ +#include +#include +#include +#include +#include +#include + +std::vector tokenize( const std::string & text , char delimiter ) { + std::istringstream allTokens { text } ; + std::vector tokens ; + std::string tok ; + while ( std::getline( allTokens , tok , delimiter ) ) { + tokens.push_back( tok ) ; + } + return tokens ; +} + +int main( ) { + std::cout << "Enter some letters, separated by blanks!\n" ; + std::string line ; + std::getline( std::cin , line ) ; + std::vector letterstrings { tokenize( line , ' ' ) } ; + std::cout << "Enter some weights, separated by blanks!\n" ; + std::getline( std::cin , line ) ; + std::vector weightstrings { tokenize( line , ' ' ) } ; + std::vector> allPairs ; + for ( int i = 0 ; i < letterstrings.size( ) ; i++ ) { + allPairs.push_back( std::make_pair( letterstrings[i].front( ) , std::stoi( + weightstrings[i].substr( 0 , 1 ) ) ) ) ; + } + std::sort( allPairs.begin( ) , allPairs.end( ) , []( const auto & p1 , const auto & p2) { + return p1.second < p2.second ; } ) ; + for ( auto p : allPairs ) + std::cout << p.first ; + std::cout << '\n' ; + return 0 ; +} + diff --git a/challenge-279/ulrich-rieke/cpp/ch-2.cpp b/challenge-279/ulrich-rieke/cpp/ch-2.cpp new file mode 100755 index 0000000000..84455bf78a --- /dev/null +++ b/challenge-279/ulrich-rieke/cpp/ch-2.cpp @@ -0,0 +1,14 @@ +#include +#include +#include + +int main( ) { + std::cout << "Enter a string!\n" ; + std::string line ; + std::getline( std::cin , line ) ; + std::string vowels {"aeiouAEIOU"} ; + std::cout << std::boolalpha << (std::count_if( line.begin( ) , line.end( ), + [vowels]( char c ) { return vowels.find( c ) != std::string::npos ; + } ) % 2 == 0 ) << '\n' ; + return 0 ; +} diff --git a/challenge-279/ulrich-rieke/haskell/ch-1.hs b/challenge-279/ulrich-rieke/haskell/ch-1.hs new file mode 100755 index 0000000000..eab77a6aa0 --- /dev/null +++ b/challenge-279/ulrich-rieke/haskell/ch-1.hs @@ -0,0 +1,16 @@ +module Challenge279 + where +import Data.Char ( digitToInt ) +import Data.List ( sortOn ) + +solution :: String -> String -> String +solution letters weights = filter (\c -> c /= ' ' ) $ unwords $ map fst $ sortOn snd $ zip + ( words letters ) ( map (\w -> digitToInt $ head w ) $ words weights ) + +main :: IO ( ) +main = do + putStrLn "Enter some letters separated by blanks!" + letterline <- getLine + putStrLn "Enter some weights separated by blanks!" + weightline <- getLine + print $ solution letterline weightline diff --git a/challenge-279/ulrich-rieke/haskell/ch-2.hs b/challenge-279/ulrich-rieke/haskell/ch-2.hs new file mode 100755 index 0000000000..8f04f999fe --- /dev/null +++ b/challenge-279/ulrich-rieke/haskell/ch-2.hs @@ -0,0 +1,12 @@ +module Challenge279_2 + where + +solution :: String -> Bool +solution str = ( length $ filter (\c -> elem c "aeiouAEIOU" ) str ) `mod` 2 + == 0 + +main :: IO ( ) +main = do + putStrLn "Enter a string!" + line <- getLine + print $ solution line diff --git a/challenge-279/ulrich-rieke/perl/ch-1.pl b/challenge-279/ulrich-rieke/perl/ch-1.pl new file mode 100755 index 0000000000..6c55640fec --- /dev/null +++ b/challenge-279/ulrich-rieke/perl/ch-1.pl @@ -0,0 +1,17 @@ +#!/usr/bin/perl ; +use strict ; +use warnings ; +use feature 'say' ; +use List::Util qw ( zip ) ; + +say "Enter some letters , separated by blanks!" ; +my $letters = ; +chomp $letters ; +my @letters = split( /\s+/ , $letters ) ; +say "Enter some weights , separated by blanks!" ; +my $weights = ; +chomp $weights ; +my @weights = split( /\s+/ , $weights ) ; +my @zipped = zip( \@letters , \@weights ) ; +my @sorted = sort { $a->[1] <=> $b->[1] } @zipped ; +say join( '' , map { $_->[0] } @sorted ) ; diff --git a/challenge-279/ulrich-rieke/perl/ch-2.pl b/challenge-279/ulrich-rieke/perl/ch-2.pl new file mode 100755 index 0000000000..0d0cfa31b4 --- /dev/null +++ b/challenge-279/ulrich-rieke/perl/ch-2.pl @@ -0,0 +1,18 @@ +#!/usr/bin/perl ; +use strict ; +use warnings ; +use feature 'say' ; + +#the task amounts to verifying whether the number of vowels in the string +#is even or odd +say "Enter a string!" ; +my $line = ; +chomp $line ; +my $vowels = "aeiouAEIOU" ; +if ( scalar( grep { index( $vowels , $_ ) != -1 } split( // , $line ) ) + % 2 == 0 ) { + say "true" ; +} +else { + say "false" ; +} diff --git a/challenge-279/ulrich-rieke/raku/ch-1.raku b/challenge-279/ulrich-rieke/raku/ch-1.raku new file mode 100755 index 0000000000..99104226cd --- /dev/null +++ b/challenge-279/ulrich-rieke/raku/ch-1.raku @@ -0,0 +1,11 @@ +use v6 ; + +say "Enter some letters , separated by blanks!" ; +my $letters = $*IN.get ; +say "Enter some weights, separated by blanks!" ; +my $weights = $*IN.get ; +my @letters_in = $letters.words ; +my @weights_in = $weights.words.map( {.Int} ) ; +my @pairs = @letters_in Z, @weights_in ; +my @sorted = @pairs.sort( *.[1] ).map( {$_[0]} ) ; +say @sorted.join ; diff --git a/challenge-279/ulrich-rieke/raku/ch-2.raku b/challenge-279/ulrich-rieke/raku/ch-2.raku new file mode 100755 index 0000000000..e7df535f8f --- /dev/null +++ b/challenge-279/ulrich-rieke/raku/ch-2.raku @@ -0,0 +1,6 @@ +use v6 ; + +say "Enter a string!" ; +my $line = $*IN.get ; +my @vowels = "aeiouAEIOU".comb ; +say $line.comb.grep( {$_ (elem) @vowels} ).elems %% 2 ; diff --git a/challenge-279/ulrich-rieke/rust/ch-1.rs b/challenge-279/ulrich-rieke/rust/ch-1.rs new file mode 100755 index 0000000000..d0e0429eea --- /dev/null +++ b/challenge-279/ulrich-rieke/rust/ch-1.rs @@ -0,0 +1,33 @@ +use std::io ; + +fn main() { + println!("Enter some letters!"); + let mut letterline : String = String::new( ) ; + io::stdin( ).read_line( &mut letterline ).unwrap( ) ; + let letterstr : &str = letterline.as_str( ).trim( ) ; + println!("Enter some weights!") ; + let mut weightline : String = String::new( ) ; + io::stdin( ).read_line( &mut weightline ).unwrap( ) ; + let weightstr : &str = weightline.as_str( ).trim( ) ; + let weights : Vec = weightstr.split_whitespace( ).map( | s | + s.parse::( ).unwrap( )).collect( ) ; + let mut letters_entered : String = String::new( ) ; + for c in letterstr.chars( ) { + if c != ' ' { + letters_entered.push( c ) ; + } + } + let single_letters = letters_entered.as_str( ) ; + let mut pairs : Vec<(char,usize)> = single_letters.chars( ).zip( weights.into_iter( )). + collect( ) ; + let pair_slice = pairs.as_mut_slice( ) ; + pair_slice.sort_by( | a , b | a.1.cmp( &b.1 )) ; + let sorted_pairs : Vec<(char, usize)> = pair_slice.to_vec( ) ; + let final_chars : Vec = sorted_pairs.into_iter( ).map( | p | p.0 ). + collect( ) ; + let mut solution : String = String::new( ) ; + for c in final_chars { + solution.push( c ) ; + } + println!("{:?}" , solution ) ; +} diff --git a/challenge-279/ulrich-rieke/rust/ch-2.rs b/challenge-279/ulrich-rieke/rust/ch-2.rs new file mode 100755 index 0000000000..92dcc015f4 --- /dev/null +++ b/challenge-279/ulrich-rieke/rust/ch-2.rs @@ -0,0 +1,18 @@ +use std::io ; + +fn is_vowel( c : char ) -> bool { + let vowels : &str = "aeiouAEIOU" ; + vowels.find( c ).is_some( ) +} + +//the task amounts to verifying whether the number of vowels in the string +//is even or odd +fn main() { + println!("Enter a string!"); + let mut inline : String = String::new( ) ; + io::stdin( ).read_line( &mut inline ).unwrap( ) ; + let entered_line : &str = inline.as_str( ).trim( ) ; + let result : bool = entered_line.chars( ).filter( | &c | is_vowel( c )) + .count( ) % 2 == 0 ; + println!("{}" , result ) ; +} -- cgit