Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
240 changes: 97 additions & 143 deletions extra-task-1.cpp
Original file line number Diff line number Diff line change
@@ -1,169 +1,123 @@
#include <cassert>
#include <iostream>

// Return the number of seconds later that a time in seconds time_2
// is than a time in seconds time_1.
double seconds_difference(double time_1, double time_2)
{
// your implementation goes here...

/*
Return the number of seconds later that a time in seconds
time_2 is than a time in seconds time_1.

>>> seconds_difference(1800.0, 3600.0)
1800.0

>>> seconds_difference(3600.0, 1800.0)
-1800.0

>>> seconds_difference(1800.0, 2160.0)
360.0

>>> seconds_difference(1800.0, 1800.0)
0.0
*/
return time_2 - time_1;
}

// Return the number of hours later that a time in seconds time_2
// is than a time in seconds time_1.
double hours_difference(double time_1, double time_2)
{
/*
Return the number of hours later that a time in seconds
time_2 is than a time in seconds time_1.

>>> hours_difference(1800.0, 3600.0)
0.5

>>> hours_difference(3600.0, 1800.0)
-0.5

>>> hours_difference(1800.0, 2160.0)
0.1

>>> hours_difference(1800.0, 1800.0)
0.0
*/
return (time_2 - time_1) / 3600.0;
}

// Return the total number of hours in the specified number
// of hours, minutes, and seconds.
double to_float_hours(int hours, int minutes, int seconds)
{
/*
Return the total number of hours in the specified number
of hours, minutes, and seconds.

Precondition: 0 <= minutes < 60 and 0 <= seconds < 60

>>> to_float_hours(0, 15, 0)
0.25

>>> to_float_hours(2, 45, 9)
2.7525

>>> to_float_hours(1, 0, 36)
1.01
*/
return hours + minutes / 60.0 + seconds / 3600.0;
}

// hours is a number of hours since midnight. Return the hour as seen
// on a 24-hour clock.
double to_24_hour_clock(double hours)
{
/*
hours is a number of hours since midnight. Return the
hour as seen on a 24-hour clock.

Precondition: hours >= 0

>>> to_24_hour_clock(24)
0

>>> to_24_hour_clock(48)
0

>>> to_24_hour_clock(25)
1

>>> to_24_hour_clock(4)
4

>>> to_24_hour_clock(28.5)
4.5

You may wish to inspect various function in <cmath> to work
with integer and fractional part of a hours separately.

*/
double result = std::fmod(hours, 24.0);
if (result < 0) {
result += 24.0;
}
return result;
}

/*
Implement three functions
* get_hours
* get_minutes
* get_seconds
They are used to determine the hours part, minutes part and seconds part
of a time in seconds. E.g.:

>>> get_hours(3800)
1

>>> get_minutes(3800)
3
// Return the hours part of a time in seconds.
int get_hours(int seconds)
{
if (seconds < 0) return 0;
return (seconds / 3600) % 24;
}

>>> get_seconds(3800)
20
// Return the minutes part of a time in seconds.
int get_minutes(int seconds)
{
if (seconds < 0) return 0;
return (seconds / 60) % 60;
}

In other words, if 3800 seconds have elapsed since midnight,
it is currently 01:03:20 (hh:mm:ss).
*/
// Return the seconds part of a time in seconds.
int get_seconds(int seconds)
{
if (seconds < 0) return 0;
return seconds % 60;
}

// Return time at UTC+0, given a local time and its utc_offset.
double time_to_utc(int utc_offset, double time)
{
/*
Return time at UTC+0, where utc_offset is the number of hours away from
UTC+0.
You may be interested in:
https://en.wikipedia.org/wiki/Coordinated_Universal_Time

>>> time_to_utc(+0, 12.0)
12.0

>>> time_to_utc(+1, 12.0)
11.0

>>> time_to_utc(-1, 12.0)
13.0

>>> time_to_utc(-11, 18.0)
5.0

>>> time_to_utc(-1, 0.0)
1.0

>>> time_to_utc(-1, 23.0)
0.0
*/
double result = time - utc_offset;
result = std::fmod(result, 24.0);
if (result < 0) {
result += 24.0;
}
return result;
}

// Return local time in time zone utc_offset, given UTC time.
double time_from_utc(int utc_offset, double time)
{
/*
Return UTC time in time zone utc_offset.

>>> time_from_utc(+0, 12.0)
12.0

>>> time_from_utc(+1, 12.0)
13.0

>>> time_from_utc(-1, 12.0)
11.0

>>> time_from_utc(+6, 6.0)
12.0

>>> time_from_utc(-7, 6.0)
23.0

>>> time_from_utc(-1, 0.0)
23.0

>>> time_from_utc(-1, 23.0)
22.0

>>> time_from_utc(+1, 23.0)
0.0
*/
double result = time + utc_offset;
result = std::fmod(result, 24.0);
if (result < 0) {
result += 24.0;
}
return result;
}

int main()
{
assert(std::fabs(seconds_difference(1800.0, 3600.0) - 1800.0) <= DBL_EPSILON);
assert(std::fabs(seconds_difference(3600.0, 1800.0) - (-1800.0)) <= DBL_EPSILON);
assert(std::fabs(seconds_difference(1800.0, 2160.0) - 360.0) <= DBL_EPSILON);
assert(std::fabs(seconds_difference(1800.0, 1800.0) - 0.0) <= DBL_EPSILON);

assert(std::fabs(hours_difference(1800.0, 3600.0) - 0.5) <= DBL_EPSILON);
assert(std::fabs(hours_difference(3600.0, 1800.0) - (-0.5)) <= DBL_EPSILON);
assert(std::fabs(hours_difference(1800.0, 2160.0) - 0.1) <= DBL_EPSILON);
assert(std::fabs(hours_difference(1800.0, 1800.0) - 0.0) <= DBL_EPSILON);

assert(std::fabs(to_float_hours(0, 15, 0) - 0.25) <= DBL_EPSILON);
assert(std::fabs(to_float_hours(2, 45, 9) - 2.7525) <= DBL_EPSILON);
assert(std::fabs(to_float_hours(1, 0, 36) - 1.01) <= DBL_EPSILON);

assert(std::fabs(to_24_hour_clock(24) - 0.0) <= DBL_EPSILON);
assert(std::fabs(to_24_hour_clock(48) - 0.0) <= DBL_EPSILON);
assert(std::fabs(to_24_hour_clock(25) - 1.0) <= DBL_EPSILON);
assert(std::fabs(to_24_hour_clock(4) - 4.0) <= DBL_EPSILON);
assert(std::fabs(to_24_hour_clock(28.5) - 4.5) <= DBL_EPSILON);

assert(get_hours(3800) == 1);
assert(get_minutes(3800) == 3);
assert(get_seconds(3800) == 20);

assert(std::fabs(time_to_utc(+0, 12.0) - 12.0) <= DBL_EPSILON);
assert(std::fabs(time_to_utc(+1, 12.0) - 11.0) <= DBL_EPSILON);
assert(std::fabs(time_to_utc(-1, 12.0) - 13.0) <= DBL_EPSILON);
assert(std::fabs(time_to_utc(-11, 18.0) - 5.0) <= DBL_EPSILON);
assert(std::fabs(time_to_utc(-1, 0.0) - 1.0) <= DBL_EPSILON);
assert(std::fabs(time_to_utc(-1, 23.0) - 0.0) <= DBL_EPSILON);

assert(std::fabs(time_from_utc(+0, 12.0) - 12.0) <= DBL_EPSILON);
assert(std::fabs(time_from_utc(+1, 12.0) - 13.0) <= DBL_EPSILON);
assert(std::fabs(time_from_utc(-1, 12.0) - 11.0) <= DBL_EPSILON);
assert(std::fabs(time_from_utc(+6, 6.0) - 12.0) <= DBL_EPSILON);
assert(std::fabs(time_from_utc(-7, 6.0) - 23.0) <= DBL_EPSILON);
assert(std::fabs(time_from_utc(-1, 0.0) - 23.0) <= DBL_EPSILON);
assert(std::fabs(time_from_utc(-1, 23.0) - 22.0) <= DBL_EPSILON);
assert(std::fabs(time_from_utc(+1, 23.0) - 0.0) <= DBL_EPSILON);

std::cout << "Tests completed\n";
return 0;
}