Skip to content

Instantly share code, notes, and snippets.

View StevenKulesza's full-sized avatar
😎

Steven Kulesza StevenKulesza

😎
  • wherever
  • San Francisco
View GitHub Profile
@StevenKulesza
StevenKulesza / iterativeBinarySearch.js
Created June 14, 2019 01:39
Iterative function to implement Binary Search
// Iterative function to implement Binary Search
let iterativeFunction = function (arr, x) {
let start=0, end=arr.length-1;
// Iterate while start not meets end
while (start<=end){
// Find the mid index
@StevenKulesza
StevenKulesza / breadthFirstSearch.js
Created June 14, 2019 01:33
breadth first search
function bfs(tree, value) {
var queue = []
queue.push(tree[0])
while (queue.length !== 0) {
for (let i = 0; i < queue.length; i++) {
var node = queue.shift()
// round robin
// takes arr and index
// iterates through
// good for load balancing
function roundRobin (array, index) {
index = index || 0;
if (array === undefined || array === null)
array = [];
else if (!Array.isArray(array))
// LRU Cache
class LruCache {
constructor(capacity) {
this.m = new Map();
this.capacity = capacity;
}
get(key) {
if (this.m.has(key)) {
const value = this.m.get(key);
@StevenKulesza
StevenKulesza / freightLoad.js
Last active April 11, 2019 02:03
Simple Freight Class
// Design a class to load freight. Freight contains weight and timestamp.
// Add method return total weight.
// Add method return weight at a given time. Probably means to implement some logging system.
class LoadFreight {
constructor () {
this.load = [{ weight: 1230, timeStamp: 'Thu Apr 11 2019' }];
}
@StevenKulesza
StevenKulesza / multiArmBanditThompson.js
Last active February 6, 2019 14:43
Multi arm bandit Thompson Sampling - I wrote this to sort products by weights
function weightedSort(data, productLimit) {
var products = [];
var output = [];
var dataSum = 0;
var sum = 0;
var sortedData;
var randomNumber;
var numberToPop;
var noOfproducts = productLimit;
var j;
@StevenKulesza
StevenKulesza / fibonacci.js
Created January 28, 2019 01:17
Recursive solution to Fibonacci series in Javascript
// 1, 1, 2, 3, 5, 8
function fib(n){
if(n < 2) return n;
return fib(n - 1) + fib(n - 2);
}
@StevenKulesza
StevenKulesza / binarySearchTree.js
Created January 28, 2019 01:16
Binary search tree implemented in Javascript
class BinarySearchTree {
constructor(){
this.root = null;
}
// add a node to the tree
add(value){
let newNode = { value, left: null, right: null};
@StevenKulesza
StevenKulesza / binarySearch.js
Created January 28, 2019 01:15
Binary search for finding an element's index in a sorted array using Javascript.
function binarySearch(arr, target) {
let left = 0;
let right = arr.length - 1;
while (left <= right) {
const mid = left + Math.floor((right - left) / 2);
if (arr[mid] === target) {
return mid;
}
if (arr[mid] < target) {
left = mid + 1;
@StevenKulesza
StevenKulesza / linearSearch.js
Created January 28, 2019 01:15
The linear search algorithm implemented in Javascript
function linearSearch(array, toFind){
for(let i = 0; i < array.length; i++){
if(array[i] === toFind) return i;
}
return -1;
}