diff options
| author | Mohammad Sajid Anwar <mohammad.anwar@yahoo.com> | 2024-09-18 20:18:52 +0100 |
|---|---|---|
| committer | Mohammad Sajid Anwar <mohammad.anwar@yahoo.com> | 2024-09-18 20:18:52 +0100 |
| commit | 0052ec63ca70eaa6d9ffb1926c294dbfd85f8c05 (patch) | |
| tree | b36287679c83fedbccaa97d8fe75c7308d5779d7 /challenge-287/ulrich-rieke/cpp | |
| parent | 467f3f3ee7cbef95158ddf81a2187e1a997acfc1 (diff) | |
| download | perlweeklychallenge-club-0052ec63ca70eaa6d9ffb1926c294dbfd85f8c05.tar.gz perlweeklychallenge-club-0052ec63ca70eaa6d9ffb1926c294dbfd85f8c05.tar.bz2 perlweeklychallenge-club-0052ec63ca70eaa6d9ffb1926c294dbfd85f8c05.zip | |
- Added solutions by Paulo Custodio.
- Added solutions by Peter Campbell Smith.
- Added solutions by Robert Ransbottom.
- Added solutions by Feng Chang.
- Added solutions by PokGoPun.
- Added solutions by Peter Pentchev.
- Added solutions by Santiago Leyva.
- Added solutions by Robbie Hatley.
- Added solutions by Ulrich Rieke.
- Added solutions by Laurent Rosenfeld.
Diffstat (limited to 'challenge-287/ulrich-rieke/cpp')
| -rwxr-xr-x | challenge-287/ulrich-rieke/cpp/ch-1.cpp | 71 | ||||
| -rwxr-xr-x | challenge-287/ulrich-rieke/cpp/ch-2.cpp | 22 |
2 files changed, 93 insertions, 0 deletions
diff --git a/challenge-287/ulrich-rieke/cpp/ch-1.cpp b/challenge-287/ulrich-rieke/cpp/ch-1.cpp new file mode 100755 index 0000000000..6048d7476f --- /dev/null +++ b/challenge-287/ulrich-rieke/cpp/ch-1.cpp @@ -0,0 +1,71 @@ +#include <iostream>
+#include <string>
+#include <regex>
+#include <vector>
+#include <numeric>
+
+int main( ) {
+ std::cout << "Enter a password!\n" ;
+ std::string word ;
+ std::cin >> word ;
+ int totalsteps = 0 ;
+ const std::regex rgx1("[a-z]") ;
+ const std::regex rgx2("[A-Z]") ;
+ const std::regex rgx3("\\d") ;
+ int len = word.length( ) ;
+ if ( len < 6 ) {
+ totalsteps += 6 - len ;
+ }
+ //beware : it took me quite some time to find out that
+ //std::regex_match does not return the right result!!
+ //if you want to check whether a given regular expression
+ //such as \d matches I had to use std::regex_search!!!
+ if ( ! std::regex_search( word , rgx1 )) {
+ if ( len >= 6 ) {
+ totalsteps++ ;
+ }
+ }
+ if ( ! std::regex_search( word , rgx2 )) {
+ if ( len >= 6 ) {
+ totalsteps++ ;
+ }
+ }
+ if ( ! std::regex_search( word , rgx3 )) {
+ if ( len >= 6 ) {
+ totalsteps++ ;
+ }
+ }
+ //std::regex appears not to allow backreferences...?
+ //so, to prevent too many identical neighbouring letters
+ //I group the letters , divide their lengths by 3 and sum the
+ //quotients up ( inspired by Haskell where this operation is so easy
+ //to perform!!!
+ std::vector<std::string> neighbouring_letters ;
+ std::string currentWord ;
+ //imitate Haskell's group function!
+ for ( auto it = word.begin( ) ; it != word.end( ) ; ++it ) {
+ if ( currentWord.empty( ) )
+ currentWord.push_back( *it ) ;
+ else {
+ char last_char = currentWord.back( ) ;
+ if ( last_char == *it ) {
+ currentWord.push_back( *it ) ;
+ }
+ else {
+ neighbouring_letters.push_back( currentWord ) ;
+ currentWord.clear( ) ;
+ currentWord.push_back( *it ) ;
+ }
+ }
+ }
+ neighbouring_letters.push_back( currentWord ) ;
+ std::vector<int> grouplengths ;
+ for ( auto v : neighbouring_letters ) {
+ grouplengths.push_back( v.length( ) / 3 ) ;
+ }
+ int replacements = std::accumulate( grouplengths.begin( ) ,
+ grouplengths.end( ) , 0 ) ;
+ totalsteps += replacements ;
+ std::cout << totalsteps << '\n' ;
+ return 0 ;
+}
diff --git a/challenge-287/ulrich-rieke/cpp/ch-2.cpp b/challenge-287/ulrich-rieke/cpp/ch-2.cpp new file mode 100755 index 0000000000..b5dc14d07a --- /dev/null +++ b/challenge-287/ulrich-rieke/cpp/ch-2.cpp @@ -0,0 +1,22 @@ +#include <iostream>
+#include <string>
+#include <regex>
+
+int main( ) {
+ std::cout << "Enter a string!\n" ;
+ std::string word ;
+ std::cin >> word ;
+ std::string inte {R"(^[-+]?\d+([eE]?[-+]?\d+)*$)"} ;
+ std::string decifirst {R"(^[-+]?\d+\.([eE]?[-+]?\d+)*$|^[-+]?\d+\.\d+([eE]?[+-]?\d+)*$)"} ;
+ std::string decisecond {R"(^[+-]?\.\d+([eE]?[+-]?\d+$)*$)" } ;
+ std::regex integer( inte ) ;
+ std::regex decimalfirst ( decifirst ) ;
+ std::regex decimalsecond ( decisecond ) ;
+ if ( std::regex_match( word , integer ) || std::regex_match( word , decimalfirst ) ||
+ std::regex_match( word , decimalsecond ) )
+ std::cout << "true" ;
+ else
+ std::cout << "false" ;
+ std::cout << '\n' ;
+ return 0 ;
+}
|
