aboutsummaryrefslogtreecommitdiff
path: root/challenge-215/ulrich-rieke/cpp/ch-2.cpp
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/ulrich-rieke/cpp/ch-2.cpp
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/ulrich-rieke/cpp/ch-2.cpp')
-rw-r--r--challenge-215/ulrich-rieke/cpp/ch-2.cpp57
1 files changed, 57 insertions, 0 deletions
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 ;
+}