diff --git a/Problems/Nom-Nom-Numbers/README.md b/Problems/Nom-Nom-Numbers/README.md new file mode 100644 index 0000000..0026b15 --- /dev/null +++ b/Problems/Nom-Nom-Numbers/README.md @@ -0,0 +1,28 @@ +# Nom Nom Numbers + +A number can "eat" the number to its right if it’s larger than that number. +When it eats, it becomes the sum of both numbers. +Keep repeating this process from left to right until no more eating can happen. + +## Infos which are maybe needed + +Some Infos. This section can also just get deleted + +## Documentation + +### Solution Idea + +This is a really simple approach. +The base idea is to loop over the given array and check the current number with the previous number. If this previous number is bigger than the current number, we set the current number to the sum of current and previous, and also set the previous number to undefined which indicates that this number ate a other number. +We repeat the progress until the last number is reached. Now we return the array without undefined values and this will be the result. + +--- + +### [Implementation](./solver.ts) + +--- + +### 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/the-future-of-software-engineering-interviews) from October 28, 2025. diff --git a/Problems/Nom-Nom-Numbers/nom-nom-numbers.test.ts b/Problems/Nom-Nom-Numbers/nom-nom-numbers.test.ts new file mode 100644 index 0000000..9d8574e --- /dev/null +++ b/Problems/Nom-Nom-Numbers/nom-nom-numbers.test.ts @@ -0,0 +1,50 @@ +import { describe, expect, it } from "vitest"; +import nom_nom from "./solver"; + +describe("Returns the correct remaining numbers", () => { + it("should return [15] for [5, 3, 7] input", () => { + expect(nom_nom([5, 3, 7])).toEqual([15]); + }); + + it("should return [8, 9] for [5, 3, 7] input", () => { + expect(nom_nom([5, 3, 7])).toEqual([15]); + }); + + it("should return [1, 2, 3] for [1, 2, 3] input", () => { + expect(nom_nom([1, 2, 3])).toEqual([1, 2, 3]); + }); + + it("should return [3, 3] for [2, 1, 3] input", () => { + expect(nom_nom([2, 1, 3])).toEqual([3, 3]); + }); + + it("should return [22] for [8, 5, 9] input", () => { + expect(nom_nom([8, 5, 9])).toEqual([22]); + }); + + it("should return [17, 100] for [6, 5, 6, 100] input", () => { + expect(nom_nom([6, 5, 6, 100])).toEqual([17, 100]); + }); + + it("should return [84] for [82, 2] input", () => { + expect(nom_nom([82, 2])).toEqual([84]); + }); + + it("should return [2, 20, 80, 1] for [2, 20, 81] input", () => { + expect(nom_nom([2, 20, 80, 1])).toEqual([2, 20, 81]); + }); + + it("should return [] for [] input", () => { + expect(nom_nom([])).toEqual([]); + }); + + it("should return [2] for [2] input", () => { + expect(nom_nom([2])).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); +// }); +// }); diff --git a/Problems/Nom-Nom-Numbers/solver.ts b/Problems/Nom-Nom-Numbers/solver.ts new file mode 100644 index 0000000..28d7126 --- /dev/null +++ b/Problems/Nom-Nom-Numbers/solver.ts @@ -0,0 +1,14 @@ +export default function nom_nom(numbers: number[]): number[] { + if (numbers.length <= 1) return numbers; + + const result: (number | undefined)[] = numbers.slice(); + + for (let i = 1; i < result.length; i++) { + if ((result[i - 1] ?? 0) > (result[i] ?? 0)) { + result[i] = (result[i - 1] ?? 0) + (result[i] ?? 0); + result[i - 1] = undefined; + } + } + + return result.filter((r) => r !== undefined); +} diff --git a/README.md b/README.md index f0ece2f..1e54b04 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,7 @@ This repository is about solving coding-problems. It contains famous (or not tha - [Phone-Letter-Combinations](./Problems/Phone-Letter-Combinations/README.md) (Easy) - [Itinerary-In-Alphabetical-Order](./Problems/Itinerary-In-Alphabetical-Order/README.md) (Easy) - [Dependable-Jobs-Schedule](./Problems/Dependable-Jobs-Schedule/README.md) (Easy) +- [Nom-Nom-Numbers](./Problems/Nom-Nom-Numbers/README.md) (Easy)