aboutsummaryrefslogtreecommitdiff
path: root/challenge-202/ulrich-rieke/cpp
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-202/ulrich-rieke/cpp')
-rw-r--r--challenge-202/ulrich-rieke/cpp/ch-1.cpp37
-rw-r--r--challenge-202/ulrich-rieke/cpp/ch-2.cpp74
2 files changed, 111 insertions, 0 deletions
diff --git a/challenge-202/ulrich-rieke/cpp/ch-1.cpp b/challenge-202/ulrich-rieke/cpp/ch-1.cpp
new file mode 100644
index 0000000000..be9c6968c1
--- /dev/null
+++ b/challenge-202/ulrich-rieke/cpp/ch-1.cpp
@@ -0,0 +1,37 @@
+#include <iostream>
+#include <vector>
+#include <string>
+#include <algorithm>
+#include <cstdlib>
+
+int main( int argc , char* argv[] ) {
+ if ( argc < 4 ) {
+ std::cout << "Enter a least 3 integers, separated by a blank!\n" ;
+ return 1 ;
+ }
+ else {
+ std::vector<int> numbers ;
+ for ( int i = 1 ; i < argc ; i++ ) {
+ numbers.push_back( std::atoi( argv[ i ] ) ) ;
+ }
+ int result = 0 ;
+ int len = numbers.size( ) ;
+ for ( int i = 0 ; i < len - 2 ; i++ ) {
+ if ( std::all_of( numbers.begin( ) + i , numbers.begin( ) + i + 3 ,
+ [ ]( int n ) { return n % 2 == 1 ; } ) )
+ result = 1 ;
+ }
+ std::cout << result << std::endl ;
+ return 0 ;
+ }
+}
+------------------------------------------------------------------------------------
+ task 1, in Haskell
+------------------------------------------------------------------------------------
+
+module Challenge202
+ where
+import Data.List.Split ( divvy )
+
+solution :: [Int] -> Int
+solution list = if any (\li -> all odd li ) $ divvy 3 1 list then 1 else 0
diff --git a/challenge-202/ulrich-rieke/cpp/ch-2.cpp b/challenge-202/ulrich-rieke/cpp/ch-2.cpp
new file mode 100644
index 0000000000..74945ca246
--- /dev/null
+++ b/challenge-202/ulrich-rieke/cpp/ch-2.cpp
@@ -0,0 +1,74 @@
+#include <iostream>
+#include <vector>
+#include <string>
+#include <algorithm>
+#include <utility>
+#include <iterator>
+
+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 ;
+}
+
+std::pair<int, int> findValley( const std::vector<int> & numbers , int pos ) {
+ int currentpos = pos ;
+ std::pair<int , int> result ;
+ int len = numbers.size( ) ;
+ do {
+ currentpos++ ;
+ } while ( currentpos < len && numbers[ currentpos ] <=
+ numbers[ currentpos - 1 ] ) ;
+ if ( currentpos == len ) {
+ result = std::make_pair( pos , currentpos - 1 - pos) ;
+ }
+ else {
+ do {
+ currentpos++ ;
+ } while ( currentpos < len && numbers[ currentpos ] >=
+ numbers[ currentpos - 1 ] ) ;
+ result = std::make_pair( pos , currentpos - 1 - pos ) ;
+ }
+ return result ;
+}
+
+bool myCompare( const std::pair<int , int> & myA , const std::pair<int , int>
+ & myB ) {
+ if ( myA.second == myB.second ) {
+ return myA.first < myB.first ;
+ }
+ else {
+ return myA.second > myB.second ;
+ }
+}
+
+int main( ) {
+ std::cout << "Enter some integers, separated by a blank!\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 ) ) ;
+ }
+ int len = numbers.size( ) ;
+ std::vector<std::pair<int , int>> positions ;
+ for ( int i = 0 ; i < len - 1 ; i++ ) {
+ std::pair<int , int> result { findValley( numbers , i ) } ;
+ positions.push_back( result ) ;
+ }
+ std::sort( positions.begin( ) , positions.end( ) , myCompare ) ;
+ int startpos = positions.begin( )->first ;
+ int stride = positions.begin( )->second ;
+ std::copy( numbers.begin( ) + startpos , numbers.begin( ) + startpos
+ + stride + 1 , std::ostream_iterator<int>( std::cout , " , " ) );
+ std::cout << std::endl ;
+ return 0 ;
+}