Kodewick
Start learning →
Starter←→0 / 60

Water bottle

#040StarterClasses3 min

A Bottle has ml, how many millilitres of water are left in it. Write drink(amount): take a sip, and ml goes down by amount. A sip is never more than what's left.

Examples
water_left(500, {100, 50})→350
water_left(300, {})→300
The functionint water_left(int start, const vector<int> &sips)
main.cpp
#include <vector>
using namespace std;

class Bottle {
public:
    int ml;

    Bottle(int start) {
        ml = start;
    }

    void drink(int amount) {
        // your code here: take amount off ml
    }
};

int water_left(int start, const vector<int> &sips) {
    Bottle b(start);
    for (int s : sips) {
        b.drink(s);
    }
    return b.ml;
}
Ctrl + Enter runs too
Result

Run tries the examples. When they look right, Submit.