aboutsummaryrefslogtreecommitdiff
path: root/challenge-249/ulrich-rieke/cpp
diff options
context:
space:
mode:
authorLuis Mochan <mochan@fis.unam.mx>2023-12-27 14:57:27 -0600
committerLuis Mochan <mochan@fis.unam.mx>2023-12-27 14:57:27 -0600
commitf9c88dbbb1e13bfb63f40d0dd3b8630850655d7b (patch)
treecb922fb2f44a7ec7bd10cd13b3db9eb2d4fd14b5 /challenge-249/ulrich-rieke/cpp
parent23f378718ed93b9b8d9e51fe18f07a55f61dc9c1 (diff)
parent4ece120b2f4c7b25b00eaad66524fb5d99fa58c3 (diff)
downloadperlweeklychallenge-club-f9c88dbbb1e13bfb63f40d0dd3b8630850655d7b.tar.gz
perlweeklychallenge-club-f9c88dbbb1e13bfb63f40d0dd3b8630850655d7b.tar.bz2
perlweeklychallenge-club-f9c88dbbb1e13bfb63f40d0dd3b8630850655d7b.zip
Merge branch 'master' of github.com:manwar/perlweeklychallenge-club into challenges
Diffstat (limited to 'challenge-249/ulrich-rieke/cpp')
-rwxr-xr-xchallenge-249/ulrich-rieke/cpp/ch-1.cpp55
-rwxr-xr-xchallenge-249/ulrich-rieke/cpp/ch-2.cpp63
2 files changed, 118 insertions, 0 deletions
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 <vector>
+#include <string>
+#include <iostream>
+#include <algorithm>
+#include <utility>
+#include <map>
+
+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 << "Enter an even number of integers, separated by blanks!\n" ;
+ std::string line ;
+ std::getline( std::cin , line ) ;
+ std::vector<int> numbers ;
+ std::vector<std::string> numberstrings { split( line , " " ) } ;
+ for ( auto s : numberstrings )
+ numbers.push_back( std::stoi( s ) ) ;
+ std::map<int , int> 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<std::pair<int , int>> result ;
+ int pos = 0 ;
+ int len = numbers.size( ) ;
+ while ( pos <= len - 2 ) {
+ std::pair<int, int> 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 <string>
+#include <iostream>
+#include <algorithm>
+#include <vector>
+#include <numeric>
+
+bool isValid( const std::vector<int> & permu , const std::vector<int> & the_is ,
+ const std::vector<int> & 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<int> ipositions ;
+ std::vector<int> 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<int> numbers ( len + 1 ) ;
+ std::iota( numbers.begin( ) , numbers.end( ) , 0 ) ;
+ std::vector<int> 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 ;
+}