Ad

why add, when we can do it our self.

Code
Diff
  • #include <bitset>
    #include <fmt/core.h>
    
    std::bitset<64> bitwiseAdd(std::bitset<64> x, std::bitset<64> y) {
        while ((x & y).any()) { 
            std::bitset<64> carry = (x & y) << 1;
            x = x ^ y;
            y = carry;
        }
        return x ^ y;
    }
    
    
    int64_t multiply(int32_t a, int32_t b) {
      std::bitset<64> bitsetA(static_cast<uint64_t>(a));
      std::bitset<64> bitsetB(static_cast<uint64_t>(b));
      std::bitset<64> result;
    
      for (int i = 0; i < 64; ++i) {
        if (bitsetB.test(i)) {
          auto shiftedA = (bitsetA << i);
          result = bitwiseAdd(result, shiftedA);
        }
      }
    
      return static_cast<int64_t>(result.to_ullong());
    }
    • int just_why(int a, int b) { return b ? a + just_why(a, b - 1) : 0; }
    • int multiply(int a, int b) { return just_why(a, b); }
    • #include <bitset>
    • #include <fmt/core.h>
    • std::bitset<64> bitwiseAdd(std::bitset<64> x, std::bitset<64> y) {
    • while ((x & y).any()) {
    • std::bitset<64> carry = (x & y) << 1;
    • x = x ^ y;
    • y = carry;
    • }
    • return x ^ y;
    • }
    • int64_t multiply(int32_t a, int32_t b) {
    • std::bitset<64> bitsetA(static_cast<uint64_t>(a));
    • std::bitset<64> bitsetB(static_cast<uint64_t>(b));
    • std::bitset<64> result;
    • for (int i = 0; i < 64; ++i) {
    • if (bitsetB.test(i)) {
    • auto shiftedA = (bitsetA << i);
    • result = bitwiseAdd(result, shiftedA);
    • }
    • }
    • return static_cast<int64_t>(result.to_ullong());
    • }
Fundamentals
Arrays
Strings

Might have cone a bit crazy with the tests, but hey now you should be able to pass any container that satisfy the requirement. And if it doesn't it wont compile.

Code
Diff
  • #include <string>
    #include <vector>
    #include <algorithm>
    
    template<typename Container>
    bool containsT_Rex(const Container &things){
      using ValueType = typename Container::value_type;
      static_assert(std::is_convertible_v<decltype(std::declval<ValueType>() == std::declval<std::string>()), bool>, "Container elements must be comparable to std::string");
      
      static constexpr auto trex = "Tyrannosaurus";
      return std::find(things.cbegin(), things.cend(), trex) != things.cend();
    }
    • public class findT_Rex {
    • public static boolean containsT_Rex(String[] things) {
    • //insert code!
    • return true;
    • }
    • #include <string>
    • #include <vector>
    • #include <algorithm>
    • template<typename Container>
    • bool containsT_Rex(const Container &things){
    • using ValueType = typename Container::value_type;
    • static_assert(std::is_convertible_v<decltype(std::declval<ValueType>() == std::declval<std::string>()), bool>, "Container elements must be comparable to std::string");
    • static constexpr auto trex = "Tyrannosaurus";
    • return std::find(things.cbegin(), things.cend(), trex) != things.cend();
    • }
Code
Diff
  • #include <type_traits>
    #include <limits>
    #include <bitset>
    
    template<typename T>
    constexpr bool is_more_than_2(T n){
      static_assert(std::is_integral_v<T>, "T must be an integral type.");
      
      constexpr size_t numBits = std::numeric_limits<T>::digits + std::is_signed_v<T>;
      std::bitset<numBits> bits(n);
      
      if constexpr (std::is_unsigned_v<T>) {
        // For unsigned, check if more than 1 bit is set
        return bits.count() > 1;
      } else {
        // For signed, ensure the sign bit is unset and more than 1 bit is set
        bool signBitUnset = !bits[numBits - 1];
        bits.reset(numBits - 1); // Mask out the sign bit
        return signBitUnset && bits.count() > 1;
      }
    }
    • def is_more_than_2(n):
    • return n > 2
    • #include <type_traits>
    • #include <limits>
    • #include <bitset>
    • template<typename T>
    • constexpr bool is_more_than_2(T n){
    • static_assert(std::is_integral_v<T>, "T must be an integral type.");
    • constexpr size_t numBits = std::numeric_limits<T>::digits + std::is_signed_v<T>;
    • std::bitset<numBits> bits(n);
    • if constexpr (std::is_unsigned_v<T>) {
    • // For unsigned, check if more than 1 bit is set
    • return bits.count() > 1;
    • } else {
    • // For signed, ensure the sign bit is unset and more than 1 bit is set
    • bool signBitUnset = !bits[numBits - 1];
    • bits.reset(numBits - 1); // Mask out the sign bit
    • return signBitUnset && bits.count() > 1;
    • }
    • }

Checked math in c++, using some buildins for gcc/clang. Translated from Rust.
Can be made without the buildins, but you would have to make your own boundary checks.

Code
Diff
  • #include <optional>
    #include <limits>
    
    std::optional<int32_t> add_checked(int32_t a, int32_t b){
      int32_t r{};
      if(!__builtin_sadd_overflow(a, b, &r)){
        return r;
      }
      
      return std::nullopt;
    }
    
    std::optional<int32_t> sub_checked(int32_t a, int32_t b){
      int32_t r{};
      if(!__builtin_ssub_overflow(a, b, &r)){
        return r;
      }
      
      return std::nullopt;
    }
    
    std::optional<int32_t> mul_checked(int32_t a, int32_t b){
      int32_t r{};
      if(!__builtin_smul_overflow(a, b, &r)){
        return r;
      }
      
      return std::nullopt;
    }
    
    std::optional<int32_t> div_checked(int32_t a, int32_t b){
      if(b == 0 || (a ==  std::numeric_limits<int32_t>::min() && b == -1)){
        return std::nullopt;
      }
      return a / b;
    }
    
    std::optional<int32_t> mod_checked(int32_t a, int32_t b){
      if(b == 0 || (a ==  std::numeric_limits<int32_t>::min() && b == -1)){
        return std::nullopt;
      }
      return a % b;
    }
    
    std::optional<int32_t> calculator(Operation op, int32_t x, int32_t y) {
      switch(op){
          case Operation::Add:return add_checked(x, y);
          case Operation::Subtract: return sub_checked(x, y);
          case Operation::Multiply: return mul_checked(x, y);
          case Operation::Divide: return div_checked(x, y);
          case Operation::Modulo: return mod_checked(x, y);
          default:
          return std::nullopt;
      }
    }
    • mod preloaded;
    • use preloaded::Operation;
    • #include <optional>
    • #include <limits>
    • fn calculator(op: Operation, x: i32, y: i32) -> Option<i32> {
    • match op {
    • Operation::Add => x.checked_add(y),
    • Operation::Subtract => x.checked_sub(y),
    • Operation::Multiply => x.checked_mul(y),
    • Operation::Divide => x.checked_div(y),
    • Operation::Modulo => x.checked_rem(y)
    • }
    • std::optional<int32_t> add_checked(int32_t a, int32_t b){
    • int32_t r{};
    • if(!__builtin_sadd_overflow(a, b, &r)){
    • return r;
    • }
    • return std::nullopt;
    • }
    • std::optional<int32_t> sub_checked(int32_t a, int32_t b){
    • int32_t r{};
    • if(!__builtin_ssub_overflow(a, b, &r)){
    • return r;
    • }
    • return std::nullopt;
    • }
    • std::optional<int32_t> mul_checked(int32_t a, int32_t b){
    • int32_t r{};
    • if(!__builtin_smul_overflow(a, b, &r)){
    • return r;
    • }
    • return std::nullopt;
    • }
    • std::optional<int32_t> div_checked(int32_t a, int32_t b){
    • if(b == 0 || (a == std::numeric_limits<int32_t>::min() && b == -1)){
    • return std::nullopt;
    • }
    • return a / b;
    • }
    • std::optional<int32_t> mod_checked(int32_t a, int32_t b){
    • if(b == 0 || (a == std::numeric_limits<int32_t>::min() && b == -1)){
    • return std::nullopt;
    • }
    • return a % b;
    • }
    • std::optional<int32_t> calculator(Operation op, int32_t x, int32_t y) {
    • switch(op){
    • case Operation::Add:return add_checked(x, y);
    • case Operation::Subtract: return sub_checked(x, y);
    • case Operation::Multiply: return mul_checked(x, y);
    • case Operation::Divide: return div_checked(x, y);
    • case Operation::Modulo: return mod_checked(x, y);
    • default:
    • return std::nullopt;
    • }
    • }
Code
Diff
  • #include <utility>
    
    template<typename T, typename U>
    constexpr auto multiply(T a, U b) -> decltype(std::declval<T>() * std::declval<U>()){
      return a * b;
    }
    • #define multiply(a,b) a*b
    • #include <utility>
    • template<typename T, typename U>
    • constexpr auto multiply(T a, U b) -> decltype(std::declval<T>() * std::declval<U>()){
    • return a * b;
    • }

I know this doesn't work in the current c+ version. Needs c++20.
But with concepts, some of this can be simplified

Concept to check if T + T is valid.
Concept to check if U(T + T) + T is valid, and is U.

T + T could give a different type U, but T + U must be U again.

Code
Diff
  • /*
    #include <concepts>
    #include <vector>
    #include <numeric>
    #include <type_traits>
    
    namespace{
        // Concept to check if T + T yields a valid type
        template <typename T>
        concept SelfAddable = requires(T a) {
            { a + a }; // Ensure T + T is valid
        };
    
    // Concept to check if U + T is valid and results in U
        template <typename T, typename U>
        concept CompatibleAddable = requires(U u, T t) {
            { u + t } -> std::same_as<U>;
        };
    
    // Function template with inferred U
        template <SelfAddable T>
        requires CompatibleAddable<T, decltype(std::declval<T>() + std::declval<T>())>
        auto sum(const std::vector<T>& v) {
            using U = decltype(std::declval<T>() + std::declval<T>()); // Infer U
            return std::accumulate(v.cbegin(), v.cend(), U{}, [](U acc, const T& elem) {
                return acc + elem;
            });
        }
    }
    */
    
    
    
    //OLD
    #include <vector>
    #include <numeric>
    #include <type_traits>
    #include <utility>
    
    namespace
    {
      template<typename, typename, typename = void>
      struct has_plus : std::false_type {};
    
      template<typename T1, typename T2>
      struct has_plus<T1, T2, std::void_t<decltype(std::declval<T1>() + std::declval<T2>())>> : std::true_type {};
    
      template<typename T1, typename T2>
      inline constexpr bool has_plus_v = has_plus<T1, T2>::value;
    
      template<typename T1, typename T2, bool = has_plus_v<T1, T2>>
      struct plus_result
      {
        using type = decltype(std::declval<T1>() + std::declval<T2>());
      };
      
      template<typename T1, typename T2>
      struct plus_result<T1, T2, false> {};
      
      template<typename T1, typename T2>
      using plus_result_t = typename plus_result<T1, T2>::type;
      
      template<typename T = double, typename = std::enable_if_t<has_plus_v<T, T>>>
      plus_result_t<T, T> sum(const std::vector<T>& v)
      {
        return std::accumulate(v.cbegin(), v.cend(), T{});
      }
    }
    • /*
    • #include <concepts>
    • #include <vector>
    • #include <numeric>
    • #include <type_traits>
    • namespace{
    • // Concept to check if T + T yields a valid type
    • template <typename T>
    • concept SelfAddable = requires(T a) {
    • { a + a }; // Ensure T + T is valid
    • };
    • // Concept to check if U + T is valid and results in U
    • template <typename T, typename U>
    • concept CompatibleAddable = requires(U u, T t) {
    • { u + t } -> std::same_as<U>;
    • };
    • // Function template with inferred U
    • template <SelfAddable T>
    • requires CompatibleAddable<T, decltype(std::declval<T>() + std::declval<T>())>
    • auto sum(const std::vector<T>& v) {
    • using U = decltype(std::declval<T>() + std::declval<T>()); // Infer U
    • return std::accumulate(v.cbegin(), v.cend(), U{}, [](U acc, const T& elem) {
    • return acc + elem;
    • });
    • }
    • }
    • */
    • //OLD
    • #include <vector>
    • #include <numeric>
    • #include <type_traits>
    • #include <utility>
    • namespace
    • {
    • template<typename, typename, typename = void>
    • struct has_plus : std::false_type {};
    • template<typename T1, typename T2>
    • struct has_plus<T1, T2, std::void_t<decltype(std::declval<T1>() + std::declval<T2>())>> : std::true_type {};
    • template<typename T1, typename T2>
    • inline constexpr bool has_plus_v = has_plus<T1, T2>::value;
    • template<typename T1, typename T2, bool = has_plus_v<T1, T2>>
    • struct plus_result
    • {
    • using type = decltype(std::declval<T1>() + std::declval<T2>());
    • };
    • template<typename T1, typename T2>
    • struct plus_result<T1, T2, false> {};
    • template<typename T1, typename T2>
    • using plus_result_t = typename plus_result<T1, T2>::type;
    • template<typename T = double, typename = std::enable_if_t<has_plus_v<T, T>>>
    • plus_result_t<T, T> sum(const std::vector<T>& v)
    • {
    • return std::accumulate(v.cbegin(), v.cend(), T{});
    • }
    • }