Micah Weston
commited on
Commit
·
d34f830
1
Parent(s):
5b49644
Adds two more tasks.
Browse files
data/genericify_cpp.jsonl
CHANGED
|
@@ -12,4 +12,6 @@
|
|
| 12 |
{"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"}
|
| 13 |
{"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"}
|
| 14 |
{"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"}
|
| 15 |
-
{"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"}
|
|
|
|
|
|
|
|
|
| 12 |
{"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"}
|
| 13 |
{"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"}
|
| 14 |
{"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"}
|
| 15 |
+
{"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"}
|
| 16 |
+
{"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"}
|
| 17 |
+
{"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 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_music<_type_>(\"\"), _type_{}));\nASSERT(issame(parse_music<_type_>(\"o o o o\"), _type_{4, 4, 4, 4}));\nASSERT(issame(parse_music<_type_>(\".| .| .| .|\"), _type_{1, 1, 1, 1}));\nASSERT(issame(parse_music<_type_>(\"o| o| .| .| o o o o\"),\n _type_{2, 2, 1, 1, 4, 4, 4, 4}));\nASSERT(issame(parse_music<_type_>(\"o| .| o| .| o o| o o|\"),\n _type_{2, 1, 2, 1, 4, 2, 4, 2}));\n}\nwhile (false) int main() {\n TEST_ON_TYPE(std::vector<int>);\n TEST_ON_TYPE(std::deque<int>);\n TEST_ON_TYPE(std::list<int>);\n }\n"}
|