aboutsummaryrefslogtreecommitdiff
path: root/challenge-215
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2023-05-02 19:35:38 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2023-05-02 19:35:38 +0100
commit6c6480baed8abb849ed2d2ffa29f5e8710b64268 (patch)
tree11a9f98fa80ea6e5d972901318d89626f204f70a /challenge-215
parent842c3a85ecdec22547f7d78e3a118ec735bab197 (diff)
downloadperlweeklychallenge-club-6c6480baed8abb849ed2d2ffa29f5e8710b64268.tar.gz
perlweeklychallenge-club-6c6480baed8abb849ed2d2ffa29f5e8710b64268.tar.bz2
perlweeklychallenge-club-6c6480baed8abb849ed2d2ffa29f5e8710b64268.zip
- Added solutions by Ulrich Rieke.
Diffstat (limited to 'challenge-215')
-rw-r--r--challenge-215/ulrich-rieke/cpp/ch-1.cpp28
-rw-r--r--challenge-215/ulrich-rieke/cpp/ch-2.cpp57
-rw-r--r--challenge-215/ulrich-rieke/haskell/ch-1.hs16
-rw-r--r--challenge-215/ulrich-rieke/haskell/ch-2.hs27
-rw-r--r--challenge-215/ulrich-rieke/perl/ch-1.pl16
-rw-r--r--challenge-215/ulrich-rieke/perl/ch-2.pl45
-rw-r--r--challenge-215/ulrich-rieke/raku/ch-1.raku11
-rw-r--r--challenge-215/ulrich-rieke/raku/ch-2.raku38
-rw-r--r--challenge-215/ulrich-rieke/rust/ch-1.rs22
-rw-r--r--challenge-215/ulrich-rieke/rust/ch-2.rs39
10 files changed, 299 insertions, 0 deletions
diff --git a/challenge-215/ulrich-rieke/cpp/ch-1.cpp b/challenge-215/ulrich-rieke/cpp/ch-1.cpp
new file mode 100644
index 0000000000..5f874934ed
--- /dev/null
+++ b/challenge-215/ulrich-rieke/cpp/ch-1.cpp
@@ -0,0 +1,28 @@
+#include <iostream>
+#include <string>
+#include <vector>
+#include <algorithm>
+
+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 ;
+}
+
+int main( ) {
+ std::cout << "Please enter some words, separated by blanks!\n" ;
+ std::string line ;
+ std::getline( std::cin , line ) ;
+ std::vector<std::string> allStrings ( split( line , " " ) ) ;
+ std::cout << std::count_if( allStrings.begin( ) , allStrings.end( ) ,
+ []( auto s ) { std::string c = s ; std::sort( c.begin( ) ,
+ c.end( ) ) ; return c != s ; } ) << std::endl ;
+ return 0 ;
+}
diff --git a/challenge-215/ulrich-rieke/cpp/ch-2.cpp b/challenge-215/ulrich-rieke/cpp/ch-2.cpp
new file mode 100644
index 0000000000..e6f54d4baa
--- /dev/null
+++ b/challenge-215/ulrich-rieke/cpp/ch-2.cpp
@@ -0,0 +1,57 @@
+#include <iostream>
+#include <string>
+#include <algorithm>
+#include <vector>
+
+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 ;
+}
+
+int main( ) {
+ std::cout << "Please enter a line of 1's and 0's, separated by blanks!\n" ;
+ std::string line ;
+ std::getline( std::cin , line ) ;
+ std::vector<std::string> numberstrings( split( line, " " ) ) ;
+ std::vector<int> numbers ;
+ for ( auto s : numberstrings )
+ numbers.push_back( std::stoi( s ) ) ;
+ std::cout << "How many 0's should be replaced ?\n" ;
+ int num ;
+ std::cin >> num ;
+ int necessary = 0 ;
+ if ( num == 1 )
+ necessary = 3 ;
+ if ( num > 1 )
+ necessary = 3 + num ;
+ int len = numbers.size( ) ;
+ if ( len < necessary || len == necessary )
+ std::cout << 0 << std::endl ;
+ else {
+ std::vector<bool> results ;
+ for ( int start = 0 ; start < len - necessary + 1 ; start++ ) {
+ if ( std::all_of( numbers.begin( ) + start , numbers.begin( ) + start
+ + necessary , []( int n ) { return n == 0 ; } ) ) {
+ results.push_back( true ) ;
+ }
+ else
+ results.push_back( false ) ;
+ }
+ if ( std::any_of( results.begin( ) , results.end( ) , []( auto b ) {
+ return b == true ; })) {
+ std::cout << 1 << std::endl ;
+ }
+ else {
+ std::cout << 0 << std::endl ;
+ }
+ }
+ return 0 ;
+}
diff --git a/challenge-215/ulrich-rieke/haskell/ch-1.hs b/challenge-215/ulrich-rieke/haskell/ch-1.hs
new file mode 100644
index 0000000000..aa89e91420
--- /dev/null
+++ b/challenge-215/ulrich-rieke/haskell/ch-1.hs
@@ -0,0 +1,16 @@
+module Challenge215
+ where
+import Data.List ( sort )
+
+solution :: [String] -> Int
+solution terms = length $ filter ( not . isSorted ) terms
+where
+ isSorted :: String -> Bool
+ isSorted str = str == sort str
+
+main :: IO ( )
+main = do
+ putStrLn "Enter some words, separated by a blank!"
+ terms <- getLine
+ let theWords = words terms
+ print $ solution theWords
diff --git a/challenge-215/ulrich-rieke/haskell/ch-2.hs b/challenge-215/ulrich-rieke/haskell/ch-2.hs
new file mode 100644
index 0000000000..637e0382d8
--- /dev/null
+++ b/challenge-215/ulrich-rieke/haskell/ch-2.hs
@@ -0,0 +1,27 @@
+module Challenge215_2
+ where
+import Data.List ( group )
+
+solution :: [Int] -> Int -> Int
+solution list zeroesToGo = if any(\li -> length li >= necessary && head li == 0
+) $ group list then 1 else 0
+where
+ necessary :: Int
+ necessary = if zeroesToGo == 1 then 3 else 3 + zeroesToGo
+
+condition :: [Int] -> Int -> Bool
+condition list toGo = length list > necessary
+where
+ necessary :: Int
+ necessary = if toGo == 1 then 3 else 3 + toGo
+
+main :: IO ( )
+main = do
+ putStrLn "Enter 1 and 0 , separated by a blank!"
+ line <- getLine
+ putStrLn "How many 0 should be removed ?"
+ toRemove <- getLine
+ let numbers = map read $ words line
+ kickout = read toRemove
+ if condition numbers kickout then print $ solution numbers kickout
+ else print 0
diff --git a/challenge-215/ulrich-rieke/perl/ch-1.pl b/challenge-215/ulrich-rieke/perl/ch-1.pl
new file mode 100644
index 0000000000..2a6166152a
--- /dev/null
+++ b/challenge-215/ulrich-rieke/perl/ch-1.pl
@@ -0,0 +1,16 @@
+#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use feature 'say' ;
+
+sub is_sorted {
+ my $word = shift ;
+ my $sorted = join( '' , sort split( // , $word )) ;
+ return $sorted eq $word ;
+}
+
+say "Enter some words, separated by blanks!" ;
+my $line = <STDIN> ;
+chomp $line ;
+my @words = split( /\s/ , $line ) ;
+say scalar( grep { not ( is_sorted( $_ ) ) } @words ) ;
diff --git a/challenge-215/ulrich-rieke/perl/ch-2.pl b/challenge-215/ulrich-rieke/perl/ch-2.pl
new file mode 100644
index 0000000000..923b3de999
--- /dev/null
+++ b/challenge-215/ulrich-rieke/perl/ch-2.pl
@@ -0,0 +1,45 @@
+#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use feature 'say' ;
+use List::Util qw ( all any ) ;
+
+say "Enter 1's and 0's, separated by a blank!" ;
+my $line = <STDIN> ;
+chomp $line ;
+my @numbers = split( /\s/ , $line ) ;
+say "Enter how many 0's are to be replaced!" ;
+my $count = <STDIN> ;
+chomp $count ;
+#if we want to replace one 0 we have to have 3 in a row, if more than 1 ,
+#then 3 + 0 to be replaced
+my $len = scalar( @numbers ) ;
+my $necessary ;
+if ( $count == 1 ) {
+ $necessary = 3 ;
+}
+if ( $count > 1 ) {
+ $necessary = 3 + $count ;
+}
+if ( $len < $necessary || $len == $necessary ) {
+ say 0 ;
+}
+else {
+ my @results ;
+ for my $i ( 0 .. $len - $necessary ) {
+ my @removed = splice( @numbers, $i , $necessary ) ;
+ if ( all { $_ == 0 } @removed ) {
+ push @results , 1 ;
+ }
+ else {
+ push @results, 0 ;
+ }
+ splice( @numbers, $i , $necessary, @removed ) ;
+ }
+ if ( any { $_ == 1 } @results ) {
+ say 1 ;
+ }
+ else {
+ say 0 ;
+ }
+}
diff --git a/challenge-215/ulrich-rieke/raku/ch-1.raku b/challenge-215/ulrich-rieke/raku/ch-1.raku
new file mode 100644
index 0000000000..6777d913c3
--- /dev/null
+++ b/challenge-215/ulrich-rieke/raku/ch-1.raku
@@ -0,0 +1,11 @@
+use v6 ;
+
+sub is_sorted( $aWord ) {
+ my $sorted = $aWord.comb.sort.join ;
+ return $sorted eq $aWord ;
+}
+
+say "Enter some words, separated by blanks!" ;
+my $line = $*IN.get ;
+my @words = $line.words ;
+say @words.grep( { not ( is_sorted( $_ ) ) } ).elems ;
diff --git a/challenge-215/ulrich-rieke/raku/ch-2.raku b/challenge-215/ulrich-rieke/raku/ch-2.raku
new file mode 100644
index 0000000000..8b4c1506b7
--- /dev/null
+++ b/challenge-215/ulrich-rieke/raku/ch-2.raku
@@ -0,0 +1,38 @@
+use v6 ;
+
+say "Enter only 1's and 0's in a row, separated by blanks!" ;
+my $line = $*IN.get ;
+my @numbers = $line.words.map( {.Int} ) ;
+say "How many zeroes would you like to replace?" ;
+my $count = $*IN.get.Int ;
+my $len = @numbers.elems ;
+my $necessary_row ;
+if ( $count = 1 ) {
+ $necessary_row = 3 ;
+}
+if ( $count > 1 ) {
+ $necessary_row = 3 + $count ;
+}
+if ( $len < $necessary_row || $len == $necessary_row ) {
+ say 0 ;
+}
+else {
+ my @results ;
+ for (0..$len - $necessary_row ) -> $i {
+ my @removed = @numbers.splice($i , $necessary_row) ;
+ if ( @removed.grep( {$_ == 0} ).elems == @removed.elems ) {
+ @results.push( True ) ;
+ }
+ else {
+ @results.push( False ) ;
+ }
+ splice( @numbers , $i , $necessary_row , @removed ) ;
+ say @numbers.join( ',' ) ;
+ }
+ if (any(@results) == True ) {
+ say 1 ;
+ }
+ else {
+ say 0 ;
+ }
+}
diff --git a/challenge-215/ulrich-rieke/rust/ch-1.rs b/challenge-215/ulrich-rieke/rust/ch-1.rs
new file mode 100644
index 0000000000..47a72432fe
--- /dev/null
+++ b/challenge-215/ulrich-rieke/rust/ch-1.rs
@@ -0,0 +1,22 @@
+use std::io ;
+
+fn is_sorted( word : &str ) -> bool {
+ let mut word_letters : Vec<char> = Vec::new( ) ;
+ for c in word.chars( ) {
+ word_letters.push( c ) ;
+ }
+ let mut compared_to : Vec<char> = word_letters.clone( ) ;
+ compared_to.sort( ) ;
+ compared_to == word_letters
+}
+
+fn main() {
+ println!("Enter some words, separated by a blank!");
+ let mut inline : String = String::new( ) ;
+ io::stdin( ).read_line( &mut inline ).unwrap( ) ;
+ let entered_line : &str = &*inline ;
+ let words : Vec<&str> = entered_line.split_whitespace( ).map( | s |
+ s.trim( ) ).collect( ) ;
+ println!( "{}" , words.iter( ).filter( | s | ! is_sorted( *s ) ).
+ count( ) ) ;
+}
diff --git a/challenge-215/ulrich-rieke/rust/ch-2.rs b/challenge-215/ulrich-rieke/rust/ch-2.rs
new file mode 100644
index 0000000000..813397a4ab
--- /dev/null
+++ b/challenge-215/ulrich-rieke/rust/ch-2.rs
@@ -0,0 +1,39 @@
+use std::io ;
+
+fn main() {
+ println!("Enter 1 and 0 only!");
+ let mut inline : String = String::new( ) ;
+ io::stdin( ).read_line( &mut inline ).unwrap( ) ;
+ let entered_line : &str = &*inline ;
+ let numbers : Vec<usize> = entered_line.split_whitespace( ).map( | s |
+ s.trim( ).parse::<usize>( ).unwrap( )).collect( ) ;
+ println!("How many 0's should be replaced ?") ;
+ let mut secondline : String = String::new( ) ;
+ io::stdin( ).read_line( &mut secondline ).unwrap( ) ;
+ //the basic idea is : to replace n zeroes you have to have 3 zeroes in
+ //a row if n is 1, 3 + ( number to replace ) otherwise
+ let zerocount : usize = secondline.trim( ).parse::<usize>( ).unwrap( ) ;
+ let necessary_row = match zerocount {
+ 0 => 0 ,
+ 1 => 3 ,
+ _ => 3 + zerocount
+ } ;
+ let len = numbers.len( ) ;
+ if len < necessary_row || len == necessary_row {
+ println!("0") ;
+ }
+ else {
+ let mut conditions : Vec<bool> = Vec::new( ) ;
+ for i in 0..=(len - necessary_row) {
+ let sub_slice = &numbers[i..i + necessary_row] ;
+ let mut slice_iter = sub_slice.iter( ) ;
+ conditions.push( slice_iter.all( | n | *n == 0 )) ;
+ }
+ if conditions.iter( ).any( | c | *c == true ) {
+ println!("1") ;
+ }
+ else {
+ println!("0") ;
+ }
+ }
+}