-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from behzadam/develop
Develop
- Loading branch information
Showing
7 changed files
with
179 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/challange/array/find-largest-and-smallest/find-largest-and-smallest.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
56 changes: 56 additions & 0 deletions
56
src/challange/array/find-largest-and-smallest/find-largest-and-smallest.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
}**/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
11 changes: 11 additions & 0 deletions
11
src/challange/array/non-constructible-change/non-constructible-change.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
); | ||
}); |
28 changes: 28 additions & 0 deletions
28
src/challange/array/non-constructible-change/non-constructible-change.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |