aboutsummaryrefslogtreecommitdiff
path: root/challenge-289/ulrich-rieke/cpp
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-289/ulrich-rieke/cpp')
-rwxr-xr-xchallenge-289/ulrich-rieke/cpp/ch-1.cpp48
-rwxr-xr-xchallenge-289/ulrich-rieke/cpp/ch-2.cpp64
2 files changed, 112 insertions, 0 deletions
diff --git a/challenge-289/ulrich-rieke/cpp/ch-1.cpp b/challenge-289/ulrich-rieke/cpp/ch-1.cpp
new file mode 100755
index 0000000000..0bdc6ca2ed
--- /dev/null
+++ b/challenge-289/ulrich-rieke/cpp/ch-1.cpp
@@ -0,0 +1,48 @@
+#include <iostream>
+#include <string>
+#include <vector>
+#include <sstream>
+#include <set>
+#include <algorithm>
+
+std::vector<std::string> split( const std::string & text , char delimiter ) {
+ std::vector<std::string> tokens ;
+ std::string word ;
+ std::istringstream istr { text } ;
+ while ( std::getline( istr , word , delimiter ))
+ tokens.push_back( word ) ;
+ return tokens ;
+}
+
+int main( ) {
+ std::cout << "Enter some integers, separated by blanks!\n" ;
+ std::string line ;
+ std::getline( std::cin , line ) ;
+ auto tokens { split( line , ' ' ) } ;
+ std::vector<int> numbers ;
+ for ( auto w : tokens ) {
+ numbers.push_back( std::stoi( w ) ) ;
+ }
+ if ( numbers.size( ) < 3 )
+ std::cout << *std::max_element( numbers.begin( ) , numbers.end( ) ) <<
+ '\n' ;
+ else {
+ std::set<int> uniques { numbers.begin( ) , numbers.end( ) } ;
+ std::vector<int> for_sorting { uniques.begin( ) , uniques.end( ) } ;
+ if ( for_sorting.size( ) < 3 ) {
+ std::cout << *std::max_element( for_sorting.begin( ) , for_sorting.end( ) )
+ << '\n' ;
+ }
+ else {
+ std::sort( for_sorting.begin( ) , for_sorting.end( ) , []( int a , int b) {
+ return a > b ; } ) ;
+ std::cout << *(for_sorting.begin( ) + 2 ) << '\n' ;
+ }
+ }
+ return 0 ;
+}
+
+
+
+
+
diff --git a/challenge-289/ulrich-rieke/cpp/ch-2.cpp b/challenge-289/ulrich-rieke/cpp/ch-2.cpp
new file mode 100755
index 0000000000..b03c0abd30
--- /dev/null
+++ b/challenge-289/ulrich-rieke/cpp/ch-2.cpp
@@ -0,0 +1,64 @@
+#include <vector>
+#include <string>
+#include <random>
+#include <iostream>
+#include <sstream>
+#include <algorithm>
+
+std::vector<std::string> split( std::string & text , char delimiter ) {
+ std::vector<std::string> tokens ;
+ std::istringstream istr { text } ;
+ std::string word ;
+ while ( std::getline( istr , word , delimiter ) ) {
+ tokens.push_back( word ) ;
+ }
+ return tokens ;
+}
+
+std::string do_shuffle( const std::string & word ) {
+ std::random_device rd ;
+ std::mt19937 g ( rd( ) ) ;
+ std::string central { word.substr( 1 , word.length( ) - 2 ) } ;
+ std::string output { word.substr( 0 , 1 ) } ;
+ std::shuffle( central.begin( ) , central.end( ) , g ) ;
+ output = output + central ;
+ output.push_back( word.back( ) ) ;
+ return output ;
+}
+
+int main( ) {
+ std::cout << "Enter some sentences, <return> to end!\n" ;
+ std::vector<std::string> block ;
+ std::string line ;
+ std::getline( std::cin , line ) ;
+ while ( ! line.empty( ) ) {
+ block.push_back( line ) ;
+ std::getline( std::cin , line ) ;
+ }
+ std::vector<std::string> shuffled ;
+ for ( auto aLine : block ) {
+ std::vector<std::string> shuffled_line ;
+ auto tokens { split( aLine , ' ' ) } ;
+ for ( auto w : tokens ) {
+ if ( w.length( ) <= 3 ) {
+ shuffled_line.push_back( w ) ;
+ }
+ else {
+ shuffled_line.push_back( do_shuffle( w ) ) ;
+ }
+ }
+ std::string changed ;
+ for ( auto w : shuffled_line ) {
+ changed = changed + w + " " ;
+ }
+ changed.pop_back( ) ;
+ shuffled.push_back( changed ) ;
+ }
+ for ( auto w : shuffled ) {
+ std::cout << w << '\n' ;
+ }
+ return 0 ;
+}
+
+
+