diff options
| author | Packy Anderson <PackyAnderson@gmail.com> | 2024-05-06 23:41:04 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-05-06 23:41:04 -0400 |
| commit | 8da3f7983b54d8c8eb96326f4d7f3d52f1a78d9b (patch) | |
| tree | 4cc9a6443382d2a32208246c481cd566b6ea0142 /challenge-266/ulrich-rieke/cpp | |
| parent | e4a2c66b30346606d347ecc4a08b83b733797d2a (diff) | |
| parent | c1756b0e7aed0ad70fa63feb2565c69215c9d426 (diff) | |
| download | perlweeklychallenge-club-8da3f7983b54d8c8eb96326f4d7f3d52f1a78d9b.tar.gz perlweeklychallenge-club-8da3f7983b54d8c8eb96326f4d7f3d52f1a78d9b.tar.bz2 perlweeklychallenge-club-8da3f7983b54d8c8eb96326f4d7f3d52f1a78d9b.zip | |
Merge branch 'manwar:master' into challenge-268
Diffstat (limited to 'challenge-266/ulrich-rieke/cpp')
| -rwxr-xr-x | challenge-266/ulrich-rieke/cpp/ch-1.cpp | 57 | ||||
| -rwxr-xr-x | challenge-266/ulrich-rieke/cpp/ch-2.cpp | 72 |
2 files changed, 129 insertions, 0 deletions
diff --git a/challenge-266/ulrich-rieke/cpp/ch-1.cpp b/challenge-266/ulrich-rieke/cpp/ch-1.cpp new file mode 100755 index 0000000000..84477e81ec --- /dev/null +++ b/challenge-266/ulrich-rieke/cpp/ch-1.cpp @@ -0,0 +1,57 @@ +#include <iostream>
+#include <vector>
+#include <string>
+#include <map>
+#include <utility>
+#include <ranges>
+#include <algorithm>
+
+namespace rng = std::ranges ;
+
+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 some words, separated by blanks!\n" ;
+ std::string line ;
+ std::getline( std::cin , line ) ;
+ std::vector<std::string> firstWords { split( line, " " ) } ;
+ std::cout << "Enter some more words, separated by blanks!\n" ;
+ line.clear( ) ;
+ std::getline( std::cin , line ) ;
+ std::vector<std::string> secondWords { split( line, " " ) } ;
+ std::map<std::string, int> frequencies ;
+ for ( auto it = firstWords.begin( ) ; it != firstWords.end( ) ; it++ )
+ frequencies[*it]++ ;
+ for ( auto it = secondWords.begin( ) ; it != secondWords.end( ) ; it++ )
+ frequencies[*it]++ ;
+ std::vector<std::pair<std::string, int>> allWords { frequencies.begin( ) ,
+ frequencies.end( ) } ;
+ std::vector<std::string> selected ;
+ if ( allWords.size( ) > 0 ) {
+ std::cout << "( " ;
+ for ( auto p : allWords | rng::views::filter([]( auto pa ){ return pa.second ==
+ 1 ; } )) {
+ selected.push_back( p.first ) ;
+ }
+ for ( auto w : selected )
+ std::cout << w << ' ' ;
+ std::cout << ")\n" ;
+ }
+ else {
+ std::cout << "()\n" ;
+ }
+ return 0 ;
+}
+
+
diff --git a/challenge-266/ulrich-rieke/cpp/ch-2.cpp b/challenge-266/ulrich-rieke/cpp/ch-2.cpp new file mode 100755 index 0000000000..277586e813 --- /dev/null +++ b/challenge-266/ulrich-rieke/cpp/ch-2.cpp @@ -0,0 +1,72 @@ +#include <iostream>
+#include <vector>
+#include <algorithm>
+#include <string>
+#include <numeric>
+
+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 ;
+}
+
+std::vector<int> enter_row( ) {
+ std::cout << "Enter some integers, separated by blanks!\n" ;
+ std::vector<int> numbers ;
+ std::string line ;
+ std::getline( std::cin , line ) ;
+ std::vector<std::string> numberstrings { split( line , " " ) } ;
+ for ( auto str : numberstrings )
+ numbers.push_back( std::stoi( str ) ) ;
+ return numbers ;
+}
+
+bool is_diagonal( const std::vector<std::vector<int>> & matrix ) {
+ int len = matrix.size( ) ;
+ std::vector<int> positions( len ) ;
+ std::iota( positions.begin( ) , positions.end( ) , 0 ) ;
+ return std::all_of( positions.begin( ) , positions.end( ) ,
+ [&matrix]( int i ) { return matrix[i][i] != 0 ; } ) ;
+}
+
+bool is_antidiagonal( const std::vector<std::vector<int>> & matrix ) {
+ int len = matrix.size( ) ;
+ std::vector<int> positions( len ) ;
+ std::iota( positions.begin( ) , positions.end( ) , 0 ) ;
+ return std::all_of( positions.begin( ) , positions.end( ) , [&matrix,len]( int i )
+ { return matrix[i][len - 1 - i] != 0 ; } ) ;
+}
+
+bool rest_zero( const std::vector<std::vector<int>> & matrix ) {
+ int len = matrix.size( ) ;
+ for ( int row = 0 ; row < len ; row++ ) {
+ for ( int col = 0 ; col < len ; col++ ) {
+ if ( row != col && row + col != len - 1 && matrix[row][col] != 0 )
+ return false ;
+ }
+ }
+ return true ;
+}
+
+int main( ) {
+ std::vector<std::vector<int>> matrix ;
+ std::vector<int> row { enter_row( ) } ;
+ int len = row.size( ) ;
+ matrix.push_back( row ) ;
+ for ( int i = 0 ; i < len - 1 ; i++ ) {
+ row = enter_row( ) ;
+ matrix.push_back( row ) ;
+ }
+ if ( is_diagonal( matrix ) && is_antidiagonal( matrix ) && rest_zero( matrix ) )
+ std::cout << "true\n" ;
+ else
+ std::cout << "false\n" ;
+ return 0 ;
+}
|
