aboutsummaryrefslogtreecommitdiff
path: root/challenge-195/ulrich-rieke/cpp/ch-2.cpp
diff options
context:
space:
mode:
authorJames Smith <js5@sanger.ac.uk>2022-12-15 01:57:55 +0000
committerGitHub <noreply@github.com>2022-12-15 01:57:55 +0000
commite2b33f546bda365fe677e14e725bb53a5f23798c (patch)
treea99ca3ed8e82e367b0aba5acd5cec50e5b48757f /challenge-195/ulrich-rieke/cpp/ch-2.cpp
parent2b99b6e28c2a3b672e50d7b22cd3e1c6998670c7 (diff)
parentc1d3932971f399789d4ee01cb886bb1e984f2563 (diff)
downloadperlweeklychallenge-club-e2b33f546bda365fe677e14e725bb53a5f23798c.tar.gz
perlweeklychallenge-club-e2b33f546bda365fe677e14e725bb53a5f23798c.tar.bz2
perlweeklychallenge-club-e2b33f546bda365fe677e14e725bb53a5f23798c.zip
Merge branch 'manwar:master' into master
Diffstat (limited to 'challenge-195/ulrich-rieke/cpp/ch-2.cpp')
-rw-r--r--challenge-195/ulrich-rieke/cpp/ch-2.cpp54
1 files changed, 54 insertions, 0 deletions
diff --git a/challenge-195/ulrich-rieke/cpp/ch-2.cpp b/challenge-195/ulrich-rieke/cpp/ch-2.cpp
new file mode 100644
index 0000000000..befdd7eb11
--- /dev/null
+++ b/challenge-195/ulrich-rieke/cpp/ch-2.cpp
@@ -0,0 +1,54 @@
+#include <iostream>
+#include <string>
+#include <map>
+#include <utility>
+#include <algorithm>
+#include <vector>
+
+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 << "Please enter some integers, separated by blanks!\n" ;
+ std::string line ;
+ std::getline( std::cin , line ) ;
+ std::vector<int> evens ;
+ std::vector<std::string> numberstrings( split( line , " " ) ) ;
+ for ( auto & s : numberstrings ) {
+ int num = std::stoi( s ) ;
+ if ( num % 2 == 0 )
+ evens.push_back( num ) ;
+ }
+ if ( evens.size( ) != 0 ) {
+ std::map<int , int> frequencies ;
+ for ( int i : evens ) {
+ frequencies[ i ]++ ;
+ }
+ std::vector<std::pair<int , int>> allFrequencies ( frequencies.begin( ),
+ frequencies.end( ) ) ;
+ std::sort ( allFrequencies.begin( ) , allFrequencies.end( ) ,
+ [ ] ( const auto & a, const auto & b ) { return a.second
+ > b.second ; } ) ;
+ if ( allFrequencies.begin( )->second !=
+ (allFrequencies.begin( ) + 1)->second ) {
+ std::cout << allFrequencies.begin( )->first << std::endl ;
+ }
+ else {
+ std::sort( evens.begin( ) , evens.end( ) ) ;
+ std::cout << *evens.begin( ) << std::endl ;
+ }
+ }
+ else {
+ std::cout << -1 << std::endl ;
+ }
+ return 0 ;
+}