Skip to content

Commit

Permalink
Merge pull request #46 from behzadam/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
behzadam authored Jul 20, 2024
2 parents 7b6d98b + 5480781 commit b0374b5
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 1 deletion.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,11 @@ This repository aims to provide a comprehensive collection of algorithms, data s

- [Find missing numbers](src/challange/array/find-missing-numbers/find-missing-numbers.ts)
- [Find max subarray](src/challange/array/find-max-subarray/find-max-subarray.ts)
- [Max profit](src/challange/array/max-profit/max-profit.ts)
- [Find majority element](src/challange/array/find-majority-element/find-majority-element.ts)
- [Find Largest And Smallest](src/challange/array/find-largest-and-smallest/find-largest-and-smallest.ts)
- [Max profit](src/challange/array/max-profit/max-profit.ts)
- [Move Zeroes](src/challange/array/move-zeroes/move-zeroes.ts)
- [Non-constructible Change](src/challange/array/non-constructible-change/non-constructible-change.ts)

Please note that the repository is still a work in progress, and new algorithms and patterns will be added over time.

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { findLargestAndSmallest } from "./find-largest-and-smallest";

describe("findLargestAndSmallest", () => {
it("returns undefined for largest and smallest when given an empty array", () => {
const result = findLargestAndSmallest([]);
expect(result.largest).toBeUndefined();
expect(result.smallest).toBeUndefined();
});

it("returns the correct largest and smallest values for an array with positive numbers", () => {
const result = findLargestAndSmallest([5, 2, 8, 1, 9]);
expect(result.largest).toBe(9);
expect(result.smallest).toBe(1);
});

it("returns the correct largest and smallest values for an array with negative numbers", () => {
const result = findLargestAndSmallest([-3, -7, -1, -5]);
expect(result.largest).toBe(-1);
expect(result.smallest).toBe(-7);
});

it("returns the correct largest and smallest values for an array with mixed positive and negative numbers", () => {
const result = findLargestAndSmallest([-2, 4, -6, 8, 0]);
expect(result.largest).toBe(8);
expect(result.smallest).toBe(-6);
});

it("returns the correct largest and smallest values for an array with duplicate numbers", () => {
const result = findLargestAndSmallest([3, 5, 3, 7, 5]);
expect(result.largest).toBe(7);
expect(result.smallest).toBe(3);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
export type LargestAndSmallest = {
largest: number | undefined;
smallest: number | undefined;
};

/**
* Given an array of numbers, returns the largest and smallest values from the given array.
* @param numbers - the given array
* @returns the largest and smallest values from the given array.
*/
export function findLargestAndSmallest(numbers: number[]): LargestAndSmallest {
if (numbers.length === 0) {
return {
largest: undefined,
smallest: undefined,
};
}

const largest = Math.max(...numbers);
const smallest = Math.min(...numbers);

return {
largest,
smallest,
};
}

/*
// Another way to solve this problem
function findLargestAndSmallest(numbers: number[]): {
largest: number;
smallest: number;
} {
if (numbers.length === 0) {
return {
largest: undefined,
smallest: undefined,
};
}
let largest: number = numbers[0];
let smallest: number = numbers[0];
for (let i = 1; i < numbers.length; i++) {
if (numbers[i] > largest) {
largest = numbers[i];
} else if (numbers[i] < smallest) {
smallest = numbers[i];
}
}
return {
largest,
smallest,
};
}**/
27 changes: 27 additions & 0 deletions src/challange/array/move-zeroes/move-zeroes.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { moveZeroes } from "./move-zeroes";

describe("moveZeroes", () => {
it("should move all zeroes to the end of the array", () => {
const input = [0, 1, 0, 3, 12];
const expected = [1, 3, 12, 0, 0];
expect(moveZeroes(input)).toEqual(expected);
});

it("handles an array with all zeroes", () => {
const input = [0, 0, 0];
const expected = [0, 0, 0];
expect(moveZeroes(input)).toEqual(expected);
});

it("handles an array with no zeroes", () => {
const input = [1, 2, 3];
const expected = [1, 2, 3];
expect(moveZeroes(input)).toEqual(expected);
});

it("handles an empty array", () => {
const input = [] as number[];
const expected = [] as number[];
expect(moveZeroes(input)).toEqual(expected);
});
});
20 changes: 20 additions & 0 deletions src/challange/array/move-zeroes/move-zeroes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Given an array nums, write a function to move all zeroes to the end of it while maintaining the relative order of the non-zero elements.
* @param nums - the given array
* @returns the modified array where all zeroes are moved to the end.
*/
export function moveZeroes(nums: number[]) {
if (nums.length === 0) return nums;

let start = 0;
for (let i = 0; i < nums.length; i++) {
if (nums[i] !== 0) {
let temp = nums[start];
nums[start] = nums[i];
nums[i] = temp;
start++;
}
}

return nums;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { nonConstructibleChange } from "./non-constructible-change";

describe("nonConstructibleChange", () => {
test.each([{ coins: [5, 7, 1, 1, 2, 3, 22], expected: 20 }])(
"returns $expected when conis are $coins",
({ coins, expected }) => {
const actual = nonConstructibleChange(coins);
expect(actual).toBe(expected);
}
);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* This function calculates the smallest non-constructible change given an array of coins.
* @param coins - the given array
* @returnn the smallest non-constructible change
*/
export function nonConstructibleChange(coins: number[]): number {
// Sort the coins in ascending order
coins.sort((a, b) => a - b);

// Keep track of the current amount of change we can create
let currentChange = 0;

// Iterate through the sorted coins
for (const coin of coins) {
// If the current coin is greater than the amount we can create + 1,
// then the smallest non-constructible change is currentChange + 1
if (coin > currentChange + 1) {
return currentChange + 1;
}

// Otherwise, add the current coin to the amount we can create
currentChange += coin;
}

// If we reach the end without finding a gap, the smallest non-constructible change
// is the sum of all coins + 1
return currentChange + 1;
}

0 comments on commit b0374b5

Please sign in to comment.