From 8d9cfd8d2ac39bb5d0b0f7452bf57d200bbdd62f Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Sat, 4 Feb 2023 14:04:38 +0000 Subject: - Added solutions by james Smith. - Added solutions by Luca Ferrari. - Added solutions by Aut0exec. - Added solutions by W. Luis Mochan. - Added solutions by Bob Lied. - Added solutions by David Ferrone. - Added solutions by E. Choroba. - Added solutions by Mark Anderson. - Added solutions by Robbie Hatley. - Added solutions by Dave Jacoby. - Added solutions by Thomas Kohler. - Added solutions by Jaldhar H. Vyas. - Added solutions by Peter Campbell Smith. - Added solutions by Mariano Spadaccini. - Added solutions by Jorg Sommrey. - Added solutions by Pip Stuart. - Added solutions by Simon Green. - Added solutions by Laurent Rosenfeld. - Added solutions by Ulrich Rieke. - Added solutions by Robert DiCicco. --- challenge-202/ulrich-rieke/cpp/ch-2.cpp | 74 +++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 challenge-202/ulrich-rieke/cpp/ch-2.cpp (limited to 'challenge-202/ulrich-rieke/cpp/ch-2.cpp') 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 +#include +#include +#include +#include +#include + +std::vector split( const std::string & startline , + const std::string & sep ) { + std::vector 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 findValley( const std::vector & numbers , int pos ) { + int currentpos = pos ; + std::pair 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 & myA , const std::pair + & 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 numberstrings ( split( line, " " ) ) ; + std::vector numbers ; + for ( auto & s : numberstrings ) { + numbers.push_back( std::stoi( s ) ) ; + } + int len = numbers.size( ) ; + std::vector> positions ; + for ( int i = 0 ; i < len - 1 ; i++ ) { + std::pair 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( std::cout , " , " ) ); + std::cout << std::endl ; + return 0 ; +} -- cgit