From dee6d2782db5c9d5bbab1bbb8d2bd9ff9f6fd34a Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Wed, 27 Dec 2023 11:07:16 +0000 Subject: - Added solutions by Ulrich Rieke. - Added solutions by Dave Jacoby. - Added solutions by Stephen G Lynn. --- challenge-249/ulrich-rieke/cpp/ch-1.cpp | 55 ++++++++++++++++++++++++++++ challenge-249/ulrich-rieke/cpp/ch-2.cpp | 63 +++++++++++++++++++++++++++++++++ 2 files changed, 118 insertions(+) create mode 100755 challenge-249/ulrich-rieke/cpp/ch-1.cpp create mode 100755 challenge-249/ulrich-rieke/cpp/ch-2.cpp (limited to 'challenge-249/ulrich-rieke/cpp') diff --git a/challenge-249/ulrich-rieke/cpp/ch-1.cpp b/challenge-249/ulrich-rieke/cpp/ch-1.cpp new file mode 100755 index 0000000000..49d2e3d9e3 --- /dev/null +++ b/challenge-249/ulrich-rieke/cpp/ch-1.cpp @@ -0,0 +1,55 @@ +#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 ; +} + +int main( ) { + std::cout << "Enter an even number of integers, separated by blanks!\n" ; + std::string line ; + std::getline( std::cin , line ) ; + std::vector numbers ; + std::vector numberstrings { split( line , " " ) } ; + for ( auto s : numberstrings ) + numbers.push_back( std::stoi( s ) ) ; + std::map frequencies ; + for ( auto it = numbers.begin( ) ; it != numbers.end( ) ; ++it ) { + frequencies[*it]++ ; + } + if ( std::all_of( frequencies.begin( ) , frequencies.end( ) , []( const + auto & p ) { return p.second % 2 == 0 ; } ) ) { + std::sort( numbers.begin( ) , numbers.end( ) ) ; + std::vector> result ; + int pos = 0 ; + int len = numbers.size( ) ; + while ( pos <= len - 2 ) { + std::pair current { std::make_pair( numbers[ pos ] , + numbers[ pos + 1 ] ) } ; + result.push_back( current ) ; + pos += 2 ; + } + std::cout << "(" ; + for ( auto p : result ) { + std::cout << "[" << p.first << ',' << p.second << "]" ; + } + std::cout << ")\n" ; + } + else { + std::cout << "()\n" ; + } + return 0 ; +} diff --git a/challenge-249/ulrich-rieke/cpp/ch-2.cpp b/challenge-249/ulrich-rieke/cpp/ch-2.cpp new file mode 100755 index 0000000000..8f52f5dcfb --- /dev/null +++ b/challenge-249/ulrich-rieke/cpp/ch-2.cpp @@ -0,0 +1,63 @@ +#include +#include +#include +#include +#include + +bool isValid( const std::vector & permu , const std::vector & the_is , + const std::vector & the_ds ) { + for ( int i = 0 ; i < the_is.size( ) ; i++ ) { + int pos = the_is[ i ] ; + if ( permu[pos] >= permu[ pos + 1] ) { + return false ; + } + } + for ( int i = 0 ; i < the_ds.size( ) ; i++ ) { + int pos = the_ds[ i ] ; + if ( permu[pos] <= permu[ pos + 1 ] ) { + return false ; + } + } + return true ; +} + +int main( ) { + std::cout << "Please enter a string consisting of capital I and D only!\n" ; + std::string line ; + std::cin >> line ; + std::vector ipositions ; + std::vector dpositions ; + for ( int i = 0 ; i < line.length( ) ; i++ ) { + if ( line.substr( i , 1 ) == "I" ) + ipositions.push_back( i ) ; + else + dpositions.push_back( i ) ; + } + int len = line.length( ) ; + std::vector numbers ( len + 1 ) ; + std::iota( numbers.begin( ) , numbers.end( ) , 0 ) ; + std::vector result ; + if ( dpositions.empty( ) ) { + result = numbers ; + } + else { + if ( ipositions.empty( ) ) { + result = numbers ; + std::reverse( result.begin( ) , result.end( ) ) ; + } + else { + while ( std::next_permutation( numbers.begin( ) , numbers.end( ) ) ) { + if ( isValid( numbers, ipositions, dpositions ) ) { + result = numbers ; + break ; + } + } + } + } + std::cout << "( " ; + for ( int i : result ) { + std::cout << i << " " ; + } + std::cout << " )\n" ; + return 0 ; +} -- cgit