Advertisement
makispaiktis

Alphabetical Addition (Codewars)

Oct 24th, 2019 (edited)
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.65 KB | None | 0 0
  1. /*
  2.  
  3. Your task is to add up letters to one letter.
  4.  
  5. The function will be given a variable amount of arguments, each one being a letter to add.
  6.  
  7. Notes:
  8. Letters will always be lowercase.
  9. Letters can overflow (see second to last example of the description)
  10. If no letters are given, the function should return 'z'
  11. Examples:
  12. add_letters('a', 'b', 'c') = 'f'
  13. add_letters('a', 'b') = 'c'
  14. add_letters('z') = 'z'
  15. add_letters('z', 'a') = 'a'
  16. add_letters('y', 'c', 'b') = 'd' # notice the letters overflowing
  17. add_letters() = 'z'
  18.  
  19. */
  20.  
  21.  
  22. #include <iostream>
  23. #include <map>
  24. #include <algorithm>
  25.  
  26. using namespace std;
  27.  
  28. // Auxiliary Functions
  29. // **************************************************************************************************************************
  30. // **************************************************************************************************************************
  31. // a) Converts a letter (such as 'd') ---> to an integer from 1 to 26 (such as 4)
  32. int charToInt(char ch){
  33.     map <char, int> myMap = map <char, int> ();
  34.     int counter = 0;
  35.     for(char i='a'; i<='z'; i++){
  36.         counter++;
  37.         myMap[i] = counter;
  38.     }
  39.  
  40.     // Iterator creation
  41.     map <char, int> :: iterator itr;
  42.     for(itr=myMap.begin(); itr!=myMap.end(); itr++){
  43.         if(itr->first == ch){
  44.             return itr->second;
  45.         }
  46.     }
  47.  
  48. }
  49.  
  50. // b) Converts an integer (such as 4) ---> to a char (such as 'd')
  51. char intToChar(int n){
  52.     map <int, char> myMap = map <int, char> ();
  53.     int counter = 0;
  54.     for(char i='a'; i<='z'; i++){
  55.         counter++;
  56.         myMap[counter] = i;
  57.     }
  58.  
  59.     // Iterator creation
  60.     map <int, char> :: iterator itr;
  61.     for(itr=myMap.begin(); itr!=myMap.end(); itr++){
  62.         if(itr->first == n){
  63.             return itr->second;
  64.         }
  65.     }
  66.  
  67. }
  68.  
  69. // **************************************************************************************************************************
  70. // **************************************************************************************************************************
  71. // 1. My Functions with no parameters
  72. char add_letters(){
  73.     return 'z';
  74. }
  75.  
  76. // 2. My function with 1 parameter
  77. char add_letters(char ch){
  78.     return ch;
  79. }
  80.  
  81. // 3. My function with 2 parameters
  82. char add_letters(char ch1, char ch2){
  83.     int num1 = charToInt(ch1);
  84.     int num2 = charToInt(ch2);
  85.     int num = (num1 + num2) % 26;
  86.     return intToChar(num);
  87. }
  88.  
  89. // 4. My function with 3 parameters
  90. char add_letters(char ch1, char ch2, char ch3){
  91.     char ch4 = add_letters(ch1, ch2);
  92.     return add_letters(ch3, ch4);
  93. }
  94.  
  95.  
  96.  
  97. // **************************************************************************************************************************
  98. // **************************************************************************************************************************
  99. // Main Function
  100.  
  101. int main()
  102. {
  103.     cout << "1.  No parameters:      add_letters() ---> " << "'" << add_letters() << "'" << endl;
  104.     cout << "2.  With 1 parameter:   add_letters('e') ---> " << "'" << add_letters('e') << "'" << endl;
  105.     cout << "3a. With 2 parameters:  add_letters('b','f') ---> " << "'" << add_letters('b', 'f') << "'" << endl;
  106.     cout << "3b. With 2 parameters:  add_letters('x','d') ---> " << "'" << add_letters('x', 'd') << "'" << endl;
  107.     cout << "4a. With 3 parameters:  add_letters('a','b', 'c') ---> " << "'" << add_letters('a', 'b', 'c') << "'" << endl;
  108.     cout << "4b. With 3 parameters:  add_letters('b','d','y') ---> " << "'" << add_letters('b', 'd', 'y') << "'" << endl;
  109.     cout << "4c. With 3 parameters:  add_letters('x','y','z') ---> " << "'" << add_letters('x', 'y', 'z') << "'" << endl;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement