aboutsummaryrefslogtreecommitdiff
path: root/challenge-067
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2020-07-01 21:42:13 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2020-07-01 21:42:13 +0100
commit56ffaee884c72324292ec6d3e69b4ee6708dbeae (patch)
treed41ac920fe0b24a3198184884405312e2447af36 /challenge-067
parent033813ec45b89fbe5653bc0189688aed724c5650 (diff)
downloadperlweeklychallenge-club-56ffaee884c72324292ec6d3e69b4ee6708dbeae.tar.gz
perlweeklychallenge-club-56ffaee884c72324292ec6d3e69b4ee6708dbeae.tar.bz2
perlweeklychallenge-club-56ffaee884c72324292ec6d3e69b4ee6708dbeae.zip
- Added C++ solution by Ulrich Rieke.
Diffstat (limited to 'challenge-067')
-rw-r--r--challenge-067/ulrich-rieke/cpp/ch-2.cpp80
1 files changed, 80 insertions, 0 deletions
diff --git a/challenge-067/ulrich-rieke/cpp/ch-2.cpp b/challenge-067/ulrich-rieke/cpp/ch-2.cpp
new file mode 100644
index 0000000000..d31d4e8c33
--- /dev/null
+++ b/challenge-067/ulrich-rieke/cpp/ch-2.cpp
@@ -0,0 +1,80 @@
+#include <string>
+#include <iostream>
+#include <map>
+#include <vector>
+
+std::vector<std::string> combineStrings( const std::string & worda ,
+ const std::string & wordb) {
+ std::vector<std::string> combis ;
+ int lena = worda.size( ) ;
+ int lenb = wordb.size( ) ;
+ if ( lena > 0 && lenb == 0 ) {
+ for ( int i = 0 ; i < lena ; i++ ) {
+ combis.push_back( worda.substr( i , 1 ) ) ;
+ }
+ }
+ if ( lena == 0 && lenb > 0 ) {
+ for ( int i = 0 ; i < lenb ; i++ ) {
+ combis.push_back( wordb.substr( i , 1 ) ) ;
+ }
+ }
+ if ( lena > 0 && lenb > 0 ) {
+ for ( int i = 0 ; i < lena ; i++ ) {
+ for ( int j = 0 ; j < lenb ; j++ ) {
+ combis.push_back( worda.substr( i , 1 ) + wordb.substr( j , 1 ) ) ;
+ }
+ }
+ }
+ return combis ;
+}
+
+std::vector<std::string> combineStringsWithString( std::vector<std::string>
+ & words, std::string & expression ) {
+ std::vector<std::string> newWords ;
+ if ( ! expression.empty( ) ) {
+ int len = expression.size( ) ;
+ for ( auto word : words ) {
+ for ( int i = 0 ; i < len ; i++ ) {
+ word.append( expression.substr( i , 1 )) ;
+ newWords.push_back( word ) ;
+ }
+ }
+ }
+ else
+ newWords = words ;
+ return newWords ;
+}
+
+int main( int argc, char * argv[] ) {
+ std::map<std::string, std::string> phone { {"1" , "_,@"} , {"2", "ABC" },
+ {"3" , "DEF"} , {"4" , "GHI"} , {"5" , "JKL"} , {"6" , "MNO"} ,
+ {"7" , "PQRS"} , {"8", "TUV"} , {"9", "WXYZ"} , {"0" , ""}} ;
+ std::string input( argv[1] ) ;
+ std::vector<std::string> combis ;
+ int len = input.size( ) ;
+ if ( len == 1 ) {
+ std::string word ( phone[ input ] ) ;
+ if ( !word.empty( ) ) {
+ for ( int i = 0 ; i < word.length( ) ; i++ ) {
+ combis.push_back( word.substr( i , 1 ) ) ;
+ }
+ }
+ }
+ if ( len == 2 ) {
+ combis = combineStrings( phone[ input.substr( 0 , 1 )] ,
+ phone[ input.substr( 1 , 1 )] ) ;
+ }
+ if ( len > 2 ) {
+ combis = combineStrings( phone[ input.substr( 0 , 1 )] ,
+ phone[ input.substr( 1 , 1 )] ) ;
+ for ( int i = 2 ; i < len ; i++ ) {
+ combis = combineStringsWithString( combis,
+ phone[ input.substr( i , 1 )] ) ;
+ }
+ }
+ for ( auto & str : combis ) {
+ std::cout << str << " " ;
+ }
+ std::cout << std::endl ;
+ return 0 ;
+}