generated from nhcarrigan/template
Port in some older solutions I've written just to get the structure established and in place.
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
/**
|
||||
* @copyright NHCarrigan
|
||||
* @license Naomi's Public License
|
||||
* @author Naomi Carrigan
|
||||
*/
|
||||
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { mineColor } from "./main.js";
|
||||
|
||||
describe("mineColor", () => {
|
||||
it("should return white if the square is white", () => {
|
||||
expect(mineColor("a", 8)).toBe("white");
|
||||
expect(mineColor("f", 5)).toBe("white");
|
||||
});
|
||||
|
||||
it("should return black if the square is black", () => {
|
||||
expect(mineColor("b", 2)).toBe("black");
|
||||
});
|
||||
|
||||
it("should handle empty strings", () => {
|
||||
expect(mineColor("", 1)).toBe("white");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,28 @@
|
||||
/**
|
||||
* @copyright NHCarrigan
|
||||
* @license Naomi's Public License
|
||||
* @author Naomi Carrigan
|
||||
*/
|
||||
|
||||
/**
|
||||
* Determines the color of a square on a chessboard based on its column and row.
|
||||
* @param column - The column of the square, represented as a letter ('a'-'h').
|
||||
* @param row - The row of the square, represented as a number (1-8).
|
||||
* @returns The color of the square, either "white" or "black".
|
||||
* @see https://www.codewars.com/kata/563319974612f4fa3f0000e0
|
||||
*/
|
||||
const mineColor = (column: string, row: number): string => {
|
||||
// Convert column letter ('a'-'h') to a number (1-8)
|
||||
const columnNumber
|
||||
= (column.toLowerCase().codePointAt(0) ?? 0)
|
||||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion, stylistic/no-mixed-operators -- "a" clearly has a code point at index 0. We're mixing operators because this is an old solution.
|
||||
- ("a".codePointAt(0)!) + 1;
|
||||
|
||||
// If the sum of column number and row is even, it's white
|
||||
if ((columnNumber + row) % 2 !== 0) {
|
||||
return "white";
|
||||
}
|
||||
return "black";
|
||||
};
|
||||
|
||||
export { mineColor };
|
||||
@@ -0,0 +1,24 @@
|
||||
/**
|
||||
* @copyright NHCarrigan
|
||||
* @license Naomi's Public License
|
||||
* @author Naomi Carrigan
|
||||
*/
|
||||
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { convert } from "./main.js";
|
||||
|
||||
describe("convert", () => {
|
||||
it("should return the correct heading", () => {
|
||||
expect(convert("# My level 1 heading")).toBe("<h1>My level 1 heading</h1>");
|
||||
expect(convert("My heading")).toBe("Invalid format");
|
||||
expect(convert("##### My level 5 heading")).toBe(
|
||||
"<h5>My level 5 heading</h5>",
|
||||
);
|
||||
expect(convert("#My heading")).toBe("Invalid format");
|
||||
expect(convert(" ### My level 3 heading")).toBe(
|
||||
"<h3>My level 3 heading</h3>",
|
||||
);
|
||||
expect(convert("####### My level 7 heading")).toBe("Invalid format");
|
||||
expect(convert("## My #2 heading")).toBe("<h2>My #2 heading</h2>");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,22 @@
|
||||
/**
|
||||
* @copyright NHCarrigan
|
||||
* @license Naomi's Public License
|
||||
* @author Naomi Carrigan
|
||||
*/
|
||||
|
||||
/**
|
||||
* Given a string representing a Markdown heading, return the equivalent HTML heading.
|
||||
* @param heading - The Markdown heading to convert.
|
||||
* @returns The equivalent HTML heading.
|
||||
* @see https://www.freecodecamp.org/learn/daily-coding-challenge/2025-11-19
|
||||
*/
|
||||
const convert = (heading: string): string => {
|
||||
if (!/^\s*#{1,6}\s+/.test(heading)) {
|
||||
return "Invalid format";
|
||||
}
|
||||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- We know the regex will match and the group will be present.
|
||||
const level = /^\s*(?<level>#{1,6})\s+/.exec(heading)!.groups!.level!.length;
|
||||
return `<h${level.toString()}>${heading.replace(/^\s*#{1,6}\s+/, "").trim()}</h${level.toString()}>`;
|
||||
};
|
||||
|
||||
export { convert };
|
||||
@@ -0,0 +1,19 @@
|
||||
/**
|
||||
* @copyright NHCarrigan
|
||||
* @license Naomi's Public License
|
||||
* @author Naomi Carrigan
|
||||
*/
|
||||
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { backspaceCompare } from "./main.js";
|
||||
|
||||
describe("backspaceCompare", () => {
|
||||
it("should return true if the strings are equal", () => {
|
||||
expect(backspaceCompare("ab#c", "ad#c")).toBe(true);
|
||||
expect(backspaceCompare("ab##", "c#d#")).toBe(true);
|
||||
});
|
||||
|
||||
it("should return false if the strings are not equal", () => {
|
||||
expect(backspaceCompare("ab#c", "b")).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,53 @@
|
||||
/**
|
||||
* @copyright NHCarrigan
|
||||
* @license Naomi's Public License
|
||||
* @author Naomi Carrigan
|
||||
*/
|
||||
|
||||
/**
|
||||
* Compares two strings to see if they are equal when both are typed into empty text editors.
|
||||
* '#' means a backspace character.
|
||||
* @param s - The first string to compare.
|
||||
* @param t - The second string to compare.
|
||||
* @returns True if the strings are equal, false otherwise.
|
||||
* @see https://leetcode.com/problems/backspace-string-compare/description/
|
||||
*/
|
||||
const backspaceCompare = (s: string, t: string): boolean => {
|
||||
let sIndex = s.length - 1;
|
||||
let tIndex = t.length - 1;
|
||||
let sSkips = 0;
|
||||
let tSkips = 0;
|
||||
|
||||
while (sIndex >= 0 || tIndex >= 0) {
|
||||
while (sIndex >= 0) {
|
||||
if (s[sIndex] === "#") {
|
||||
sSkips = sSkips + 1;
|
||||
sIndex = sIndex - 1;
|
||||
} else if (sSkips > 0) {
|
||||
sSkips = sSkips - 1;
|
||||
sIndex = sIndex - 1;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
while (tIndex >= 0) {
|
||||
if (t[tIndex] === "#") {
|
||||
tSkips = tSkips + 1;
|
||||
tIndex = tIndex - 1;
|
||||
} else if (tSkips > 0) {
|
||||
tSkips = tSkips - 1;
|
||||
tIndex = tIndex - 1;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (s[sIndex] !== t[tIndex]) {
|
||||
return false;
|
||||
}
|
||||
sIndex = sIndex - 1;
|
||||
tIndex = tIndex - 1;
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
export { backspaceCompare };
|
||||
Reference in New Issue
Block a user