From 1ec9c03f74891bd3cc8c14f8371de567474927bb Mon Sep 17 00:00:00 2001 From: Dennis Date: Sun, 25 Jan 2026 18:21:56 +0100 Subject: [PATCH 1/3] Setup problem --- Problems/Sleep-Time/README.md | 26 ++++++++++++++++++++ Problems/Sleep-Time/sleep-time.test.ts | 34 ++++++++++++++++++++++++++ Problems/Sleep-Time/solver.ts | 3 +++ 3 files changed, 63 insertions(+) create mode 100644 Problems/Sleep-Time/README.md create mode 100644 Problems/Sleep-Time/sleep-time.test.ts create mode 100644 Problems/Sleep-Time/solver.ts diff --git a/Problems/Sleep-Time/README.md b/Problems/Sleep-Time/README.md new file mode 100644 index 0000000..bf375e0 --- /dev/null +++ b/Problems/Sleep-Time/README.md @@ -0,0 +1,26 @@ +# Sleep-Time + +The Problem text which is given + +## Infos which are maybe needed + +Some Infos. This section can also just get deleted + +## Documentation + +### Solution Idea + +The Idea to solve this problem + +--- + +### [Implementation](./solver.ts) + +The implementation for solving this problem + +--- + +### Information's %Informations where the problem comes from. Most of them came from Sloth Byte% + +This problem comes from the newsletter [Sloth Bytes](https://slothbytes.beehiiv.com). +[Post](https://slothbytes.beehiiv.com/p/two-factor-codes) from August 26, 2025. diff --git a/Problems/Sleep-Time/sleep-time.test.ts b/Problems/Sleep-Time/sleep-time.test.ts new file mode 100644 index 0000000..921903f --- /dev/null +++ b/Problems/Sleep-Time/sleep-time.test.ts @@ -0,0 +1,34 @@ +import { describe, expect, it } from "vitest"; +import bedTime from "./solver"; + +describe("Returns always the correct time to go to sleep", () => { + it("should return '00:00' for start time '07:50' and duration '07:50'", () => { + expect(bedTime("07:50", "07:50")).toEqual("00:00"); + }); + + it("should return '20:15' for start time '06:15' and duration '10:00'", () => { + expect(bedTime("06:15", "10:00")).toEqual("20:15"); + }); + + it("should return '22:00' for start time '08:00' and duration '10:00'", () => { + expect(bedTime("22:00", "08:00")).toEqual("10:00"); + }); + + it("should return '23:30' for start time '09:30' and duration '10:00'", () => { + expect(bedTime("22:30", "09:30")).toEqual("10:00"); + }); + + it("should return '01:45' for start time '05:45' and duration '04:00'", () => { + expect(bedTime("01:45", "05:45")).toEqual("04:00"); + }); + + it("should return '02:40' for start time '07:10' and duration '04:30'", () => { + expect(bedTime("02:40", "07:10")).toEqual("04:30"); + }); +}); + +// describe("Another description for the tests. Maybe those which are throwing errors", () => { +// it("should return something for this input", () => { +// expect(true).toEqual(true); +// }); +// }); diff --git a/Problems/Sleep-Time/solver.ts b/Problems/Sleep-Time/solver.ts new file mode 100644 index 0000000..08ab1e9 --- /dev/null +++ b/Problems/Sleep-Time/solver.ts @@ -0,0 +1,3 @@ +export default function bedTime(wakeTime: string, sleepTime: string): string { + throw new Error("Solution yet not implemented!"); +} From 2ddeb0ff11d2bf9ac6a14b08f2036a7bb9363f6a Mon Sep 17 00:00:00 2001 From: Dennis Date: Sun, 25 Jan 2026 18:51:52 +0100 Subject: [PATCH 2/3] Included error tests --- Problems/Sleep-Time/sleep-time.test.ts | 22 ++++++++++-------- Problems/Sleep-Time/solver.ts | 32 +++++++++++++++++++++++++- 2 files changed, 44 insertions(+), 10 deletions(-) diff --git a/Problems/Sleep-Time/sleep-time.test.ts b/Problems/Sleep-Time/sleep-time.test.ts index 921903f..a7e24fe 100644 --- a/Problems/Sleep-Time/sleep-time.test.ts +++ b/Problems/Sleep-Time/sleep-time.test.ts @@ -11,24 +11,28 @@ describe("Returns always the correct time to go to sleep", () => { }); it("should return '22:00' for start time '08:00' and duration '10:00'", () => { - expect(bedTime("22:00", "08:00")).toEqual("10:00"); + expect(bedTime("08:00", "10:00")).toEqual("22:00"); }); it("should return '23:30' for start time '09:30' and duration '10:00'", () => { - expect(bedTime("22:30", "09:30")).toEqual("10:00"); + expect(bedTime("09:30", "10:00")).toEqual("23:30"); }); it("should return '01:45' for start time '05:45' and duration '04:00'", () => { - expect(bedTime("01:45", "05:45")).toEqual("04:00"); + expect(bedTime("05:45", "04:00")).toEqual("01:45"); }); it("should return '02:40' for start time '07:10' and duration '04:30'", () => { - expect(bedTime("02:40", "07:10")).toEqual("04:30"); + expect(bedTime("07:10", "04:30")).toEqual("02:40"); }); }); -// describe("Another description for the tests. Maybe those which are throwing errors", () => { -// it("should return something for this input", () => { -// expect(true).toEqual(true); -// }); -// }); +describe("Throws the correct error when having the wrong time format", () => { + it("should throw an Error because the hour is bigger than allowed", () => { + expect(() => bedTime("26:23", "00:00")).toThrowError("The given times haven't the correct format!"); + }); + + it("should throw an Error because the minute is bigger than allowed", () => { + expect(() => bedTime("00:59", "00:80")).toThrowError("The given times haven't the correct format!"); + }); +}); diff --git a/Problems/Sleep-Time/solver.ts b/Problems/Sleep-Time/solver.ts index 08ab1e9..25b49bc 100644 --- a/Problems/Sleep-Time/solver.ts +++ b/Problems/Sleep-Time/solver.ts @@ -1,3 +1,33 @@ +const checkBoundaries = (time: string) => { + const hour = Number.parseInt(time.split(":")[0], 10); + const minutes = Number.parseInt(time.split(":")[1], 10); + + if (hour > 24 || minutes > 59 || hour < 0 || minutes < 0) + throw new Error("The given times haven't the correct format!"); +}; + export default function bedTime(wakeTime: string, sleepTime: string): string { - throw new Error("Solution yet not implemented!"); + checkBoundaries(wakeTime); + checkBoundaries(sleepTime); + + const endTimeHour = Number.parseInt(wakeTime.split(":")[0], 10); + const endTimeMinutes = Number.parseInt(wakeTime.split(":")[1], 10); + + const durationTimeHour = Number.parseInt(sleepTime.split(":")[0], 10); + const durationTimeMinutes = Number.parseInt(sleepTime.split(":")[1], 10); + + let startTimeHours = endTimeHour - durationTimeHour; + let startTimeMinutes = endTimeMinutes - durationTimeMinutes; + + if (startTimeMinutes < 0) { + startTimeMinutes = 60 + startTimeMinutes; + + startTimeHours--; + } + + if (startTimeHours < 0) { + startTimeHours = 24 + startTimeHours; + } + + return `${startTimeHours < 9 ? `0${startTimeHours}` : startTimeHours}:${startTimeMinutes < 9 ? `0${startTimeMinutes}` : startTimeMinutes}`; } From 4eb4bffcc08d520d692ad04f81b377cc55cfc0d3 Mon Sep 17 00:00:00 2001 From: Dennis Date: Sun, 25 Jan 2026 18:58:09 +0100 Subject: [PATCH 3/3] Updated readme --- Problems/Sleep-Time/README.md | 14 ++++++-------- README.md | 1 + 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/Problems/Sleep-Time/README.md b/Problems/Sleep-Time/README.md index bf375e0..383b6cf 100644 --- a/Problems/Sleep-Time/README.md +++ b/Problems/Sleep-Time/README.md @@ -1,23 +1,21 @@ -# Sleep-Time +# Sleep Time -The Problem text which is given +You're setting an alarm for the next morning and know how long you want to sleep. The job is to figure out what time you need to go to bed to get that amount of sleep. -## Infos which are maybe needed - -Some Infos. This section can also just get deleted +Return the time when you should go to bed in the 24-hour-format ("HH:MM") ## Documentation ### Solution Idea -The Idea to solve this problem +The Idea is to subtract the duration you want to sleep from the time you want to wake up. If you now don't have any negativ values, this is the solution. +If any of the values are negativ, start with the minutes. We now add the negativ minute value from 60 and have with that the minute when we need to go to bed. If we did this we also need to subtract one from the hour value. +Now we check if the hour value is negativ. If yes, we add this value to 24. These values are now our solution. --- ### [Implementation](./solver.ts) -The implementation for solving this problem - --- ### Information's %Informations where the problem comes from. Most of them came from Sloth Byte% diff --git a/README.md b/README.md index a565583..632d7dd 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,7 @@ This repository is about solving coding-problems. It contains famous (or not tha - [Dependable-Jobs-Schedule](./Problems/Dependable-Jobs-Schedule/README.md) (Easy) - [Nom-Nom-Numbers](./Problems/Nom-Nom-Numbers/README.md) (Easy) - [Shortes-Path-Dijkstra](./Problems/Shortes-Path-Dijkstra/README.md) Medium +- [Sleep-Time](./Problems/Sleep-Time/README.md) (Easy)