aboutsummaryrefslogtreecommitdiff
path: root/challenge-184
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2022-09-27 09:48:40 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2022-09-27 09:48:40 +0100
commit642b9c4df5cd463cbb354cbcfbefecb97cc97fa5 (patch)
treef389ae9815c2884a8eb405663d23554f5fd6c30e /challenge-184
parentd2dbff2eae0647c51ba7795ed75b7978b1896177 (diff)
downloadperlweeklychallenge-club-642b9c4df5cd463cbb354cbcfbefecb97cc97fa5.tar.gz
perlweeklychallenge-club-642b9c4df5cd463cbb354cbcfbefecb97cc97fa5.tar.bz2
perlweeklychallenge-club-642b9c4df5cd463cbb354cbcfbefecb97cc97fa5.zip
- Added solutions by Ulrich Rieke.
Diffstat (limited to 'challenge-184')
-rw-r--r--challenge-184/ulrich-rieke/cpp/ch-1.cpp36
-rw-r--r--challenge-184/ulrich-rieke/cpp/ch-2.cpp54
-rw-r--r--challenge-184/ulrich-rieke/haskell/ch-1.hs10
-rw-r--r--challenge-184/ulrich-rieke/haskell/ch-2.hs13
-rw-r--r--challenge-184/ulrich-rieke/perl/ch-1.pl29
-rw-r--r--challenge-184/ulrich-rieke/perl/ch-2.pl36
-rw-r--r--challenge-184/ulrich-rieke/raku/ch-1.raku25
-rw-r--r--challenge-184/ulrich-rieke/raku/ch-2.raku33
-rw-r--r--challenge-184/ulrich-rieke/rust/ch-1.rs35
-rw-r--r--challenge-184/ulrich-rieke/rust/ch-2.rs43
10 files changed, 314 insertions, 0 deletions
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 <vector>
+#include <iostream>
+#include <string>
+
+int main( ) {
+ std::vector<std::string> input ;
+ std::cout << "Please enter a string, starting with 2 digits and then 4 letters!\n" ;
+ std::cout << "enter <return> 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<std::string> 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 <iostream>
+#include <string>
+#include <vector>
+#include <cctype>
+#include <iterator>
+#include <algorithm>
+
+int main( ) {
+ std::cout << "Please enter lines of letters and digits only, separated by blanks!\n" ;
+ std::cout << "enter <return> to end!\n" ;
+ std::string line ;
+ std::getline( std::cin , line ) ;
+ std::vector<std::string> allInput ;
+ while ( !line.empty( ) ) {
+ allInput.push_back( line ) ;
+ std::getline( std::cin , line ) ;
+ }
+ std::vector<std::vector<int>> allDigits ;
+ std::vector<std::vector<char>> allLetters ;
+ for ( const auto & s : allInput ) {
+ std::vector<int> currentDigits ;
+ std::vector<char> 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<char>(
+ std::cout , " " )) ;
+ std::cout << "]," ;
+ }
+ std::cout << "] and [" ;
+ for ( const auto & sequence : allLetters ) {
+ std::cout << "[ " ;
+ std::copy( sequence.begin( ) , sequence.end( ) , std::ostream_iterator<char>(
+ 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, <return> to end!" ;
+my @list ;
+my $line = <STDIN> ;
+chomp $line ;
+while ( $line ) {
+ while ( $line !~ /^[[:lower:]]{2}[[:digit:]]{4}$/ ) {
+ say "String should start with two lower-case letters and the 4 digits!" ;
+ $line = <STDIN> ;
+ chomp $line ;
+ }
+ push @list , $line ;
+ $line = <STDIN> ;
+ 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, <return> to end!" ;
+my @list ;
+my $line = <STDIN> ;
+chomp $line ;
+while ( $line ) {
+ push @list , $line ;
+ $line = <STDIN> ;
+ 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 <return> 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, <return> 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 <return> to end!" ) ;
+ let mut input : Vec<String> = 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<String> = 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 <return> 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<char>> = Vec::new( ) ;
+ let mut all_digits : Vec<Vec<char>> = Vec::new( ) ;
+ for a_line in all_lines {
+ let mut current_letters : Vec<char> = Vec::new( ) ;
+ let mut current_digits : Vec<char> = 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(())
+}