diff --git a/Problems/Vertical-List/README.md b/Problems/Vertical-List/README.md new file mode 100644 index 0000000..3171a17 --- /dev/null +++ b/Problems/Vertical-List/README.md @@ -0,0 +1,30 @@ +# Vertical-List + +Create a function that converts a string into a matrix of characters that can be read vertically. Add spaces when characters are missing. + +## Documentation + +### Solution Idea & [Implementation](./solver.ts) + +The idea is to loop over every word in the text and process each character separately. + +For every character, we first check whether an array already exists in the solution array at the index of the character's position in the word. +If not, we create a new array there. + +Then we place the character into the solution array at this position: + +\[character position\]\[word index\] + +This correctly rearranges the characters, but the spaces for shorter words are still missing. + +To fix this, whenever the word index is greater than `0`, we check whether the previous character position exists for the previous word. +If it does not exist, we know that the previous word was shorter, so we insert an empty space at that position. + +By doing this for every word and every character, the solution array is built correctly and the final transformed text can be returned. + +--- + +### Information's + +This problem comes from the newsletter [Sloth Bytes](https://slothbytes.beehiiv.com). +[Post](https://slothbytes.beehiiv.com/p/why-your-delete-button-is-a-lie) from May 18, 2026. diff --git a/Problems/Vertical-List/solver.ts b/Problems/Vertical-List/solver.ts new file mode 100644 index 0000000..bce5a72 --- /dev/null +++ b/Problems/Vertical-List/solver.ts @@ -0,0 +1,17 @@ +export default function verticalTxt(text: string): string[][] { + const textArray = text.split(" "); + const solution: string[][] = []; + + textArray.forEach((word, wordNumber) => { + word.split("").forEach((char, charNumber) => { + if (solution[charNumber] === undefined) solution[charNumber] = []; + + if (wordNumber > 0 && solution[charNumber][wordNumber - 1] === undefined) + solution[charNumber][wordNumber - 1] = " "; + + solution[charNumber][wordNumber] = char; + }); + }); + + return solution; +} diff --git a/Problems/Vertical-List/vertical-list.test.ts b/Problems/Vertical-List/vertical-list.test.ts new file mode 100644 index 0000000..755d2bf --- /dev/null +++ b/Problems/Vertical-List/vertical-list.test.ts @@ -0,0 +1,38 @@ +import { describe, expect, it } from "vitest"; +import verticalTxt from "./solver"; + +describe("Description for the tests", () => { + it("should return a 2x7 Matrix for 'Holy bananas'", () => { + expect(verticalTxt("Holy bananas")).toEqual([ + ["H", "b"], + ["o", "a"], + ["l", "n"], + ["y", "a"], + [" ", "n"], + [" ", "a"], + [" ", "s"], + ]); + }); + + it("should return a 2x6 Matrix for 'Hello fellas'", () => { + expect(verticalTxt("Hello fellas")).toEqual([ + ["H", "f"], + ["e", "e"], + ["l", "l"], + ["l", "l"], + ["o", "a"], + [" ", "s"], + ]); + }); + + it("should return a 4x6 Matrix for 'Hello fellas I'm Dennis'", () => { + expect(verticalTxt("Hello fellas I'm Dennis")).toEqual([ + ["H", "f", "I", "D"], + ["e", "e", "'", "e"], + ["l", "l", "m", "n"], + ["l", "l", " ", "n"], + ["o", "a", " ", "i"], + [" ", "s", " ", "s"], + ]); + }); +}); diff --git a/README.md b/README.md index 0901ee3..04aebcc 100644 --- a/README.md +++ b/README.md @@ -2,15 +2,13 @@ This repository is about solving coding-problems. It contains famous (or not that famous) problems with my solution (Dennis Bauer). I ranked the problems as 🟢 Easy, 🟠 Medium and 🔴 Hard. These ranks reflect the time I invested in the problem and how difficult it felt to me. -# Problem-list - ## Problems -Solved: **26 problems** +Solved: **27 problems** -🟢 Easy: 20 +🟢 Easy: 21 🟠 Medium: 5 🔴 Hard: 1 @@ -42,10 +40,11 @@ Solved: **26 problems** | [Bingo Check](./Problems/Bingo-Check/README.md) | 🟢 Easy | | [Missing Number](./Problems/Missing-Number/README.md) | 🟢 Easy | | [Seven Boom!](./Problems/Seven-Boom/README.md) | 🟢 Easy | +| [Vertical List](./Problems/Vertical-List/README.md) | 🟢 Easy | -## npm-Version: +## npm-Version The version indicates how much problems are implemented (cp -> Coding Problem) diff --git a/package-lock.json b/package-lock.json index 9c8d4bc..3b4f3b3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "problem-solving", - "version": "CP-23", + "version": "CP-26", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "problem-solving", - "version": "CP-23", + "version": "CP-26", "license": "ISC", "dependencies": { "decimal.js": "^10.6.0",