Drill

ProblemsC++ › dates

Read a duration a human typed

mediumdatesParsingStringsMathC++

A config file lets people write timeouts as "1h30m" instead of counting seconds, and the loader has to turn that into a number without trusting it.

parseDuration(text: string) → int

C++ needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.

Solve it in Python →

Where you start

int parseDuration(std::string text) {
    
}

Worked examples

CallResult
parseDuration(std::string("1h30m"))5400
parseDuration(std::string("45s"))45
parseDuration(std::string("2h"))7200
parseDuration(std::string("90m"))5400

Hint

Scan digits, then expect exactly one unit letter. Remember which units you have already taken so order and repeats are both caught by the same check.

Reference solution in C++
int parseDuration(std::string text) {
    std::map<char, int> rank{{'h', 1}, {'m', 2}, {'s', 3}};
    std::map<char, int> mult{{'h', 3600}, {'m', 60}, {'s', 1}};
    size_t i = 0;
    int total = 0, last = 0;
    while (i < text.size()) {
        int n = 0, digits = 0;
        while (i < text.size() && text[i] >= '0' && text[i] <= '9') { n = n * 10 + (text[i] - '0'); i++; digits++; }
        if (digits == 0 || i >= text.size()) return -1;
        char u = text[i];
        if (!rank.count(u) || rank[u] <= last) return -1;
        last = rank[u];
        total += n * mult[u];
        i++;
    }
    return total;
}

The same problem in another language

More dates problems in C++