Commit c7884868 by Thibaut Lunet

### TL: added reference solution for sO1

parent 4afc4f8f
 ... ... @@ -14,6 +14,7 @@ But in any case, **the oral evaluation must be done during the day of the exerci :bell: _You are free to discuss with your teammates or other teams, and eventually to ask many questions to your tutor, before trying the oral evaluation._ Both proposed problems give 6 points, so you can choose to solve only one of the two. Reference solution for both problems are given in [`prob1.py`](./prob1.py) and [`prob2.cpp`](./prob2.cpp) ## Problem 1 : Looking for some Python weirdness (6 points) ... ...
 class Obj1: def __init__(self, a, b): self.a = a self.b = b def sum(self): return self.a+self.b class Obj2: def __init__(self, x): self.x = x def power(self, p): return self.x**p o1 = Obj1(1,2) o2 = Obj2(3) def fun1(a, b): return a+b def fun2(a): print(a) funTmp = fun1 def swap(o1, o2): if o1 is o2: return for arg, pos in zip([o1, o2], ['first', 'second']): if not hasattr(arg, '__dict__'): raise ValueError(f'cannot swap {pos} argument {arg}') try: o1.__class__, o2.__class__ = o2.__class__, o1.__class__ except TypeError: raise ValueError(f'cannot swap one of the two arguments {o1}, {o2}') o1.__dict__, o2.__dict__ = o2.__dict__, o1.__dict__ def swapGlobal(a, b): aRef = [] bRef = [] for key, val in globals().items(): if val is a: aRef.append(key) if val is b: bRef.append(key) if len(aRef) == 0: raise ValueError('cannot swap first argument') if len(bRef) == 0: raise ValueError('cannot swap second argument') for ref in aRef: globals()[ref] = b for ref in bRef: globals()[ref] = a print(o1, o2) swap(o1, o2) print(o1, o2) print(fun1, funTmp, fun2) swapGlobal(fun1, fun2) print(fun1, funTmp, fun2)
 #include #include #include #include #include #include using std::cout, std::endl, std::ostream; using std::vector; using std::string; using std::ofstream, std::ifstream; using std::istringstream; typedef vector Vector; class Polynom { private: Vector coeffs; void resizeToRealDegree() { size_t realDeg = coeffs.size()-1; for (int i = realDeg; i > 0; i--){ if (coeffs[i] != 0) { break; } realDeg--; } coeffs.resize(realDeg+1); } public: // Base constructor Polynom(Vector c) : coeffs(c) { resizeToRealDegree(); } // Default constructor Polynom() : coeffs({0, 1}) {} // Copy constructor Polynom(const Polynom& other) : coeffs(other.coeffs) { cout << "Using copy constructor" << endl; } // Copy assignment operator Polynom& operator=(const Polynom& other) { if (this != &other) { coeffs = other.coeffs; cout << "Using copy assignment operator" << endl; } return *this; } // Move constructor Polynom(Polynom&& other) : coeffs(std::move(other.coeffs)) { cout << "Using move constructor" << endl; } // Move assignment operator Polynom& operator=(Polynom&& other) { if (this != &other) { coeffs = std::move(other.coeffs); cout << "Using move assignment operator" << endl; } return *this; } // Degree size_t deg() const { return coeffs.size()-1; } // Output into a string string to_string() const { auto d = deg(); // Degree 0 polynomial if (d == 0) { return fmt::format("{:1.1f}", coeffs[0]); } // General case string out = ""; for (int i = d; i > -1; i--) { auto c_i = coeffs[i]; // ignore null coefficients if (c_i == 0) { continue; } // look at sign string sign; if (c_i < 0) { c_i *= -1; if (i == d) { sign = "-"; } else { sign = " - "; } } else if (i < d) { sign = " + "; } // add sign between coefficient if (c_i == 1 and i != 0) { out += sign; } else { out += fmt::format("{}{:1.1f}", sign, c_i); } // print monomial if (i > 1) { out += "x^"; out += std::to_string(i); } else if (i == 1) { out += "x"; } } return out; } // Addition operators Polynom& operator+=(const Polynom& other) { size_t otherDeg = other.deg(); if (otherDeg > deg()) { coeffs.resize(otherDeg+1, 0.0); } for (size_t i = 0; i < otherDeg+1; i++) { coeffs[i] += other.coeffs[i]; } resizeToRealDegree(); return *this; } Polynom& operator+=(double scalar) { coeffs[0] += scalar; return *this; } // Multiplication operators Polynom& operator*=(const Polynom& other) { Vector prodCoeffs(deg() + other.deg() + 1, 0.0); for (size_t i = 0; i < deg()+1; i++) { for (size_t j = 0; j < other.deg()+1; j++) { prodCoeffs[i+j] += coeffs[i]*other.coeffs[j]; } } coeffs = prodCoeffs; return *this; } Polynom& operator*=(double other) { for (auto &c : coeffs) { c *= other; } return *this; } }; // External operator overloading // -- addition Polynom&& operator+(const Polynom& p1, const Polynom& p2) { cout << "const Polynom& + other : (" << p1.to_string() << ") + (" << p2.to_string() << ")" << endl; Polynom* temp = new Polynom(p1); return std::move(*temp += p2); } Polynom&& operator+(Polynom&& p1, const Polynom& p2) { cout << "Polynom&& + other : (" << p1.to_string() << ") + (" << p2.to_string() << ")" << endl; return std::move(p1 += p2); } Polynom&& operator+(const Polynom& p1, Polynom&& p2) { return std::move(p2) + p1; } Polynom&& operator+(Polynom&& p1, Polynom&& p2) { cout << "Polynom&& + other&& : (" << p1.to_string() << ") + (" << p2.to_string() << ")" << endl; return std::move(p1 += p2); } // -- multiplication Polynom&& operator*(const Polynom& p1, const Polynom& p2) { cout << "const Polynom& * other : (" << p1.to_string() << ") * (" << p2.to_string() << ")" << endl; Polynom* temp = new Polynom(p1); return std::move(*temp *= p2); } Polynom&& operator*(Polynom&& p1, const Polynom& p2) { cout << "Polynom&& * other : (" << p1.to_string() << ") * (" << p2.to_string() << ")" << endl; return std::move(p1 *= p2); } Polynom&& operator*(const Polynom& p1, Polynom&& p2) { return std::move(p2) * p1; } Polynom&& operator*(Polynom&& p1, Polynom&& p2) { cout << "Polynom&& * other&& : (" << p1.to_string() << ") * (" << p2.to_string() << ")" << endl; return std::move(p1 += p2); } // Cross operation with scalar // -- addition Polynom&& operator+(const Polynom& p, double scalar) { cout << "const Polynom& + scalar : (" << p.to_string() << ") + " << scalar << endl; Polynom* temp = new Polynom(p); return std::move(*temp += scalar); } Polynom&& operator+(Polynom&& p, double scalar) { cout << "Polynom&& + scalar : (" << p.to_string() << ") + " << scalar << endl; return std::move(p += scalar); } Polynom&& operator+(double scalar, const Polynom& p) { return std::move(p) + scalar; } Polynom&& operator+(double scalar, Polynom&& p) { return std::move(p) + scalar; } // -- multiplication Polynom&& operator*(const Polynom& p, double scalar) { cout << "const Polynom& * scalar : (" << p.to_string() << ") * " << scalar << endl; Polynom* temp = new Polynom(p); return std::move(*temp *= scalar); } Polynom&& operator*(Polynom&& p, double scalar) { cout << "Polynom&& * scalar : (" << p.to_string() << ") * " << scalar << endl; return std::move(p *= scalar); } Polynom&& operator*(double scalar, const Polynom& p) { return std::move(p) * scalar; } Polynom&& operator*(double scalar, Polynom&& p) { return std::move(p) * scalar; } // Stream operator ostream& operator<<(ostream& out, const Polynom& p) { out << p.to_string(); return out; } int main() { Polynom x; cout << "Building p0 --------------------------------" << endl; Polynom p0 = 1 + 2*x + x*x; cout << "Building p1 --------------------------------" << endl; Polynom p1 = 1 + 2*x + x*x*x*x*x*x; cout << "Building p2 --------------------------------" << endl; Polynom p2 = x*x + 2*x + 1; cout << "End output ---------------------------------" << endl; cout << x << endl; cout << p0 << endl; cout << p1 << endl; cout << p2 << endl; } \ No newline at end of file
 # News and announcements # 05.07.2022 - Added [reference solutions for sO1](./exercises/sO1/README.md) # 04.07.2022 - Closed evaluation for S7. - Closed evaluation for S7 # 01.07.2022 ... ...
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!