From 642b9c4df5cd463cbb354cbcfbefecb97cc97fa5 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Tue, 27 Sep 2022 09:48:40 +0100 Subject: - Added solutions by Ulrich Rieke. --- challenge-184/ulrich-rieke/cpp/ch-1.cpp | 36 ++++++++++++++++++++ challenge-184/ulrich-rieke/cpp/ch-2.cpp | 54 ++++++++++++++++++++++++++++++ challenge-184/ulrich-rieke/haskell/ch-1.hs | 10 ++++++ challenge-184/ulrich-rieke/haskell/ch-2.hs | 13 +++++++ challenge-184/ulrich-rieke/perl/ch-1.pl | 29 ++++++++++++++++ challenge-184/ulrich-rieke/perl/ch-2.pl | 36 ++++++++++++++++++++ challenge-184/ulrich-rieke/raku/ch-1.raku | 25 ++++++++++++++ challenge-184/ulrich-rieke/raku/ch-2.raku | 33 ++++++++++++++++++ challenge-184/ulrich-rieke/rust/ch-1.rs | 35 +++++++++++++++++++ challenge-184/ulrich-rieke/rust/ch-2.rs | 43 ++++++++++++++++++++++++ 10 files changed, 314 insertions(+) create mode 100644 challenge-184/ulrich-rieke/cpp/ch-1.cpp create mode 100644 challenge-184/ulrich-rieke/cpp/ch-2.cpp create mode 100644 challenge-184/ulrich-rieke/haskell/ch-1.hs create mode 100644 challenge-184/ulrich-rieke/haskell/ch-2.hs create mode 100644 challenge-184/ulrich-rieke/perl/ch-1.pl create mode 100644 challenge-184/ulrich-rieke/perl/ch-2.pl create mode 100644 challenge-184/ulrich-rieke/raku/ch-1.raku create mode 100644 challenge-184/ulrich-rieke/raku/ch-2.raku create mode 100644 challenge-184/ulrich-rieke/rust/ch-1.rs create mode 100644 challenge-184/ulrich-rieke/rust/ch-2.rs (limited to 'challenge-184') diff --git a/challenge-184/ulrich-rieke/cpp/ch-1.cpp b/challenge-184/ulrich-rieke/cpp/ch-1.cpp new file mode 100644 index 0000000000..92fe5d702c --- /dev/null +++ b/challenge-184/ulrich-rieke/cpp/ch-1.cpp @@ -0,0 +1,36 @@ +#include +#include +#include + +int main( ) { + std::vector input ; + std::cout << "Please enter a string, starting with 2 digits and then 4 letters!\n" ; + std::cout << "enter to end!\n" ; + std::string line ; + std::getline( std::cin , line ) ; + while ( !line.empty( ) ) { + input.push_back( line ) ; + std::getline( std::cin , line ) ; + } + int count = 0 ; + std::vector output ; + for ( auto it = input.begin( ) ; it != input.end( ) ; ++it ) { + std::string newWord ; + if ( count < 10 ) { + newWord = "0" + std::to_string( count ) + it->substr( 2 ) ; + } + if ( count > 9 ) { + newWord = std::to_string( count ) + it->substr( 2 ) ; + } + output.push_back( newWord ) ; + count++ ; + } + std::cout << '(' ; + for ( const auto & w : output ) { + std::cout << w ; + if ( w != output.back( ) ) + std::cout << ", " ; + } + std::cout << ')' << std::endl ; + return 0 ; +} diff --git a/challenge-184/ulrich-rieke/cpp/ch-2.cpp b/challenge-184/ulrich-rieke/cpp/ch-2.cpp new file mode 100644 index 0000000000..4f05b42cf2 --- /dev/null +++ b/challenge-184/ulrich-rieke/cpp/ch-2.cpp @@ -0,0 +1,54 @@ +#include +#include +#include +#include +#include +#include + +int main( ) { + std::cout << "Please enter lines of letters and digits only, separated by blanks!\n" ; + std::cout << "enter to end!\n" ; + std::string line ; + std::getline( std::cin , line ) ; + std::vector allInput ; + while ( !line.empty( ) ) { + allInput.push_back( line ) ; + std::getline( std::cin , line ) ; + } + std::vector> allDigits ; + std::vector> allLetters ; + for ( const auto & s : allInput ) { + std::vector currentDigits ; + std::vector currentLetters ; + for ( char c : s ) { + if ( std::islower( c ) ) { + currentLetters.push_back( c ) ; + } + if ( std::isdigit( c ) ) { + currentDigits.push_back( c ) ; + } + } + if ( currentDigits.size( ) > 0 ) { + allDigits.push_back( currentDigits ) ; + } + if ( currentLetters.size( ) > 0 ) { + allLetters.push_back( currentLetters ) ; + } + } + std::cout << '[' ; + for ( const auto & sequence : allDigits ) { + std::cout << "[ " ; + std::copy( sequence.begin( ) , sequence.end( ) , std::ostream_iterator( + std::cout , " " )) ; + std::cout << "]," ; + } + std::cout << "] and [" ; + for ( const auto & sequence : allLetters ) { + std::cout << "[ " ; + std::copy( sequence.begin( ) , sequence.end( ) , std::ostream_iterator( + std::cout , " " )) ; + std::cout << "]," ; + } + std::cout << ']' << std::endl ; + return 0 ; +} diff --git a/challenge-184/ulrich-rieke/haskell/ch-1.hs b/challenge-184/ulrich-rieke/haskell/ch-1.hs new file mode 100644 index 0000000000..f773db06f4 --- /dev/null +++ b/challenge-184/ulrich-rieke/haskell/ch-1.hs @@ -0,0 +1,10 @@ +module Challenge184 + where + +convert :: String -> Int -> String +convert st n + |n < 10 = "0" ++ show n ++ ( drop 2 st ) + |otherwise = show n ++ ( drop 2 st ) + +solution :: [String] -> [String] +solution strings = map (\st -> convert ( fst st ) ( snd st )) $ zip strings [0 ..] diff --git a/challenge-184/ulrich-rieke/haskell/ch-2.hs b/challenge-184/ulrich-rieke/haskell/ch-2.hs new file mode 100644 index 0000000000..f75cca1536 --- /dev/null +++ b/challenge-184/ulrich-rieke/haskell/ch-2.hs @@ -0,0 +1,13 @@ +module Challenge184_2 + where +import Data.Char ( digitToInt , isDigit , isLower ) +import Data.List( intersperse ) + +takeApart :: String -> ( [Int] , [Char] ) +takeApart st = (map digitToInt $ filter isDigit st , filter isLower st) + +solution :: [String] -> ([[Int]] , [[Char]] ) +solution input = ( filter ( not .null ) $ map fst pairs , + filter ( not . null ) $ map ( intersperse ',' ) $ map snd pairs ) +where + pairs = map takeApart input diff --git a/challenge-184/ulrich-rieke/perl/ch-1.pl b/challenge-184/ulrich-rieke/perl/ch-1.pl new file mode 100644 index 0000000000..6ee50b2e6a --- /dev/null +++ b/challenge-184/ulrich-rieke/perl/ch-1.pl @@ -0,0 +1,29 @@ +#!/usr/bin/perl ; +use strict ; +use warnings ; +use feature 'say' ; + +say "Enter a string , starting with 2 letters and then 4 digits, to end!" ; +my @list ; +my $line = ; +chomp $line ; +while ( $line ) { + while ( $line !~ /^[[:lower:]]{2}[[:digit:]]{4}$/ ) { + say "String should start with two lower-case letters and the 4 digits!" ; + $line = ; + chomp $line ; + } + push @list , $line ; + $line = ; + chomp $line ; +} +my @output ; +for my $i ( 0 .. $#list ) { + if ( $i < 10 ) { + push @output , "0$i" . substr( $list[ $i ] , 2 ) ; + } + else { + push @output , "$i" . substr( $list[ $i ] , 2 ) ; + } +} +say '(' . join( ',' , @output ) . ')' ; diff --git a/challenge-184/ulrich-rieke/perl/ch-2.pl b/challenge-184/ulrich-rieke/perl/ch-2.pl new file mode 100644 index 0000000000..e61f0662d4 --- /dev/null +++ b/challenge-184/ulrich-rieke/perl/ch-2.pl @@ -0,0 +1,36 @@ +#!/usr/bin/perl ; +use strict ; +use warnings ; +use feature 'say' ; + +say "Enter single letters and digits, separated by a blank, to end!" ; +my @list ; +my $line = ; +chomp $line ; +while ( $line ) { + push @list , $line ; + $line = ; + chomp $line ; +} ; +my @numberlist ; +my @letterlist ; +for my $str ( @list ) { + my @currentNumbers ; + my @currentLetters ; + for my $l ( split( /\s/ , $str ) ) { + if ( $l =~ /^[[:digit:]]$/ ) { + push @currentNumbers , $l ; + } + if ( $l =~ /^[[:lower:]]$/ ) { + push @currentLetters , $l ; + } + } + if ( @currentNumbers ) { + push @numberlist , '[' . join( ',' , @currentNumbers ) . ']' ; + } + if ( @currentLetters ) { + push @letterlist , '[' . join( ',' , @currentLetters ) . ']' ; + } +} +print ('[' . join( ',' , @numberlist ) . ']' . " and " ) ; +say ( '[' . join( ',' , @letterlist ) . ']' ) ; diff --git a/challenge-184/ulrich-rieke/raku/ch-1.raku b/challenge-184/ulrich-rieke/raku/ch-1.raku new file mode 100644 index 0000000000..56ce6f4a5e --- /dev/null +++ b/challenge-184/ulrich-rieke/raku/ch-1.raku @@ -0,0 +1,25 @@ +use v6 ; + +say "Enter strings in the form of aa9999, that is 2 letters and 4 digits!" ; +say "Enter to end!" ; +my @strings ; +my $line = $*IN.get ; +while ( $line ) { + while ( $line !~~ /<[a..z]>**2<[0..9]>**4/ ) { + say "string should start with 2 letters and then 4 digits! Re-enter!" ; + $line = $*IN.get ; + } + @strings.push( $line ) ; + $line = $*IN.get ; +} +my @output ; +for (0..@strings.elems - 1 ) -> $i { + say $i ; + if ( $i < 10 ) { + @output.push( "0$i" ~ @strings[ $i ].substr( 2 ) ) ; + } + else { + @output.push( ~$i ~ @strings[ $i ].substr( 2 ) ) ; + } +} +say @output.join( ',' ) ; diff --git a/challenge-184/ulrich-rieke/raku/ch-2.raku b/challenge-184/ulrich-rieke/raku/ch-2.raku new file mode 100644 index 0000000000..7b290eadaa --- /dev/null +++ b/challenge-184/ulrich-rieke/raku/ch-2.raku @@ -0,0 +1,33 @@ +use v6 ; + +say "Enter a list of strings containing only digits and letters!" ; +say "They should be separated by blanks! Enter string by string, to end!" ; +my @strings ; +my $line = $*IN.get ; +while ( $line ) { + @strings.push( $line ) ; + $line = $*IN.get ; +} +my @numberlist ; +my @letterlist ; +for @strings -> $word { + my @currentNumbers ; + my @currentLetters ; + for $word.split( /\s/ ) -> $l { + if ( $l ~~ /<[0..9]>/ ) { + @currentNumbers.push( $l ) ; + } + if ( $l ~~ /<[a..z]>/ ) { + @currentLetters.push( $l ) ; + } + } + if ( @currentNumbers ) { + @numberlist.push( '[' ~ @currentNumbers.join( ',' ) ~ ']' ) ; + } + if ( @currentLetters ) { + @letterlist.push( '[' ~ @currentLetters.join( ',' ) ~ ']' ) ; + } +} +print ('[' ~ @numberlist.join( ',' ) ~ ']' ) ; +print " and " ; +say ('[' ~ @letterlist.join( ',' ) ~ ']' ) ; diff --git a/challenge-184/ulrich-rieke/rust/ch-1.rs b/challenge-184/ulrich-rieke/rust/ch-1.rs new file mode 100644 index 0000000000..851065e5e6 --- /dev/null +++ b/challenge-184/ulrich-rieke/rust/ch-1.rs @@ -0,0 +1,35 @@ +use std::io ; +use std::io::BufRead ; + +fn main() -> io::Result<( )> { + println!("Please enter strings with 2 starting letters, followed by 4 digits!") ; + println!("Enter to end!" ) ; + let mut input : Vec = Vec::new( ) ; + let mut lines = io::stdin( ).lock( ).lines( ) ; + while let Some( line ) = lines.next( ) { + let last_input = line.unwrap( ) ; + if last_input.len( ) == 0 { + break ; + } + else { + input.push( last_input.to_string( )) ; + } + } + let mut count : i32 = 0 ; + let mut output : Vec = Vec::new( ) ; + for a_word in input { + let word_ref : &str = &a_word.as_str( ) ; + let letterpart : &str = &word_ref[2..=5] ; + let mut combined = String::new( ) ; + if count < 10 { + combined = "0".to_owned( ) + &count.to_string( ) + &letterpart.to_owned( ); + } + else { + combined = count.to_string( ) + &letterpart.to_owned( ) ; + } + output.push( combined.clone( ) ) ; + count += 1 ; + } + println!("{:?}" , output ) ; + Ok(()) +} diff --git a/challenge-184/ulrich-rieke/rust/ch-2.rs b/challenge-184/ulrich-rieke/rust/ch-2.rs new file mode 100644 index 0000000000..892409b11c --- /dev/null +++ b/challenge-184/ulrich-rieke/rust/ch-2.rs @@ -0,0 +1,43 @@ +use std::io ; +use std::io::BufRead ; + +fn main() -> io::Result<( )> { + println!("Please enter strings of single letters and digits, separated by blanks!") ; + println!("Enter to end!" ) ; + let mut lines = io::stdin( ).lock( ).lines( ) ; + let mut user_input = String::new( ) ; + while let Some( line ) = lines.next( ) { + let last_input = line.unwrap( ) ; + if last_input.len( ) == 0 { + break ; + } + else { + user_input.push_str("\n" ) ; + } + user_input.push_str( &last_input ) ; + } + let all_lines : Vec<&str> = user_input.split("\n").collect( ) ; + let mut all_letters : Vec> = Vec::new( ) ; + let mut all_digits : Vec> = Vec::new( ) ; + for a_line in all_lines { + let mut current_letters : Vec = Vec::new( ) ; + let mut current_digits : Vec = Vec::new( ) ; + let string = a_line.to_string( ) ; + for c in string.chars( ) { + if c.is_numeric( ) { + current_digits.push( c ) ; + } + if c.is_lowercase( ) { + current_letters.push( c ) ; + } + } + if current_digits.len( ) > 0 { + all_digits.push( current_digits.clone( ) ) ; + } + if current_letters.len( ) > 0 { + all_letters.push( current_letters.clone( ) ) ; + } + } + println!("{:?} and {:?}" , all_digits , all_letters) ; + Ok(()) +} -- cgit