Skip to content

Commit

Permalink
v1 more
Browse files Browse the repository at this point in the history
  • Loading branch information
ogroppo committed Jul 5, 2024
1 parent 5f9c598 commit 3571737
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 40 deletions.
37 changes: 7 additions & 30 deletions src/math/percentageChange.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,12 @@ import { percentageChange } from "./percentageChange";

describe("percentageChange", () => {
test("simple", async () => {
expect(
percentageChange({
current: 10,
previous: 12,
})
).toBe(-0.1667);
expect(
percentageChange({
current: 12,
previous: 10,
})
).toBe(0.2);
expect(
percentageChange({
current: 0,
previous: 12,
})
).toBe(0);
expect(
percentageChange({
current: 0,
previous: 0,
})
).toBe(0);
expect(
percentageChange({
current: 99,
previous: 0,
})
).toBe(0);
expect(percentageChange(-0.1, 0.2)).toBe(0);
expect(percentageChange(0.2, 0.1)).toBe(-0.5);
expect(percentageChange(0.1, 0.2)).toBe(1);
expect(percentageChange(0.3, 0.333)).toBe(0.11);
expect(percentageChange(0, 0.12)).toBe(0);
expect(percentageChange(0, 0)).toBe(0);
expect(percentageChange(0.99, 0)).toBe(0);
});
});
18 changes: 8 additions & 10 deletions src/math/percentageChange.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { isPositiveInt } from "../validators";

export const percentageChange = ({
previous,
current,
}: {
previous: number;
current: number;
}): number => {
if (!isPositiveInt(previous) || !isPositiveInt(current)) return 0;
/**
*
* @param previous Positive percentage i.e. 0.1 for 10%
* @param current Positive percentage i.e. 0.2 for 20%
* @returns
*/
export const percentageChange = (previous: number, current: number): number => {
if (previous < 0 || current < 0) return 0;
if (current === 0 && previous === 0) return 0;
if (current === 0 && previous !== 0) return -1;
if (current !== 0 && previous === 0) return 1;
Expand Down
25 changes: 25 additions & 0 deletions src/merginIntervals.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { describe, expect, test } from "@jest/globals";

describe("merginIntervals", () => {
const isPrime = (number: number) => {
if (number <= 1) return false;
if (number <= 3) return true;
return Array.from({ length: number - 2 }).every((_, index) => {
const divider = index + 2;
if (number % divider === 0) {
return false;
}
return true;
});
};

test("no arg", async () => {
expect(isPrime(0)).toBe(false);
// expect(isPrime(1)).toBe(false);
// expect(isPrime(2)).toBe(true);
// expect(isPrime(3)).toBe(true);
// expect(isPrime(23)).toBe(true);
// expect(isPrime(22)).toBe(false);
// expect(isPrime(21)).toBe(false);
});
});
35 changes: 35 additions & 0 deletions src/pal.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { describe, expect, test } from "@jest/globals";

describe("validPalindrome", () => {
function validPalindrome(s: string, MAX_DIFFS = 1): boolean {
const isPalindrome = (sub: string, depth = 0): boolean => {
if (depth > 0) console.log(sub);
let leftPointer = 0;
let rightPointer = sub.length - 1;

while (leftPointer < rightPointer) {
const leftChar = sub[leftPointer];
const rightChar = sub[rightPointer];
if (leftChar !== rightChar)
return depth < MAX_DIFFS
? isPalindrome(sub.slice(leftPointer, rightPointer), depth + 1) ||
isPalindrome(
sub.slice(leftPointer + 1, rightPointer + 1),
depth + 1
)
: false;

leftPointer++;
rightPointer--;
}

return true;
};

return isPalindrome(s);
}

test("no arg", async () => {
expect(validPalindrome("12", 0)).toBe(true);
});
});

0 comments on commit 3571737

Please sign in to comment.