File size: 8,062 Bytes
b1ec51e
1dbcd48
1
2
{"base_canonical_solution": "\ntemplate <typename T>\nbool has_close_elements(T numbers, float threshold) {\n  for (int i = 0; i < numbers.size(); i++)\n    for (int j = i + 1; j < numbers.size(); j++)\n      if (std::abs(numbers[i] - numbers[j]) < threshold)\n        return true;\n\n  return false;\n}\n", "base_prompt": "Make the following function generic for the parameter `numbers`.", "concepts_canonical_solution": "\ntemplate <typename T>\n  requires std::same_as<typename T::value_type, float>\nbool has_close_elements(T numbers, float threshold) {\n  for (int i = 0; i < numbers.size(); i++)\n    for (int j = i + 1; j < numbers.size(); j++)\n      if (std::abs(numbers[i] - numbers[j]) < threshold)\n        return true;\n\n  return false;\n}\n", "concepts_prompt": "Constrain the generic code using C++20 Concepts so that it accepts a container with value type of floats.", "invalids": "\nint main() {\n  std::string s{};\n  has_close_elements(s, 3.4);\n}\n", "sfinae_canonical_solution": "\ntemplate <\n    typename T,\n    std::enable_if_t<std::is_same_v<typename T::value_type, float>, int> = 0>\nbool has_close_elements(T numbers, float threshold) {\n  for (int i = 0; i < numbers.size(); i++)\n    for (int j = i + 1; j < numbers.size(); j++)\n      if (std::abs(numbers[i] - numbers[j]) < threshold)\n        return true;\n\n  return false;\n}\n", "sfinae_prompt": "Constrain the generic code using C++17 SFINAE so that it accepts a container with value type of floats.", "starter_code": "\nbool has_close_elements(std::vector<float> numbers, float threshold) {\n  for (int i = 0; i < numbers.size(); i++)\n    for (int j = i + 1; j < numbers.size(); j++)\n      if (std::abs(numbers[i] - numbers[j]) < threshold)\n        return true;\n\n  return false;\n}\n", "task_id": "HEP/0", "tests": "\n#define ASSERT(...)                                                            \\\n  do {                                                                         \\\n    if (!(__VA_ARGS__)) {                                                      \\\n      std::exit(-1);                                                           \\\n    }                                                                          \\\n  } while (false)\n\n#define TEST_ON_TYPE(_type_)                                                   \\\n  do {                                                                         \\\n    _type_ a = {1.0, 2.0, 3.9, 4.0, 5.0, 2.2};                                 \\\n    ASSERT(has_close_elements(a, 0.3) == true);                                \\\n    ASSERT(has_close_elements(a, 0.05) == false);                              \\\n    ASSERT(has_close_elements(_type_{1.0, 2.0, 5.9, 4.0, 5.0}, 0.95) == true); \\\n    ASSERT(has_close_elements(_type_{1.0, 2.0, 5.9, 4.0, 5.0}, 0.8) == false); \\\n    ASSERT(has_close_elements(_type_{1.0, 2.0, 3.0, 4.0, 5.0}, 2.0) == true);  \\\n    ASSERT(has_close_elements(_type_{1.1, 2.2, 3.1, 4.1, 5.1}, 1.0) == true);  \\\n    ASSERT(has_close_elements(_type_{1.1, 2.2, 3.1, 4.1, 5.1}, 0.5) == false); \\\n  } while (false)\n\nint main() {\n  TEST_ON_TYPE(std::vector<float>);\n  TEST_ON_TYPE(std::deque<float>);\n}\n"}
{"base_canonical_solution": "template <typename InputStr>\nstd::vector<std::string> separate_paren_groups(InputStr paren_string) {\n  std::vector<std::string> all_parens;\n  std::string current_paren;\n  int level = 0;\n  for (int i = 0; i < paren_string.size(); i++) {\n    char chr = paren_string[i];\n    if (chr == '(') {\n      level += 1;\n      current_paren += chr;\n    }\n    if (chr == ')') {\n      level -= 1;\n      current_paren += chr;\n      if (level == 0) {\n        all_parens.push_back(current_paren);\n        current_paren = \"\";\n      }\n    }\n  }\n  return all_parens;\n}", "base_prompt": "Make the following function generic for the parameter `paren_string`.", "concepts_canonical_solution": "template <typename InputStr>\n  requires requires(const InputStr& s, std::size_t i) {\n    { s[i] } -> std::convertible_to<char>;\n    { s.size() } -> std::convertible_to<int>;\n    std::same_as<typename InputStr::value_type, char>;\n  }\nstd::vector<std::string> separate_paren_groups(InputStr paren_string) {\n  std::vector<std::string> all_parens;\n  std::string current_paren;\n  int level = 0;\n  for (int i = 0; i < paren_string.length(); i++) {\n    char chr = paren_string[i];\n    if (chr == '(') {\n      level += 1;\n      current_paren += chr;\n    }\n    if (chr == ')') {\n      level -= 1;\n      current_paren += chr;\n      if (level == 0) {\n        all_parens.push_back(current_paren);\n        current_paren = \"\";\n      }\n    }\n  }\n  return all_parens;\n}", "concepts_prompt": "Constrain the generic code using C++20 Concepts so that the parameter `paren_string` must be a sequenced container of characters like a string.", "invalids": "int main() {\n  float v = 3;\n  separate_paren_groups(v);\n}", "sfinae_canonical_solution": "template <\n    typename InputStr,\n    std::enable_if_t<\n        std::conjunction_v<\n            std::is_same<char, typename InputStr::value_type>,\n            std::is_same<char, std::decay_t<decltype(std::declval<\n                                                     InputStr>()[std::declval<\n                                   std::size_t>()])>>>,\n        int> = 0>\nstd::vector<std::string> separate_paren_groups(InputStr paren_string) {\n  std::vector<std::string> all_parens;\n  std::string current_paren;\n  int level = 0;\n  for (int i = 0; i < paren_string.size(); i++) {\n    char chr = paren_string[i];\n    if (chr == '(') {\n      level += 1;\n      current_paren += chr;\n    }\n    if (chr == ')') {\n      level -= 1;\n      current_paren += chr;\n      if (level == 0) {\n        all_parens.push_back(current_paren);\n        current_paren = \"\";\n      }\n    }\n  }\n  return all_parens;\n}\n", "sfinae_prompt": "Constrain the generic code using C++17 SFINAE so that the parameter `paren_string` must be a sequenced container of characters like a string.", "starter_code": "std::vector<std::string> separate_paren_groups(std::string paren_string) {\n  std::vector<std::string> all_parens;\n  std::string current_paren;\n  int level = 0;\n  for (int i = 0; i < paren_string.length(); i++) {\n    char chr = paren_string[i];\n    if (chr == '(') {\n      level += 1;\n      current_paren += chr;\n    }\n    if (chr == ')') {\n      level -= 1;\n      current_paren += chr;\n      if (level == 0) {\n        all_parens.push_back(current_paren);\n        current_paren = \"\";\n      }\n    }\n  }\n  return all_parens;\n}", "task_id": "HEP/1", "tests": "bool issame(std::vector<std::string> a, std::vector<std::string> b) {\n  if (a.size() != b.size()) return false;\n  for (int i = 0; i < a.size(); i++) {\n    if (a[i] != b[i]) return false;\n  }\n  return true;\n}\n\n#define ASSERT(...)       \\\n  do {                    \\\n    if (!(__VA_ARGS__)) { \\\n      std::exit(-1);      \\\n    }                     \\\n  } while (false)\n\n#define TEST_ON_TYPE(_type_)                                                 \\\n  do {                                                                       \\\n    ASSERT(                                                                  \\\n        issame(separate_paren_groups(_type_(\"(()()) ((())) () ((())()())\")), \\\n               {\"(()())\", \"((()))\", \"()\", \"((())()())\"}));                   \\\n    ASSERT(issame(separate_paren_groups(_type_(\"() (()) ((())) (((())))\")),  \\\n                  {\"()\", \"(())\", \"((()))\", \"(((())))\"}));                    \\\n    ASSERT(issame(separate_paren_groups(_type_(\"(()(())((())))\")),           \\\n                  {\"(()(())((())))\"}));                                      \\\n    ASSERT(issame(separate_paren_groups(_type_(\"( ) (( )) (( )( ))\")),       \\\n                  {\"()\", \"(())\", \"(()())\"}));                                \\\n  } while (false)\n\nint main() {\n  TEST_ON_TYPE(std::string);\n  TEST_ON_TYPE(std::string_view);\n}"}