From 87058a4eff65f1369ee21330e7bce0ceb143549a Mon Sep 17 00:00:00 2001 From: Dennis Bauer Date: Sat, 7 Mar 2026 21:42:19 +0100 Subject: [PATCH 1/4] MAIN_COMMIT Upgraded the createNewProblem script --- .vscode/createNewProblem.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.vscode/createNewProblem.sh b/.vscode/createNewProblem.sh index 1efec7c..33259e7 100755 --- a/.vscode/createNewProblem.sh +++ b/.vscode/createNewProblem.sh @@ -31,7 +31,7 @@ cp -R "$SRC_DIR" "$DEST_DIR" # --- rename files inside the copied folder --- # 1) Rename a specific file if [[ -f "$DEST_DIR/template.test.ts" ]]; then - mv "$DEST_DIR/template.test.ts" "$DEST_DIR/${NAME}.test.ts" + mv "$DEST_DIR/template.test.ts" "$DEST_DIR/${NAME,,}.test.ts" fi From 6e3dc50a9d965d01a92f622cb7e84647ae7412c1 Mon Sep 17 00:00:00 2001 From: Dennis Bauer Date: Sat, 7 Mar 2026 21:49:12 +0100 Subject: [PATCH 2/4] Setup problem --- .../Day-Number-Of-Year.test.ts | 29 ++++++-- Problems/Day-Number-Of-Year/README.md | 26 ++++++++ Problems/Day-Number-Of-Year/solver.ts | 3 + biome.jsonc | 3 + package-lock.json | 4 +- package.json | 66 +++++++++---------- 6 files changed, 89 insertions(+), 42 deletions(-) create mode 100644 Problems/Day-Number-Of-Year/README.md create mode 100644 Problems/Day-Number-Of-Year/solver.ts diff --git a/Problems/Day-Number-Of-Year/Day-Number-Of-Year.test.ts b/Problems/Day-Number-Of-Year/Day-Number-Of-Year.test.ts index 14cb7c8..47e7c2c 100644 --- a/Problems/Day-Number-Of-Year/Day-Number-Of-Year.test.ts +++ b/Problems/Day-Number-Of-Year/Day-Number-Of-Year.test.ts @@ -1,13 +1,28 @@ import { describe, expect, it } from "vitest"; +import dayOfYear from "./solver"; -describe("Description for the tests", () => { - it("should return something for this input", () => { - expect(true).toEqual(true); +describe("Returns the correct day number of the year", () => { + it("should return 348 for 12/13/2020", () => { + expect(dayOfYear("12/13/2020")).toEqual(348); + }); + + it("should return 321 for 11/16/2020", () => { + expect(dayOfYear("11/16/2020")).toEqual(321); + }); + + it("should return 9 for 1/9/2019", () => { + expect(dayOfYear("1/9/2019")).toEqual(9); + }); + + it("should return 61 for 3/1/2004", () => { + expect(dayOfYear("3/1/2004")).toEqual(61); + }); + + it("should return 366 (leap) for 12/31/2000", () => { + expect(dayOfYear("12/31/2000")).toEqual(366); }); -}); -describe("Another description for the tests. Maybe those which are throwing errors", () => { - it("should return something for this input", () => { - expect(true).toEqual(true); + it("should return 365 for 12/31/2019", () => { + expect(dayOfYear("12/31/2019")).toEqual(365); }); }); diff --git a/Problems/Day-Number-Of-Year/README.md b/Problems/Day-Number-Of-Year/README.md new file mode 100644 index 0000000..0022a55 --- /dev/null +++ b/Problems/Day-Number-Of-Year/README.md @@ -0,0 +1,26 @@ +# Day-Number-Of-Year + +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/Day-Number-Of-Year/solver.ts b/Problems/Day-Number-Of-Year/solver.ts new file mode 100644 index 0000000..08b7ab5 --- /dev/null +++ b/Problems/Day-Number-Of-Year/solver.ts @@ -0,0 +1,3 @@ +export default function dayOfYear(date: string): number { + throw new Error("Not implemented!"); +} diff --git a/biome.jsonc b/biome.jsonc index cec14ed..f4c75de 100644 --- a/biome.jsonc +++ b/biome.jsonc @@ -11,6 +11,9 @@ "indentWidth": 2, "lineWidth": 120 }, + "files": { + "includes": ["**", "!!package.json", "!!package-lock.json"] + }, "linter": { "enabled": true, "rules": { diff --git a/package-lock.json b/package-lock.json index 6698f03..c4b58d1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "problem-solving", - "version": "CP-19", + "version": "CP-21", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "problem-solving", - "version": "CP-19", + "version": "CP-21", "license": "ISC", "dependencies": { "decimal.js": "^10.6.0", diff --git a/package.json b/package.json index b2fa8ea..bc496da 100644 --- a/package.json +++ b/package.json @@ -1,35 +1,35 @@ { - "name": "problem-solving", - "version": "CP-21", - "description": "This Package is all about solving coding problems", - "homepage": "https://github.com/Dennis-Bauer/Problem-Solving#readme", - "bugs": { - "url": "https://github.com/Dennis-Bauer/Problem-Solving/issues" - }, - "repository": { - "type": "git", - "url": "git+https://github.com/Dennis-Bauer/Problem-Solving.git" - }, - "license": "ISC", - "author": "Dennis Bauer", - "type": "commonjs", - "scripts": { - "test": "vitest run ./Problems", - "test:dev": "vitest ./Problems --reporter=verbose", - "test:file": "vitest", - "test:ci": "vitest --run --passWithNoTests --reporter=json > vitest-report.json", - "build": "tsc", - "normalize": "biome check --write --unsafe", - "prepare": "husky install" - }, - "dependencies": { - "decimal.js": "^10.6.0", - "husky": "^9.1.7", - "typescript": "^5.9.2", - "vitest": "^3.2.4" - }, - "devDependencies": { - "@biomejs/biome": "2.3.11", - "@types/node": "^24.3.0" - } + "name": "problem-solving", + "version": "CP-21", + "description": "This Package is all about solving coding problems", + "homepage": "https://github.com/Dennis-Bauer/Problem-Solving#readme", + "bugs": { + "url": "https://github.com/Dennis-Bauer/Problem-Solving/issues" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/Dennis-Bauer/Problem-Solving.git" + }, + "license": "ISC", + "author": "Dennis Bauer", + "type": "commonjs", + "scripts": { + "test": "vitest run ./Problems", + "test:dev": "vitest ./Problems --reporter=verbose", + "test:file": "vitest", + "test:ci": "vitest --run --passWithNoTests --reporter=json > vitest-report.json", + "build": "tsc", + "normalize": "biome check --write --unsafe", + "prepare": "husky install" + }, + "dependencies": { + "decimal.js": "^10.6.0", + "husky": "^9.1.7", + "typescript": "^5.9.2", + "vitest": "^3.2.4" + }, + "devDependencies": { + "@biomejs/biome": "2.3.11", + "@types/node": "^24.3.0" + } } From 85a34076f166ed32232023eaf5411a6825a5d0e8 Mon Sep 17 00:00:00 2001 From: Dennis Bauer Date: Sun, 8 Mar 2026 10:38:36 +0100 Subject: [PATCH 3/4] Solved the problem --- .../Day-Number-Of-Year.test.ts | 26 +++++++ Problems/Day-Number-Of-Year/solver.ts | 71 ++++++++++++++++++- 2 files changed, 96 insertions(+), 1 deletion(-) diff --git a/Problems/Day-Number-Of-Year/Day-Number-Of-Year.test.ts b/Problems/Day-Number-Of-Year/Day-Number-Of-Year.test.ts index 47e7c2c..c8a8d9b 100644 --- a/Problems/Day-Number-Of-Year/Day-Number-Of-Year.test.ts +++ b/Problems/Day-Number-Of-Year/Day-Number-Of-Year.test.ts @@ -25,4 +25,30 @@ describe("Returns the correct day number of the year", () => { it("should return 365 for 12/31/2019", () => { expect(dayOfYear("12/31/2019")).toEqual(365); }); + + it("should return 2 for 1/2/2019", () => { + expect(dayOfYear("1/2/2019")).toEqual(2); + }); +}); + +describe("Catches a wrong date formate", () => { + it("should throw a error for 14/13/2020", () => { + expect(() => dayOfYear("14/13/2020")).toThrowError("The Month 14 isn't a valid Month!"); + }); + + it("should throw a error for 11/31/2020", () => { + expect(() => dayOfYear("11/31/2020")).toThrowError("The 11 has only 30 days!"); + }); + + it("should throw a error for 1/40/2020", () => { + expect(() => dayOfYear("1/40/2020")).toThrowError("The day 40 does not exist in any Month!"); + }); + + it("should throw a error for 2/29/2019", () => { + expect(() => dayOfYear("2/29/2019")).toThrowError("February has only 28 days (No Leap)"); + }); + + it("should throw a error for 2/30/2020", () => { + expect(() => dayOfYear("2/30/2020")).toThrowError("February has only 29 days"); + }); }); diff --git a/Problems/Day-Number-Of-Year/solver.ts b/Problems/Day-Number-Of-Year/solver.ts index 08b7ab5..712ffdf 100644 --- a/Problems/Day-Number-Of-Year/solver.ts +++ b/Problems/Day-Number-Of-Year/solver.ts @@ -1,3 +1,72 @@ export default function dayOfYear(date: string): number { - throw new Error("Not implemented!"); + const [month, day, year] = date.split("/").map(Number); + + if (day <= 0 || day > 31) throw new Error(`The day ${day} does not exist in any Month!`); + + // Check if leap year + const leap = (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0; + switchMonth( + month, + () => {}, + () => { + if (day > 30) throw new Error(`The ${month} has only 30 days!`); + }, + () => { + if (!leap && day > 28) throw new Error("February has only 28 days (No Leap)"); + if (day > 29) throw new Error("February has only 29 days"); + } + ); + + let dayCount = day; + + for (let m = month - 1; m >= 1; m--) { + switchMonth( + m, + () => { + dayCount = dayCount + 31; + }, + () => { + dayCount = dayCount + 30; + }, + () => { + if (!leap) dayCount = dayCount + 28; + else dayCount = dayCount + 29; + } + ); + } + + return dayCount; +} + +function switchMonth( + month: number, + func31: (month?: number) => void, + func30: (month?: number) => void, + func2: (month?: number) => void +) { + switch (month) { + case 1: + case 3: + case 5: + case 7: + case 8: + case 10: + case 12: { + func31(month); + break; + } + case 4: + case 6: + case 9: + case 11: { + func30(month); + break; + } + case 2: { + func2(month); + break; + } + default: + throw new Error(`The Month ${month} isn't a valid Month!`); + } } From ce3ca512cff87be76d34b9194c218ab81f8ee8a4 Mon Sep 17 00:00:00 2001 From: Dennis Bauer Date: Sun, 8 Mar 2026 10:50:35 +0100 Subject: [PATCH 4/4] Finished the problem --- Problems/Day-Number-Of-Year/README.md | 40 +++++++++++++++++++++++---- README.md | 5 ++-- 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/Problems/Day-Number-Of-Year/README.md b/Problems/Day-Number-Of-Year/README.md index 0022a55..4a2283f 100644 --- a/Problems/Day-Number-Of-Year/README.md +++ b/Problems/Day-Number-Of-Year/README.md @@ -1,26 +1,54 @@ # Day-Number-Of-Year -The Problem text which is given +You’re given a date string in the format month/day/year, based on the Gregorian calendar. Your task is to return which day of the year that date corresponds to (1–365, or 1–366 for leap years). ## Infos which are maybe needed -Some Infos. This section can also just get deleted +`January`, `March`, `May`, `July`, `August`, `October`, `December` are the months which have 31 days. `April`, `June`, `September`, `November` are the months which have 30 days. February has 28 days and in a leap year it has 29 days. Leap year is a year which divides by 4 but not 100 unless also divisible by 400. ## Documentation ### Solution Idea -The Idea to solve this problem +The idea is to use a counter variable that counts the total number of days. + +The counter starts with the number of days that have passed in the current month. +Then, a loop iterates over every month before the current month and adds the number of days each of those months had. + +After the loop finishes, the counter contains the total number of days that have passed since the beginning of the year. --- ### [Implementation](./solver.ts) -The implementation for solving this problem +First, we check whether the given day is between 1 and 31. +After that, we determine whether the given year is a leap year. + +To do this, we check if the year is divisible by 4 but not by 100. +If this condition is not met, we additionally check whether the year is divisible by 400. +With these checks we can determine whether the given year is a leap year. + +Next, we use a switch case to verify that the given month has a valid number of days. +For example, if the month is January, the day must not be greater than 31. + +If the date is valid, we create a day counter that starts with the given day. + +Then we start a loop that runs from `month - 1` down to `1`. +For each month, we add the number of days that month has to the counter. + +This is determined as follows: + +- We keep a list of months that have 30 days. +- We keep another list of months that have 31 days. +- For February, we check whether the year is a leap year. + - If it is a leap year, we add 29 days. + - Otherwise, we add 28 days. + +At the end we return the day counter. --- -### Information's %Informations where the problem comes from. Most of them came from Sloth Byte% +### Information's 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. +[Post](https://slothbytes.beehiiv.com/p/being-wrong-is-your-job) from December 02, 2025. diff --git a/README.md b/README.md index ab41507..a016001 100644 --- a/README.md +++ b/README.md @@ -8,9 +8,9 @@ This repository is about solving coding-problems. It contains famous (or not tha ## Problems -Solved: **21 problems** +Solved: **22 problems** -🟒 Easy: 15 +🟒 Easy: 16 🟠 Medium: 5 πŸ”΄ Hard: 1 @@ -37,6 +37,7 @@ Solved: **21 problems** | [Sleep Time](./Problems/Sleep-Time/README.md) | 🟒 Easy | | [Reach Exit](./Problems/Reach-Exit/README.md) | 🟒 Easy | | [One Two Skip a Few](./Problems/One-Two-Skip-A-Few/README.md) | 🟒 Easy | +| [Day number of year](./Problems/Day-Number-Of-Year/README.md) | 🟒 Easy |