exercism / cpp

Exercism exercises in C++.
https://exercism.org/tracks/cpp
MIT License
254 stars 207 forks source link

clock: the test timed out #723

Closed nozomirin closed 11 months ago

nozomirin commented 11 months ago

This is my solution.

#if !defined(CLOCK_H)
#define CLOCK_H
#include <string>
namespace date_independent {
    class clock {
    public:
        clock(int hour, int minute);  
        static clock at(int hour, int minute);
        operator std::string() const;
        clock& plus(int minutes);
        bool operator==(clock &rhs) noexcept;
        bool operator!=(clock &rhs) noexcept;
    private:
        int hour_;
        int minute_;
    };
}  // namespace date_independent

#endif // CLOCK_H
#include "clock.h"
#include <cmath>
namespace date_independent {
clock::clock(int hour, int minute): hour_(hour), minute_(minute) {

}
clock clock::at(int hour, int minute) {
    int actual_hour = ( hour + static_cast<int>(std::floor(minute / 60.0)) ) % 24;
    int actual_minute = minute % 60;
    return clock(actual_hour, actual_minute);
}

clock::operator std::string() const {
    std::string hour = std::to_string(hour_);
    if (hour_ < 10) {
        hour = "0" + hour;
    }
    std::string minute = std::to_string(minute_);
    if (minute_ < 10) {
        minute = "0" + minute;
    } 
    return hour + ":" + minute ;
}

clock& clock::plus(int minutes) {
    int hour = std::floor(minutes / 60.0);
    int minute = minutes % 60;
    hour_ = (hour + hour_ + (minute + minute_) / 60) % 24;
    minute_ = (minute + minute_) % 60;
    return *this;
}

bool clock::operator==(clock &rhs) noexcept {
    return this->hour_ == rhs.hour_ && this->minute_ == rhs.minute_;
}

bool clock::operator!=(clock &rhs) noexcept {
    return !this->operator==(rhs);
}

}  // namespace date_independent

When I ran tests, it timed out. I tried to comment out these member functions, and I found the at function is the problem. But it seems that there is nothing special in this static member function (I searched other people's solutions and can't find something useful. I even copied someone's code and it just worked. I also paste them in godbolt.org and it compiled.)

Please forgive me if I miss something stupid.

github-actions[bot] commented 11 months ago

Hello. Thanks for opening an issue on Exercism. We are currently in a phase of our journey where we have paused community contributions to allow us to take a breather and redesign our community model. You can learn more in this blog post. As such, all issues and PRs in this repository are being automatically closed.

That doesn't mean we're not interested in your ideas, or that if you're stuck on something we don't want to help. The best place to discuss things is with our community on the Exercism Community Forum. You can use [this link](https://forum.exercism.org/new-topic?title=clock:%20the%20test%20timed%20out&body=This%20is%20my%20solution.%0D%0A%60%60%60cpp%0D%0A#if%20!defined(CLOCK_H)%0D%0A#define%20CLOCK_H%0D%0A#include%20%3Cstring%3E%0D%0Anamespace%20date_independent%20%7B%0D%0A%20%20%20%20class%20clock%20%7B%0D%0A%20%20%20%20public:%0D%0A%20%20%20%20%20%20%20%20clock(int%20hour,%20int%20minute);%20%20%0D%0A%20%20%20%20%20%20%20%20static%20clock%20at(int%20hour,%20int%20minute);%0D%0A%20%20%20%20%20%20%20%20operator%20std::string()%20const;%0D%0A%20%20%20%20%20%20%20%20clock&%20plus(int%20minutes);%0D%0A%20%20%20%20%20%20%20%20bool%20operator==(clock%20&rhs)%20noexcept;%0D%0A%20%20%20%20%20%20%20%20bool%20operator!=(clock%20&rhs)%20noexcept;%0D%0A%20%20%20%20private:%0D%0A%20%20%20%20%20%20%20%20int%20hour_;%0D%0A%20%20%20%20%20%20%20%20int%20minute_;%0D%0A%20%20%20%20%7D;%0D%0A%7D%20%20//%20namespace%20date_independent%0D%0A%0D%0A#endif%20//%20CLOCK_H%0D%0A%60%60%60%0D%0A%60%60%60cpp%0D%0A#include%20%22clock.h%22%0D%0A#include%20%3Ccmath%3E%0D%0Anamespace%20date_independent%20%7B%0D%0Aclock::clock(int%20hour,%20int%20minute):%20hour_(hour),%20minute_(minute)%20%7B%0D%0A%20%20%20%20%0D%0A%7D%0D%0Aclock%20clock::at(int%20hour,%20int%20minute)%20%7B%0D%0A%20%20%20%20int%20actual_hour%20=%20(%20hour%20+%20static_cast%3Cint%3E(std::floor(minute%20/%2060.0))%20)%20%25%2024;%0D%0A%20%20%20%20int%20actual_minute%20=%20minute%20%25%2060;%0D%0A%20%20%20%20return%20clock(actual_hour,%20actual_minute);%0D%0A%7D%0D%0A%0D%0Aclock::operator%20std::string()%20const%20%7B%0D%0A%20%20%20%20std::string%20hour%20=%20std::to_string(hour_);%0D%0A%20%20%20%20if%20(hour_%20%3C%2010)%20%7B%0D%0A%20%20%20%20%20%20%20%20hour%20=%20%220%22%20+%20hour;%0D%0A%20%20%20%20%7D%0D%0A%20%20%20%20std::string%20minute%20=%20std::to_string(minute_);%0D%0A%20%20%20%20if%20(minute_%20%3C%2010)%20%7B%0D%0A%20%20%20%20%20%20%20%20minute%20=%20%220%22%20+%20minute;%0D%0A%20%20%20%20%7D%20%0D%0A%20%20%20%20return%20hour%20+%20%22:%22%20+%20minute%20;%0D%0A%7D%0D%0A%0D%0Aclock&%20clock::plus(int%20minutes)%20%7B%0D%0A%20%20%20%20int%20hour%20=%20std::floor(minutes%20/%2060.0);%0D%0A%20%20%20%20int%20minute%20=%20minutes%20%25%2060;%0D%0A%20%20%20%20hour_%20=%20(hour%20+%20hour_%20+%20(minute%20+%20minute_)%20/%2060)%20%25%2024;%0D%0A%20%20%20%20minute_%20=%20(minute%20+%20minute_)%20%25%2060;%0D%0A%20%20%20%20return%20*this;%0D%0A%7D%0D%0A%0D%0Abool%20clock::operator==(clock%20&rhs)%20noexcept%20%7B%0D%0A%20%20%20%20return%20this-%3Ehour_%20==%20rhs.hour_%20&&%20this-%3Eminute_%20==%20rhs.minute_;%0D%0A%7D%0D%0A%0D%0Abool%20clock::operator!=(clock%20&rhs)%20noexcept%20%7B%0D%0A%20%20%20%20return%20!this-%3Eoperator==(rhs);%0D%0A%7D%0D%0A%0D%0A%7D%20%20//%20namespace%20date_independent%0D%0A%60%60%60%0D%0AWhen%20I%20ran%20tests,%20it%20timed%20out.%0D%0AI%20tried%20to%20comment%20out%20these%20member%20functions,%20and%20I%20found%20the%20at%20function%20is%20the%20problem.%20But%20it%20seems%20that%20there%20is%20nothing%20special%20in%20this%20static%20member%20function%20(I%20searched%20other%20people's%20solutions%20and%20can't%20find%20something%20useful.%20I%20even%20copied%20someone's%20code%20and%20it%20just%20worked.%20I%20also%20paste%20them%20in%20godbolt.org%20and%20it%20compiled.)%0D%0A%0D%0APlease%20forgive%20me%20if%20I%20miss%20something%20stupid.&category=cpp) to copy this into a new topic there.


Note: If this issue has been pre-approved, please link back to this issue on the forum thread and a maintainer or staff member will reopen it.

nozomirin commented 11 months ago

I know why it is not right now. The behavior of the modulo operation in c++ is not the same as python. I'm sorry for the disturbance.

siebenschlaefer commented 11 months ago

Great to hear that you could solve the problem.

For anybody else: I think the reason why the tests do time out is that in this solution the operator== and operator!= cannot be called with const objects. But our testing framework Catch2 does some complex meta-programming magic and the evaluation of REQUIRE(clock1 == clock2); and REQUIRE(clock1 != clock2); causes lots and lots of error messages, too many for our test runner to handle.

nozomirin commented 11 months ago

Both const for parameter and for function are necessary. It takes me so much time...