base_canonical_solution
stringlengths
104
597
base_prompt
stringlengths
56
103
concepts_canonical_solution
stringlengths
115
787
concepts_prompt
stringlengths
86
147
invalids
stringlengths
36
70
sfinae_canonical_solution
stringlengths
174
1.15k
sfinae_prompt
stringlengths
84
145
starter_code
stringlengths
76
1.27k
task_id
stringlengths
5
6
tests
stringlengths
680
1.71k
template <typename T> bool has_close_elements(T numbers, float threshold) { for (int i = 0; i < numbers.size(); i++) for (int j = i + 1; j < numbers.size(); j++) if (std::abs(numbers[i] - numbers[j]) < threshold) return true; return false; }
Make the following function generic for the parameter `numbers`.
template <typename T> requires std::same_as<typename T::value_type, float> bool has_close_elements(T numbers, float threshold) { for (int i = 0; i < numbers.size(); i++) for (int j = i + 1; j < numbers.size(); j++) if (std::abs(numbers[i] - numbers[j]) < threshold) return true; return false; }...
Constrain the generic code using C++20 Concepts so that it accepts a container with value type of floats.
int main() { std::string s{}; has_close_elements(s, 3.4); }
template < typename T, std::enable_if_t<std::is_same_v<typename T::value_type, float>, int> = 0> bool has_close_elements(T numbers, float threshold) { for (int i = 0; i < numbers.size(); i++) for (int j = i + 1; j < numbers.size(); j++) if (std::abs(numbers[i] - numbers[j]) < threshold) ret...
Constrain the generic code using C++17 SFINAE so that it accepts a container with value type of floats.
bool has_close_elements(std::vector<float> numbers, float threshold) { for (int i = 0; i < numbers.size(); i++) for (int j = i + 1; j < numbers.size(); j++) if (std::abs(numbers[i] - numbers[j]) < threshold) return true; return false; }
HEP/0
#define ASSERT(...) \ do { \ if (!(__VA_ARGS__)) { \ std::exit(-1); ...
template <typename InputStr> std::vector<std::string> separate_paren_groups(InputStr paren_string) { std::vector<std::string> all_parens; std::string current_paren; int level = 0; for (int i = 0; i < paren_string.size(); i++) { char chr = paren_string[i]; if (chr == '(') { level += 1; curren...
Make the following function generic for the parameter `paren_string`.
template <typename InputStr> requires requires(const InputStr& s, std::size_t i) { { s[i] } -> std::convertible_to<char>; { s.size() } -> std::convertible_to<int>; std::same_as<typename InputStr::value_type, char>; } std::vector<std::string> separate_paren_groups(InputStr paren_string) { std::vector<s...
Constrain the generic code using C++20 Concepts so that the parameter `paren_string` must be a sequenced container of characters like a string.
int main() { float v = 3; separate_paren_groups(v); }
template < typename InputStr, std::enable_if_t< std::conjunction_v< std::is_same<char, typename InputStr::value_type>, std::is_same<char, std::decay_t<decltype(std::declval< InputStr>()[std::declval< ...
Constrain the generic code using C++17 SFINAE so that the parameter `paren_string` must be a sequenced container of characters like a string.
std::vector<std::string> separate_paren_groups(std::string paren_string) { std::vector<std::string> all_parens; std::string current_paren; int level = 0; for (int i = 0; i < paren_string.length(); i++) { char chr = paren_string[i]; if (chr == '(') { level += 1; current_paren += chr; } ...
HEP/1
bool issame(std::vector<std::string> a, std::vector<std::string> b) { if (a.size() != b.size()) return false; for (int i = 0; i < a.size(); i++) { if (a[i] != b[i]) return false; } return true; } #define ASSERT(...) \ do { \ if (!(__VA_ARGS__)) { \ std::exit(-1); \...
template <typename Float> Float truncate_number(Float number) { return number - (long long)(number); }
Make the following function generic for the parameter `number` and return value.
template <std::floating_point Float> Float truncate_number(Float number) { return number - (long long)(number); }
Constrain the generic code using C++20 Concepts so that the generate parameter is only floating point types.
int main() { truncate_number((int)3); }
template <typename Float, std::enable_if_t<std::is_floating_point_v<Float>, int> = 0> Float truncate_number(Float number) { return number - (long long)(number); }
Constrain the generic code using C++17 SFINAE so that the generate parameter is only floating point types.
float truncate_number(float number) { return number - (long long)(number); }
HEP/2
#define ASSERT(...) \ do { \ if (!(__VA_ARGS__)) { \ std::exit(-1); \ } \ } while (false) #define TEST_ON_TYPE(_type_) \ do { \ ...
template <typename T> bool below_zero(std::vector<T> operations) { T num = 0; for (int i = 0; i < operations.size(); i++) { num += operations[i]; if (num < 0) return true; } return false; }
Make the following function generic for the value type of the vector parameter `operations`.
template <typename T> requires std::integral<T> || std::floating_point<T> bool below_zero(std::vector<T> operations) { T num = 0; for (int i = 0; i < operations.size(); i++) { num += operations[i]; if (num < 0) return true; } return false; }
Constrain the generic code using C++20 Concepts so that the value type of the vector is only number types.
int main() { below_zero(std::vector<int*>{}); }
template <typename T, std::enable_if_t<std::disjunction_v<std::is_integral<T>, std::is_floating_point<T>>, int> = 0> bool below_zero(std::vector<T> operations) { T num = 0; for (int i = 0; i < operations.size(); i++) { num += ope...
Constrain the generic code using C++17 SFINAE so that the value type of the vector is only number types.
bool below_zero(std::vector<int> operations) { int num = 0; for (int i = 0; i < operations.size(); i++) { num += operations[i]; if (num < 0) return true; } return false; }
HEP/3
#define ASSERT(...) \ do { \ if (!(__VA_ARGS__)) { \ std::exit(-1); \ } \ } while (false) #define TEST_ON_TYPE(_type_) \ do { \ ASSE...
template <typename T> T mean_absolute_deviation(std::vector<T> numbers) { T sum = 0; for (int i = 0; i < numbers.size(); i++) sum += numbers[i]; T avg = sum / numbers.size(); T msum = 0; for (int i = 0; i < numbers.size(); i++) msum += std::abs(numbers[i] - avg); return msum / numbers.size(); }
Make the following function generic for the value type of the vector parameter `numbers`.
template <std::floating_point T> T mean_absolute_deviation(std::vector<T> numbers) { T sum = 0; for (int i = 0; i < numbers.size(); i++) sum += numbers[i]; T avg = sum / numbers.size(); T msum = 0; for (int i = 0; i < numbers.size(); i++) msum += std::abs(numbers[i] - avg); return msum / numbers.size(); }
Constrain the generic code using C++20 Concepts so that the generic type is only floating point types.
int main() { mean_absolute_deviation(std::vector<int>{0, 1, 2}); }
template <typename T, std::enable_if_t<std::is_floating_point_v<T>, int> = 0> T mean_absolute_deviation(std::vector<T> numbers) { T sum = 0; for (int i = 0; i < numbers.size(); i++) sum += numbers[i]; T avg = sum / numbers.size(); T msum = 0; for (int i = 0; i < numbers.size(); i++) msum += std::abs(numbers[i...
Constrain the generic code using C++17 SFINAE so that the generic type is only floating point types.
float mean_absolute_deviation(std::vector<float> numbers) { float sum = 0; for (int i = 0; i < numbers.size(); i++) sum += numbers[i]; float avg = sum / numbers.size(); float msum = 0; for (int i = 0; i < numbers.size(); i++) msum += std::abs(numbers[i] - avg); return msum / numbers.size(); }
HEP/4
#define ASSERT(...) \ do { \ if (!(__VA_ARGS__)) { \ std::exit(-1); \ } \ } while (false) #define TEST_ON_TYPE(_type_) \ do { \...
template <typename Container, typename Value> Container intersperse(Container numbers, Value delimeter) { Container out; if (numbers.size() > 0) out.push_back(numbers[0]); for (int i = 1; i < numbers.size(); i++) { out.push_back(delimeter); out.push_back(numbers[i]); } return out; }
Make the following function generic for both parameters and return type.
template <typename Container, typename Value> requires requires(Container &c, Value v) { { c.push_back(v) }; } Container intersperse(Container numbers, Value delimeter) { Container out; if (numbers.size() > 0) out.push_back(numbers[0]); for (int i = 1; i < numbers.size(); i++) { out.push_back(delimete...
Constrain the generic code using C++20 Concepts so that the value to intersperse can be pushed back on the container.
int main() { std::vector<int*> v; intersperse(v, int{0}); }
template <typename Container, typename Value, typename = std::void_t<decltype(std::declval<Container &>().push_back( std::declval<Value>()))>> Container intersperse(Container numbers, Value delimeter) { Container out; if (numbers.size() > 0) out.push_back(numbers[0]); for (int i = 1; i < n...
Constrain the generic code using C++17 SFINAE so that the value to intersperse can be pushed back on the container.
std::vector<int> intersperse(std::vector<int> numbers, int delimeter) { std::vector<int> out; if (numbers.size() > 0) out.push_back(numbers[0]); for (int i = 1; i < numbers.size(); i++) { out.push_back(delimeter); out.push_back(numbers[i]); } return out; }
HEP/5
template <typename T> bool issame(T a, T b) { if (a.size() != b.size()) return false; for (int i = 0; i < a.size(); i++) { if (a[i] != b[i]) return false; } return true; } #define ASSERT(...) \ do { \ if (!(__VA_ARGS__)) { \ std::exit(-1); \ } ...
template <typename T> std::vector<int> parse_nested_parens(T paren_string) { std::vector<int> all_levels; std::string current_paren; int level = 0, max_level = 0; for (int i = 0; i < paren_string.size(); i++) { char chr = paren_string[i]; if (chr == '(') { level += 1; if (level > max_level) ...
Make the following function generic for the parameter `paren_string`.
template <typename T> requires requires(const T& s, std::size_t i) { { s[i] } -> std::convertible_to<char>; { s.size() } -> std::convertible_to<int>; std::same_as<typename T::value_type, char>; } std::vector<int> parse_nested_parens(T paren_string) { std::vector<int> all_levels; std::string current_...
Constrain the generic code using C++20 Concepts so that the parameter `paren_string` must be a sequenced container of characters like a string.
int main() { parse_nested_parens(float{3}); }
template < typename T, std::enable_if_t< std::conjunction_v< std::is_same<char, typename T::value_type>, std::is_same<char, std::decay_t<decltype(std::declval<T>()[std::declval< std::size_t>()])>>>, int> = 0> std::vect...
Constrain the generic code using C++17 SFINAE so that the parameter `paren_string` must be a sequenced container of characters like a string.
std::vector<int> parse_nested_parens(std::string paren_string) { std::vector<int> all_levels; std::string current_paren; int level = 0, max_level = 0; for (int i = 0; i < paren_string.size(); i++) { char chr = paren_string[i]; if (chr == '(') { level += 1; if (level > max_level) max_level = ...
HEP/6
bool issame(std::vector<int> a, std::vector<int> b) { if (a.size() != b.size()) return false; for (int i = 0; i < a.size(); i++) { if (a[i] != b[i]) return false; } return true; } #define ASSERT(...) \ do { \ if (!(__VA_ARGS__)) { \ std::exit(-1); \ } ...
template <typename T> T filter_by_substring(T strings, std::string substring) { T out; for (int i = 0; i < strings.size(); i++) { if (strings[i].find(substring) != strings[i].npos) out.push_back(strings[i]); } return out; }
Make the following function generic for the parameter `string` and return value.
template <typename T> requires std::same_as<typename T::value_type, std::string> T filter_by_substring(T strings, std::string substring) { T out; for (int i = 0; i < strings.size(); i++) { if (strings[i].find(substring) != strings[i].npos) out.push_back(strings[i]); } return out; }
Constrain the generic code using C++20 Concepts so that it accepts a sequenced container with value type of string.
int main() { filter_by_substring(std::vector<int>{}, "hello"); }
template <typename T, std::enable_if_t<std::is_same_v<typename T::value_type, std::string>, int> = 0> T filter_by_substring(T strings, std::string substring) { T out; for (int i = 0; i < strings.size(); i++) { if (strings[i].find(substring) != strings[i].npos) out.push...
Constrain the generic code using C++17 SFINAE so that it accepts a sequenced container with value type of string.
std::vector<string> filter_by_substring(std::vector<string> strings, std::string substring) { std::vector<string> out; for (int i = 0; i < strings.size(); i++) { if (strings[i].find(substring) != strings[i].npos) out.push_back(strings[i]); } return out; }
HEP/7
template <typename T> bool issame(T a, T b) { if (a.size() != b.size()) return false; for (int i = 0; i < a.size(); i++) { if (a[i] != b[i]) return false; } return true; } #define ASSERT(...) \ do { \ if (!(__VA_ARGS__)) { \ std::exit(-1); \ } ...
template <typename T> std::pair<T, T> sum_product(std::vector<T> numbers) { T sum = 0, product = 1; for (int i = 0; i < numbers.size(); i++) { sum += numbers[i]; product *= numbers[i]; } return {sum, product}; }
Make the following function generic for the value types of the parameter and return pair.
template <typename T> requires std::integral<T> || std::floating_point<T> std::pair<T, T> sum_product(std::vector<T> numbers) { T sum = 0, product = 1; for (int i = 0; i < numbers.size(); i++) { sum += numbers[i]; product *= numbers[i]; } return {sum, product}; }
Constrain the generic code using C++20 Concepts so that the generic parameter is a number type.
int main() { sum_product(std::vector<int*>{}); }
template <typename T, std::enable_if_t<std::disjunction_v<std::is_integral<T>, std::is_floating_point<T>>, int> = 0> std::pair<T, T> sum_product(std::vector<T> numbers) { T sum = 0, product = 1; for (int i = 0; i < numbers.size(); i+...
Constrain the generic code using C++17 SFINAE so that the generic parameter is a number type.
std::pair<int, int> sum_product(std::vector<int> numbers) { int sum = 0, product = 1; for (int i = 0; i < numbers.size(); i++) { sum += numbers[i]; product *= numbers[i]; } return {sum, product}; }
HEP/8
#define ASSERT(...) \ do { \ if (!(__VA_ARGS__)) { \ std::exit(-1); \ } \ } while (false) #define TEST_ON_TYPE(_type_) \ do { \ ASSERT(sum_product(std::vector<...
template <typename T> std::vector<T> rolling_max(std::vector<T> numbers) { if (numbers.size() == 0) return {}; std::vector<T> out; T max = numbers.front(); for (int i = 0; i < numbers.size(); i++) { if (numbers[i] > max) max = numbers[i]; out.push_back(max); } return out; }
Make the following function generic for value type of the parameter and return type.
template <typename T> requires requires(const T& lhs, const T& rhs) { { lhs > rhs } -> std::convertible_to<bool>; } std::vector<T> rolling_max(std::vector<T> numbers) { if (numbers.size() == 0) return {}; std::vector<T> out; T max = numbers.front(); for (int i = 0; i < numbers.size(); i++) { if (num...
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.
int main() { struct S {}; rolling_max(std::vector<S>{}); }
template < typename T, std::enable_if_t<std::is_convertible_v<decltype(std::declval<const T &>() > std::declval<const T &>()), bool>, int> = 0> std::vector<T> rolling_max(std::vector<T> numbers) {...
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.
std::vector<int> rolling_max(std::vector<int> numbers) { if (numbers.size() == 0) return {}; std::vector<int> out; int max = numbers.front(); for (int i = 0; i < numbers.size(); i++) { if (numbers[i] > max) max = numbers[i]; out.push_back(max); } return out; }
HEP/9
template <typename T> bool issame(std::vector<T> a, std::vector<T> b) { if (a.size() != b.size()) return false; for (int i = 0; i < a.size(); i++) { if (a[i] != b[i]) return false; } return true; } #define ASSERT(...) \ do { \ if (!(__VA_ARGS__)) { \ std::exit(-1); ...
template <typename T> std::string string_xor(T a, T b) { std::string output = ""; for (int i = 0; (i < a.size() && i < b.size()); i++) { if (i < a.size() && i < b.size()) { if (a[i] == b[i]) { output += '0'; } else output += '1'; } else { if (i >= a.size()) { output...
Make the following function generic for the type of the two parameters.
template <typename T> requires std::same_as<typename T::value_type, char> std::string string_xor(T a, T b) { std::string output = ""; for (int i = 0; (i < a.size() && i < b.size()); i++) { if (i < a.size() && i < b.size()) { if (a[i] == b[i]) { output += '0'; } else output += '1'; ...
Constrain the generic code using C++20 Concepts so that the generic parameter is a sequenced container of characters.
int main() { string_xor(std::vector<int>{}, std::vector<int>{}); }
template < typename T, std::enable_if_t<std::is_same_v<typename T::value_type, char>, int> = 0> std::string string_xor(T a, T b) { std::string output = ""; for (int i = 0; (i < a.size() && i < b.size()); i++) { if (i < a.size() && i < b.size()) { if (a[i] == b[i]) { output += '0'; } ...
Constrain the generic code using C++17 SFINAE so that the generic parameter is a sequenced container of characters.
std::string string_xor(std::string a, std::string b) { std::string output = ""; for (int i = 0; (i < a.size() && i < b.size()); i++) { if (i < a.size() && i < b.size()) { if (a[i] == b[i]) { output += '0'; } else output += '1'; } else { if (i >= a.size()) { output +...
HEP/11
#define ASSERT(...) \ do { \ if (!(__VA_ARGS__)) { \ std::exit(-1); \ } \ } while (false) int main() { ASSERT(string_xor(std::string{"111000"}, std::string{"101010"}) == std::string{"010010"}); ASSERT(string_xor(std::string{"1"}, std::str...
template <typename T> std::string longest(T strings) { std::string out; for (int i = 0; i < strings.size(); i++) { if (strings[i].length() > out.length()) out = strings[i]; } return out; }
Make the following function generic for the container parameter `strings`.
template <typename T> requires std::same_as<typename T::value_type, std::string> std::string longest(T strings) { std::string out; for (int i = 0; i < strings.size(); i++) { if (strings[i].length() > out.length()) out = strings[i]; } return out; }
Constrain the generic code using C++20 Concepts so that the generic parameter is a sequenced container of value type string.
int main() { longest(std::vector<int>{}) }
template <typename T, std::enable_if_t<std::is_same_v<typename T::value_type, std::string>, int> = 0> std::string longest(T strings) { std::string out; for (int i = 0; i < strings.size(); i++) { if (strings[i].length() > out.length()) out = strings[i]; } return out; }
Constrain the generic code using C++17 SFINAE so that the generic parameter is a sequenced container of value type string.
std::string longest(std::vector<std::string> strings) { std::string out; for (int i = 0; i < strings.size(); i++) { if (strings[i].length() > out.length()) out = strings[i]; } return out; }
HEP/12
#define ASSERT(...) \ do { \ if (!(__VA_ARGS__)) { \ std::exit(-1); \ } \ } while (false) #define TEST_ON_TYPE(_type_) \ do { \ ASSERT(lo...
template <typename T> T greatest_common_divisor(T a, T b) { T out, m; while (true) { if (a < b) { m = a; a = b; b = m; } a = a % b; if (a == 0) return b; } }
Make the following function generic to share same type for all parameters and return type.
template <std::integral T> T greatest_common_divisor(T a, T b) { T out, m; while (true) { if (a < b) { m = a; a = b; b = m; } a = a % b; if (a == 0) return b; } }
Constrain the generic code using C++20 Concepts so that the generic parameter is an integer type.
int main() { greatest_common_divisor(3.0, 4.0); }
template <typename T, std::enable_if_t<std::is_integral_v<T>, int> = 0> T greatest_common_divisor(T a, T b) { T out, m; while (true) { if (a < b) { m = a; a = b; b = m; } a = a % b; if (a == 0) return b; } }
Constrain the generic code using C++17 SFINAE so that the generic parameter is an integer type.
int greatest_common_divisor(int a, int b) { int out, m; while (true) { if (a < b) { m = a; a = b; b = m; } a = a % b; if (a == 0) return b; } }
HEP/13
#define ASSERT(...) \ do { \ if (!(__VA_ARGS__)) { \ std::exit(-1); \ } \ } while (false) #define TEST_ON_TYPE(_type_) \ do { \ A...
template <typename T> std::vector<std::string> all_prefixes(T str) { std::vector<std::string> out; std::string current = ""; for (int i = 0; i < str.size(); i++) { current = current + str[i]; out.push_back(current); } return out; }
Make the following function generic for the parameter `str`.
template <typename T> requires requires(const T& s, std::size_t i) { { s[i] } -> std::convertible_to<char>; { s.size() } -> std::convertible_to<int>; std::same_as<typename T::value_type, char>; } std::vector<std::string> all_prefixes(T str) { std::vector<std::string> out; std::string current = ""; ...
Constrain the generic code using C++20 Concepts so that the generic type is a sequenced container of characters like a string.
int main() { all_prefixes(std::vector<int>{}); }
template < typename T, std::enable_if_t< std::conjunction_v< std::is_same<char, typename T::value_type>, std::is_same<char, std::decay_t<decltype(std::declval<T>()[std::declval< std::size_t>()])>>>, int> = 0> std::vect...
Constrain the generic code using C++17 SFINAE so that the generic type is a sequenced container of characters like a string.
std::vector<std::string> all_prefixes(std::string str) { std::vector<std::string> out; std::string current = ""; for (int i = 0; i < str.size(); i++) { current = current + str[i]; out.push_back(current); } return out; }
HEP/14
bool issame(std::vector<std::string> a, std::vector<std::string> b) { if (a.size() != b.size()) return false; for (int i = 0; i < a.size(); i++) { if (a[i] != b[i]) return false; } return true; } #define ASSERT(...) \ do { \ if (!(__VA_ARGS__)) { \ std::exit(-1); \...
template <typename T> std::string string_sequence(T n) { std::string out = "0"; for (T i = 1; i <= n && i != 0; i++) out = out + " " + std::to_string(i); return out; }
Make the following function generic for the parameter `n`.
template <std::unsigned_integral T> std::string string_sequence(T n) { std::string out = "0"; for (T i = 1; i <= n && i != 0; i++) out = out + " " + std::to_string(i); return out; }
Constrain the generic code using C++20 Concepts so that the generic parameter is an unsigned number.
int main() { string_sequence((int)5); }
template <typename T, std::enable_if_t<std::is_unsigned_v<T>, int> = 0> std::string string_sequence(T n) { std::string out = "0"; for (T i = 1; i <= n && i != 0; i++) out = out + " " + std::to_string(i); return out; }
Constrain the generic code using C++17 SFINAE so that the generic parameter is an unsigned number.
std::string string_sequence(unsigned n) { std::string out = "0"; for (unsigned i = 1; i <= n && i != 0; i++) out = out + " " + std::to_string(i); return out; }
HEP/15
#define ASSERT(...) \ do { \ if (!(__VA_ARGS__)) { \ std::exit(-1); \ } \ } while (false) #define TEST_ON_TYPE(_type_) \ do { \ ASSERT(string_s...
template <typename T> int count_distinct_characters(T str) { std::vector<char> distinct = {}; std::transform(str.begin(), str.end(), str.begin(), ::tolower); for (int i = 0; i < str.size(); i++) { bool isin = false; for (int j = 0; j < distinct.size(); j++) if (distinct[j] == str[i]) isin = true; ...
Make the following function generic for the parameter `str`.
template <typename T> requires std::same_as<typename T::value_type, char> int count_distinct_characters(T str) { std::vector<char> distinct = {}; std::transform(str.begin(), str.end(), str.begin(), ::tolower); for (int i = 0; i < str.size(); i++) { bool isin = false; for (int j = 0; j < distinct.size();...
Constrain the generic code using C++20 Concepts so that the generic parameter is a sequenced container of value type character.
int main() { count_distinct_characters(std::vector<int>{}); }
template < typename T, std::enable_if_t<std::is_same_v<typename T::value_type, char>, int> = 0> int count_distinct_characters(T str) { std::vector<char> distinct = {}; std::transform(str.begin(), str.end(), str.begin(), ::tolower); for (int i = 0; i < str.size(); i++) { bool isin = false; for (int...
Constrain the generic code using C++17 SFINAE so that the generic parameter is a sequenced container of value type character.
int count_distinct_characters(std::string str) { std::vector<char> distinct = {}; std::transform(str.begin(), str.end(), str.begin(), ::tolower); for (int i = 0; i < str.size(); i++) { bool isin = false; for (int j = 0; j < distinct.size(); j++) if (distinct[j] == str[i]) isin = true; if (isin =...
HEP/16
#define ASSERT(...) \ do { \ if (!(__VA_ARGS__)) { \ std::exit(-1); \ } \ } while (false) int main() { ASSERT(count_distinct_characters(std::string{""}) == 0); ASSERT(count_distinct_characters(std::string{"abcde"}) == 5); ASSERT(count_distinct_c...
template <typename T> T parse_music(std::string music_string) { std::string current = ""; T out = {}; if (music_string.size() > 0) music_string = music_string + ' '; for (int i = 0; i < music_string.length(); i++) { if (music_string[i] == ' ') { if (current == "o") out.push_back(4); if (current ...
Make the following function generic for the return type.
template <typename T> requires requires(T& t, int v) { { t.push_back(v) }; } T parse_music(std::string music_string) { std::string current = ""; T out = {}; if (music_string.size() > 0) music_string = music_string + ' '; for (int i = 0; i < music_string.length(); i++) { if (music_string[i] == ' ') {...
Constrain the generic code using C++20 Concepts so that the generic parameter is a container of int that can be pushed back into.
int main() { parse_music(float{3.0}) }
template <typename T, typename = std::void_t< decltype(std::declval<T&>().push_back(std::declval<int>()))>> T parse_music(std::string music_string) { std::string current = ""; T out = {}; if (music_string.size() > 0) music_string = music_string + ' '; for (int i = 0; i < music_string.len...
Constrain the generic code using C++17 SFINAE so that the generic parameter is a container of int that can be pushed back into.
std::vector<int> parse_music(std::string music_string) { std::string current = ""; std::vector<int> out = {}; if (music_string.size() > 0) music_string = music_string + ' '; for (int i = 0; i < music_string.length(); i++) { if (music_string[i] == ' ') { if (current == "o") out.push_back(4); if (...
HEP/17
template <typename T> bool issame(T a, T b) { if (a.size() != b.size()) return false; return std::equal(a.begin(), a.end(), b.begin()); } #define ASSERT(...) \ do { \ if (!(__VA_ARGS__)) { \ std::exit(-1); \ } \ } while (false) #define TEST_ON_T...
template <typename T> T how_many_times(std::string str, std::string substring) { T out = 0; if (str.length() == 0) return 0; for (int i = 0; i <= str.length() - substring.length(); i++) if (str.substr(i, substring.length()) == substring) out += 1; return out; }
Make the following function generic for the return value.
template <std::integral T> T how_many_times(std::string str, std::string substring) { T out = 0; if (str.length() == 0) return 0; for (int i = 0; i <= str.length() - substring.length(); i++) if (str.substr(i, substring.length()) == substring) out += 1; return out; }
Constrain the generic code using C++20 Concepts so that the generic parameter is an integer type.
int main() { how_many_times<float>("", "x"); }
template <typename T, std::enable_if_t<std::is_integral_v<T>, int> = 0> T how_many_times(std::string str, std::string substring) { T out = 0; if (str.length() == 0) return 0; for (int i = 0; i <= str.length() - substring.length(); i++) if (str.substr(i, substring.length()) == substring) out += 1; return out...
Constrain the generic code using C++17 SFINAE so that the generic parameter is an integer type.
int how_many_times(std::string str, std::string substring) { int out = 0; if (str.length() == 0) return 0; for (int i = 0; i <= str.length() - substring.length(); i++) if (str.substr(i, substring.length()) == substring) out += 1; return out; }
HEP/18
#define ASSERT(...) \ do { \ if (!(__VA_ARGS__)) { \ std::exit(-1); \ } \ } while (false) #define TEST_ON_TYPE(_type_) \ do { \ ASSERT(how_many...
template <typename T> std::vector<T> find_closest_elements(std::vector<T> numbers) { std::vector<T> out = {}; for (int i = 0; i < numbers.size(); i++) { for (int j = i + 1; j < numbers.size(); j++) { if (out.size() == 0 || std::abs(numbers[i] - numbers[j]) < std::abs(out[0] - out[1])) { ...
Make the following function generic for the value type of the vector parameter and vector return value.
template <std::floating_point T> std::vector<T> find_closest_elements(std::vector<T> numbers) { std::vector<T> out = {}; for (int i = 0; i < numbers.size(); i++) { for (int j = i + 1; j < numbers.size(); j++) { if (out.size() == 0 || std::abs(numbers[i] - numbers[j]) < std::abs(out[0] - out[1]))...
Constrain the generic code using C++20 Concepts so that the generic parameter is a floating point type.
int main() { find_closest_elements(std::vector<int>{1, 2, 3, 4, 5}); }
template <typename T, std::enable_if_t<std::is_floating_point_v<T>, int> = 0> std::vector<T> find_closest_elements(std::vector<T> numbers) { std::vector<T> out = {}; for (int i = 0; i < numbers.size(); i++) { for (int j = i + 1; j < numbers.size(); j++) { if (out.size() == 0 || std::abs(numbers[...
Constrain the generic code using C++17 SFINAE so that the generic parameter is a floating point type.
std::vector<float> find_closest_elements(std::vector<float> numbers) { std::vector<float> out = {}; for (int i = 0; i < numbers.size(); i++) { for (int j = i + 1; j < numbers.size(); j++) { if (out.size() == 0 || std::abs(numbers[i] - numbers[j]) < std::abs(out[0] - out[1])) { out = {num...
HEP/20
template <typename T> bool issame(std::vector<T> a, std::vector<T> b) { if (a.size() != b.size()) return false; for (int i = 0; i < a.size(); i++) { if (std::abs(a[i] - b[i]) > 1e-4) return false; } return true; } #define ASSERT(...) \ do { \ if (!(__VA_ARGS__)) { \ std...
template <typename T> std::vector<T> rescale_to_unit(std::vector<T> numbers) { T min = 100000, max = -100000; for (int i = 0; i < numbers.size(); i++) { if (numbers[i] < min) min = numbers[i]; if (numbers[i] > max) max = numbers[i]; } for (int i = 0; i < numbers.size(); i++) numbers[i] = (numbers[i]...
Make the following function generic for the value type of the vector parameter and vector return value.
template <std::floating_point T> std::vector<T> rescale_to_unit(std::vector<T> numbers) { T min = 100000, max = -100000; for (int i = 0; i < numbers.size(); i++) { if (numbers[i] < min) min = numbers[i]; if (numbers[i] > max) max = numbers[i]; } for (int i = 0; i < numbers.size(); i++) numbers[i] = ...
Constrain the generic code using C++20 Concepts so that the generic parameter is a floating point type.
int main() { rescale_to_unit(std::vector<int>{1, 2, 3}); }
template <typename T, std::enable_if_t<std::is_floating_point_v<T>, int> = 0> std::vector<T> rescale_to_unit(std::vector<T> numbers) { T min = 100000, max = -100000; for (int i = 0; i < numbers.size(); i++) { if (numbers[i] < min) min = numbers[i]; if (numbers[i] > max) max = numbers[i]; } for (int i = ...
Constrain the generic code using C++17 SFINAE so that the generic parameter is a floating point type.
std::vector<float> rescale_to_unit(std::vector<float> numbers) { float min = 100000, max = -100000; for (int i = 0; i < numbers.size(); i++) { if (numbers[i] < min) min = numbers[i]; if (numbers[i] > max) max = numbers[i]; } for (int i = 0; i < numbers.size(); i++) numbers[i] = (numbers[i] - min) / ...
HEP/21
template <typename T> bool issame(std::vector<T> a, std::vector<T> b) { if (a.size() != b.size()) return false; for (int i = 0; i < a.size(); i++) { if (std::abs(a[i] - b[i]) > 1e-4) return false; } return true; } #define ASSERT(...) \ do { \ if (!(__VA_ARGS__)) { \ std...
template <typename T> T largest_divisor(T n) { for (T i = 2; i * i <= n; i++) if (n % i == 0) return n / i; return 1; }
Replace these functions with a single generic function preserving the original behaviors.
template <std::integral T> T largest_divisor(T n) { for (T i = 2; i * i <= n; i++) if (n % i == 0) return n / i; return 1; }
Constrain the generic code using C++20 Concepts so that it only accepts integer types.
int main() { largest_divisor((float)3.5); }
template <typename T, std::enable_if_t<std::is_integral_v<T>, int> = 0> T largest_divisor(T n) { for (T i = 2; i * i <= n; i++) if (n % i == 0) return n / i; return 1; }
Constrain the generic code using C++17 SFINAE so that it only accepts integer types.
int largest_divisor(int n) { for (int i = 2; i * i <= n; i++) if (n % i == 0) return n / i; return 1; } unsigned largest_divisor(unsigned n) { for (unsigned i = 2; i * i <= n; i++) if (n % i == 0) return n / i; return 1; } long largest_divisor(long n) { for (long i = 2; i * i <= n; i++) if (n % i ...
HEP/24
#define ASSERT(...) \ do { \ if (!(__VA_ARGS__)) { \ std::exit(-1); \ } \ } while (false) #define TEST_ON_TYPE(_type_) \ do { \ ASSERT(largest_divisor((_type_)3) == (_ty...
template <typename T> std::vector<T> factorize(T n) { std::vector<T> out = {}; for (T i = 2; i * i <= n; i++) { if (n % i == 0) { n = n / i; out.push_back(i); i -= 1; } } out.push_back(n); return out; }
Replace these functions with a single generic function preserving the original behaviors.
template <std::integral T> std::vector<T> factorize(T n) { std::vector<T> out = {}; for (T i = 2; i * i <= n; i++) { if (n % i == 0) { n = n / i; out.push_back(i); i -= 1; } } out.push_back(n); return out; }
Constrain the generic code using C++20 Concepts so that it only accepts integer types.
int main() { factorize((float)3.5); }
template <typename T, std::enable_if_t<std::is_integral_v<T>, int> = 0> std::vector<T> factorize(T n) { std::vector<T> out = {}; for (T i = 2; i * i <= n; i++) { if (n % i == 0) { n = n / i; out.push_back(i); i -= 1; } } out.push_back(n); return out; }
Constrain the generic code using C++17 SFINAE so that it only accepts integer types.
std::vector<int> factorize(int n) { std::vector<int> out = {}; for (int i = 2; i * i <= n; i++) { if (n % i == 0) { n = n / i; out.push_back(i); i -= 1; } } out.push_back(n); return out; } std::vector<unsigned> factorize(unsigned n) { std::vector<unsigned> out = {}; for (unsigned...
HEP/25
template <typename T> bool issame(std::vector<T> a, std::vector<T> b) { if (a.size() != b.size()) return false; for (int i = 0; i < a.size(); i++) { if (a[i] != b[i]) return false; } return true; } #define ASSERT(...) \ do { \ if (!(__VA_ARGS__)) { \ std::exit(-1); ...
template <typename T> T remove_duplicates(T numbers) { T out = {}; T has1 = {}; T has2 = {}; for (int i = 0; i < numbers.size(); i++) { if (std::find(has2.begin(), has2.end(), numbers[i]) != has2.end()) continue; if (std::find(has1.begin(), has1.end(), numbers[i]) != has1.end()) { has2.push_back(n...
Replace these functions with a single generic function preserving the original behaviors.
template <typename T> requires requires(const T &container, std::size_t idx, std::size_t oidx) { { container[idx] } -> std::same_as<typename T::const_reference>; { container[idx] == container[oidx] } -> std::convertible_to<bool>; } T remove_duplicates(T numbers) { T out = {}; T has1 = {}; T has2 = {};...
Constrain the generic code using C++20 Concepts so that it only accepts sequenced containers with comparable elements.
int main() { remove_duplicates(3); }
template < typename T, std::enable_if_t< std::conjunction_v< std::is_same<typename T::const_reference, decltype(std::declval< const T &>()[std::declval<std::size_t>()])>, std::is_convertible<decltype(std::declval<const T ...
Constrain the generic code using C++17 SFINAE so that it only accepts sequenced containers with comparable elements.
std::vector<int> remove_duplicates(std::vector<int> numbers) { std::vector<int> out = {}; std::vector<int> has1 = {}; std::vector<int> has2 = {}; for (int i = 0; i < numbers.size(); i++) { if (std::find(has2.begin(), has2.end(), numbers[i]) != has2.end()) continue; if (std::find(has1.begin(), has1.end()...
HEP/26
template <typename T> bool issame(T a, T b) { if (a.size() != b.size()) return false; for (int i = 0; i < a.size(); i++) { if (a[i] != b[i]) return false; } return true; } #define ASSERT(...) \ do { \ if (!(__VA_ARGS__)) { \ std::exit(-1); \ } ...