code.cpp 1.1 KB

123456789101112131415161718192021222324252627282930
  1. /* Create a function that takes two integers and returns true if a number
  2. * repeats three times in a row at any place in num1
  3. * AND that same number repeats two times in a row in num2.
  4. */
  5. bool trouble(int num1, int num2) {
  6. // convert the integers to strings to access each number
  7. std::string num1string = std::to_string (num1);
  8. std::string num2string = std::to_string (num2);
  9. std::vector <std::string> sVal;
  10. for(int i = 0;i<9;i++){
  11. char c = i + '0';
  12. int counter = 0;
  13. // when the counter hits 3, it will store the value into sVal
  14. for(int j = 0;j<num1string.size();j++){
  15. if(num1string[j] == c){
  16. if(3 == ++counter){
  17. sVal.push_back(std::string(2,c));
  18. break;
  19. }
  20. }
  21. // reset the counter when not in a row
  22. else{counter = 0;}
  23. }
  24. }
  25. // all numbers that repeat three times in a row have been stored into sVal
  26. for(int i = 0;i<sVal.size();i++){
  27. if(std::string::npos!= num2string.find(sVal[i])) {return true;}
  28. }
  29. return false;
  30. }