Skip to content

Instantly share code, notes, and snippets.

View Giagnus64's full-sized avatar

Gianfranco Nuschese Giagnus64

View GitHub Profile
@Giagnus64
Giagnus64 / app.test.js
Last active January 7, 2020 04:03
Insertion Sort Initial Tests
const {insertionSort, swap} = require('./app');
//unit test
test('Swaps two array entries', () => {
expect(swap([1,2,3], 0, 1)).toStrictEqual([2,1,3]);
});
//integration test
test('Sorts array from lowest to highest - insertion sort', () => {
expect (insertionSort([8,5,3,1,2,4])).toStrictEqual([1,2,3,4,5,8]);
test('Adds 2 numbers', () => {
expect(add(2,2)).toBe(4);
});
@Giagnus64
Giagnus64 / app.test.js
Created January 7, 2020 04:20
Insertion Sort Fixed Tests
const {insertionSort, swap} = require('./app');
//unit test
test('Swaps two array entries', () => {
const arr1 = [1,2,3]
swap(arr1, 0, 1);
expect(arr1).toStrictEqual([2,1,3]);
});
//integration test
@Giagnus64
Giagnus64 / singlyLinkedList.js
Last active March 29, 2021 03:29
Singly Linked List implementation
class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}
// linkedlist class
class SinglyLinkedList {
constructor() {
@Giagnus64
Giagnus64 / DoublyLinkedList.js
Created January 17, 2020 23:10
Doubly Linked List
//DOUBLY LINKED
class Node {
constructor(value) {
this.value = value;
this.prev = null;
this.next = null;
}
}
class DoublyLinkedList {
constructor() {
@Giagnus64
Giagnus64 / reverse.js
Created January 20, 2020 18:28
Reverse a string
function reverse(str){
// if we arrive at the final character of the string, return that character
if (str.length === 1) return str[0];
//otherwise return the last character of the string, plus the reverse function called on a sliced version of that string
return str[str.length - 1] + reverse(str.slice(0, str.length - 1))
}
@Giagnus64
Giagnus64 / reverseBrokenDown.js
Created January 20, 2020 18:44
Reverse Broken Down
function reverse(str){
// if we arrive at the final character of the string, return that character
if (str.length === 1) return str[0];
//otherwise return the last character of the string, plus the reverse function called on a sliced version of that string
return str[str.length - 1] + reverse(str.slice(0, str.length - 1))
}
//1st Call returns "r" + reverse("ba")
//2nd call "a" + reverse("b")
//3rd Call "b"
@Giagnus64
Giagnus64 / merge.js
Created January 20, 2020 19:56
Merging function for Merge Sort
//ASSUMES THE ARRAYS ARE SORTED*****
const merge = (arr1, arr2) => {
//establish a final sorted array to sort the elements, and a pointer for each array
let sortedArray = [];
let firstPointer = 0;
let secondPointer = 0;
//while the first pointer and the second pointer have not reached the end of their respective arrays
while (firstPointer < arr1.length && secondPointer < arr2.length){
//add lesser number to array, and move pointer
if(arr2[secondPointer] >= arr1[firstPointer]){
@Giagnus64
Giagnus64 / mergeSort.js
Created January 20, 2020 20:00
Merge Sort w/out merge function
const mergeSort = (arr) => {
//if array is SORTED(length is 0 or 1), return array
if (arr.length <= 1) return arr;
//find midpoint
let midpoint = Math.floor(arr.length/2);
//split array by midpoint and merge sort the split arrays
let arr1 = mergeSort(arr.slice(0, midpoint))
let arr2 = mergeSort(arr.slice(midpoint))
//return the merged arrays
@Giagnus64
Giagnus64 / pivot.js
Created January 20, 2020 22:24
Pivot Helper for Quick Sort
const swap = (arr, index1, index2) => {
let temp = arr[index1];
arr[index1] = arr[index2];
arr[index2] = temp;
}
//Quick Sort
// setting default cases for our start and end index since we'll later need to change them when recursively calling
const pivot = (arr, startIndex = 0, endIndex = arr.length - 1) => {
//pick a pivot number (In this case, the 1st number in array)