Permutations
Algorithms
Code
Diff
  • #include <map>
    #include <array>
    #include <string>
    #include <numeric> // accumulate
    
    double oc(char c, double d1, double d2) {
      switch (c) {
          case '+':return d1+d2;
          case '-':return d1-d2;
          case '/':return d1/d2;
          case '*':return d1*d2;
          default: return 0.0;
      }
    //  return 0.0;
    }
    
    double calculate(const array<int, 5>& nums, const string& ops) {
        return std::accumulate(std::next(nums.begin()),nums.end(),nums[0],
                              [&,i=0](auto acc,auto d) mutable {
                                  return oc(ops[i++],acc,d); });
    }
    
    std::pair<std::string, std::string> MinMaxEquations(const array<int, 5>& numbers) {
        std::map<double,string> results; // double first: insertion is sorted
        std::string ops = "+-*/";
    //  No need to sort here ?
        do {
            results[calculate(numbers, ops)]=ops;
        } while (std::next_permutation(ops.begin(), ops.end()));
        return {results.begin()->second,results.rbegin()->second};
    }
    
  • 1#include <map>
    2#include <array>
    3#include <string>
    4
    5using namespace std;
    6
    7double add(double a, double b) { return a + b; }
    8double sub(double a, double b) { return a - b; }
    9double mul(double a, double b) { return a * b; }
    10double div(double a, double b) { return a / b; }
    11
    12typedef double (*opFunc)(double a, double b);
    13
    14double calculate(array<int, 5> const numbers, string ops) {
    15 map<char const, opFunc> const opFuncs = {
    16 {'+', add}, {'-', sub}, {'*', mul}, {'/', div},
    17 };
    18 double result = numbers[0];
    19 for (int i = 0; i < 4; i++) {
    20 opFunc op = opFuncs.at(ops[i]);
    21 result = op(result, numbers[i + 1]);
    22 }
    23 return result;
    24}
    25
    26pair<string, string> MinMaxEquations(array<int, 5> const& numbers) {
    27
    28 map<string, double> results;
    29 string ops = "+-*/";
    30 sort(ops.begin(), ops.end());
    31 do {
    32 results[ops] = calculate(numbers, ops);
    33 } while (next_permutation(ops.begin(), ops.end()));
    34
    35 auto [min, max] = minmax_element(results.begin(), results.end(),
    36 [](const pair<string, double>& p1, const pair<string, double>& p2) {
    37 return p1.second < p2.second; });
    38 return make_pair(min->first, max->first);
    39}
    1+#include <map>
    2+#include <array>
    3+#include <string>
    4+#include <numeric> // accumulate
    5+
    6+double oc(char c, double d1, double d2) {
    7+ switch (c) {
    8+ case '+':return d1+d2;
    9+ case '-':return d1-d2;
    10+ case '/':return d1/d2;
    11+ case '*':return d1*d2;
    12+ default: return 0.0;
    13+ }
    14+// return 0.0;
    15+}
    16+
    17+double calculate(const array<int, 5>& nums, const string& ops) {
    18+ return std::accumulate(std::next(nums.begin()),nums.end(),nums[0],
    19+ [&,i=0](auto acc,auto d) mutable {
    20+ return oc(ops[i++],acc,d); });
    21+}
    22+
    23+std::pair<std::string, std::string> MinMaxEquations(const array<int, 5>& numbers) {
    24+ std::map<double,string> results; // double first: insertion is sorted
    25+ std::string ops = "+-*/";
    26+// No need to sort here ?
    27+ do {
    28+ results[calculate(numbers, ops)]=ops;
    29+ } while (std::next_permutation(ops.begin(), ops.end()));
    30+ return {results.begin()->second,results.rbegin()->second};
    31+}
Code
Diff
  • #include <array>
    enum BoolOp { O, A, X };
    
    template <BoolOp O> 
    class Any2boolOp {
       static std::array<bool,4> tabvals;
    public:
        static bool exec(bool a, bool b) { return tabvals[a+2*b]; }
    };
    template <> std::array<bool, 4> Any2boolOp<O>::tabvals=  { false,  true,  true, true};
    template <> std::array<bool, 4> Any2boolOp<A>::tabvals = { false, false, false, true};
    template <> std::array<bool, 4> Any2boolOp<X>::tabvals = { false, true, true, false};
    
    bool Or(bool a, bool b) { return Any2boolOp<O>::exec(a, b); }
    bool And(bool a, bool b) { return Any2boolOp<A>::exec(a, b); }
    bool Xor(bool a, bool b) { return Any2boolOp<X>::exec(a, b); }
  • 1bool Or(bool a, bool b) {
    2 return a+b;
    3}
    1+#include <array>
    2+enum BoolOp { O, A, X };
    44
    5bool And(bool a, bool b) {
    6 return a*b;
    7}
    8bool Xor(bool a, bool b) {
    9 return a!=b;
    10}
    4+template <BoolOp O>
    5+class Any2boolOp {
    6+ static std::array<bool,4> tabvals;
    7+public:
    8+ static bool exec(bool a, bool b) { return tabvals[a+2*b]; }
    9+};
    10+template <> std::array<bool, 4> Any2boolOp<O>::tabvals= { false, true, true, true};
    11+template <> std::array<bool, 4> Any2boolOp<A>::tabvals = { false, false, false, true};
    12+template <> std::array<bool, 4> Any2boolOp<X>::tabvals = { false, true, true, false};
    13+
    14+bool Or(bool a, bool b) { return Any2boolOp<O>::exec(a, b); }
    15+bool And(bool a, bool b) { return Any2boolOp<A>::exec(a, b); }
    16+bool Xor(bool a, bool b) { return Any2boolOp<X>::exec(a, b); }