From 5ba428b6c66548e501eafc41c7a729456cbe10dd Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Wed, 24 Apr 2024 19:21:32 +0100 Subject: - Added solutions by Ulrich Rieke. - Added solutions by Ali Moradi. --- challenge-266/ulrich-rieke/cpp/ch-1.cpp | 57 +++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100755 challenge-266/ulrich-rieke/cpp/ch-1.cpp (limited to 'challenge-266/ulrich-rieke/cpp/ch-1.cpp') 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 +#include +#include +#include +#include +#include +#include + +namespace rng = std::ranges ; + +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 some words, separated by blanks!\n" ; + std::string line ; + std::getline( std::cin , line ) ; + std::vector firstWords { split( line, " " ) } ; + std::cout << "Enter some more words, separated by blanks!\n" ; + line.clear( ) ; + std::getline( std::cin , line ) ; + std::vector secondWords { split( line, " " ) } ; + std::map 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> allWords { frequencies.begin( ) , + frequencies.end( ) } ; + std::vector 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 ; +} + + -- cgit