Kodewick
Start learning →
Mini challenge←→0 / 60

Stack the plates

#039Mini challengeReferencesvector6 min

Clean plates are piled on the rack; you take them off the top one at a time and stack them in the cupboard. Write move_one(from, to): take the last plate off from and put it at the end of to. If from is empty, do nothing. Both are references, so both change.

Examples
stacked({"blue", "white", "red"}, 2)→['red', 'white']
stacked({"blue"}, 3)→['blue']
The functionvector<string> stacked(vector<string> rack, int n)
main.cpp
#include <string>
#include <vector>
using namespace std;

void move_one(vector<string> &from, vector<string> &to) {
    // your code here: move the last plate of from to to
}

vector<string> stacked(vector<string> rack, int n) {
    vector<string> cupboard;
    for (int i = 0; i < n; i++) {
        move_one(rack, cupboard);
    }
    return cupboard;
}
Ctrl + Enter runs too
Result

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