aboutsummaryrefslogtreecommitdiff
path: root/challenge-185
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2022-10-04 20:17:51 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2022-10-04 20:17:51 +0100
commitc2c9d22ffcf55b02aa3a5aeda5553f5b556e81af (patch)
tree2975f24483e974256c048b6ccb9d6002733b20e9 /challenge-185
parentdf70f22400eb0a746fe221429e8a239407e3348e (diff)
downloadperlweeklychallenge-club-c2c9d22ffcf55b02aa3a5aeda5553f5b556e81af.tar.gz
perlweeklychallenge-club-c2c9d22ffcf55b02aa3a5aeda5553f5b556e81af.tar.bz2
perlweeklychallenge-club-c2c9d22ffcf55b02aa3a5aeda5553f5b556e81af.zip
- Added solutions by Ulrich Rieke.
Diffstat (limited to 'challenge-185')
-rw-r--r--challenge-185/ulrich-rieke/cpp/ch-1.cpp20
-rw-r--r--challenge-185/ulrich-rieke/cpp/ch-2.cpp45
-rw-r--r--challenge-185/ulrich-rieke/haskell/ch-1.hs16
-rw-r--r--challenge-185/ulrich-rieke/haskell/ch-2.hs23
-rw-r--r--challenge-185/ulrich-rieke/perl/ch-1.pl20
-rw-r--r--challenge-185/ulrich-rieke/perl/ch-2.pl28
-rw-r--r--challenge-185/ulrich-rieke/raku/ch-1.raku15
-rw-r--r--challenge-185/ulrich-rieke/raku/ch-2.raku23
-rw-r--r--challenge-185/ulrich-rieke/rust/ch-1.rs28
-rw-r--r--challenge-185/ulrich-rieke/rust/ch-2.rs17
10 files changed, 235 insertions, 0 deletions
diff --git a/challenge-185/ulrich-rieke/cpp/ch-1.cpp b/challenge-185/ulrich-rieke/cpp/ch-1.cpp
new file mode 100644
index 0000000000..244b482591
--- /dev/null
+++ b/challenge-185/ulrich-rieke/cpp/ch-1.cpp
@@ -0,0 +1,20 @@
+#include <iostream>
+#include <string>
+
+int main( ) {
+ std::cout << "Please enter a MAC address in the form hhhh.hhhh.hhhh!\n" ;
+ std::string address ;
+ std::getline( std::cin , address ) ;
+ int pos = 0 ;
+ std::string output ;
+ while ( pos < address.length( ) - 2 ) {
+ output.append( address.substr( pos , 2 )) ;
+ output.append( ":" ) ;
+ pos += 2 ;
+ if ( address.substr( pos , 1 ) == "." )
+ pos++ ;
+ }
+ //take the output substr because we want to leave out the final ':'
+ std::cout << output.substr(0 , output.length( ) - 1 ) << std::endl ;
+ return 0 ;
+}
diff --git a/challenge-185/ulrich-rieke/cpp/ch-2.cpp b/challenge-185/ulrich-rieke/cpp/ch-2.cpp
new file mode 100644
index 0000000000..cc3d7b73ae
--- /dev/null
+++ b/challenge-185/ulrich-rieke/cpp/ch-2.cpp
@@ -0,0 +1,45 @@
+#include <iostream>
+#include <string>
+#include <cctype>
+#include <algorithm>
+#include <vector>
+
+std::string substitute( const std::string & word ) {
+ std::string output ;
+ int count = 0 ;
+ int pos = 0 ;
+ while ( count < 4 ) {
+ if (std::isalnum( static_cast<int>(word[ pos ] )) != 0 ) {
+ output.append( "x") ;
+ pos++ ;
+ count++ ;
+ }
+ else {
+ output.append( word.substr( pos, 1 )) ;
+ pos++ ;
+ }
+ }
+ output.append( word.substr( pos )) ;
+ return output ;
+}
+
+int main( ) {
+ std::cout << "Please enter codes with a minimum length of 4!\n" ;
+ std::cout << "Enter an empty string to end!\n" ;
+ std::vector<std::string> allInput ;
+ std::string line ;
+ std::getline( std::cin , line ) ;
+ while ( line.length( ) > 0 ) {
+ allInput.push_back( line ) ;
+ std::getline( std::cin , line ) ;
+ }
+ std::transform( allInput.begin( ) , allInput.end( ) , allInput.begin( ),
+ substitute ) ;
+ for ( auto & s : allInput ) {
+ std::cout << s ;
+ if ( s != allInput.back( ) )
+ std::cout << " , " ;
+ }
+ std::cout << std::endl ;
+ return 0 ;
+}
diff --git a/challenge-185/ulrich-rieke/haskell/ch-1.hs b/challenge-185/ulrich-rieke/haskell/ch-1.hs
new file mode 100644
index 0000000000..e17f5d9494
--- /dev/null
+++ b/challenge-185/ulrich-rieke/haskell/ch-1.hs
@@ -0,0 +1,16 @@
+module Challenge185
+ where
+import Data.List.Split ( splitOn )
+import Data.List ( intercalate )
+
+miniconvert :: String -> String
+miniconvert quad = take 2 quad ++ ":" ++ drop 2 quad
+
+convert :: String -> String
+convert = intercalate ":" . map miniconvert . splitOn "."
+
+main :: IO ( )
+main = do
+ putStrLn "Enter a MAC address in the form hhhh.hhhh.hhhh!"
+ address <- getLine
+ putStrLn $ convert address
diff --git a/challenge-185/ulrich-rieke/haskell/ch-2.hs b/challenge-185/ulrich-rieke/haskell/ch-2.hs
new file mode 100644
index 0000000000..0fc9ad9faf
--- /dev/null
+++ b/challenge-185/ulrich-rieke/haskell/ch-2.hs
@@ -0,0 +1,23 @@
+module Challenge185_2
+ where
+import Data.Char ( isAlpha, isDigit )
+import Data.List.Split ( splitOn )
+import Data.List ( findIndices )
+
+step :: String -> String
+step [] = []
+step (l:ls)
+ |not (l == 'x' ) && (isAlpha l || isDigit l) = 'x': ls
+ |otherwise = l : step ls
+
+convert :: String -> String
+convert s = until myCondition step s
+where
+ myCondition :: String -> Bool
+ myCondition st = (length $ findIndices (== 'x') st) == 4
+
+main :: IO ( )
+main = do
+ putStrLn "Enter a number of codes, separated by a blank!"
+ inputline <- getLine
+ print $ map convert $ splitOn " " inputline
diff --git a/challenge-185/ulrich-rieke/perl/ch-1.pl b/challenge-185/ulrich-rieke/perl/ch-1.pl
new file mode 100644
index 0000000000..8d29925c1b
--- /dev/null
+++ b/challenge-185/ulrich-rieke/perl/ch-1.pl
@@ -0,0 +1,20 @@
+#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use feature 'say' ;
+
+say "Enter a MAC address in the form hhhh.hhhh.hhhh!" ;
+my $address = <STDIN> ;
+chomp $address ;
+while ( $address !~ /\A(\w{4}\.){2}\w{4}\z/ ) {
+ say "The MAC address should have the form hhhh.hhhh.hhhh!" ;
+ $address = <STDIN> ;
+ chomp $address ;
+}
+my @parts = split( /\./ , $address ) ;
+my @chunks ;
+for my $part( @parts ) {
+ push @chunks , substr( $part , 0 , 2 ) ;
+ push @chunks, substr( $part , 2 , 2 ) ;
+}
+say join( ':' , @chunks ) ;
diff --git a/challenge-185/ulrich-rieke/perl/ch-2.pl b/challenge-185/ulrich-rieke/perl/ch-2.pl
new file mode 100644
index 0000000000..4752a93b7b
--- /dev/null
+++ b/challenge-185/ulrich-rieke/perl/ch-2.pl
@@ -0,0 +1,28 @@
+#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use feature 'say' ;
+
+sub mySubstitute {
+ my $word = shift ;
+ my $pos = 0 ;
+ my $count = 0 ;
+ while ( $count < 4 ) {
+ if ( substr( $word , $pos , 1 ) =~ /\w/ ) {
+ substr( $word, $pos , 1 ) = "x" ;
+ $count++ ;
+ $pos++ ;
+ }
+ else {
+ $pos++ ;
+ }
+ }
+ return $word ;
+}
+
+say "Please enter some codes, separated by a blank!" ;
+my $line = <STDIN> ;
+chomp $line ;
+my @codes = split( /\s/ , $line ) ;
+my @changed = map { mySubstitute( $_ ) } @codes ;
+say join ( ',' , @changed ) ;
diff --git a/challenge-185/ulrich-rieke/raku/ch-1.raku b/challenge-185/ulrich-rieke/raku/ch-1.raku
new file mode 100644
index 0000000000..c84b294a32
--- /dev/null
+++ b/challenge-185/ulrich-rieke/raku/ch-1.raku
@@ -0,0 +1,15 @@
+use v6 ;
+
+say "Please enter a MAC address in the form hhhh.hhhh.hhhh!" ;
+my $line = $*IN.get ;
+while ( $line !~~ /^(\w **4 '.') ** 2 \w ** 4 $/ ) {
+ say "Enter a Mac address in the form hhhh.hhhh.hhhh!" ;
+ $line = $*IN.get ;
+}
+my @parts = split( /'.'/ , $line ) ;
+my @chunks ;
+for @parts -> $part {
+ @chunks.push( $part.substr( 0 , 2 )) ;
+ @chunks.push( $part.substr( 2 , 2 )) ;
+}
+say @chunks.join( ':' ) ;
diff --git a/challenge-185/ulrich-rieke/raku/ch-2.raku b/challenge-185/ulrich-rieke/raku/ch-2.raku
new file mode 100644
index 0000000000..d7aad90770
--- /dev/null
+++ b/challenge-185/ulrich-rieke/raku/ch-2.raku
@@ -0,0 +1,23 @@
+use v6 ;
+
+sub mySubstitution( Str $word is copy ) {
+ my $count = 0 ;
+ my $pos = 0 ;
+ while ( $count < 4 ) {
+ if ( $word.substr( $pos , 1 ) ~~ /\w/ ) {
+ $word.substr-rw( $pos , 1 ) = "x" ;
+ $count++ ;
+ $pos++ ;
+ }
+ else {
+ $pos++ ;
+ }
+ }
+ return $word ;
+}
+
+say "Enter some strings, separated by blanks!" ;
+my $line = $*IN.get ;
+my @words = $line.split( /\s/ ) ;
+my @transformed = @words.map( { mySubstitution( $_ ) } ) ;
+say @transformed.join( ',' ) ;
diff --git a/challenge-185/ulrich-rieke/rust/ch-1.rs b/challenge-185/ulrich-rieke/rust/ch-1.rs
new file mode 100644
index 0000000000..a033022a47
--- /dev/null
+++ b/challenge-185/ulrich-rieke/rust/ch-1.rs
@@ -0,0 +1,28 @@
+use std::io ;
+
+fn main() {
+ println!("Please enter an MAC address in the form hhhh.hhhh.hhhh!") ;
+ let mut input : String = String::new( ) ;
+ io::stdin( ).read_line( &mut input ).unwrap( ) ;
+ let mut entered_line : &str = &*input ;
+ entered_line = entered_line.trim( ) ;
+ let mut from : usize = 0 ;
+ let mut to : usize = 1 ;
+ let mut chunks : Vec<&str> = Vec::new( ) ;
+ while to < input.len( ) {
+ chunks.push( &entered_line[from..=to] ) ;
+ from += 2 ;
+ to += 2 ;
+ match entered_line.chars( ).nth( from ) {
+ Some ( '.' ) => { from += 1 ; to += 1 ; } ,
+ None => {} ,
+ _ => {} ,
+ }
+ }
+ let mut outputstring : String = String::new( ) ;
+ for s in chunks {
+ outputstring.push_str( &s.to_string( ) ) ;
+ outputstring.push( ':' ) ;
+ }
+ println!("{}" , &outputstring[0..outputstring.len( ) - 1] ) ;
+}
diff --git a/challenge-185/ulrich-rieke/rust/ch-2.rs b/challenge-185/ulrich-rieke/rust/ch-2.rs
new file mode 100644
index 0000000000..377baa891b
--- /dev/null
+++ b/challenge-185/ulrich-rieke/rust/ch-2.rs
@@ -0,0 +1,17 @@
+use std::io ;
+
+fn main() {
+ println!("Please enter some strings, separated by a blank!" ) ;
+ let mut input : String = String::new( ) ;
+ io::stdin( ).read_line( &mut input ).unwrap( ) ;
+ let entered_line = &*input ;
+ let mut outputwords : Vec<&str> = Vec::new( ) ;
+ let mut iter = entered_line.split_whitespace( ) ;
+ while let Some( word ) = iter.next( ) {
+ outputwords.push( word ) ;
+ }
+ for s in outputwords {
+ print!("{} " , s.replacen( char::is_alphanumeric , "x" , 4 )) ;
+ }
+ println!() ;
+}