| {"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}"} | |
| {"base_canonical_solution": "template <typename Float>\nFloat truncate_number(Float number) {\n return number - (long long)(number);\n}", "base_prompt": "Make the following function generic for the parameter `number` and return value.", "concepts_canonical_solution": "template <std::floating_point Float>\nFloat truncate_number(Float number) {\n return number - (long long)(number);\n}", "concepts_prompt": "Constrain the generic code using C++20 Concepts so that the generate parameter is only floating point types.", "invalids": "int main() { truncate_number((int)3); }", "sfinae_canonical_solution": "template <typename Float,\n std::enable_if_t<std::is_floating_point_v<Float>, int> = 0>\nFloat truncate_number(Float number) {\n return number - (long long)(number);\n}", "sfinae_prompt": "Constrain the generic code using C++17 SFINAE so that the generate parameter is only floating point types.", "starter_code": "float truncate_number(float number) { return number - (long long)(number); }", "task_id": "HEP/2", "tests": "#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(truncate_number((_type_)3.5) == (_type_)0.5); \\\n ASSERT(abs(truncate_number((_type_)1.33) - (_type_)0.33) < (_type_)1e-4); \\\n ASSERT(abs(truncate_number((_type_)123.456) - (_type_)0.456) < \\\n (_type_)1e-4); \\\n \\\n } while (false)\n\nint main() {\n TEST_ON_TYPE(float);\n TEST_ON_TYPE(double);\n}"} | |
| {"base_canonical_solution": "template <typename T>\nbool below_zero(std::vector<T> operations) {\n T num = 0;\n for (int i = 0; i < operations.size(); i++) {\n num += operations[i];\n if (num < 0) return true;\n }\n return false;\n}", "base_prompt": "Make the following function generic for the value type of the vector parameter `operations`.", "concepts_canonical_solution": "template <typename T>\n requires std::integral<T> || std::floating_point<T>\nbool below_zero(std::vector<T> operations) {\n T num = 0;\n for (int i = 0; i < operations.size(); i++) {\n num += operations[i];\n if (num < 0) return true;\n }\n return false;\n}", "concepts_prompt": "Constrain the generic code using C++20 Concepts so that the value type of the vector is only number types.", "invalids": "int main() { below_zero(std::vector<int*>{}); }", "sfinae_canonical_solution": "template <typename T,\n std::enable_if_t<std::disjunction_v<std::is_integral<T>,\n std::is_floating_point<T>>,\n int> = 0>\nbool below_zero(std::vector<T> operations) {\n T num = 0;\n for (int i = 0; i < operations.size(); i++) {\n num += operations[i];\n if (num < 0) return true;\n }\n return false;\n}", "sfinae_prompt": "Constrain the generic code using C++17 SFINAE so that the value type of the vector is only number types.", "starter_code": "bool below_zero(std::vector<int> operations) {\n int num = 0;\n for (int i = 0; i < operations.size(); i++) {\n num += operations[i];\n if (num < 0) return true;\n }\n return false;\n}", "task_id": "HEP/3", "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 ASSERT(below_zero(std::vector<_type_>{}) == false); \\\n ASSERT(below_zero(std::vector<_type_>{1, 2, -3, 1, 2, -3}) == false); \\\n ASSERT(below_zero(std::vector<_type_>{1, 2, -4, 5, 6}) == true); \\\n ASSERT(below_zero(std::vector<_type_>{1, -1, 2, -2, 5, -5, 4, -4}) == \\\n false); \\\n ASSERT(below_zero(std::vector<_type_>{1, -1, 2, -2, 5, -5, 4, -5}) == \\\n true); \\\n ASSERT(below_zero(std::vector<_type_>{1, -2, 2, -2, 5, -5, 4, -4}) == \\\n true); \\\n \\\n } while (false)\n\nint main() {\n TEST_ON_TYPE(int);\n TEST_ON_TYPE(char);\n TEST_ON_TYPE(float);\n}"} | |
| {"base_canonical_solution": "template <typename T>\nT mean_absolute_deviation(std::vector<T> numbers) {\n T sum = 0;\n for (int i = 0; i < numbers.size(); i++) sum += numbers[i];\n T avg = sum / numbers.size();\n T msum = 0;\n for (int i = 0; i < numbers.size(); i++) msum += std::abs(numbers[i] - avg);\n return msum / numbers.size();\n}", "base_prompt": "Make the following function generic for the value type of the vector parameter `numbers`.", "concepts_canonical_solution": "template <std::floating_point T>\nT mean_absolute_deviation(std::vector<T> numbers) {\n T sum = 0;\n for (int i = 0; i < numbers.size(); i++) sum += numbers[i];\n T avg = sum / numbers.size();\n T msum = 0;\n for (int i = 0; i < numbers.size(); i++) msum += std::abs(numbers[i] - avg);\n return msum / numbers.size();\n}", "concepts_prompt": "Constrain the generic code using C++20 Concepts so that the generic type is only floating point types.", "invalids": "int main() { mean_absolute_deviation(std::vector<int>{0, 1, 2}); }", "sfinae_canonical_solution": "template <typename T, std::enable_if_t<std::is_floating_point_v<T>, int> = 0>\nT mean_absolute_deviation(std::vector<T> numbers) {\n T sum = 0;\n for (int i = 0; i < numbers.size(); i++) sum += numbers[i];\n T avg = sum / numbers.size();\n T msum = 0;\n for (int i = 0; i < numbers.size(); i++) msum += std::abs(numbers[i] - avg);\n return msum / numbers.size();\n}", "sfinae_prompt": "Constrain the generic code using C++17 SFINAE so that the generic type is only floating point types.", "starter_code": "float mean_absolute_deviation(std::vector<float> numbers) {\n float sum = 0;\n for (int i = 0; i < numbers.size(); i++) sum += numbers[i];\n float avg = sum / numbers.size();\n float msum = 0;\n for (int i = 0; i < numbers.size(); i++) msum += std::abs(numbers[i] - avg);\n return msum / numbers.size();\n}", "task_id": "HEP/4", "tests": "#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(abs(mean_absolute_deviation(std::vector<_type_>{1.0, 2.0, 3.0}) - \\\n (_type_)2.0 / (_type_)3.0) < (_type_)1e-4); \\\n ASSERT( \\\n abs(mean_absolute_deviation(std::vector<_type_>{1.0, 2.0, 3.0, 4.0}) - \\\n (_type_)1.0) < (_type_)1e-4); \\\n ASSERT(abs(mean_absolute_deviation( \\\n std::vector<_type_>{1.0, 2.0, 3.0, 4.0, 5.0}) - \\\n (_type_)6.0 / (_type_)5.0) < (_type_)1e-4); \\\n } while (false)\n\nint main() {\n TEST_ON_TYPE(float);\n TEST_ON_TYPE(double);\n}"} | |
| {"base_canonical_solution": "template <typename Container, typename Value>\nContainer intersperse(Container numbers, Value delimeter) {\n Container out;\n if (numbers.size() > 0) out.push_back(numbers[0]);\n for (int i = 1; i < numbers.size(); i++) {\n out.push_back(delimeter);\n out.push_back(numbers[i]);\n }\n return out;\n}", "base_prompt": "Make the following function generic for both parameters and return type.", "concepts_canonical_solution": "template <typename Container, typename Value>\n requires requires(Container &c, Value v) {\n { c.push_back(v) };\n }\nContainer intersperse(Container numbers, Value delimeter) {\n Container out;\n if (numbers.size() > 0) out.push_back(numbers[0]);\n for (int i = 1; i < numbers.size(); i++) {\n out.push_back(delimeter);\n out.push_back(numbers[i]);\n }\n return out;\n}", "concepts_prompt": "Constrain the generic code using C++20 Concepts so that the value to intersperse can be pushed back on the container.", "invalids": "int main() {\n std::vector<int*> v;\n intersperse(v, int{0});\n}", "sfinae_canonical_solution": "template <typename Container, typename Value,\n typename = std::void_t<decltype(std::declval<Container &>().push_back(\n std::declval<Value>()))>>\nContainer intersperse(Container numbers, Value delimeter) {\n Container out;\n if (numbers.size() > 0) out.push_back(numbers[0]);\n for (int i = 1; i < numbers.size(); i++) {\n out.push_back(delimeter);\n out.push_back(numbers[i]);\n }\n return out;\n}", "sfinae_prompt": "Constrain the generic code using C++17 SFINAE so that the value to intersperse can be pushed back on the container.", "starter_code": "std::vector<int> intersperse(std::vector<int> numbers, int delimeter) {\n std::vector<int> out;\n if (numbers.size() > 0) out.push_back(numbers[0]);\n for (int i = 1; i < numbers.size(); i++) {\n out.push_back(delimeter);\n out.push_back(numbers[i]);\n }\n return out;\n}", "task_id": "HEP/5", "tests": "template <typename T>\nbool issame(T a, T 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_, _value_) \\\n do { \\\n ASSERT(issame(intersperse(_type_{}, (_value_)7), _type_{})); \\\n ASSERT(issame(intersperse(_type_{5, 6, 3, 2}, (_value_)8), \\\n _type_{5, 8, 6, 8, 3, 8, 2})); \\\n ASSERT(issame(intersperse(_type_{2, 2, 2}, (_value_)2), \\\n _type_{2, 2, 2, 2, 2})); \\\n } while (false)\n\nint main() {\n TEST_ON_TYPE(std::vector<int>, int);\n TEST_ON_TYPE(std::vector<float>, long);\n TEST_ON_TYPE(std::deque<float>, float);\n}"} | |
| {"base_canonical_solution": "template <typename T>\nstd::vector<int> parse_nested_parens(T paren_string) {\n std::vector<int> all_levels;\n std::string current_paren;\n int level = 0, max_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 if (level > max_level) max_level = level;\n current_paren += chr;\n }\n if (chr == ')') {\n level -= 1;\n current_paren += chr;\n if (level == 0) {\n all_levels.push_back(max_level);\n current_paren = \"\";\n max_level = 0;\n }\n }\n }\n return all_levels;\n}", "base_prompt": "Make the following function generic for the parameter `paren_string`.", "concepts_canonical_solution": "template <typename T>\n requires requires(const T& s, std::size_t i) {\n { s[i] } -> std::convertible_to<char>;\n { s.size() } -> std::convertible_to<int>;\n std::same_as<typename T::value_type, char>;\n }\nstd::vector<int> parse_nested_parens(T paren_string) {\n std::vector<int> all_levels;\n std::string current_paren;\n int level = 0, max_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 if (level > max_level) max_level = level;\n current_paren += chr;\n }\n if (chr == ')') {\n level -= 1;\n current_paren += chr;\n if (level == 0) {\n all_levels.push_back(max_level);\n current_paren = \"\";\n max_level = 0;\n }\n }\n }\n return all_levels;\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() { parse_nested_parens(float{3}); }", "sfinae_canonical_solution": "template <\n typename T,\n std::enable_if_t<\n std::conjunction_v<\n std::is_same<char, typename T::value_type>,\n std::is_same<char,\n std::decay_t<decltype(std::declval<T>()[std::declval<\n std::size_t>()])>>>,\n int> = 0>\nstd::vector<int> parse_nested_parens(T paren_string) {\n std::vector<int> all_levels;\n std::string current_paren;\n int level = 0, max_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 if (level > max_level) max_level = level;\n current_paren += chr;\n }\n if (chr == ')') {\n level -= 1;\n current_paren += chr;\n if (level == 0) {\n all_levels.push_back(max_level);\n current_paren = \"\";\n max_level = 0;\n }\n }\n }\n return all_levels;\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<int> parse_nested_parens(std::string paren_string) {\n std::vector<int> all_levels;\n std::string current_paren;\n int level = 0, max_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 if (level > max_level) max_level = level;\n current_paren += chr;\n }\n if (chr == ')') {\n level -= 1;\n current_paren += chr;\n if (level == 0) {\n all_levels.push_back(max_level);\n current_paren = \"\";\n max_level = 0;\n }\n }\n }\n return all_levels;\n}", "task_id": "HEP/6", "tests": "bool issame(std::vector<int> a, std::vector<int> 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(issame(parse_nested_parens(_type_{\"(()()) ((())) () ((())()())\"}), \\\n {2, 3, 1, 3})); \\\n ASSERT(issame(parse_nested_parens(_type_{\"() (()) ((())) (((())))\"}), \\\n {1, 2, 3, 4})); \\\n ASSERT(issame(parse_nested_parens(_type_{\"(()(())((())))\"}), {4})); \\\n } while (false)\n\nint main() {\n TEST_ON_TYPE(std::string);\n TEST_ON_TYPE(std::string_view);\n}"} | |
| {"base_canonical_solution": "template <typename T>\nT filter_by_substring(T strings, std::string substring) {\n T out;\n for (int i = 0; i < strings.size(); i++) {\n if (strings[i].find(substring) != strings[i].npos)\n out.push_back(strings[i]);\n }\n return out;\n}", "base_prompt": "Make the following function generic for the parameter `string` and return value.", "concepts_canonical_solution": "template <typename T>\n requires std::same_as<typename T::value_type, std::string>\nT filter_by_substring(T strings, std::string substring) {\n T out;\n for (int i = 0; i < strings.size(); i++) {\n if (strings[i].find(substring) != strings[i].npos)\n out.push_back(strings[i]);\n }\n return out;\n}", "concepts_prompt": "Constrain the generic code using C++20 Concepts so that it accepts a sequenced container with value type of string.", "invalids": "int main() { filter_by_substring(std::vector<int>{}, \"hello\"); }", "sfinae_canonical_solution": "template <typename T,\n std::enable_if_t<std::is_same_v<typename T::value_type, std::string>,\n int> = 0>\nT filter_by_substring(T strings, std::string substring) {\n T out;\n for (int i = 0; i < strings.size(); i++) {\n if (strings[i].find(substring) != strings[i].npos)\n out.push_back(strings[i]);\n }\n return out;\n}", "sfinae_prompt": "Constrain the generic code using C++17 SFINAE so that it accepts a sequenced container with value type of string.", "starter_code": "std::vector<string> filter_by_substring(std::vector<string> strings,\n std::string substring) {\n std::vector<string> out;\n for (int i = 0; i < strings.size(); i++) {\n if (strings[i].find(substring) != strings[i].npos)\n out.push_back(strings[i]);\n }\n return out;\n}", "task_id": "HEP/7", "tests": "template <typename T>\nbool issame(T a, T 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(issame(filter_by_substring(_type_{}, \"john\"), _type_{})); \\\n ASSERT(issame( \\\n filter_by_substring( \\\n _type_{\"xxx\", \"asd\", \"xxy\", \"john doe\", \"xxxAAA\", \"xxx\"}, \"xxx\"), \\\n _type_{\"xxx\", \"xxxAAA\", \"xxx\"})); \\\n ASSERT(issame(filter_by_substring(_type_{\"xxx\", \"asd\", \"aaaxxy\", \\\n \"john doe\", \"xxxAAA\", \"xxx\"}, \\\n \"xx\"), \\\n _type_{\"xxx\", \"aaaxxy\", \"xxxAAA\", \"xxx\"})); \\\n ASSERT(issame(filter_by_substring( \\\n _type_{\"grunt\", \"trumpet\", \"prune\", \"gruesome\"}, \"run\"), \\\n _type_{\"grunt\", \"prune\"})); \\\n } while (false)\n\nint main() {\n TEST_ON_TYPE(std::vector<std::string>);\n TEST_ON_TYPE(std::deque<std::string>);\n}\n"} | |
| {"base_canonical_solution": "template <typename T>\nstd::pair<T, T> sum_product(std::vector<T> numbers) {\n T sum = 0, product = 1;\n for (int i = 0; i < numbers.size(); i++) {\n sum += numbers[i];\n product *= numbers[i];\n }\n return {sum, product};\n}", "base_prompt": "Make the following function generic for the value types of the parameter and return pair.", "concepts_canonical_solution": "template <typename T>\n requires std::integral<T> || std::floating_point<T>\nstd::pair<T, T> sum_product(std::vector<T> numbers) {\n T sum = 0, product = 1;\n for (int i = 0; i < numbers.size(); i++) {\n sum += numbers[i];\n product *= numbers[i];\n }\n return {sum, product};\n}", "concepts_prompt": "Constrain the generic code using C++20 Concepts so that the generic parameter is a number type.", "invalids": "int main() { sum_product(std::vector<int*>{}); }", "sfinae_canonical_solution": "template <typename T,\n std::enable_if_t<std::disjunction_v<std::is_integral<T>,\n std::is_floating_point<T>>,\n int> = 0>\nstd::pair<T, T> sum_product(std::vector<T> numbers) {\n T sum = 0, product = 1;\n for (int i = 0; i < numbers.size(); i++) {\n sum += numbers[i];\n product *= numbers[i];\n }\n return {sum, product};\n}", "sfinae_prompt": "Constrain the generic code using C++17 SFINAE so that the generic parameter is a number type.", "starter_code": "std::pair<int, int> sum_product(std::vector<int> numbers) {\n int sum = 0, product = 1;\n for (int i = 0; i < numbers.size(); i++) {\n sum += numbers[i];\n product *= numbers[i];\n }\n return {sum, product};\n}", "task_id": "HEP/8", "tests": "#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(sum_product(std::vector<_type_>{}) == \\\n std::pair<_type_, _type_>{0, 1}); \\\n ASSERT(sum_product(std::vector<_type_>{1, 1, 1}) == \\\n std::pair<_type_, _type_>{3, 1}); \\\n ASSERT(sum_product(std::vector<_type_>{100, 0}) == \\\n std::pair<_type_, _type_>{100, 0}); \\\n ASSERT(sum_product(std::vector<_type_>{3, 5, 7}) == \\\n std::pair<_type_, _type_>{3 + 5 + 7, 3 * 5 * 7}); \\\n ASSERT(sum_product(std::vector<_type_>{10}) == \\\n std::pair<_type_, _type_>{10, 10}); \\\n } while (false)\n\nint main() {\n TEST_ON_TYPE(float);\n TEST_ON_TYPE(int);\n TEST_ON_TYPE(long);\n}\n"} | |
| {"base_canonical_solution": "template <typename T>\nstd::vector<T> rolling_max(std::vector<T> numbers) {\n if (numbers.size() == 0) return {};\n std::vector<T> out;\n T max = numbers.front();\n for (int i = 0; i < numbers.size(); i++) {\n if (numbers[i] > max) max = numbers[i];\n out.push_back(max);\n }\n return out;\n}", "base_prompt": "Make the following function generic for value type of the parameter and return type.", "concepts_canonical_solution": "template <typename T>\n requires requires(const T& lhs, const T& rhs) {\n { lhs > rhs } -> std::convertible_to<bool>;\n }\nstd::vector<T> rolling_max(std::vector<T> numbers) {\n if (numbers.size() == 0) return {};\n std::vector<T> out;\n T max = numbers.front();\n for (int i = 0; i < numbers.size(); i++) {\n if (numbers[i] > max) max = numbers[i];\n out.push_back(max);\n }\n return out;\n}", "concepts_prompt": "Constrain the generic code using C++20 Concepts so that the generic parameter must be comparable to another of its type with greater than operator.", "invalids": "int main() {\n struct S {};\n rolling_max(std::vector<S>{});\n}", "sfinae_canonical_solution": "template <\n typename T,\n std::enable_if_t<std::is_convertible_v<decltype(std::declval<const T &>() >\n std::declval<const T &>()),\n bool>,\n int> = 0>\nstd::vector<T> rolling_max(std::vector<T> numbers) {\n if (numbers.size() == 0) return {};\n std::vector<T> out;\n T max = numbers.front();\n for (int i = 0; i < numbers.size(); i++) {\n if (numbers[i] > max) max = numbers[i];\n out.push_back(max);\n }\n return out;\n}", "sfinae_prompt": "Constrain the generic code using C++17 SFINAE so that the generic parameter must be comparable to another of its type with greater than operator.", "starter_code": "std::vector<int> rolling_max(std::vector<int> numbers) {\n if (numbers.size() == 0) return {};\n std::vector<int> out;\n int max = numbers.front();\n for (int i = 0; i < numbers.size(); i++) {\n if (numbers[i] > max) max = numbers[i];\n out.push_back(max);\n }\n return out;\n}", "task_id": "HEP/9", "tests": "\ntemplate <typename T>\nbool issame(std::vector<T> a, std::vector<T> 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(issame(rolling_max(std::vector<_type_>{}), std::vector<_type_>{})); \\\n ASSERT(issame(rolling_max(std::vector<_type_>{1, 2, 3, 4}), \\\n std::vector<_type_>{1, 2, 3, 4})); \\\n ASSERT(issame(rolling_max(std::vector<_type_>{4, 3, 2, 1}), \\\n std::vector<_type_>{4, 4, 4, 4})); \\\n ASSERT(issame(rolling_max(std::vector<_type_>{3, 2, 3, 100, 3}), \\\n std::vector<_type_>{3, 3, 3, 100, 100})); \\\n } while (false)\n\nint main() {\n TEST_ON_TYPE(int);\n TEST_ON_TYPE(float);\n\n ASSERT(issame(rolling_max(std::vector<std::string>{}),\n std::vector<std::string>{}));\n ASSERT(issame(rolling_max(std::vector<std::string>{\"1\", \"2\", \"3\", \"4\"}),\n std::vector<std::string>{\"1\", \"2\", \"3\", \"4\"}));\n ASSERT(issame(rolling_max(std::vector<std::string>{\"4\", \"3\", \"2\", \"1\"}),\n std::vector<std::string>{\"4\", \"4\", \"4\", \"4\"}));\n ASSERT(issame(rolling_max(std::vector<std::string>{\"3\", \"2\", \"3\", \"5\", \"3\"}),\n std::vector<std::string>{\"3\", \"3\", \"3\", \"5\", \"5\"}));\n}"} | |
| {"base_canonical_solution": "template <typename T>\nstd::string string_xor(T a, T b) {\n std::string output = \"\";\n for (int i = 0; (i < a.size() && i < b.size()); i++) {\n if (i < a.size() && i < b.size()) {\n if (a[i] == b[i]) {\n output += '0';\n } else\n output += '1';\n } else {\n if (i >= a.size()) {\n output += b[i];\n } else\n output += a[i];\n }\n }\n return output;\n}", "base_prompt": "Make the following function generic for the type of the two parameters.", "concepts_canonical_solution": "template <typename T>\n requires std::same_as<typename T::value_type, char>\nstd::string string_xor(T a, T b) {\n std::string output = \"\";\n for (int i = 0; (i < a.size() && i < b.size()); i++) {\n if (i < a.size() && i < b.size()) {\n if (a[i] == b[i]) {\n output += '0';\n } else\n output += '1';\n } else {\n if (i >= a.size()) {\n output += b[i];\n } else\n output += a[i];\n }\n }\n return output;\n}", "concepts_prompt": "Constrain the generic code using C++20 Concepts so that the generic parameter is a sequenced container of characters.", "invalids": "int main() { string_xor(std::vector<int>{}, std::vector<int>{}); }", "sfinae_canonical_solution": "template <\n typename T,\n std::enable_if_t<std::is_same_v<typename T::value_type, char>, int> = 0>\nstd::string string_xor(T a, T b) {\n std::string output = \"\";\n for (int i = 0; (i < a.size() && i < b.size()); i++) {\n if (i < a.size() && i < b.size()) {\n if (a[i] == b[i]) {\n output += '0';\n } else\n output += '1';\n } else {\n if (i >= a.size()) {\n output += b[i];\n } else\n output += a[i];\n }\n }\n return output;\n}", "sfinae_prompt": "Constrain the generic code using C++17 SFINAE so that the generic parameter is a sequenced container of characters.", "starter_code": "std::string string_xor(std::string a, std::string b) {\n std::string output = \"\";\n for (int i = 0; (i < a.size() && i < b.size()); i++) {\n if (i < a.size() && i < b.size()) {\n if (a[i] == b[i]) {\n output += '0';\n } else\n output += '1';\n } else {\n if (i >= a.size()) {\n output += b[i];\n } else\n output += a[i];\n }\n }\n return output;\n}", "task_id": "HEP/11", "tests": "#define ASSERT(...) \\\n do { \\\n if (!(__VA_ARGS__)) { \\\n std::exit(-1); \\\n } \\\n } while (false)\n\nint main() {\n ASSERT(string_xor(std::string{\"111000\"}, std::string{\"101010\"}) ==\n std::string{\"010010\"});\n ASSERT(string_xor(std::string{\"1\"}, std::string{\"1\"}) == std::string{\"0\"});\n ASSERT(string_xor(std::string{\"0101\"}, std::string{\"0000\"}) ==\n std::string{\"0101\"});\n\n ASSERT(string_xor(std::vector<char>{'1', '1', '1', '0', '0', '0'},\n std::vector<char>{'1', '0', '1', '0', '1', '0'}) ==\n std::string{\"010010\"});\n ASSERT(string_xor(std::vector<char>{'1'}, std::vector<char>{'1'}) ==\n std::string{\"0\"});\n ASSERT(string_xor(std::vector<char>{'0', '1', '0', '1'},\n std::vector<char>{'0', '0', '0', '0'}) ==\n std::string{\"0101\"});\n}\n"} | |
| {"base_canonical_solution": "template <typename T>\nstd::string longest(T strings) {\n std::string out;\n for (int i = 0; i < strings.size(); i++) {\n if (strings[i].length() > out.length()) out = strings[i];\n }\n return out;\n}", "base_prompt": "Make the following function generic for the container parameter `strings`.", "concepts_canonical_solution": "template <typename T>\n requires std::same_as<typename T::value_type, std::string>\nstd::string longest(T strings) {\n std::string out;\n for (int i = 0; i < strings.size(); i++) {\n if (strings[i].length() > out.length()) out = strings[i];\n }\n return out;\n}", "concepts_prompt": "Constrain the generic code using C++20 Concepts so that the generic parameter is a sequenced container of value type string.", "invalids": "int main() { longest(std::vector<int>{}) }", "sfinae_canonical_solution": "template <typename T,\n std::enable_if_t<std::is_same_v<typename T::value_type, std::string>,\n int> = 0>\nstd::string longest(T strings) {\n std::string out;\n for (int i = 0; i < strings.size(); i++) {\n if (strings[i].length() > out.length()) out = strings[i];\n }\n return out;\n}", "sfinae_prompt": "Constrain the generic code using C++17 SFINAE so that the generic parameter is a sequenced container of value type string.", "starter_code": "std::string longest(std::vector<std::string> strings) {\n std::string out;\n for (int i = 0; i < strings.size(); i++) {\n if (strings[i].length() > out.length()) out = strings[i];\n }\n return out;\n}", "task_id": "HEP/12", "tests": "#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(longest(_type_{}) == \"\"); \\\n ASSERT(longest(_type_{\"x\", \"y\", \"z\"}) == \"x\"); \\\n ASSERT(longest(_type_{\"x\", \"yyy\", \"zzzz\", \"www\", \"kkkk\", \"abc\"}) == \\\n \"zzzz\"); \\\n \\\n } while (false)\n\nint main() {\n TEST_ON_TYPE(std::vector<std::string>);\n TEST_ON_TYPE(std::deque<std::string>);\n}\n"} | |
| {"base_canonical_solution": "template <typename T>\nT greatest_common_divisor(T a, T b) {\n T out, m;\n while (true) {\n if (a < b) {\n m = a;\n a = b;\n b = m;\n }\n a = a % b;\n if (a == 0) return b;\n }\n}", "base_prompt": "Make the following function generic to share same type for all parameters and return type.", "concepts_canonical_solution": "template <std::integral T>\nT greatest_common_divisor(T a, T b) {\n T out, m;\n while (true) {\n if (a < b) {\n m = a;\n a = b;\n b = m;\n }\n a = a % b;\n if (a == 0) return b;\n }\n}", "concepts_prompt": "Constrain the generic code using C++20 Concepts so that the generic parameter is an integer type.", "invalids": "int main() { greatest_common_divisor(3.0, 4.0); }", "sfinae_canonical_solution": "template <typename T, std::enable_if_t<std::is_integral_v<T>, int> = 0>\nT greatest_common_divisor(T a, T b) {\n T out, m;\n while (true) {\n if (a < b) {\n m = a;\n a = b;\n b = m;\n }\n a = a % b;\n if (a == 0) return b;\n }\n}", "sfinae_prompt": "Constrain the generic code using C++17 SFINAE so that the generic parameter is an integer type.", "starter_code": "int greatest_common_divisor(int a, int b) {\n int out, m;\n while (true) {\n if (a < b) {\n m = a;\n a = b;\n b = m;\n }\n a = a % b;\n if (a == 0) return b;\n }\n}", "task_id": "HEP/13", "tests": "#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(greatest_common_divisor((_type_)3, (_type_)7) == (_type_)1); \\\n ASSERT(greatest_common_divisor((_type_)10, (_type_)15) == (_type_)5); \\\n ASSERT(greatest_common_divisor((_type_)49, (_type_)14) == (_type_)7); \\\n ASSERT(greatest_common_divisor((_type_)144, (_type_)60) == (_type_)12); \\\n \\\n } while (false)\n\nint main() {\n TEST_ON_TYPE(int);\n TEST_ON_TYPE(long);\n TEST_ON_TYPE(long long);\n}\n"} | |
| {"base_canonical_solution": "template <typename T>\nstd::vector<std::string> all_prefixes(T str) {\n std::vector<std::string> out;\n std::string current = \"\";\n for (int i = 0; i < str.size(); i++) {\n current = current + str[i];\n out.push_back(current);\n }\n return out;\n}", "base_prompt": "Make the following function generic for the parameter `str`.", "concepts_canonical_solution": "template <typename T>\n requires requires(const T& s, std::size_t i) {\n { s[i] } -> std::convertible_to<char>;\n { s.size() } -> std::convertible_to<int>;\n std::same_as<typename T::value_type, char>;\n }\nstd::vector<std::string> all_prefixes(T str) {\n std::vector<std::string> out;\n std::string current = \"\";\n for (int i = 0; i < str.size(); i++) {\n current = current + str[i];\n out.push_back(current);\n }\n return out;\n}", "concepts_prompt": "Constrain the generic code using C++20 Concepts so that the generic type is a sequenced container of characters like a string.", "invalids": "int main() { all_prefixes(std::vector<int>{}); }", "sfinae_canonical_solution": "template <\n typename T,\n std::enable_if_t<\n std::conjunction_v<\n std::is_same<char, typename T::value_type>,\n std::is_same<char,\n std::decay_t<decltype(std::declval<T>()[std::declval<\n std::size_t>()])>>>,\n int> = 0>\nstd::vector<std::string> all_prefixes(T str) {\n std::vector<std::string> out;\n std::string current = \"\";\n for (int i = 0; i < str.size(); i++) {\n current = current + str[i];\n out.push_back(current);\n }\n return out;\n}", "sfinae_prompt": "Constrain the generic code using C++17 SFINAE so that the generic type is a sequenced container of characters like a string.", "starter_code": "std::vector<std::string> all_prefixes(std::string str) {\n std::vector<std::string> out;\n std::string current = \"\";\n for (int i = 0; i < str.size(); i++) {\n current = current + str[i];\n out.push_back(current);\n }\n return out;\n}", "task_id": "HEP/14", "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(issame(all_prefixes(_type_{\"\"}), {})); \\\n ASSERT(issame(all_prefixes(_type_{\"asdfgh\"}), \\\n {\"a\", \"as\", \"asd\", \"asdf\", \"asdfg\", \"asdfgh\"})); \\\n ASSERT(issame(all_prefixes(_type_{\"WWW\"}), {\"W\", \"WW\", \"WWW\"})); \\\n \\\n } while (false)\n\nint main() {\n TEST_ON_TYPE(std::string);\n TEST_ON_TYPE(std::string_view);\n}\n"} | |
| {"base_canonical_solution": "template <typename T>\nstd::string string_sequence(T n) {\n std::string out = \"0\";\n for (T i = 1; i <= n && i != 0; i++) out = out + \" \" + std::to_string(i);\n return out;\n}", "base_prompt": "Make the following function generic for the parameter `n`.", "concepts_canonical_solution": "template <std::unsigned_integral T>\nstd::string string_sequence(T n) {\n std::string out = \"0\";\n for (T i = 1; i <= n && i != 0; i++) out = out + \" \" + std::to_string(i);\n return out;\n}", "concepts_prompt": "Constrain the generic code using C++20 Concepts so that the generic parameter is an unsigned number.", "invalids": "int main() { string_sequence((int)5); }", "sfinae_canonical_solution": "template <typename T, std::enable_if_t<std::is_unsigned_v<T>, int> = 0>\nstd::string string_sequence(T n) {\n std::string out = \"0\";\n for (T i = 1; i <= n && i != 0; i++) out = out + \" \" + std::to_string(i);\n return out;\n}", "sfinae_prompt": "Constrain the generic code using C++17 SFINAE so that the generic parameter is an unsigned number.", "starter_code": "std::string string_sequence(unsigned n) {\n std::string out = \"0\";\n for (unsigned i = 1; i <= n && i != 0; i++)\n out = out + \" \" + std::to_string(i);\n return out;\n}", "task_id": "HEP/15", "tests": "#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(string_sequence((_type_)0) == \"0\"); \\\n ASSERT(string_sequence((_type_)3) == \"0 1 2 3\"); \\\n ASSERT(string_sequence((_type_)10) == \"0 1 2 3 4 5 6 7 8 9 10\"); \\\n \\\n } while (false)\n\nint main() {\n TEST_ON_TYPE(unsigned long);\n TEST_ON_TYPE(unsigned int);\n}\n"} | |
| {"base_canonical_solution": "template <typename T>\nint count_distinct_characters(T str) {\n std::vector<char> distinct = {};\n std::transform(str.begin(), str.end(), str.begin(), ::tolower);\n for (int i = 0; i < str.size(); i++) {\n bool isin = false;\n for (int j = 0; j < distinct.size(); j++)\n if (distinct[j] == str[i]) isin = true;\n if (isin == false) distinct.push_back(str[i]);\n }\n return distinct.size();\n}", "base_prompt": "Make the following function generic for the parameter `str`.", "concepts_canonical_solution": "template <typename T>\n requires std::same_as<typename T::value_type, char>\nint count_distinct_characters(T str) {\n std::vector<char> distinct = {};\n std::transform(str.begin(), str.end(), str.begin(), ::tolower);\n for (int i = 0; i < str.size(); i++) {\n bool isin = false;\n for (int j = 0; j < distinct.size(); j++)\n if (distinct[j] == str[i]) isin = true;\n if (isin == false) distinct.push_back(str[i]);\n }\n return distinct.size();\n}", "concepts_prompt": "Constrain the generic code using C++20 Concepts so that the generic parameter is a sequenced container of value type character.", "invalids": "int main() { count_distinct_characters(std::vector<int>{}); }", "sfinae_canonical_solution": "template <\n typename T,\n std::enable_if_t<std::is_same_v<typename T::value_type, char>, int> = 0>\nint count_distinct_characters(T str) {\n std::vector<char> distinct = {};\n std::transform(str.begin(), str.end(), str.begin(), ::tolower);\n for (int i = 0; i < str.size(); i++) {\n bool isin = false;\n for (int j = 0; j < distinct.size(); j++)\n if (distinct[j] == str[i]) isin = true;\n if (isin == false) distinct.push_back(str[i]);\n }\n return distinct.size();\n}", "sfinae_prompt": "Constrain the generic code using C++17 SFINAE so that the generic parameter is a sequenced container of value type character.", "starter_code": "int count_distinct_characters(std::string str) {\n std::vector<char> distinct = {};\n std::transform(str.begin(), str.end(), str.begin(), ::tolower);\n for (int i = 0; i < str.size(); i++) {\n bool isin = false;\n for (int j = 0; j < distinct.size(); j++)\n if (distinct[j] == str[i]) isin = true;\n if (isin == false) distinct.push_back(str[i]);\n }\n return distinct.size();\n}", "task_id": "HEP/16", "tests": "#define ASSERT(...) \\\n do { \\\n if (!(__VA_ARGS__)) { \\\n std::exit(-1); \\\n } \\\n } while (false)\n\nint main() {\n ASSERT(count_distinct_characters(std::string{\"\"}) == 0);\n ASSERT(count_distinct_characters(std::string{\"abcde\"}) == 5);\n ASSERT(count_distinct_characters(std::string{\"abcdecadeCADE\"}) == 5);\n ASSERT(count_distinct_characters(std::string{\"aaaaAAAAaaaa\"}) == 1);\n ASSERT(count_distinct_characters(std::string{\"Jerry jERRY JeRRRY\"}) == 5);\n\n ASSERT(count_distinct_characters(std::vector<char>{}) == 0);\n ASSERT(count_distinct_characters(\n std::vector<char>{'a', 'b', 'c', 'd', 'e'}) == 5);\n ASSERT(count_distinct_characters(std::vector<char>{'a', 'b', 'c', 'd', 'e',\n 'c', 'a', 'd', 'e', 'C',\n 'A', 'D', 'E'}) == 5);\n ASSERT(count_distinct_characters(std::vector<char>{\n 'a', 'a', 'a', 'a', 'A', 'A', 'A', 'A', 'a', 'a', 'a', 'a'}) == 1);\n ASSERT(count_distinct_characters(\n std::vector<char>{'J', 'e', 'r', 'r', 'y', ' ', 'j', 'E', 'R', 'R',\n 'Y', ' ', 'J', 'e', 'R', 'R', 'R', 'Y'}) == 5);\n}\n"} | |
| {"base_canonical_solution": "template <typename T>\nT parse_music(std::string music_string) {\n std::string current = \"\";\n T out = {};\n if (music_string.size() > 0) music_string = music_string + ' ';\n for (int i = 0; i < music_string.length(); i++) {\n if (music_string[i] == ' ') {\n if (current == \"o\") out.push_back(4);\n if (current == \"o|\") out.push_back(2);\n if (current == \".|\") out.push_back(1);\n current = \"\";\n } else\n current += music_string[i];\n }\n return out;\n}", "base_prompt": "Make the following function generic for the return type.", "concepts_canonical_solution": "template <typename T>\n requires requires(T& t, int v) {\n { t.push_back(v) };\n }\nT parse_music(std::string music_string) {\n std::string current = \"\";\n T out = {};\n if (music_string.size() > 0) music_string = music_string + ' ';\n for (int i = 0; i < music_string.length(); i++) {\n if (music_string[i] == ' ') {\n if (current == \"o\") out.push_back(4);\n if (current == \"o|\") out.push_back(2);\n if (current == \".|\") out.push_back(1);\n current = \"\";\n } else\n current += music_string[i];\n }\n return out;\n}", "concepts_prompt": "Constrain the generic code using C++20 Concepts so that the generic parameter is a container of int that can be pushed back into.", "invalids": "int main() { parse_music(float{3.0}) }", "sfinae_canonical_solution": "template <typename T,\n typename = std::void_t<\n decltype(std::declval<T&>().push_back(std::declval<int>()))>>\nT parse_music(std::string music_string) {\n std::string current = \"\";\n T out = {};\n if (music_string.size() > 0) music_string = music_string + ' ';\n for (int i = 0; i < music_string.length(); i++) {\n if (music_string[i] == ' ') {\n if (current == \"o\") out.push_back(4);\n if (current == \"o|\") out.push_back(2);\n if (current == \".|\") out.push_back(1);\n current = \"\";\n } else\n current += music_string[i];\n }\n return out;\n}", "sfinae_prompt": "Constrain the generic code using C++17 SFINAE so that the generic parameter is a container of int that can be pushed back into.", "starter_code": "std::vector<int> parse_music(std::string music_string) {\n std::string current = \"\";\n std::vector<int> out = {};\n if (music_string.size() > 0) music_string = music_string + ' ';\n for (int i = 0; i < music_string.length(); i++) {\n if (music_string[i] == ' ') {\n if (current == \"o\") out.push_back(4);\n if (current == \"o|\") out.push_back(2);\n if (current == \".|\") out.push_back(1);\n current = \"\";\n } else\n current += music_string[i];\n }\n return out;\n}", "task_id": "HEP/17", "tests": "\ntemplate <typename T>\nbool issame(T a, T b) {\n if (a.size() != b.size()) return false;\n return std::equal(a.begin(), a.end(), b.begin());\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(issame(parse_music<_type_>(\"\"), _type_{})); \\\n ASSERT(issame(parse_music<_type_>(\"o o o o\"), _type_{4, 4, 4, 4})); \\\n ASSERT(issame(parse_music<_type_>(\".| .| .| .|\"), _type_{1, 1, 1, 1})); \\\n ASSERT(issame(parse_music<_type_>(\"o| o| .| .| o o o o\"), \\\n _type_{2, 2, 1, 1, 4, 4, 4, 4})); \\\n ASSERT(issame(parse_music<_type_>(\"o| .| o| .| o o| o o|\"), \\\n _type_{2, 1, 2, 1, 4, 2, 4, 2})); \\\n } while (false)\n\nint main() {\n TEST_ON_TYPE(std::vector<int>);\n TEST_ON_TYPE(std::deque<int>);\n TEST_ON_TYPE(std::list<int>);\n}\n"} | |
| {"base_canonical_solution": "template <typename T>\nT how_many_times(std::string str, std::string substring) {\n T out = 0;\n if (str.length() == 0) return 0;\n for (int i = 0; i <= str.length() - substring.length(); i++)\n if (str.substr(i, substring.length()) == substring) out += 1;\n return out;\n}", "base_prompt": "Make the following function generic for the return value.", "concepts_canonical_solution": "template <std::integral T>\nT how_many_times(std::string str, std::string substring) {\n T out = 0;\n if (str.length() == 0) return 0;\n for (int i = 0; i <= str.length() - substring.length(); i++)\n if (str.substr(i, substring.length()) == substring) out += 1;\n return out;\n}", "concepts_prompt": "Constrain the generic code using C++20 Concepts so that the generic parameter is an integer type.", "invalids": "int main() { how_many_times<float>(\"\", \"x\"); }", "sfinae_canonical_solution": "template <typename T, std::enable_if_t<std::is_integral_v<T>, int> = 0>\nT how_many_times(std::string str, std::string substring) {\n T out = 0;\n if (str.length() == 0) return 0;\n for (int i = 0; i <= str.length() - substring.length(); i++)\n if (str.substr(i, substring.length()) == substring) out += 1;\n return out;\n}", "sfinae_prompt": "Constrain the generic code using C++17 SFINAE so that the generic parameter is an integer type.", "starter_code": "int how_many_times(std::string str, std::string substring) {\n int out = 0;\n if (str.length() == 0) return 0;\n for (int i = 0; i <= str.length() - substring.length(); i++)\n if (str.substr(i, substring.length()) == substring) out += 1;\n return out;\n}", "task_id": "HEP/18", "tests": "#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(how_many_times<_type_>(\"\", \"x\") == (_type_)0); \\\n ASSERT(how_many_times<_type_>(\"xyxyxyx\", \"x\") == (_type_)4); \\\n ASSERT(how_many_times<_type_>(\"cacacacac\", \"cac\") == (_type_)4); \\\n ASSERT(how_many_times<_type_>(\"john doe\", \"john\") == (_type_)1); \\\n \\\n } while (false)\n\nint main() {\n TEST_ON_TYPE(int);\n TEST_ON_TYPE(unsigned);\n TEST_ON_TYPE(unsigned long long);\n TEST_ON_TYPE(short);\n}\n"} | |
| {"base_canonical_solution": "template <typename T>\nstd::vector<T> find_closest_elements(std::vector<T> numbers) {\n std::vector<T> out = {};\n for (int i = 0; i < numbers.size(); i++) {\n for (int j = i + 1; j < numbers.size(); j++) {\n if (out.size() == 0 ||\n std::abs(numbers[i] - numbers[j]) < std::abs(out[0] - out[1])) {\n out = {numbers[i], numbers[j]};\n }\n }\n }\n if (out[0] > out[1]) {\n out = {out[1], out[0]};\n }\n return out;\n}", "base_prompt": "Make the following function generic for the value type of the vector parameter and vector return value.", "concepts_canonical_solution": "template <std::floating_point T>\nstd::vector<T> find_closest_elements(std::vector<T> numbers) {\n std::vector<T> out = {};\n for (int i = 0; i < numbers.size(); i++) {\n for (int j = i + 1; j < numbers.size(); j++) {\n if (out.size() == 0 ||\n std::abs(numbers[i] - numbers[j]) < std::abs(out[0] - out[1])) {\n out = {numbers[i], numbers[j]};\n }\n }\n }\n if (out[0] > out[1]) {\n out = {out[1], out[0]};\n }\n return out;\n}", "concepts_prompt": "Constrain the generic code using C++20 Concepts so that the generic parameter is a floating point type.", "invalids": "int main() { find_closest_elements(std::vector<int>{1, 2, 3, 4, 5}); }", "sfinae_canonical_solution": "template <typename T, std::enable_if_t<std::is_floating_point_v<T>, int> = 0>\nstd::vector<T> find_closest_elements(std::vector<T> numbers) {\n std::vector<T> out = {};\n for (int i = 0; i < numbers.size(); i++) {\n for (int j = i + 1; j < numbers.size(); j++) {\n if (out.size() == 0 ||\n std::abs(numbers[i] - numbers[j]) < std::abs(out[0] - out[1])) {\n out = {numbers[i], numbers[j]};\n }\n }\n }\n if (out[0] > out[1]) {\n out = {out[1], out[0]};\n }\n return out;\n}", "sfinae_prompt": "Constrain the generic code using C++17 SFINAE so that the generic parameter is a floating point type.", "starter_code": "std::vector<float> find_closest_elements(std::vector<float> numbers) {\n std::vector<float> out = {};\n for (int i = 0; i < numbers.size(); i++) {\n for (int j = i + 1; j < numbers.size(); j++) {\n if (out.size() == 0 ||\n std::abs(numbers[i] - numbers[j]) < std::abs(out[0] - out[1])) {\n out = {numbers[i], numbers[j]};\n }\n }\n }\n if (out[0] > out[1]) {\n out = {out[1], out[0]};\n }\n return out;\n}", "task_id": "HEP/20", "tests": "template <typename T>\nbool issame(std::vector<T> a, std::vector<T> b) {\n if (a.size() != b.size()) return false;\n for (int i = 0; i < a.size(); i++) {\n if (std::abs(a[i] - b[i]) > 1e-4) 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(issame(find_closest_elements(_type_{1.0, 2.0, 3.9, 4.0, 5.0, 2.2}), \\\n _type_{3.9, 4.0})); \\\n ASSERT(issame(find_closest_elements(_type_{1.0, 2.0, 5.9, 4.0, 5.0}), \\\n _type_{5.0, 5.9})); \\\n ASSERT(issame(find_closest_elements(_type_{1.0, 2.0, 3.0, 4.0, 5.0, 2.2}), \\\n _type_{2.0, 2.2})); \\\n ASSERT(issame(find_closest_elements(_type_{1.0, 2.0, 3.0, 4.0, 5.0, 2.0}), \\\n _type_{2.0, 2.0})); \\\n ASSERT(issame(find_closest_elements(_type_{1.1, 2.2, 3.1, 4.1, 5.1}), \\\n _type_{2.2, 3.1})); \\\n \\\n } while (false)\n\nint main() {\n TEST_ON_TYPE(std::vector<float>);\n TEST_ON_TYPE(std::vector<double>);\n}\n"} | |
| {"base_canonical_solution": "template <typename T>\nstd::vector<T> rescale_to_unit(std::vector<T> numbers) {\n T min = 100000, max = -100000;\n for (int i = 0; i < numbers.size(); i++) {\n if (numbers[i] < min) min = numbers[i];\n if (numbers[i] > max) max = numbers[i];\n }\n for (int i = 0; i < numbers.size(); i++)\n numbers[i] = (numbers[i] - min) / (max - min);\n return numbers;\n}", "base_prompt": "Make the following function generic for the value type of the vector parameter and vector return value.", "concepts_canonical_solution": "template <std::floating_point T>\nstd::vector<T> rescale_to_unit(std::vector<T> numbers) {\n T min = 100000, max = -100000;\n for (int i = 0; i < numbers.size(); i++) {\n if (numbers[i] < min) min = numbers[i];\n if (numbers[i] > max) max = numbers[i];\n }\n for (int i = 0; i < numbers.size(); i++)\n numbers[i] = (numbers[i] - min) / (max - min);\n return numbers;\n}", "concepts_prompt": "Constrain the generic code using C++20 Concepts so that the generic parameter is a floating point type.", "invalids": "int main() { rescale_to_unit(std::vector<int>{1, 2, 3}); }", "sfinae_canonical_solution": "template <typename T, std::enable_if_t<std::is_floating_point_v<T>, int> = 0>\nstd::vector<T> rescale_to_unit(std::vector<T> numbers) {\n T min = 100000, max = -100000;\n for (int i = 0; i < numbers.size(); i++) {\n if (numbers[i] < min) min = numbers[i];\n if (numbers[i] > max) max = numbers[i];\n }\n for (int i = 0; i < numbers.size(); i++)\n numbers[i] = (numbers[i] - min) / (max - min);\n return numbers;\n}", "sfinae_prompt": "Constrain the generic code using C++17 SFINAE so that the generic parameter is a floating point type.", "starter_code": "std::vector<float> rescale_to_unit(std::vector<float> numbers) {\n float min = 100000, max = -100000;\n for (int i = 0; i < numbers.size(); i++) {\n if (numbers[i] < min) min = numbers[i];\n if (numbers[i] > max) max = numbers[i];\n }\n for (int i = 0; i < numbers.size(); i++)\n numbers[i] = (numbers[i] - min) / (max - min);\n return numbers;\n}", "task_id": "HEP/21", "tests": "template <typename T>\nbool issame(std::vector<T> a, std::vector<T> b) {\n if (a.size() != b.size()) return false;\n for (int i = 0; i < a.size(); i++) {\n if (std::abs(a[i] - b[i]) > 1e-4) 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(issame(rescale_to_unit(_type_{2.0, 49.9}), _type_{0.0, 1.0})); \\\n ASSERT(issame(rescale_to_unit(_type_{100.0, 49.9}), _type_{1.0, 0.0})); \\\n ASSERT(issame(rescale_to_unit(_type_{1.0, 2.0, 3.0, 4.0, 5.0}), \\\n _type_{0.0, 0.25, 0.5, 0.75, 1.0})); \\\n ASSERT(issame(rescale_to_unit(_type_{2.0, 1.0, 5.0, 3.0, 4.0}), \\\n _type_{0.25, 0.0, 1.0, 0.5, 0.75})); \\\n ASSERT(issame(rescale_to_unit(_type_{12.0, 11.0, 15.0, 13.0, 14.0}), \\\n _type_{0.25, 0.0, 1.0, 0.5, 0.75})); \\\n } while (false)\n\nint main() {\n TEST_ON_TYPE(std::vector<float>);\n TEST_ON_TYPE(std::vector<double>);\n}\n"} | |
| {"base_canonical_solution": "template <typename T>\nT largest_divisor(T n) {\n for (T i = 2; i * i <= n; i++)\n if (n % i == 0) return n / i;\n return 1;\n}\n", "base_prompt": "Replace these functions with a single generic function preserving the original behaviors.", "concepts_canonical_solution": "template <std::integral T>\nT largest_divisor(T n) {\n for (T i = 2; i * i <= n; i++)\n if (n % i == 0) return n / i;\n return 1;\n}\n", "concepts_prompt": "Constrain the generic code using C++20 Concepts so that it only accepts integer types.", "invalids": "int main() { largest_divisor((float)3.5); }", "sfinae_canonical_solution": "template <typename T, std::enable_if_t<std::is_integral_v<T>, int> = 0>\nT largest_divisor(T n) {\n for (T i = 2; i * i <= n; i++)\n if (n % i == 0) return n / i;\n return 1;\n}\n", "sfinae_prompt": "Constrain the generic code using C++17 SFINAE so that it only accepts integer types.", "starter_code": "int largest_divisor(int n) {\n for (int i = 2; i * i <= n; i++)\n if (n % i == 0) return n / i;\n return 1;\n}\nunsigned largest_divisor(unsigned n) {\n for (unsigned i = 2; i * i <= n; i++)\n if (n % i == 0) return n / i;\n return 1;\n}\nlong largest_divisor(long n) {\n for (long i = 2; i * i <= n; i++)\n if (n % i == 0) return n / i;\n return 1;\n}\nunsigned short largest_divisor(unsigned short n) {\n for (unsigned short i = 2; i * i <= n; i++)\n if (n % i == 0) return n / i;\n return 1;\n}", "task_id": "HEP/24", "tests": "#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(largest_divisor((_type_)3) == (_type_)1); \\\n ASSERT(largest_divisor((_type_)7) == (_type_)1); \\\n ASSERT(largest_divisor((_type_)10) == (_type_)5); \\\n ASSERT(largest_divisor((_type_)100) == (_type_)50); \\\n ASSERT(largest_divisor((_type_)49) == (_type_)7); \\\n } while (false)\n\nint main() {\n TEST_ON_TYPE(int);\n TEST_ON_TYPE(long);\n TEST_ON_TYPE(unsigned);\n TEST_ON_TYPE(unsigned short);\n TEST_ON_TYPE(unsigned long long);\n}\n"} | |
| {"base_canonical_solution": "template <typename T>\nstd::vector<T> factorize(T n) {\n std::vector<T> out = {};\n for (T i = 2; i * i <= n; i++) {\n if (n % i == 0) {\n n = n / i;\n out.push_back(i);\n i -= 1;\n }\n }\n out.push_back(n);\n return out;\n}\n", "base_prompt": "Replace these functions with a single generic function preserving the original behaviors.", "concepts_canonical_solution": "template <std::integral T>\nstd::vector<T> factorize(T n) {\n std::vector<T> out = {};\n for (T i = 2; i * i <= n; i++) {\n if (n % i == 0) {\n n = n / i;\n out.push_back(i);\n i -= 1;\n }\n }\n out.push_back(n);\n return out;\n}\n", "concepts_prompt": "Constrain the generic code using C++20 Concepts so that it only accepts integer types.", "invalids": "int main() { factorize((float)3.5); }", "sfinae_canonical_solution": "template <typename T, std::enable_if_t<std::is_integral_v<T>, int> = 0>\nstd::vector<T> factorize(T n) {\n std::vector<T> out = {};\n for (T i = 2; i * i <= n; i++) {\n if (n % i == 0) {\n n = n / i;\n out.push_back(i);\n i -= 1;\n }\n }\n out.push_back(n);\n return out;\n}\n", "sfinae_prompt": "Constrain the generic code using C++17 SFINAE so that it only accepts integer types.", "starter_code": "std::vector<int> factorize(int n) {\n std::vector<int> out = {};\n for (int i = 2; i * i <= n; i++) {\n if (n % i == 0) {\n n = n / i;\n out.push_back(i);\n i -= 1;\n }\n }\n out.push_back(n);\n return out;\n}\nstd::vector<unsigned> factorize(unsigned n) {\n std::vector<unsigned> out = {};\n for (unsigned i = 2; i * i <= n; i++) {\n if (n % i == 0) {\n n = n / i;\n out.push_back(i);\n i -= 1;\n }\n }\n out.push_back(n);\n return out;\n}\nstd::vector<long> factorize(long n) {\n std::vector<long> out = {};\n for (long i = 2; i * i <= n; i++) {\n if (n % i == 0) {\n n = n / i;\n out.push_back(i);\n i -= 1;\n }\n }\n out.push_back(n);\n return out;\n}", "task_id": "HEP/25", "tests": "template <typename T>\nbool issame(std::vector<T> a, std::vector<T> 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(issame(factorize((_type_)2), std::vector<_type_>{2})); \\\n ASSERT(issame(factorize((_type_)4), std::vector<_type_>{2, 2})); \\\n ASSERT(issame(factorize((_type_)8), std::vector<_type_>{2, 2, 2})); \\\n ASSERT(issame(factorize((_type_)(3 * 19)), std::vector<_type_>{3, 19})); \\\n ASSERT(issame(factorize((_type_)(3 * 19 * 3 * 19)), \\\n std::vector<_type_>{3, 3, 19, 19})); \\\n ASSERT(issame(factorize((_type_)(3 * 19 * 3 * 19 * 3 * 19)), \\\n std::vector<_type_>{3, 3, 3, 19, 19, 19})); \\\n ASSERT(issame(factorize((_type_)(3 * 19 * 19 * 19)), \\\n std::vector<_type_>{3, 19, 19, 19})); \\\n ASSERT( \\\n issame(factorize((_type_)(3 * 2 * 3)), std::vector<_type_>{2, 3, 3})); \\\n } while (false)\n\nint main() {\n TEST_ON_TYPE(int);\n TEST_ON_TYPE(unsigned);\n TEST_ON_TYPE(long);\n TEST_ON_TYPE(unsigned long long);\n}\n"} | |
| {"base_canonical_solution": "template <typename T>\nT remove_duplicates(T numbers) {\n T out = {};\n T has1 = {};\n T has2 = {};\n for (int i = 0; i < numbers.size(); i++) {\n if (std::find(has2.begin(), has2.end(), numbers[i]) != has2.end()) continue;\n if (std::find(has1.begin(), has1.end(), numbers[i]) != has1.end()) {\n has2.push_back(numbers[i]);\n } else {\n has1.push_back(numbers[i]);\n }\n }\n for (int i = 0; i < numbers.size(); i++) {\n if (std::find(has2.begin(), has2.end(), numbers[i]) == has2.end()) {\n out.push_back(numbers[i]);\n }\n }\n return out;\n}\n", "base_prompt": "Replace these functions with a single generic function preserving the original behaviors.", "concepts_canonical_solution": "template <typename T>\n requires requires(const T &container, std::size_t idx, std::size_t oidx) {\n { container[idx] } -> std::same_as<typename T::const_reference>;\n { container[idx] == container[oidx] } -> std::convertible_to<bool>;\n }\nT remove_duplicates(T numbers) {\n T out = {};\n T has1 = {};\n T has2 = {};\n for (int i = 0; i < numbers.size(); i++) {\n if (std::find(has2.begin(), has2.end(), numbers[i]) != has2.end()) continue;\n if (std::find(has1.begin(), has1.end(), numbers[i]) != has1.end()) {\n has2.push_back(numbers[i]);\n } else {\n has1.push_back(numbers[i]);\n }\n }\n for (int i = 0; i < numbers.size(); i++) {\n if (std::find(has2.begin(), has2.end(), numbers[i]) == has2.end()) {\n out.push_back(numbers[i]);\n }\n }\n return out;\n}", "concepts_prompt": "Constrain the generic code using C++20 Concepts so that it only accepts sequenced containers with comparable elements.", "invalids": "int main() { remove_duplicates(3); }", "sfinae_canonical_solution": "template <\n typename T,\n std::enable_if_t<\n std::conjunction_v<\n std::is_same<typename T::const_reference,\n decltype(std::declval<\n const T &>()[std::declval<std::size_t>()])>,\n std::is_convertible<decltype(std::declval<const T &>()\n [std::declval<std::size_t>()] ==\n std::declval<const T &>()\n [std::declval<std::size_t>()]),\n bool>>,\n int> = 0>\nT remove_duplicates(T numbers) {\n T out = {};\n T has1 = {};\n T has2 = {};\n for (int i = 0; i < numbers.size(); i++) {\n if (std::find(has2.begin(), has2.end(), numbers[i]) != has2.end()) continue;\n if (std::find(has1.begin(), has1.end(), numbers[i]) != has1.end()) {\n has2.push_back(numbers[i]);\n } else {\n has1.push_back(numbers[i]);\n }\n }\n for (int i = 0; i < numbers.size(); i++) {\n if (std::find(has2.begin(), has2.end(), numbers[i]) == has2.end()) {\n out.push_back(numbers[i]);\n }\n }\n return out;\n}", "sfinae_prompt": "Constrain the generic code using C++17 SFINAE so that it only accepts sequenced containers with comparable elements.", "starter_code": "std::vector<int> remove_duplicates(std::vector<int> numbers) {\n std::vector<int> out = {};\n std::vector<int> has1 = {};\n std::vector<int> has2 = {};\n for (int i = 0; i < numbers.size(); i++) {\n if (std::find(has2.begin(), has2.end(), numbers[i]) != has2.end()) continue;\n if (std::find(has1.begin(), has1.end(), numbers[i]) != has1.end()) {\n has2.push_back(numbers[i]);\n } else {\n has1.push_back(numbers[i]);\n }\n }\n for (int i = 0; i < numbers.size(); i++) {\n if (std::find(has2.begin(), has2.end(), numbers[i]) == has2.end()) {\n out.push_back(numbers[i]);\n }\n }\n return out;\n}\nstd::deque<std::string> remove_duplicates(std::deque<std::string> numbers) {\n std::deque<std::string> out = {};\n std::deque<std::string> has1 = {};\n std::deque<std::string> has2 = {};\n for (int i = 0; i < numbers.size(); i++) {\n if (std::find(has2.begin(), has2.end(), numbers[i]) != has2.end()) continue;\n if (std::find(has1.begin(), has1.end(), numbers[i]) != has1.end()) {\n has2.push_back(numbers[i]);\n } else {\n has1.push_back(numbers[i]);\n }\n }\n for (int i = 0; i < numbers.size(); i++) {\n if (std::find(has2.begin(), has2.end(), numbers[i]) == has2.end()) {\n out.push_back(numbers[i]);\n }\n }\n return out;\n}", "task_id": "HEP/26", "tests": "template <typename T>\nbool issame(T a, T 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\nint main() {\n ASSERT(issame(remove_duplicates(std::vector<int>{}), std::vector<int>{}));\n ASSERT(issame(remove_duplicates(std::vector<int>{1, 2, 3, 4}),\n std::vector<int>{1, 2, 3, 4}));\n ASSERT(issame(remove_duplicates(std::vector<int>{1, 2, 3, 2, 4, 3, 5}),\n std::vector<int>{1, 4, 5}));\n\n ASSERT(issame(remove_duplicates(std::vector<char>{}), std::vector<char>{}));\n ASSERT(issame(remove_duplicates(std::vector<char>{1, 2, 3, 4}),\n std::vector<char>{1, 2, 3, 4}));\n ASSERT(issame(remove_duplicates(std::vector<char>{1, 2, 3, 2, 4, 3, 5}),\n std::vector<char>{1, 4, 5}));\n\n ASSERT(issame(remove_duplicates(std::deque<std::string>{}),\n std::deque<std::string>{}));\n ASSERT(issame(remove_duplicates(std::deque<std::string>{\"1\", \"2\", \"3\", \"4\"}),\n std::deque<std::string>{\"1\", \"2\", \"3\", \"4\"}));\n ASSERT(issame(remove_duplicates(\n std::deque<std::string>{\"1\", \"2\", \"3\", \"2\", \"4\", \"3\", \"5\"}),\n std::deque<std::string>{\"1\", \"4\", \"5\"}));\n}\n"} | |