Skip to content

Instantly share code, notes, and snippets.

View ungarson's full-sized avatar
🎯
Focusing

Yo ungarson

🎯
Focusing
View GitHub Profile
@ungarson
ungarson / findAmountOfSmallestUnitLengthClosedIntervals.js
Created July 6, 2019 06:11
find amount of smallest unit length closed intervals in javascript
export default function findAmountOfSmallestUnitLengthClosedIntervals(sortedPoints) {
if(sortedPoints.length <= 1) return 1;
let amount = 0;
for (let i = 0, len = sortedPoints.length - 1; i < len;) {
let unitClosed = false;
for (let z = i + 1; z < sortedPoints.length; z++) {
if (sortedPoints[z] - sortedPoints[i] < 1) {
unitClosed = true;
if (z === sortedPoints.length - 1) {
@ungarson
ungarson / kNearestNeighbors.js
Last active June 22, 2019 06:50
k-nearest neighbors in js
function findMetrics(values) {
return Math.hypot(...values);
}
export function updateWithMetrics(usersVotes) {
if (obj(usersVotes).doesntExist()) return null;
for (let i = 0, len = usersVotes.length; i < len; i++) {
const currentUser = usersVotes[i];
@ungarson
ungarson / simpleGenerator.js
Last active June 15, 2019 08:55
Simple javascript generator. It is not iterable through for of.
export default function generator() {
const valuesToReturn = generator.prototype.valuesToReturn;
return {
value: null,
done: null,
next() {
const returnThis = valuesToReturn.pop();
return {
value: returnThis,
@ungarson
ungarson / dijkstraShortestWay.js
Last active June 15, 2019 05:59
dijkstraShortestWay.js - there is a function which uses dijkstra algorithm to find the shortest way in graph
/**
* This function uses dijkstra algorithm to find the shortest way in graph.
* So for this function to work properly, graph must be directed and acyclic with positive weights only
*/
export default function findShortestWayIn(graph) {
const minimumCosts = {};
let queue = [{}];
const visitedPointsFromSpecificParent = {}; // to detect a cycle in a graph
return {
@ungarson
ungarson / ifThereIsRoute.js
Created June 9, 2019 06:02
A function that tells you if there is a nested object with specific name
function ifThereIsRouteFrom(start) {
const queue = [start];
return {
to(finish) {
currentCity = queue.shift();
if (typeof currentCity === "undefined") return false;
for (let i = 0, len = currentCity.length; i < len; i++) {
if (currentCity[i]['name'] === finish) return true;
queue.push(currentCity[i]);
}
@ungarson
ungarson / longestCommonSubsequence.js
Last active May 12, 2019 09:37
Finding a Longest Common Subsequence of two strings in javascript
function forwardTrace(lcsMatrix, set1, set2) {
for (let rowIndex = 1; rowIndex <= set2.length; rowIndex += 1) {
for (let columnIndex = 1; columnIndex <= set1.length; columnIndex += 1) {
if (set1[columnIndex - 1] === set2[rowIndex - 1]) {
lcsMatrix[rowIndex][columnIndex] = lcsMatrix[rowIndex - 1][columnIndex - 1] + 1;
} else {
lcsMatrix[rowIndex][columnIndex] = Math.max(
lcsMatrix[rowIndex - 1][columnIndex],
lcsMatrix[rowIndex][columnIndex - 1],
);
@ungarson
ungarson / fibonacciMemo.js
Last active June 12, 2019 05:19
Fibonacci with memoization
function fib(pos, fibValues) {
if (typeof fibValues[pos] !== "undefined") {
return fibValues[pos];
}
result = fib(pos - 1, fibValues) + fib(pos - 2, fibValues);
fibValues[pos] = result;
return result;
}
// This is how tree should look like
// const tree = {
// root: {
// left: {
// left: {
// left: null,
// right: null,
// value: 3
// },
// right: {
@ungarson
ungarson / bucketSort.js
Last active April 6, 2019 06:59
Bucket sort in javascript
let insertionSort = (inputArr) => {
let length = inputArr.length;
for (let i = 1; i < length; i++) {
let key = inputArr[i];
let j = i - 1;
while (j >= 0 && inputArr[j] > key) {
inputArr[j + 1] = inputArr[j];
j = j - 1;
}
inputArr[j + 1] = key;
@ungarson
ungarson / radixSort.js
Created April 6, 2019 06:18
Radix sort in javascript
function arrayConcatenating(...arrs) {
let newArr = [];
for (let i = 0; i < arrs.length; i++) {
newArr = newArr.concat(arrs[i]);
}
return newArr;
}