diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..8df737a2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,421 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. +## +## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore + +# User-specific files +*.rsuser +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Mono auto generated files +mono_crash.* + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +[Aa][Rr][Mm]/ +[Aa][Rr][Mm]64/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ + +# Visual Studio 2015/2017 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# Visual Studio 2017 auto generated files +Generated\ Files/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUNIT +*.VisualState.xml +TestResult.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# Benchmark Results +BenchmarkDotNet.Artifacts/ + +# .NET Core +project.lock.json +project.fragment.lock.json +artifacts/ + +# StyleCop +StyleCopReport.xml + +# Files built by Visual Studio +*_i.c +*_p.c +*_h.h +*.ilk +*.meta +*.obj +*.iobj +*.pch +*.pdb +*.ipdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*_wpftmp.csproj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# Visual Studio Trace Files +*.e2e + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# JustCode is a .NET coding add-in +.JustCode + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# AxoCover is a Code Coverage Tool +.axoCover/* +!.axoCover/settings.json + +# Visual Studio code coverage results +*.coverage +*.coveragexml + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# Note: Comment the next line if you want to checkin your web deploy settings, +# but database connection strings (with potential passwords) will be unencrypted +*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# The packages folder can be ignored because of Package Restore +**/[Pp]ackages/* +# except build/, which is used as an MSBuild target. +!**/[Pp]ackages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/[Pp]ackages/repositories.config +# NuGet v3's project.json files produces more ignorable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt +*.appx +*.appxbundle +*.appxupload + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!?*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +orleans.codegen.cs + +# Including strong name files can present a security risk +# (https://github.com/github/gitignore/pull/2483#issue-259490424) +#*.snk + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm +ServiceFabricBackup/ +*.rptproj.bak + +# SQL Server files +*.mdf +*.ldf +*.ndf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings +*.rptproj.rsuser +*- Backup*.rdl + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat +node_modules/ + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) +*.vbw + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# CodeRush personal settings +.cr/personal + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc + +# Cake - Uncomment if you are using it +# tools/** +# !tools/packages.config + +# Tabs Studio +*.tss + +# Telerik's JustMock configuration file +*.jmconfig + +# BizTalk build output +*.btp.cs +*.btm.cs +*.odx.cs +*.xsd.cs + +# OpenCover UI analysis results +OpenCover/ + +# Azure Stream Analytics local run output +ASALocalRun/ + +# MSBuild Binary and Structured Log +*.binlog + +# NVidia Nsight GPU debugger configuration file +*.nvuser + +# MFractors (Xamarin productivity tool) working folder +.mfractor/ + +# Local History for Visual Studio +.localhistory/ + +# BeatPulse healthcheck temp database +healthchecksdb + +# Backup folder for Package Reference Convert tool in Visual Studio 2017 +MigrationBackup/ + + + +## VS CODE +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json + + +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/**/usage.statistics.xml +.idea/**/dictionaries +.idea/**/shelf + +# Generated files +.idea/**/contentModel.xml + +# Sensitive or high-churn files +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml +.idea/**/dbnavigator.xml + +# Gradle +.idea/**/gradle.xml +.idea/**/libraries + +# Gradle and Maven with auto-import +# When using Gradle or Maven with auto-import, you should exclude module files, +# since they will be recreated, and may cause churn. Uncomment if using +# auto-import. +# .idea/modules.xml +# .idea/*.iml +# .idea/modules + +# CMake +cmake-build-*/ + +# Mongo Explorer plugin +.idea/**/mongoSettings.xml + +# File-based project format +*.iws + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +# Editor-based Rest Client +.idea/httpRequests + +# Android studio 3.1+ serialized cache file +.idea/caches/build_file_checksums.ser \ No newline at end of file diff --git a/extra-task-1.cpp b/extra-task-1.cpp index 6c8c1612..2bf39cc5 100644 --- a/extra-task-1.cpp +++ b/extra-task-1.cpp @@ -1,169 +1,59 @@ 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 - */ + // Returns the number of seconds later that time_2 is than time_1 + return time_2 - 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 - */ + // Returns the number of hours later that time_2 is than time_1 + return (time_2 - time_1) / 3600.0; } 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 - */ + // Returns the total number of hours in the specified hours, minutes, and seconds + // Precondition: 0 <= minutes < 60 and 0 <= seconds < 60 + return hours + minutes / 60.0 + seconds / 3600.0; } 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 to work - with integer and fractional part of a hours separately. - - */ + // Returns the hour as seen on a 24-hour clock + // Precondition: hours >= 0 + return fmod(hours, 24.0); } -/* - 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 +// Returns the hours part of a time in seconds +int get_hours(int seconds) +{ + return seconds / 3600; +} - >>> get_seconds(3800) - 20 +// Returns the minutes part of a time in seconds +int get_minutes(int seconds) +{ + return (seconds % 3600) / 60; +} - In other words, if 3800 seconds have elapsed since midnight, - it is currently 01:03:20 (hh:mm:ss). -*/ +// Returns the seconds part of a time in seconds +int get_seconds(int seconds) +{ + return seconds % 60; +} 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 - */ + // Returns time at UTC+0, given a local time and UTC offset + double utc_time = fmod(time - utc_offset, 24.0); + if (utc_time < 0) utc_time += 24.0; + return 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 - */ + // Returns local time in time zone utc_offset, given a UTC time + double local_time = fmod(time + utc_offset, 24.0); + if (local_time < 0) local_time += 24.0; + return local_time; } diff --git a/extra-task-1.h b/extra-task-1.h new file mode 100644 index 00000000..8cb70dd6 --- /dev/null +++ b/extra-task-1.h @@ -0,0 +1,36 @@ +#pragma once + +#include +#include +#include + +using namespace std; + +// Returns the number of seconds later that time_2 is than time_1 +double seconds_difference(double time_1, double time_2); + +// Returns the number of hours later that time_2 is than time_1 +double hours_difference(double time_1, double time_2); + +// Returns the total number of hours in the specified hours, minutes, and seconds +// Precondition: 0 <= minutes < 60 and 0 <= seconds < 60 +double to_float_hours(int hours, int minutes, int seconds); + +// Returns the hour as seen on a 24-hour clock +// Precondition: hours >= 0 +double to_24_hour_clock(double hours); + +// Returns the hours part of a time in seconds +int get_hours(int seconds); + +// Returns the minutes part of a time in seconds +int get_minutes(int seconds); + +// Returns the seconds part of a time in seconds +int get_seconds(int seconds); + +// Returns time at UTC+0, given a local time and UTC offset +double time_to_utc(int utc_offset, double time); + +// Returns local time in time zone utc_offset, given a UTC time +double time_from_utc(int utc_offset, double time); \ No newline at end of file diff --git a/main.cpp b/main.cpp new file mode 100644 index 00000000..8d1fd547 --- /dev/null +++ b/main.cpp @@ -0,0 +1,51 @@ +#include "extra-task-1.h" + +int main() { + // seconds_difference tests + assert(seconds_difference(1800.0, 3600.0) == 1800.0); + assert(seconds_difference(3600.0, 1800.0) == -1800.0); + assert(seconds_difference(1800.0, 1800.0) == 0.0); + + // hours_difference tests + assert(fabs(hours_difference(1800.0, 3600.0) - 0.5) < 1e-9); + assert(fabs(hours_difference(3600.0, 1800.0) + 0.5) < 1e-9); + assert(fabs(hours_difference(1800.0, 2160.0) - 0.1) < 1e-9); + + // to_float_hours tests + assert(fabs(to_float_hours(0, 15, 0) - 0.25) < 1e-9); + assert(fabs(to_float_hours(2, 45, 9) - 2.7525) < 1e-9); + assert(fabs(to_float_hours(1, 0, 36) - 1.01) < 1e-9); + + // to_24_hour_clock tests + assert(fabs(to_24_hour_clock(24) - 0.0) < 1e-9); + assert(fabs(to_24_hour_clock(25) - 1.0) < 1e-9); + assert(fabs(to_24_hour_clock(28.5) - 4.5) < 1e-9); + + // get_hours tests + assert(get_hours(3800) == 1); + assert(get_hours(7200) == 2); + assert(get_hours(59) == 0); + + // get_minutes tests + assert(get_minutes(3800) == 3); + assert(get_minutes(3661) == 1); + assert(get_minutes(59) == 0); + + // get_seconds tests + assert(get_seconds(3800) == 20); + assert(get_seconds(3661) == 1); + assert(get_seconds(59) == 59); + + // time_to_utc tests + assert(fabs(time_to_utc(+0, 12.0) - 12.0) < 1e-9); + assert(fabs(time_to_utc(+1, 12.0) - 11.0) < 1e-9); + assert(fabs(time_to_utc(-1, 12.0) - 13.0) < 1e-9); + + // time_from_utc tests + assert(fabs(time_from_utc(+0, 12.0) - 12.0) < 1e-9); + assert(fabs(time_from_utc(+1, 12.0) - 13.0) < 1e-9); + assert(fabs(time_from_utc(-1, 12.0) - 11.0) < 1e-9); + + cout << "All tests passed successfully!" << endl; + return 0; +} \ No newline at end of file