fork download
  1. #include <iostream>
  2. #include <cassert>
  3. #include <vector>
  4. #include <string>
  5. using namespace std;
  6.  
  7. bool validate_extension_downgrade_error_message(std::vector<std::string> &args) {
  8. if(args.size() != 2) {
  9. return false;
  10. }
  11. int from[2] = {0, 0};
  12. int to[2] = {0, 0};
  13.  
  14. auto parser = [](std::string version, int result[2]) {
  15. auto dot_pos = version.find('.');
  16. if (dot_pos == std::string::npos) {
  17. result[0] = std::stoi(version);
  18. } else {
  19. result[0] = std::stoi(version.substr(0, dot_pos));
  20. assert(dot_pos < version.length() - 1);
  21. result[1] = std::stoi(version.substr(dot_pos + 1));
  22. }
  23. if (result[0] < 0 || result[1] < 0) {
  24. throw std::invalid_argument("Version cant be negative");
  25. }
  26. };
  27. try {
  28. parser(args[0], from);
  29. parser(args[1], to);
  30. } catch (const std::invalid_argument &e) {
  31. return false;
  32. }
  33. // cout << from[0] << " " << from[1] << " " << " vs " << to[0] << " " << to[1] << endl;
  34. return !std::lexicographical_compare(from, from + 2, to, to + 2);;
  35. }
  36.  
  37. int main() {
  38. std::vector<std::string> input;
  39. input = {"1.100", "1.96"};
  40. cout << validate_extension_downgrade_error_message(input) << endl;
  41. input = {"2", "1.96"};
  42. cout << validate_extension_downgrade_error_message(input) << endl;
  43. input = {"1.0", "1.96"};
  44. cout << validate_extension_downgrade_error_message(input) << endl;
  45. input = {"1.00", "-1.96"};
  46. cout << validate_extension_downgrade_error_message(input) << endl;
  47. input = {"1.0", "1.96", "2"};
  48. cout << validate_extension_downgrade_error_message(input) << endl;
  49. input = {"afsgaw", "idjwubqqdds"};
  50. cout << validate_extension_downgrade_error_message(input) << endl;
  51. return 0;
  52. }
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
1
1
0
0
0
0