aboutsummaryrefslogtreecommitdiff
path: root/challenge-055/ulrich-rieke/cpp/ch-2.cpp
diff options
context:
space:
mode:
authorYsmael Ebreo <Ysmael.Ebreo@latticesemi.com>2020-04-12 13:35:02 +0800
committerYsmael Ebreo <Ysmael.Ebreo@latticesemi.com>2020-04-12 13:35:02 +0800
commitf8962b11d7445b396285c9ac8916835277c8fa36 (patch)
tree6eb35018b224a6345c799e7eff7a95f09a3cb06e /challenge-055/ulrich-rieke/cpp/ch-2.cpp
parentb8473690a44cb00df2ee0c39c8c2cd907b4c006b (diff)
parent3e3892a127e0393baed83fc53fbf8523ce9f20e7 (diff)
downloadperlweeklychallenge-club-f8962b11d7445b396285c9ac8916835277c8fa36.tar.gz
perlweeklychallenge-club-f8962b11d7445b396285c9ac8916835277c8fa36.tar.bz2
perlweeklychallenge-club-f8962b11d7445b396285c9ac8916835277c8fa36.zip
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
Diffstat (limited to 'challenge-055/ulrich-rieke/cpp/ch-2.cpp')
-rw-r--r--challenge-055/ulrich-rieke/cpp/ch-2.cpp67
1 files changed, 67 insertions, 0 deletions
diff --git a/challenge-055/ulrich-rieke/cpp/ch-2.cpp b/challenge-055/ulrich-rieke/cpp/ch-2.cpp
new file mode 100644
index 0000000000..10adfc4756
--- /dev/null
+++ b/challenge-055/ulrich-rieke/cpp/ch-2.cpp
@@ -0,0 +1,67 @@
+#include <iostream>
+#include <vector>
+#include <iterator>
+#include <algorithm>
+#include <utility>
+#include <cstdlib>
+
+//this function creates pairs of neighbouring numbers in a vector
+std::vector<std::pair<int, int>> createDoublets ( const std::vector<int> & numbers ) {
+ std::vector<std::pair<int , int>> doublets ;
+ int len = numbers.size( ) ;
+ for ( int i = 0 ; i < len - 1 ; i++ ) {
+ std::pair<int , int> p { *(numbers.begin( ) + i ) ,
+ *(numbers.begin( ) + i + 1 ) } ;
+ doublets.push_back( p ) ;
+ }
+ return doublets ;
+}
+
+//in order to create a "wavy list of numbers", a vector of numbers is
+//converted into a vector of pairs of neighbouring numbers. To be wavy,
+//even-indexed pairs must be descending, odd-indexed ones must ascend.
+//if this holds true for all pairs a permutation is "wavy"
+bool myCondition( const std::vector<int> & numbers ) {
+ std::vector<std::pair<int, int>> doublets { createDoublets( numbers ) } ;
+ int len = doublets.size( ) ;
+ std::vector<bool> validWavy ;
+ for (int i = 0 ; i < len ; i++ ) {
+ std::pair<int , int> p = doublets[ i ] ;
+ validWavy.push_back (( i % 2 == 0 ) && ( p.first >= p.second ) ) ;
+ validWavy.push_back (( i % 2 == 1 ) && ( p.first <= p.second ) ) ;
+ }
+ int i = 0 ;
+ for ( auto b : validWavy ) {
+ if ( b )
+ i++ ;
+ }
+ return ( i == len ) ;
+}
+
+//numbers are entered as arguments on the command line. A minimum number
+//of 2 is desirable and requested
+int main( int argc, char * argv[] ) {
+ std::vector<int> input ;
+ if ( argc < 3 ) {
+ std::cout << "There is little sense in permuting such short sequences!\n" ;
+ return 1 ;
+ }
+ for ( int i = 1 ; i < argc ; i++ ) {
+ input.push_back( std::atoi( argv[ i ] ) ) ;
+ }
+ std::vector<std::vector<int>> permus ;
+ std::vector<std::vector<int>> wavies ;
+ do {
+ permus.push_back( input ) ;
+ } while ( std::next_permutation( input.begin( ) , input.end( ) )) ;
+ std::copy_if( permus.begin( ) , permus.end( ) ,
+ std::back_inserter( wavies ) , myCondition ) ;
+ for ( auto & it : wavies ) {
+ std::cout << "[ " ;
+ for ( int i : it ) {
+ std::cout << i << " " ;
+ }
+ std::cout << " ]\n" ;
+ }
+ return 0 ;
+}