Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion .vscode/createNewProblem.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
53 changes: 47 additions & 6 deletions Problems/Day-Number-Of-Year/Day-Number-Of-Year.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,54 @@
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);
});

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("Another description for the tests. Maybe those which are throwing errors", () => {
it("should return something for this input", () => {
expect(true).toEqual(true);
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");
});
});
54 changes: 54 additions & 0 deletions Problems/Day-Number-Of-Year/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Day-Number-Of-Year

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

`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 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)

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

This problem comes from the newsletter [Sloth Bytes](https://slothbytes.beehiiv.com).
[Post](https://slothbytes.beehiiv.com/p/being-wrong-is-your-job) from December 02, 2025.
72 changes: 72 additions & 0 deletions Problems/Day-Number-Of-Year/solver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
export default function dayOfYear(date: string): number {
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!`);
}
}
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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 |

<!-- PROBLEMS_END -->

Expand Down
3 changes: 3 additions & 0 deletions biome.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
"indentWidth": 2,
"lineWidth": 120
},
"files": {
"includes": ["**", "!!package.json", "!!package-lock.json"]
},
"linter": {
"enabled": true,
"rules": {
Expand Down
Loading