Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module.exports = {
env: {
commonjs: true,
es2021: true,
node: true,
},
extends: "eslint:recommended",
parserOptions: {
ecmaVersion: "latest",
},
rules: {},
globals: {
describe: true,
it: true,
expect: true,
test: true,
beforeEach: true,
afterEach: true,
beforeAll: true,
afterAll: true,
},
};
17,137 changes: 17,088 additions & 49 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
},
"devDependencies": {
"@babel/core": "7.12.0",
"eslint": "^8.14.0",
"jest": "^26.6.3"
},
"resolutions": {
Expand Down
24 changes: 24 additions & 0 deletions specs/bubble-sort/bubble-sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import unittest


def bubble_sort(nums):
swapped = True
while swapped:
swapped = False
for i in range(0, len(nums) - 1):
temp = nums[i]
if nums[i] > nums[i+1]:
nums[i] = nums[i+1]
nums[i+1] = temp
swapped = True
return nums

class TestBubbleSort(unittest.TestCase):
def test_bubble_sort(self):
nums = [10, 5, 3, 8, 2, 6, 4, 7, 9, 1]
sorted_nums = bubble_sort(nums)
self.assertEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], sorted_nums)


if __name__ == '__main__':
unittest.main()
17 changes: 16 additions & 1 deletion specs/bubble-sort/bubble-sort.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,26 @@

function bubbleSort(nums) {
// code goes here
let swapped = true;
let iterations = 0;
while (swapped) {
swapped = false;
for (let index = 0; index < nums.length - iterations; index++) {
const num = nums[index];
if (num > nums[index + 1]) {
nums[index] = nums[index + 1];
nums[index + 1] = num;
swapped = true;
}
}
iterations++;
}
return nums;
}

// unit tests
// do not modify the below code
test.skip("bubble sort", function () {
test("bubble sort", function () {
const nums = [10, 5, 3, 8, 2, 6, 4, 7, 9, 1];
const sortedNums = bubbleSort(nums);
expect(sortedNums).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
Expand Down
25 changes: 25 additions & 0 deletions specs/insertion-sort/insertion-sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from typing import List
import unittest


def insertion_sort(nums: List):
for i in range(1, len(nums)):
numberToInsert = nums[i]
for j in range(i, -1, -1):
if nums[j] < nums[j - 1] and j > 0:
temp = nums[j-1]
nums[j - 1] = nums[j]
nums[j] = temp
else:
break


class TestInsertionSort(unittest.TestCase):
def test_insertion_sort(self):
nums = [10, 5, 3, 8, 2, 6, 4, 7, 9, 1]
insertion_sort(nums)
self.assertEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], nums)


if __name__ == '__main__':
unittest.main()
29 changes: 27 additions & 2 deletions specs/insertion-sort/insertion-sort.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,39 @@
And you put xdescribe instead of describe if you want to suspend running the unit tests.
*/

function insertionSort(nums) {
function insertionSort(nums = []) {
// code goes here
// Iterate over entire arrry picking a number to insert
let n = 1;
while (n < nums.length) {
// Insert it in the correct place in sorted list (left)
for (let index = n; index >= 0; index--) {
// Swap with prev adjacent element if it is greater
if (nums[index] < nums[index - 1]) {
const temp = nums[index];
nums[index] = nums[index - 1];
nums[index - 1] = temp;
} else {
break;
}
}
n++;
}

console.log({ nums });
}

// unit tests
// do not modify the below code
test.skip("insertion sort", function () {
test("insertion sort", function () {
const nums = [10, 5, 3, 8, 2, 6, 4, 7, 9, 1];
insertionSort(nums);
expect(nums).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
});

// Another test
test("insertion sort", function () {
const nums = [1, 5, 3, 8, 2, 6, 4, 7, 900, -1];
insertionSort(nums);
expect(nums).toEqual([-1, 1, 2, 3, 4, 5, 6, 7, 8, 900]);
});
35 changes: 33 additions & 2 deletions specs/merge-sort/merge-sort.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,43 @@
*/

const mergeSort = (nums) => {
// code goes here
// Merge
const merge = (arrayA = [], arrayB = []) => {
const result = [];

while (arrayA.length > 0 || arrayB.length > 0) {
if (arrayA.length === 0 || arrayB.length === 0) {
// Return combined sorted list once any one of the array is completed
return [...result, ...arrayA, ...arrayB];
}

if (arrayA[0] < arrayB[0]) {
result.push(arrayA.shift());
} else {
result.push(arrayB.shift());
}
}
};

// Divide
const divide = (array = []) => {
const middleIndex = Math.ceil(array.length / 2);
return [array.slice(0, middleIndex), array.slice(middleIndex)];
};

// Base case
if (nums.length === 1) {
return nums;
}

// Recursion step
const [left, right] = divide(nums);
return merge(mergeSort(left), mergeSort(right));
};

// unit tests
// do not modify the below code
test.skip("merge sort", function () {
test("merge sort", function () {
const nums = [10, 5, 3, 8, 2, 6, 4, 7, 9, 1];
const ans = mergeSort(nums);
expect(ans).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
Expand Down
34 changes: 34 additions & 0 deletions specs/merge-sort/merge_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from ast import If
from cgitb import reset
from math import ceil
from typing import List
import unittest


def merge_sort(nums: List):
def merge(listA: List, listB: List):
result = []
while len(listA) > 0 or len(listB) > 0:
if len(listA) == 0 or len(listB) == 0:
return result + listA + listB
if listA[0] < listB[0]:
result.append(listA.pop(0))
else:
result.append(listB.pop(0))

if len(nums) == 1:
return nums

middleIndex = ceil(len(nums) / 2)
return merge(merge_sort(nums[:middleIndex]), merge_sort(nums[middleIndex:]))


class TestMergeSort(unittest.TestCase):
def test_merge_sort(self):
nums = [10, 5, 3, 8, 2, 6, 4, 7, 9, 1]
result = merge_sort(nums)
self.assertEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], result)


if __name__ == '__main__':
unittest.main()
30 changes: 30 additions & 0 deletions specs/quick-sort/quick-sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from typing import List
import unittest


def quick_sort(nums: List):
if len(nums) <= 1:
return nums

left = []
pivot = nums.pop()
right = []

for num in nums:
if num < pivot:
left.append(num)
else:
right.append(num)

return quick_sort(left) + [pivot] + quick_sort(right)


class TestQuickSort(unittest.TestCase):
def test_quick_sort(self):
nums = [10, 5, 3, 8, 2, 6, 4, 7, 9, 1]
result = quick_sort(nums)
self.assertEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], result)


if __name__ == '__main__':
unittest.main()
28 changes: 27 additions & 1 deletion specs/quick-sort/quick-sort.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,37 @@

function quickSort(nums) {
// code goes here
const divide = (array) => {
const pivot = array.pop();
const left = [];
const right = [];
for (let index = 0; index < array.length; index++) {
const element = array[index];
if (element < pivot) {
left.push(element);
} else {
right.push(element);
}
}

return [left, pivot, right];
};

const merge = (left, pivot, right) => {
return [...left, pivot, ...right];
};

if (nums.length <= 1) {
return nums;
}

const [left, pivot, right] = divide(nums);
return merge(quickSort(left), pivot, quickSort(right));
}

// unit tests
// do not modify the below code
test.skip("quickSort", function () {
test("quickSort", function () {
const input = [10, 8, 2, 1, 6, 3, 9, 4, 7, 5];
const answer = quickSort(input);

Expand Down
2 changes: 1 addition & 1 deletion specs/radix-sort/radix-sort.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,6 @@ describe.skip("radix sort", function () {
.fill()
.map(() => Math.floor(Math.random() * 500000));
const ans = radixSort(nums);
expect(ans).toEqual(nums.sort());
expect(ans).toEqual(nums.sort((a, b) => a - b));
});
});
10 changes: 8 additions & 2 deletions specs/recursion/factorials.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,17 @@
factorial(3) = 6
*/

function factorial(n) {}
function factorial(n) {
if (n === 1) {
return 1;
}

return n * factorial(n - 1);
}

// unit tests
// do not modify the below code
test.skip("factorials", () => {
test("factorials", () => {
expect(factorial(1)).toEqual(1);
expect(factorial(2)).toEqual(2);
expect(factorial(3)).toEqual(6);
Expand Down
13 changes: 11 additions & 2 deletions specs/recursion/nested-arrays.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,20 @@

*/

function nestedAdd(array) {
function nestedAdd(array = []) {
if (array.length === 0) {
return 0;
}

// write code here
if (Array.isArray(array[0])) {
return nestedAdd([...array[0], ...array.slice(1)]);
} else {
return array[0] + nestedAdd(array.slice(1));
}
}

test.skip("nested arrays addition", () => {
test("nested arrays addition", () => {
expect(nestedAdd([1, 2, 3])).toEqual(6);
expect(nestedAdd([1, [2], 3])).toEqual(6);
expect(nestedAdd([[[[[[[[[5]]]]]]]]])).toEqual(5);
Expand Down