Micah Weston commited on
Commit
196d44a
·
1 Parent(s): ea1caab

Adds three more tasks.

Browse files
Files changed (1) hide show
  1. data/genericify_cpp.jsonl +4 -1
data/genericify_cpp.jsonl CHANGED
@@ -2,4 +2,7 @@
2
  {"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}"}
3
  {"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}"}
4
  {"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}"}
5
- {"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}"}
 
 
 
 
2
  {"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}"}
3
  {"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}"}
4
  {"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}"}
5
+ {"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}"}
6
+ {"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}"}
7
+ {"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}"}
8
+ {"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"}