Skip to content

Instantly share code, notes, and snippets.

@pollardld
Last active July 30, 2024 00:08
Show Gist options
  • Save pollardld/0f19c46ce87a24f100cec7abac7477c9 to your computer and use it in GitHub Desktop.
Save pollardld/0f19c46ce87a24f100cec7abac7477c9 to your computer and use it in GitHub Desktop.
Cookbook

Algorithms

Algorithms are a set of instructions that are followed to solve a problem. They are a step-by-step procedure that can be followed to achieve a particular goal. There are many different types of algorithms, each with their own purpose and use.

Check if a number is a perfect square

import math

number = int(input())
square_number = int(math.sqrt(number))

if (number == square_number * square_number):
    print("perfect square: %d * %d = %d"%(square_number,square_number,number))
else:
    print("not perfect square")

In order tree traversal

var tree = {
  value: 10,
  left: { 
    value: 5,
    left: {
      value: 3
    }, 
    right: {
      value: 7, 
      right: {
        value: 9
      }
    }
  },
  right: { 
    value: 12 
  }
}

var arr = [];

function traversalInOrder(tree) {
  
  if ( tree.left !== undefined ) {
    traversalInOrder(tree.left);
  } 
  
  // push value. goes in between so right value is not pushed before left
  arr.push(tree.value);
  
  if ( tree.right !== undefined ) {
    traversalInOrder(tree.right);
  } 
  
  return arr;
}

console.log(traversalInOrder(tree));

/* 
Please perform an in-order tree traversal.

          10
        /    \
       5     12 
      / \
     3   7
          \
           9
          
So traversing this tree in order would produce the sequence 3,5,7,9,10,12.
 */

Climb Stairs

/**
 * @param {number} n
 * @return {number}
 */
var climbStairs = function(n) {
    if (n < 2) {
        return 1;
    }
    let l = 2,
        ll = 1,
        answer = 2;
        
    while (n > 2) {
        answer = l + ll;
        ll = l;
        l = answer;
        n--;
    }
    return answer;
};

Misc Algorithms

function id(x) {
	return x;
}

function add(x,y) {
	return x + y;
}

function mult(x,y) {
	return x * y;
}

function first(x) {
	return function (x) {
		return x;
	};
}

function f(x) {
	return function (y) {
		return x + y;
	};
}

function applyf(b) {
	return function(x) {
		return function(y) {
			return b(x,y);
		};
	};
}

function curry(f,x) {
	return function(y) {
			return f(x,y);
	};
}

function curryTwo(f,x) {
	return applyf(f,x);
}

var z = 5;

add(z,1);
f(z)(1);
applyf(add,1);

Number.prototype.add = methodize(add);

function methodize(add) {
	return this + add;
}

function demethodize(d) {
	return function (x,y) {
		return d.call(x,y);
	};
}

function demethodize(f) {
	return function (x,y) {
		return f.apply(x,y);
	};
}

function twice(b) {
	return function (x) {
		return b(x, x);
	};
}

function composeu(funcOne,funcTwo) {
	return function (x) {
		return funcTwo(funcOne(a));
	};
}

function composeb(funcOne,funcTwo) {
	return function (x,y,z) {
		return funcTwo(funcOne(x,y), z);
	};
}

function once(func) {
	return function () {
		var f = func;
		func = null;
		return f.apply(
			this,
			arguments
		);
	}
}

function counterf(x) {
	return {
		inc: function () {
			x = x + 1;
			return x;
		},
		dec: function () {
			x = x - 1;
			return x;
		}
	};
}

function revocable(x) {
	return {
		invoke: function () {
			return x.apply(
				this,
				arguments
			);
		},
		revoke: function () {
			x = null;
		}
	};
}

TYPES OF ALGORITHMS

Sorting Algorithms: These algorithms are used to put a collection of items in a specific order. Some of the most common sorting algorithms include bubble sort, quicksort, and merge sort.

Hashing Algorithms: These algorithms are used to map data of arbitrary size to data of a fixed size. They are commonly used in computer security and data integrity applications.

Hash Table: A hash table is a data structure that uses a hash function to map keys to values. It is used to implement an associative array, a structure that can map keys to values.

  • Hash table should have a size - this will be used by the hashing function to determine what index to map the key to.
  • A hashing function is used to map the key to an integer, which is the index that the value is to be stored at.
  • Since our hashing function might map multiple keys to the same integer, we have to deal with collisions by creating buckets at each index of the storage array. These buckets can be arrays or linked lists.

Note: ES6 includes a Map data structure. It differs from the JavaScript object because the keys can be any value (not just strings like for objects), there is a size property, and there is a guaranteed order (the insertion order).

Search Algorithms: These algorithms are used to find a specific item in a collection of items. Some of the most common search algorithms include linear search, binary search, and depth-first search.

Graph Algorithms: These algorithms are used to traverse or search a graph. Some of the most common graph algorithms include breadth-first search, depth-first search, and Dijkstra's algorithm.

String Algorithms: These algorithms are used to perform operations on strings, such as searching, sorting, and matching.

Dynamic Programming: This is a method for solving complex problems by breaking them down into simpler subproblems. It is used in a wide range of applications, including optimization, bioinformatics, and game theory.

Greedy Algorithms: These algorithms are used to find the optimal solution for a given problem by making a sequence of choices. They are often used for optimization problems.

Divide and Conquer Algorithms: These algorithms are used to solve a problem by breaking it down into smaller subproblems, solving the subproblems, and then combining the solutions to the subproblems to solve the original problem.

Backtracking Algorithms: These algorithms are used to solve problems by building a solution incrementally, and then removing the solution when it is determined that the solution cannot be completed.

Randomized Algorithms: These algorithms use random numbers to solve problems that might be deterministic in nature. They are often used in applications such as cryptography and game theory.

Parallel Algorithms: These algorithms are designed to run on parallel computing architectures, such as multi-core processors and distributed systems. They are used to solve problems that can be divided into smaller subproblems that can be solved concurrently.

Approximation Algorithms: These algorithms are used to find approximate solutions to optimization problems. They are often used in applications such as scheduling and resource allocation.

Heuristic Algorithms: These algorithms are used to find approximate solutions to problems that are difficult to solve exactly. They are often used in applications such as artificial intelligence and optimization.

Metaheuristic Algorithms: These algorithms are used to find approximate solutions to optimization problems by iteratively improving a candidate solution. They are often used in applications such as genetic algorithms and simulated annealing.

Online Algorithms: These algorithms are used to solve problems in which the input is presented in a sequential order. They are often used in applications such as data mining and machine learning.

External Algorithms: These algorithms are used to solve problems that involve large amounts of data that cannot fit into memory. They are often used in applications such as database management and file systems.

Quantum Algorithms: These algorithms are used to solve problems using quantum computers. They are often used in applications such as cryptography and optimization.

Bioinformatics Algorithms: These algorithms are used to solve problems in the field of bioinformatics, such as sequence alignment and protein folding.

Numerical Algorithms: These algorithms are used to solve problems in numerical analysis, such as solving systems of linear equations and finding roots of polynomials.

Optimization Algorithms: These algorithms are used to find the best solution to a given problem. They are often used in applications such as engineering and economics.

Machine Learning Algorithms: These algorithms are used to train models to make predictions or decisions based on data. They are often used in applications such as image recognition and natural language processing.

Deep Learning Algorithms: These algorithms are used to train deep neural networks to make predictions or decisions based on data. They are often used in applications such as computer vision and speech recognition.

Reinforcement Learning Algorithms: These algorithms are used to train agents to make decisions in an environment in order to maximize some notion of cumulative reward. They are often used in applications such as robotics and game playing.

Evolutionary Algorithms: These algorithms are used to solve optimization problems by simulating the process of natural selection. They are often used in applications such as genetic programming and evolutionary robotics.

Swarm Intelligence Algorithms: These algorithms are used to solve optimization problems by simulating the behavior of swarms of insects or other animals. They are often used in applications such as ant colony optimization and particle swarm optimization.

Artificial Immune System Algorithms: These algorithms are used to solve optimization problems by simulating the behavior of the human immune system. They are often used in applications such as anomaly detection and pattern recognition.

Cellular Automata Algorithms: These algorithms are used to solve problems by simulating the behavior of a grid of cells that change state over time. They are often used in applications such as modeling complex systems and simulating physical processes.

Chaos Theory Algorithms: These algorithms are used to solve problems by simulating the behavior of chaotic systems. They are often used in applications such as weather forecasting and financial modeling.

Fuzzy Logic Algorithms: These algorithms are used to solve problems by simulating the behavior of systems that are based on imprecise or uncertain information. They are often used in applications such as control systems and decision support systems.

Neural Network Algorithms: These algorithms are used to solve problems by simulating the behavior of interconnected nodes that are inspired by the structure of the human brain. They are often used in applications such as pattern recognition and data mining.

Genetic Algorithms: These algorithms are used to solve optimization problems by simulating the process of natural selection. They are often used in applications such as genetic programming and evolutionary robotics.

Simulated Annealing Algorithms: These algorithms are used to solve optimization problems by simulating the process of annealing in metallurgy. They are often used in applications such as combinatorial optimization and scheduling.

Ant Colony Optimization Algorithms: These algorithms are used to solve optimization problems by simulating the behavior of ant colonies. They are often used in applications such as routing and scheduling.

Particle Swarm Optimization Algorithms: These algorithms are used to solve optimization problems by simulating the behavior of swarms of particles. They are often used in applications such as function optimization and data clustering.

Tabu Search Algorithms: These algorithms are used to solve optimization problems by simulating the behavior of a search process that uses memory to avoid revisiting previously visited solutions. They are often used in applications such as scheduling and routing.

Variable Neighborhood Search Algorithms: These algorithms are used to solve optimization problems by simulating the behavior of a search process that uses different neighborhoods to explore the solution space. They are often used in applications such as graph coloring and timetabling.

Memetic Algorithms: These algorithms are used to solve optimization problems by combining genetic algorithms with local search methods. They are often used in applications such as vehicle routing and job scheduling.

Harmony Search Algorithms: These algorithms are used to solve optimization problems by simulating the process of creating music. They are often used in applications such as engineering design and water resource management.

Firefly Algorithm: This algorithm is used to solve optimization problems by simulating the behavior of fireflies. It is often used in applications such as function optimization and data clustering.

Cuckoo Search Algorithm: This algorithm is used to solve optimization problems by simulating the behavior of cuckoo birds. It is often used in applications such as function optimization and data clustering.

Bat Algorithm: This algorithm is used to solve optimization problems by simulating the behavior of bats. It is often used in applications such as function optimization and data clustering.

Flower Pollination Algorithm: This algorithm is used to solve optimization problems by simulating the process of flower pollination. It is often used in applications such as function optimization and data clustering.

Artificial Bee Colony Algorithm: This algorithm is used to solve optimization problems by simulating the behavior of honey bees. It is often used in applications such as function optimization and data clustering.

Grey Wolf Optimizer: This algorithm is used to solve optimization problems by simulating the behavior of grey wolves. It is often used in applications such as function optimization and data clustering.

Krill Herd Algorithm: This algorithm is used to solve optimization problems by simulating the behavior of krill. It is often used in applications such as function optimization and data clustering.

Moth Flame Optimization: This algorithm is used to solve optimization problems by simulating the behavior of moths. It is often used in applications such as function optimization and data clustering.

Pigeon-Inspired Optimization: This algorithm is used to solve optimization problems by simulating the behavior of pigeons. It is often used in applications such as function optimization and data clustering.

Squirrel Search Algorithm: This algorithm is used to solve optimization problems by simulating the behavior of squirrels. It is often used in applications such as function optimization and data clustering.

Wolf Search Algorithm: This algorithm is used to solve optimization problems by simulating the behavior of wolves. It is often used in applications such as function optimization and data clustering.

Cheetah Search Algorithm: This algorithm is used to solve optimization problems by simulating the behavior of cheetahs. It is often used in applications such as function optimization and data clustering.

Elephant Herding Optimization: This algorithm is used to solve optimization problems by simulating the behavior of elephants. It is often used in applications such as function optimization and data clustering.

Glowworm Swarm Optimization: This algorithm is used to solve optimization problems by simulating the behavior of glowworms. It is often used in applications such as function optimization and data clustering.

Horsefly Optimization Algorithm: This algorithm is used to solve optimization problems by simulating the behavior of horseflies. It is often used in applications such as function optimization and data clustering.

Humpback Whale Optimization: This algorithm is used to solve optimization problems by simulating the behavior of humpback whales. It is often used in applications such as function optimization and data clustering.

Jellyfish Algorithm: This algorithm is used to solve optimization problems by simulating the behavior of jellyfish. It is often used in applications such as function optimization and data clustering.

Kangaroo Search Algorithm: This algorithm is used to solve optimization problems by simulating the behavior of kangaroos. It is often used in applications such as function optimization and data clustering.

Lion Optimization Algorithm: This algorithm is used to solve optimization problems by simulating the behavior of lions. It is often used in applications such as function optimization and data clustering.

Manta Ray Foraging Optimization: This algorithm is used to solve optimization problems by simulating the behavior of manta rays. It is often used in applications such as function optimization and data clustering.

Moth Search Algorithm: This algorithm is used to solve optimization problems by simulating the behavior of moths. It is often used in applications such as function optimization and data clustering.

Owl Search Algorithm: This algorithm is used to solve optimization problems by simulating the behavior of owls. It is often used in applications such as function optimization and data clustering.

Penguin Search Algorithm: This algorithm is used to solve optimization problems by simulating the behavior of penguins. It is often used in applications such as function optimization and data clustering.

Polar Bear Optimization: This algorithm is used to solve optimization problems by simulating the behavior of polar bears. It is often used in applications such as function optimization and data clustering.

Classification

Boosting

Stump weights (𝐰̂) and data point weights (𝛼) are two different concepts. Stump weights (𝐰̂) tell you how important each stump is while making predictions with the entire boosted ensemble. Data point weights (𝛼) tell you how important each data point is while training a decision stump.

Greedy Decision Tree Al

  • Selecting the best feature to split on
  • Make a decision tree for each feature M
    • Step 1: Start with an empty tree
    • Step 2: Select the best feature to split on
      • Select feature with lowest classification error
      • To compute the error for each feature M: * *for each stump node divide the number of incorrect classifications by the total number of classifications
    • Once you have selected best feature for each split in tree:
      • Step 3: if no more features to split on, make predictions
      • Step 4: otherwise recurse back to step 2

Stopping Criteria Stop splitting once all decision stumps are classified OR out of features to split on

Rotational Cipher

function rotationalCipher(input, rotationFactor) {
  // Write your code here
  // 1. loop through input
  // 2. check if alphanumeric 
  // 3. rotate by rotationFactor
  // 4. return string
  return [...input].map(char => {
    var isAlphanumeric = /[a-zA-Z]/.test(char);
    var isNumber = /[0-9]/.test(char);
        
    if (!isAlphanumeric && !isNumber) {
      return char;
    }
    
    var charCode = char.charCodeAt(0);
    
    if (isAlphanumeric) {
      if (charCode >= 65 && charCode <= 90) {
        var rotated = (charCode - 65 + rotationFactor) % 26 + 65;
        return String.fromCharCode(rotated);
      } else if (charCode >= 97 && charCode <=122) {
        var rotated = (charCode - 97 + rotationFactor) % 26 + 97;
        return String.fromCharCode(rotated);
      }
    } else if (isNumber) {
      var rotated = (charCode - 48 + rotationFactor) % 10 + 48;
      return String.fromCharCode(rotated);
    }
        
  }).join('');
}

Count Subarrays

function countSubarrays(arr) {
  // Write your code here
  // at each index in arr check if index next to it is smaller
  // if so, add 1 to count and then check the index next, ... until an index is larger or reach the end off arr
  // then check previous index
  // if so, add 1 to count and then check previous inext, ... until an index ...
  // [0] is 3 
  // i - 1, if i is 0 stop
  // i + 1, if i == arr.lenth stop
  // if [i] > [i - 1] add 1, if [i] > [i - 2]
  // if [i] > [i + 1]
  var output = []

  for (var i = 0; i < arr.length; i++) {
    var count = 1;
    var prev = i - 1;
    while (arr[i] > arr[prev]) {
      count = count + 1;
      prev = prev - 1;
    }
    var next = i + 1;
    while (arr[i] > arr[next]) {
      count = count + 1;
      next = next + 1;
    }
    output[i] = count;
  }
  
  return output
}

Check if word is a palindrome

function palindrome(string) { 
	if (string.length < 2) {
    // if string is empty or has one character, it is a palindrome
		return true;
	} else {
	  if ( string[0] === string[string.length - 1] ) {
      // if first and last characters are the same, remove them and check the rest of the string
    	string = string.slice(1,-1);
  	  return palindrome(string);
	  } else {
      // if first and last characters are not the same, it is not a palindrome
    	return false;
  	}
	}
}

Fibonacci

function fibonacci(n) { 
	var c = 0,
			y = 1,
			f = 0;
	if (n == 0) {
		h.innerHTML = 0;
	} else {
		for(let i = 0; i < n; i++) {
			f = y + c;
			y = c;
			c = f;
		}
		h.innerHTML = f;
	}
}

Binary Gap Max

function solution(N) {
    // write your code in JavaScript (Node.js 4.0.0)
    var bin = N.toString(2);
    if ( !bin.includes('0') ) {
        return 0;
    }
    var longest = 0;
    var currentLongest = 0;
    
    for (var i = 0; i < bin.length; i++) {
        if ( bin.charAt(i) == '0' && i !== 0 ) {
            currentLongest = currentLongest + 1;
        } else {
            if (currentLongest > longest) {
                longest = currentLongest;
            }
            currentLongest = 0;
        }
            
    }
    return longest;
}

Staircase Challenge

function main() {
    var n = parseInt(readLine());
    for (var i = 0; i < n; i++) {
        var s = '';
        var len = n - i;
        for (var y = 0; y < len - 1; y++) {
            s = s + ' ';
        }
        for (var x = 0; x < i + 1; x++) {
            s = s + '#';
        }
        console.log(s);
    }
}

Factorial

function factorial(n) {
	if (n === 0) {
		return 1;
	} else {
		return n * factorial(n - 1);
	}
}

Extra long factorial

function main() {
    var n = parseInt(readLine());
    var y = factorial(n);
    console.log(y);
}

function factorial(n) {
    if (n === 0) {
        return 1;
    } else {
       return n * factorial(n - 1);
    }
}
def factorial(n):
    if n == 0:
        return 1
    else:
        return n * factorial(n - 1)    
y = factorial(n)
print(y) 

Greatest Common Divisor

var gcd = memoize(function(a,b){
    var t;
    if (a < b) t=b, b=a, a=t;
    while(b != 0) t=b, b = a%b, a=t;
    return a;
})
gcd(27,183); //=> 3

Greedy Nap Sack

var items = ["A", "B", "C", "D"];
var values = [50, 140, 60, 60];
var weights = [5, 20, 10, 12];
var capacity = 30;

ksack(values, weights, capacity);

function ksack(values, weights, capacity) {
	var load = 0;
	var i = 0;
	var w = 0;

	while (load < capacity && i < 4) {
		if (weights[i] <= (capacity-load)) {
			w += values[i];
			load += weights[i];
		} else {
			var r = (capacity-load)/weights[i];
			w += r * values[i];
			load += weights[i];
		}
		++i;
	}
	
	output.innerHTML = w;
}

Binary Gap Map

function solution(N) {
    // write your code in JavaScript (Node.js 4.0.0)
    var bin = N.toString(2);
    if ( !bin.includes('0') ) {
        return 0;
    }
    var longest = 0;
    var currentLongest = 0;
    
    for (var i = 0; i < bin.length; i++) {
        if ( bin.charAt(i) == '0' && i !== 0 ) {
            currentLongest = currentLongest + 1;
        } else {
            if (currentLongest > longest) {
                longest = currentLongest;
            }
            currentLongest = 0;
        }
            
    }
    return longest;
}

Fibonacci Sequence

function fibs(arr) {
	arr.unshift(0,1,1);
	seq.innerHTML = arr;
}

function fib(n) {
	var last = 1,
			nextLast = 1,
			arr = [],
			answer = 1;
	for (var i = 2; i < n; ++i) {
		answer = last + nextLast;
		nextLast = last;
		last = answer;
		arr.push(answer);
	}
		fibs(arr);
	return answer;
}

Binary Search Magic

for (var i = 1; i < 101; ++i) {
	arr[i] = i;
}

function binarySearch(n) {
	var upperBound = arr.length - 1,
			lowerBound = 0;
	while (lowerBound <= upperBound) {
		var mid = Math.floor((upperBound + lowerBound) / 2);
		if (arr[mid] < n) {
			lowerBound = mid + 1;
		} else if (arr[mid] > n) {
			upperBound = mid - 1;
		} else {
			return mid;
		}
	}
}

Knapsack Problem

function max(a, b) {
	return (a > b) ? a : b;
}

function dKnapsack(capacity, size, value, n) {
	var K = [];
	for (var i = 0; i <= capacity+1; i++) {
		K[i] = [];
	}
	for (var i = 0; i <= n; i++) {
		for (var w = 0; w <= capacity; w++) {
			if (i == 0 || w == 0) {
				K[i][w] = 0;
			} else if (size[i-1] <= w) {
				K[i][w] = max(value[i-1] + K[i-1][w-size[i-1]],
				K[i-1][w]);
			} else {
				K[i][w] = K[i-1][w];
			}
		}
		arr.push(K[i] + '<br>');
	}
	seq.innerHTML = arr;
	output.innerHTML = K[n][capacity];
}

Merge Sort

// Recursive
function mergeSortRecursive (array) {
  // base case
  if (array.length <= 1) return array;

  // divide and conquer!!
  var leftHalf = array.slice(0, array.length/2);
  var rightHalf = array.slice(array.length/2);
  var leftSorted = mergeSortRecursive(leftHalf);
  var rightSorted = mergeSortRecursive(rightHalf);

  // merge subarrays
  return merge(leftSorted, rightSorted);
};

function mergeSortIterative (array) {
  // create array of subarrays with each element
  var splitArr = array.map(function(element) { return [element]; });

  // while there is more than one subarray
  while (splitArr.length > 1) {
    var result = [];
    // merge adjacent
    for (var i=0; i<splitArr.length; i+=2) {
      // for pairs merge
      if (splitArr[i+1]) result.push(merge(splitArr[i], splitArr[i+1]));
      // for last odd element, just add to results
      else result.push(splitArr[i]);
    }
    // overwrite old splitArr
    splitArr = result;
  }
  return splitArr[0];

};

function merge(left, right) {
  var result = [], iLeft = 0, iRight = 0;

  // while result is not fully populated
  while (result.length < (left.length + right.length)) {
    // if all elements in left have been added, then add remaining right elements
    if (iLeft === left.length) result = result.concat(right.slice(iRight));
    // if all elements in right have been added, then add remaining left elements
    else if (iRight === right.length) result = result.concat(left.slice(iLeft));
    // compare elements in subarrays and add lower of the two to result
    else if (left[iLeft] <= right[iRight]) result.push(left[iLeft++]);
    else result.push(right[iRight++]);
  }
  return result;
};

Bubble Sort

function bubbleSort(list) {
	var done = true;
	for (var i = 0; i < list.length; i++) {
		var cur = list[i];
		var next = list[i+1]
		if (cur > next) {
			list[i] = next;
			list[i+1] = cur;
			done = done && false;
		}
	}
	if (done === false) {
		return bubbleSort(list);
	}
	return list;
}

console.log(bubbleSort([1,2,4,9,5,6,3,8,7]));

Quick Sort

function quicksort(array, lo, hi) {
  if (lo === undefined) lo = 0;
  if (hi === undefined) hi = array.length-1;

  if (lo < hi) {
    // partition array
    var p = partition(array, lo, hi);
    console.log('partitioning from', lo, 'to', hi, '=> partition:',  p);
    // sort subarrays
    quicksort(array, lo, p-1);
    quicksort(array, p+1, hi);
  }

  // for initial call, return sorted array
  if (hi-lo === array.length-1) return array;
}

// Lomuto partition scheme
function partition(arr, lo, hi) {
  // choose last element as pivot
  var pivot = arr[hi];
  // keep track of index to put pivot at
  var pivotLoc = lo;
  // iterate through subarray and if element <= pivot, place element before pivotLoc
  for (var i=lo; i<hi; i++) {
    if (arr[i] <= pivot) {
      swap(arr, pivotLoc, i);
      pivotLoc++;
    }
  }
  // move pivot to its proper location
  swap(arr, pivotLoc, hi);
  return pivotLoc;
}

function swap (arr, i1, i2) {
  if (i1 === i2) return;
  var temp = arr[i1];
  arr[i1] = arr[i2];
  arr[i2] = temp;
  console.log('swapped', arr[i1], arr[i2], 'in', arr);
  return arr;
}

Data Structures

Lists

Lists are mutable sequences, typically used to store collections of homogeneous items.

fruits = ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana']

Trees

Trees are a hierarchical data structure that consists of nodes connected by edges.

class Node:
    def __init__(self, data):
        self.data = data
        self.children = []

    def add_child(self, child):
        self.children.append(child)

Graphs

Graphs are a collection of nodes and edges that connect pairs of nodes.

class Graph:
    def __init__(self, graph_dict=None):
        if graph_dict is None:
            graph_dict = {}
        self.graph_dict = graph_dict

Stacks

Stacks are a collection of elements, with two main principal operations: push, which adds an element to the collection, and pop, which removes the most recently added element.

"""
Python Data Structures - A Game-Based Approach
Stack class
Robin Andrews - https://compucademy.net/
"""


class Stack:
    def __init__(self):
        self.items = []

    def is_empty(self):
        # return len(self.items) == 0
        return not self.items

    def push(self, item):
        self.items.append(item)

    def pop(self):
        return self.items.pop()

    def peek(self):
        return self.items[-1]

    def size(self):
        return len(self.items)

    def __str__(self):
        return str(self.items)


if __name__ == "__main__":
    s = Stack()

JS Stack Prototype

function Stack(capacity) {
	this._capacity = capacity || Infinity;
	this._storage = {};
	this._count = 0;
}

Stack.prototype.push = function(val) {
	if (this._count < this._capacity) {
		this._storage[this._count++] = val;
		return this._count;
	}
	return 'max capacity reached. remove element before adding another';
};

Stack.prototype.pop = function() {
	var val = this._storage[--this._count];
	delete this._storage[this._count];
	if (this._count < 0) {
		this._count = 0;
	}
	return val;
};

Linked List

function Node(value) {
  this.next = null;
  this.value = value;
}

function LinkedList(headValue) {
  if (headValue === undefined) console.log('Must provide value for first node');
  this.head = new Node(headValue);
}

LinkedList.prototype.forEach = function(callback) {
  // implement me...
};
// Time complexity:

LinkedList.prototype.print = function() {
  // implement me...
};
// Time complexity:

LinkedList.prototype.insertAfter = function(node, value) {
  // implement me...
};
// Time complexity:

LinkedList.prototype.removeAfter = function(node) {
  // implement me...
};
// Time complexity:

LinkedList.prototype.insertHead = function(value) {
  // implement me...
};
// Time complexity:

LinkedList.prototype.removeHead = function() {
  // implement me...
}

LinkedList.prototype.findNode = function(value) {
  // implement me...
};
// Time complexity:

LinkedList.prototype.appendToTail = function(value) {
  // implement me...
};
// Time complexity:


// PART 2:

LinkedList.prototype.insertBefore = function(node, value) {
  // implement me...
};
// Time complexity:

LinkedList.prototype.removeBefore = function(node) {
  // implement me...
};
// Time complexity:

Tree

function Tree (value) {
  	this.value = value;
	this.child = null;
	this.parent = null;
}

Tree.prototype.addChild = function(value) {
  	var aTree = new Tree('sequoia');
	aTree.child = value;
	aTree.child.parent = aTree;
};

Hash Table / Hash Map

class Node:
    """A node in the linked list used for chaining."""
    def __init__(self, key, value):
        self.key = key
        self.value = value
        self.next = None

class HashMap:
    def __init__(self, size=10):
        self.size = size
        self.table = [None] * size

    def hash_function(self, key):
        """Simple hash function to compute an index."""
        hash_value = sum(ord(char) for char in key)
        return hash_value % self.size

    def insert(self, key, value):
        """Insert a key-value pair into the hash map."""
        index = self.hash_function(key)
        new_node = Node(key, value)
        if self.table[index] is None:
            self.table[index] = new_node
        else:
            current = self.table[index]
            while current.next is not None:
                if current.key == key:
                    current.value = value  # Update value if key already exists
                    return
                current = current.next
            if current.key == key:
                current.value = value  # Update value if key already exists
            else:
                current.next = new_node  # Add new node at the end of the chain

    def retrieve(self, key):
        """Retrieve the value for a given key from the hash map."""
        index = self.hash_function(key)
        current = self.table[index]
        while current is not None:
            if current.key == key:
                return current.value
            current = current.next
        return None  # Key not found

    def delete(self, key):
        """Delete a key-value pair from the hash map."""
        index = self.hash_function(key)
        current = self.table[index]
        prev = None
        while current is not None:
            if current.key == key:
                if prev is None:
                    self.table[index] = current.next
                else:
                    prev.next = current.next
                return True
            prev = current
            current = current.next
        return False  # Key not found

# Example usage
hash_map = HashMap()

# Insert key-value pairs
hash_map.insert('apple', 1)
hash_map.insert('banana', 2)
hash_map.insert('orange', 3)

# Retrieve values
print(hash_map.retrieve('apple'))   # Output: 1
print(hash_map.retrieve('banana'))  # Output: 2
print(hash_map.retrieve('grape'))   # Output: None

# Delete a key-value pair
hash_map.delete('banana')
print(hash_map.retrieve('banana'))  # Output: None
/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number[]}
 */
var twoSum = function(nums, target) {
    var hash = {};
    for (i in nums) {
        if (hash[target - nums[i]]) {
            return [i, hash[target - nums[i]]];
        } else {
            hash[nums[i]] = i;
        }
    }
};

var twoSum = function (nums, target) {
    const map = new Map();
    for (let i = 0; i < nums.length; i++) {
        const complement = target - nums[i];
        if (map.has(complement)) {
            return [map.get(complement), i];
        }
        map.set(nums[i], i);
    }
    return null;
};

Graph

function Graph () {
  this._nodes = {};
}

Graph.prototype.addNode = function(value) {
	if (value === undefined) {
		return;
	}
  	this._nodes[value] = this._nodes[value] || [];
};
// Time complexity:

Graph.prototype.removeNode = function(value) {
  this._nodes[value] = [];
};
// Time complexity:

Graph.prototype.contains = function(value) {
  // implement me...
};
// Time complexity:

Graph.prototype.addEdge = function(value1, value2) {
  	if (!this._nodes[value1] || !this._nodes[value2])
		return 'invalid node value';
	this._nodes[value1].push(value2);
	this._nodes[value2].push(value1);
};
// Time complexity:

Graph.prototype.removeEdge = function(value1, value2) {
  // implement me...
};
// Time complexity:

Graph.prototype.hasEdge = function(value1, value2) {
  // implement me...
};
// Time complexity:

Graph.prototype.forEach = function(fn) {
  for (var node in this._nodes) {
    fn(node, this._nodes[node], this._nodes);
  }
};
// Time complexity:

Graph.prototype.traverseDepthFirst = function(value, fn, visited, distance) {
	if (!this._nodes[value] || typeof fn !== 'function') return 'Invalid value or function';
  	visited = visited || {};
  	distance = distance || 0;
  	fn(value, distance);
	visited[value] = true;
  	this._nodes[value].forEach(function(neighbor) {
	if (visited[neighbor]) return;
		this.traverseDepthFirst(neighbor, fn, visited, distance+1);
	}, this);
};
// Time complexity:

Graph.prototype.traverseBreadthFirst = function(value, fn) {
	if (!this._nodes[value] || typeof fn !== 'function') return 'Invalid value or function';
};
// Time complexity:

var graph = new Graph();

Queue

A queue is a collection of elements, supporting two principal operations: enqueue, which inserts an element into the queue, and dequeue, which removes an element from the queue.

"""
Python Data Structures - A Game-Based Approach
Queue class
Robin Andrews - https://compucademy.net/
"""

from collections import deque


class Queue:
    def __init__(self):
        self.items = deque()

    def is_empty(self):
        return not self.items
        # return len(self.items) == 0

    def enqueue(self, item):
        self.items.append(item)

    def dequeue(self):
        return self.items.popleft()

    def size(self):
        return len(self.items)

    def peek(self):
        return self.items[0]

    def __str__(self):
        return str(self.items)


if __name__ == "__main__":
    q = Queue()

Definitions

Data structures can often be a major part of optimizing and organizing your codebase. While deep-dives on each of these concepts are outside the scope of this article, we’ve provided a cheat-sheet for your next technical interview below. Find any that sound interesting? We encourage further reading to master them!

Heap This kind of binary tree places the smallest value at the top. Helpful when creating priority queues.

Array A data structure that places items sequentially. Offers fast lookups and appends, but its fixed size requires careful planning.

Dynamic Array Similar to an ordinary array, except that a dynamic array expands to fit additional elements as needed.

Linked List A flexibly-sized list in which each item contains a pointer for the next element. Although the list is easy to expand, lookups can take a lot of time.

Queue A data structure in which each item is stored in the order it’s received.

Stack A data structure in which the last item to be stored is the first item to be addressed.

Graph Not an X/Y chart. Graphs are formed by nodes, and the relationships between the nodes are denoted via lines (edges). Good for creating functional diagrams, but difficult to scale.

Trie Also known as a prefix tree, the trie is designed to store strings in a compact manner—mostly in cases where you’re storing words with a large number of shared prefixes.

Priority Queue Distinct from a regular queue. Each item in the queue is given a priority, and higher-priority items are addressed first regardless of when they entered the queue. See also: Heap.

Bloom Filter A probabilistic data structure that allows administrators to understand whether a set contains a given item—sometimes.

LRU Cache Here, LRU stands for “Least Recently Used.” It’s a data structure that shows which item has gone unused for longest.

Hash Table Also known as a hash table, this data structure stores hashed items for quick lookup.

Binary Tree (AKA Binary Search Tree or BST) A tree in which the nodes to the left are always smaller than the nodes to the right. Balanced trees offer the best performance.

Red-Black Tree A tree in which the black values remain constant, and the red values may change.

B-Tree A self-balancing BST variant in which nodes are allowed to have more than two children.

Union Find Also known as disjoint set structure. Stores non-overlapping sets.

Full Stack Engineering

Data Structures and Algorithms

Common Data Structures

  • Arrays: Contiguous area of memory consisting of equal-size elements indexed by contiguous integers.
  • Linked Lists: Composed of nodes that hold data and a reference (or link) to the next node in the sequence.
  • Stacks: LIFO (last in, first out) data structure implemented with linked lists or arrays.
  • Queues: FIFO (first in, first out) data structure that can be implemented using arrays, stack, or linked lists.
  • Hash Tables: Key-value pairs storage for efficient lookup, addition, and deletion.
  • Trees: Hierarchical data structure with a root value and subtrees of children with a parent node.
  • Graphs: Consists of nodes (vertices) and edges that connect pairs or groups of nodes.

Key Algorithms

  • Sorting: Quick sort, merge sort, bubble sort, insertion sort.
  • Searching: Binary search, depth-first search (DFS), breadth-first search (BFS).
  • Graph Algorithms: Dijkstra’s shortest path, A* algorithm, Bellman-Ford algorithm.
  • Dynamic Programming: Fibonacci series, knapsack problem, minimum path sum.

Python Cheat Sheet

Basic Syntax

  • Variables: x = 10
  • Data Types: int, float, str, bool, list, tuple, dict
  • Control Structures:
    if x < 10:
        print("Less than 10")
    elif x > 10:
      print("Greater than 10")
    else:
        print("Equals 10")
  • Loops:
    for i in range(10):
    print(i)
    
    while x < 10:
        print(x)
        x += 1
  • Functions:
    def my_function(param1, param2):
        return param1 + param2
  • Classes:
    class MyClass:
        def __init__(self, param):
            self.param = param
        
        def method(self):
            return self.param
  • Modules:
    import math
    from math import pi
  • Exceptions:
    try:
        x = 1 / 0
    except ZeroDivisionError as e:
        print(e)
  • File I/O:
    with open('file.txt', 'r') as file:
        data = file.read()
  • List Comprehension:
    squares = [x**2 for x in range(10)]
  • Lambda Functions:
    f = lambda x: x**2
  • Map and Filter:
    numbers = [1, 2, 3, 4, 5]
    squares = list(map(lambda x: x**2, numbers))
    evens = list(filter(lambda x: x % 2 == 0, numbers))
  • Generators:
    def my_generator(n):
        for i in range(n):
            yield i**2

Important Libraries

  • NumPy: Numerical computing library for working with arrays.
  • Pandas: Data manipulation and analysis library.
  • Matplotlib: Data visualization library.
  • Scikit-learn: Machine learning library for classification, regression, clustering, etc.
  • TensorFlow: Open-source machine learning library for high-performance numerical computation.
  • PyTorch: Open-source machine learning library for natural language processing, computer vision, etc.
  • SciPy: Library for scientific and technical computing.
  • Django: High-level web framework for rapid development and clean design.

JavaScript Cheat Sheet

Basic Syntax

  • Variables: let x = 10
  • Data Types: number, string, boolean, array, object, function
  • Control Structures:
    if (x < 10) {
        console.log("Less than 10");
    } else if (x > 10) {
        console.log("Greater than 10");
    } else {
        console.log("Equals 10");
    }
  • Loops:
    for (let i = 0; i < 10; i++) {
        console.log(i);
    }
  • Functions:
    function myFunction(param1, param2) {
        return param1 + param2;
    }
  • Classes:
    class MyClass {
        constructor(param) {
            this.param = param;
        }
        
        method() {
            return this.param;
        }
    }
  • Modules:
    import math from 'math';
  • Exceptions:
    try {
        x = 1 / 0;
    } catch (e) {
        console.log(e);
    }
  • Promises:
    const myPromise = new Promise((resolve, reject) => {
        if (success) {
            resolve('Success');
        } else {
            reject('Failure');
        }
    });
  • Async/Await:
    async function myAsyncFunction() {
        let result = await promise;
        console.log(result);
    }
  • Array Methods:
    const numbers = [1, 2, 3, 4, 5];
    const squares = numbers.map(x => x**2);
    const evens = numbers.filter(x => x % 2 === 0);
  • Arrow Functions:
    const f = x => x**2;
  • Destructuring:
    const { param1, param2 } = obj;
  • Spread Operator:
    const arr1 = [1, 2, 3];
    const arr2 = [...arr1, 4, 5];
  • Template Literals:
    const name = 'Alice';
    const greeting = `Hello, ${name}!`;

Key Features

  • Event Handling: Respond to user actions like clicks, scrolls, etc.
    • document.getElementById('myButton').addEventListener('click', () => {
          console.log('Button clicked');
      });
  • DOM Manipulation: Modify the structure, style, or content of HTML elements.
    • document.getElementById('myId').innerHTML = 'Hello, World!';
  • AJAX: Asynchronous JavaScript and XML for making requests to the server.
    • fetch('https://api.example.com/data')
          .then(response => response.json())
          .then(data => console.log(data));
  • Promises: Handle asynchronous operations and avoid callback hell.
    • const myPromise = new Promise((resolve, reject) => {
          if (success) {
              resolve('Success');
          } else {
              reject('Failure');
          }
      });
  • Async/Await: Syntactic sugar for promises to write asynchronous code.
    • async function myAsyncFunction() {
          let result = await promise;
          console.log(result);
      }

Important Frameworks and Libraries

  • React: JavaScript library for building user interfaces.
  • Node.js: JavaScript runtime built on Chrome's V8 JavaScript engine.
  • Express: Web application framework for Node.js.

Git

git rebase is a command that allows you to easily change a series of commits, reordering, editing, or squashing commits together into a single commit.

git reset is a command that allows you to undo changes by moving the HEAD and branch ref to the previous commit.

git revert is a command that allows you to create a new commit that undoes a previous commit.

git cherry-pick is a command that allows you to apply a commit from one branch to another.

git checkout is a command that allows you to switch branches or restore working tree files.

git clean is a command that allows you to remove untracked files from the working tree.

git stash is a command that allows you to save changes that you don't want to commit immediately.

git bisect is a command that allows you to find the commit that introduced a bug.

git reflog is a command that allows you to see a log of changes to the HEAD.

git blame is a command that allows you to see who last modified each line of a file.

git log is a command that allows you to see a log of commits.

git status is a command that allows you to see the status of the working tree.

git diff is a command that allows you to see changes between commits, branches, etc.

git merge is a command that allows you to merge changes from one branch into another.

git pull is a command that allows you to fetch and merge changes from a remote repository.

git push is a command that allows you to push changes to a remote repository.

git clone is a command that allows you to clone a repository from a remote server.

git init is a command that allows you to create a new repository.

git config is a command that allows you to set configuration options.

git remote is a command that allows you to manage remote repositories.

git tag is a command that allows you to create, list, delete, or verify tags.

git branch is a command that allows you to list, create, or delete branches.

HTML

rel

Possible values for rel attribute:

<a rel="alternate">Print page, translated, mirror, etc.</a>
<a rel="author">creator credit</a>
<a rel="bookmark">permanent url</a>
<a rel="external">link to different site</a>
<a rel="help">more info</a>
<a rel="license">info</a>
<a rel="next">series</a>
<a rel="nofollow">unendorsed, used by Google. Google won't follow</a>
<a rel="noopener">no context passed</a>
<a rel="noreferrer">won't know where they came from</a>
<a rel="prev">series</a>
<a rel="search"></a>
<a rel="tag">keyword, taxonomy, etc.</a>

meta

Possible values for meta attribute:

<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Free Web tutorials">
<meta name="keywords" content="HTML, CSS, JavaScript">
<meta name="author" content="John Doe">
<meta name="robots" content="index, follow">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="Content-Language" content="en">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<meta http-equiv="refresh" content="30">
<meta http-equiv="expires" content="0">
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="imagetoolbar" content="no">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta http-equiv="x-dns-prefetch-control" content="on">

JavaScript

Ways to write an arrow function with args

Option 1

const uWhat = what => 'You ' + what;

var whatUDid = '__Did What?__';
  uWhat(whatUDid);

Option 2

const uWhat2 = (what) => 'You ' + what;

var whatUDid2 = '__Did What Again?__';
uWhat2(whatUDid2);

Basic TDD in JS

const sumAB = (a, b) => a + b;

function sumABTest() {
    if (sumAB(5,11) === 16) {
        return 'Pass';
    } else {
        return 'Fail';
    }
}

document.body.innerHTML = sumABTest();

JS Imports

// Define the appmodule
const appmodule = {
  // Define some functionalities
  greet: function(name) {
    console.log(`Hello, ${name}!`);
  },
  // Add more functionalities as needed
};

// Export the appmodule
export { appmodule };

Machine Learning

Machine Learning Steps

  1. Problem Statement
  2. Data Collection
  3. Data Preprocessing
  4. Feature Selection
  5. Choose Model
  6. Train Model
  7. Parameter Tuning
  8. Prediction

Problem Statement

Source: https://colab.research.google.com/drive/13beu02DtqZM3zoMpYjvO25fLXXGCl5tN?usp=sharing A series of questions to help pick an algorithm for a problem.

Questions

  1. Which scientific question would you like to answer?
  2. What information is missing to answer the question?
  3. What data do you have available?
  4. Do you have training data available?
  5. What is the structure of your data?
  6. Is your dependant variable continuous or discrete?
  7. Are there any performance requirements? (e.g., real time vs static)
  8. Where are is your software running? (e.g., cloud, local, embedded)
  9. What is more important for your project: inference time or model performance?

Alogrithms

  1. Linear Regression
  2. Logistic Regression
  3. Support Vector Machines
  4. Decision Trees
  5. Random Forests
  6. K-Nearest Neighbors
  7. Naive Bayes
  8. Neural Networks
  9. K-means Clustering splits the data set into "k" clusters using euclidean distance. the center of each group is the centroid.
    • a K number of centroids ish chosen
    • the data i shuffled and K data points for centroids are chosen
    • create new centroids by calculating the mean of all samples assigned to each previous centroid
    • repeat random assignment of centroid until the centroids no longer move
  10. Principal Component Analysis

Linear Regression

Simple Linear Regression

The relationship between an independent variable and a dependent variable. Example: What is the relationship between the curve of a shot and where the foot strikes the ball? ŷ (curve) = β0 + β1 (where the foot strikes the ball) β0 is the curve of the ball when the foot strikes the ball at the center β1 is the change in curve with one unit of change in where the foot strikes the ball

Example 2: What is the change in relationship between sunlight and the growth of a redwood? ŷ (growth) = β0 + β1 (sunlight) β0 is the growth of the redwood when there is no sunlight β1 is the change in growth when there is 1 lux of sunlight

Clustering

Goal of clustering is to create groups of data. Clusters are sometimes enough for classification, but sometimes the data is too complex to be classified by clusters alone.

Random Forest

Glossary of Domain, DNS, and Networking Terminology

A

  • APIPA (Automatic Private IP Addressing): A feature in Windows operating systems for automatically assigning a unique IP address from a predefined range when a DHCP server is not available.
    • Example: A computer might automatically assign itself an IP address in the range of 169.254.x.x if it cannot find a DHCP server.

B

  • Bandwidth: The maximum rate of data transfer across a given path.
    • Example: A home internet connection might have a bandwidth of 100 Mbps.

C

  • CIDR (Classless Inter-Domain Routing): A method for allocating IP addresses and routing Internet Protocol packets.

    • Example: An IP address with CIDR notation like 192.168.0.0/24 indicates a subnet mask of 255.255.255.0.
  • CNAME (Canonical Name Record): A type of DNS record that maps an alias name to a true or canonical domain name.

    • Example: A CNAME record might map the alias www.example.com to the canonical name example.com.

D

  • DHCP (Dynamic Host Configuration Protocol): A network management protocol used on IP networks whereby a DHCP server dynamically assigns an IP address and other network configuration parameters to each device on a network.

    • Example: A home router often acts as a DHCP server to provide IP addresses to devices on your home network.
  • DNS (Domain Name System): A hierarchical and decentralized naming system for computers, services, or other resources connected to the Internet or a private network.

    • Example: Typing www.google.com into your browser uses DNS to find the IP address of Google's servers.
  • Domain Name: A human-readable address used to access websites on the Internet.

    • Example: www.ecotrust.org is a domain name for Ecotrust's website.

E

  • Ethernet: A family of networking technologies commonly used in local area networks (LAN), metropolitan area networks (MAN) and wide area networks (WAN).
    • Example: The typical blue or yellow cables used to connect devices to a network are Ethernet cables.

F

  • Firewall: A network security system that monitors and controls incoming and outgoing network traffic based on predetermined security rules.
    • Example: A company may use a firewall to block access to certain websites within its network.

G

  • Gateway: A network node that serves as an access point to another network, often involving not only a change of addressing, but also a different networking technology.
    • Example: A home router can act as a gateway between your local home network and the internet.

H

  • HTTP (Hypertext Transfer Protocol): The foundation of data communication for the World Wide Web.
    • Example: When you visit a website, your browser uses HTTP to request web pages from a server.

I

  • IP Address (Internet Protocol Address): A numerical label assigned to each device connected to a computer network that uses the Internet Protocol for communication.

    • Example: An IP address could be something like 192.168.1.1.
  • ISP (Internet Service Provider): An organization that provides services for accessing, using, or participating in the Internet.

    • Example: Comcast, Verizon, and AT&T are examples of ISPs in the United States.

L

  • LAN (Local Area Network): A network that connects computers and devices in a limited geographical area such as a home, school, computer laboratory, or office building.
    • Example: The network in an office building connecting all the computers to each other and to the printer is a LAN.

M

  • MAC Address (Media Access Control Address): A unique identifier assigned to a network interface controller for use as a network address in communications within a network segment.
    • Example: A MAC address may look like 00:1A:C2:7B:00:47.

N

  • NAT (Network Address Translation): A method of remapping one IP address space into another by modifying network address information in the IP header of packets while they are in transit across a traffic routing device.
    • Example: A router uses NAT to translate the private IP addresses of devices on your home network to a single public IP address for internet access.

P

  • Ping: A diagnostic utility used to test the reachability of a host on an IP network and to measure the round-trip time for messages sent from the originating host to a destination computer.

    • Example: Using the command ping www.google.com in your terminal or command prompt to check your connection to Google's servers.
  • Protocol: A set of rules or procedures for transmitting data between electronic devices, such as computers.

    • Example: HTTPS is a protocol used for secure communication over a computer network.

R

  • Router: A networking device that forwards data packets between computer networks.
    • Example: A device in your home that connects your local network to the internet is a router.

S

  • SSL (Secure Sockets Layer): A standard security technology for establishing an encrypted link between a server and a client.
    • Example: Websites use SSL to secure transactions and data transfer, as indicated by https:// in the URL.

T

  • TCP/IP (Transmission Control Protocol/Internet Protocol): The basic communication language or protocol of the Internet.
    • Example: When you load a webpage, your computer uses TCP/IP to request data from the server and assemble the web page.

U

  • UDP (User Datagram Protocol): A communications protocol used for time-sensitive transmissions such as video playback or DNS lookups.
    • Example: Streaming services might use UDP for transmitting video data due to its speed.

V

  • VPN (Virtual Private Network): A service that allows you to connect to the Internet via a server run by a VPN provider.
    • Example: Using a VPN service to access content that is region-locked or to secure your internet connection on public Wi-Fi.

W

  • WAN (Wide Area Network): A telecommunications network that extends over a large geographical area for the primary purpose of computer networking.
    • Example: The internet itself is the largest example of a WAN, connecting millions of networks worldwide.

PSQL

Connect to a Database: psql -d db_name -U user_name. Check Postgres Version: SELECT VERSION();. List All Databases: \l. Access or Switch a Database: \c db_name. List All Tables: \dt. Describe All Tables: \d. Describe a Specific Table: \d tab_name. List All Schemas: \dn. List All Views: \dv. List All Functions: \df. List All Users: \du. Show Commands History: \s Save Query’s Results to a Specific File: \o file_name. Run psql Commands/queries From a Particular File: \i file_name. Execute Previous Command: \g. Show Query Execution Time: \timing. Get Output in HTML Format: \H. Align Columns Output: \a. Get Help: \h. Get All psql Commands: \?. Clear Screen: \! cls. Quit psql: \q.

Python

Data Types

* Numbers
    * int: Integer `1`
    * float: Floating-point number `2.5`
    * complex: Complex number `1 + 2j`
* Strings: str `'hello'`
* Lists: list `[1, 2, 3]`
    * Ordered list of items, like JS arrays
* Dictionaries: dict `{'key': 'value'}`
    * Unordered key-value pairs
* Tuples: tuple `(1, 2, 3)`
    * Immutable ordered list of items
* Sets: set `{1, 2, 3}`
    * Unordered collection of unique items
* Booleans: bool 
    * `True` or `False`
* None: NoneType
    * Represents the absence of a value

Determining the Type of an Object

type(1) # int

Python Operators

* Arithmetic Operators
    * `+` Addition
    * `-` Subtraction
    * `*` Multiplication
    * `/` Division
    * `//` Floor Division
        * Returns the integer part of the division
        * `5 // 2` returns `2` because `5 / 2 = 2.5`
        * `4 // 2` returns `2` because `4 / 2 = 2`
    * `%` Modulus
        * Returns the remainder of integer division
        * `5 % 2` returns `1` because `5 / 2 = 2 remainder 1`
        * `4 % 2` returns `0` because `4 / 2 = 2 remainder 0
        * `2 % 4` returns `2` because `2 / 4 = 0 remainder 2`
    * `**` Exponentiation

Data Structures

Queue

class Queue:
    def __init__(self):
        self.items = deque()

    def is_empty(self):
        return not self.items
        
    def enqueue(self, item):
        self.items.append(item)

    def dequeue(self):
        return self.items.popleft()

    def size(self):
        return len(self.items)

    def peek(self):
        return self.items[0]

    def __str__(self):
        return str(self.items)

Python Files

Teminology

Shallow Copy: A shallow copy creates a new object but does not create copies of nested objects. Changes to the original object will be reflected in the shallow copy.

import copy
a = [1, 2, [3, 4]]
b = copy.copy(a)

Deep Copy: A deep copy creates a new object and recursively creates copies of nested objects. Changes to the original object will not be reflected in the deep copy. List Comprehension: A concise way to create lists in Python.

squares = [x**2 for x in range(10)]

Generator Expression: A concise way to create generators in Python.

squares = (x**2 for x in range(10))

Lambda Function: An anonymous function defined using the lambda keyword.

add = lambda x, y: x + y

Map Function: A function that applies another function to each item in an iterable.

squares = map(lambda x: x**2, range(10))

Filter Function: A function that filters items in an iterable based on a predicate function.

evens = filter(lambda x: x % 2 == 0, range(10))

Reduce Function: A function that applies another function cumulatively to items in an iterable.

sum = reduce(lambda x, y: x + y, range(10))

Decorator: A function that modifies the behavior of another function or class.

def decorator(func):
    def wrapper(*args, **kwargs):
        print('Before function call')
        result = func(*args, **kwargs)
        print('After function call')
        return result
    return wrapper

Context Manager: An object that enables the use of the with statement.

with open('file.txt', 'r') as f:
    data = f.read()

GIL: Global Interpreter Lock, a mutex that protects access to Python objects in memory.

import threading
lock = threading.Lock()
lock.acquire()
# Critical section
lock.release()

File Types

py - Python script pyc - Compiled Python script pyd - Python script with C extensions pyo - Optimized Python script pyw - Python script without console window

Installation

Django

Clear Django Cache

from django.core.cache import cache
cache.clear()

# If using uwsgi
sudo service uwsgi restart

# If using nginx
sudo service nginx restart

Exceptions

Try Except

try:
    # Code that may raise an exception
except Exception as e:
    # Handle the exception

Testing

* unittest: Standard library for testing
* Doctest: Test cases embedded in docstrings
* pytest: Third-party testing framework
* Hypothesis: Property-based testing library
* mock: Library for mocking objects
* tox: Tool for testing across multiple Python versions
* coverage: Tool for measuring code coverage

Common Algorithms

Depth First Search [Stack]

Depth First Search (DFS) is an algorithm for traversing or searching tree or graph data structures. The algorithm starts at the root node and explores as far as possible along each branch before backtracking.

Stack: [B, E]
Predecessor: {
    A: None
    B: A
    D: A
    E: D
    G: D
    H: G
    I: H
}

* Pop the stack
* Is this the goal?
* If so, done
* Otherwise, push undiscovered neighbors and update predecessor
* Repeat until stack is empty
def dfs(graph, start, visited=None):
    if visited is None:
        visited = set()
    visited.add(start)
    for next in graph[start] - visited:
        dfs(graph, next, visited)
    return visited

Breadth First Search [Queue]

Breadth First Search (BFS) is an algorithm for traversing or searching tree or graph data structures. The algorithm starts at the root node and explores all of the neighbor nodes at the present depth prior to moving on to the nodes at the next depth level.

Uses:

* GPS systems
* Social networking sites to find connections between users
* Flight reservation systems
* Finding neighbor nodes in peer-to-peer networks
* Web crawlers
* Many application in artifical intelligence
* Electronic and communication engineering
* Scientific modeling

Sample Problems

Two Sum

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

def two_sum(nums, target):
    num_map = {}
    for i, num in enumerate(nums):
        if target - num in num_map:
            return [num_map[target - num], i]
        num_map[num] = i
    return []

Design a Scalable URL Shortener

* Components:
    * Database: To store URL mappings.
    * Service: To encode and decode URLs.
    * API: To handle HTTP requests.
* Considerations:
    * Uniqueness: Ensure that the generated short URL is unique.
    * Scalability: Ensure that the system can handle a large number of requests.
    * Redundancy: Ensure data is not lost.

Sort using a lambda function

data = [{'name': 'Alice', 'age': 28}, {'name': 'Bob', 'age': 21}, {'name': 'Charlie', 'age': 35}]

sorted_data = sorted(data, key=lambda x: x['age'])
print(sorted_data)

Sample Questions

Decorators

Question: What are decorators in Python, and how are they used?

Answer: Decorators are a way to modify the behavior of a function or class in Python without changing its source code. They are implemented as callable objects that take another function or class as an argument and return a modified version of it.

None or == None

Question: What is the difference between x is None and x == None in Python?

Answer: * x is None expression checks if x is the None object * x == None expression checks if x is equal to None * Tricky question, in the case of None there is no difference

Syntax

Question: What is the output of the following code?

def f(a,list=[]):
    for i in range(a):
        list.append(i*i)
    print(list) 
 
f(3)
f(2,[1,2,3])
f(2) 

Answer: [0,1,4] [1,2,3,0,1] [0,1,4]

Function Keyword Arguments

Question: In a function, what is the meaning of *args and **kwargs?

Answer: * *args is used to pass a variable number of non-keyword arguments to a function * **kwargs is used to pass a variable number of keyword arguments to a function

List Slice

Question: What is the output of the following code?

list = ['1', ‘2', '3', '4', '5']
print (list[12:]) 

Answer: []

Differences between Python 2 and Python 3

Question: What are the key differences between Python 2 and Python 3?

Answer: * Print Statement: Python 2 uses print as a statement, while Python 3 uses print as a function. * Integer Division: Python 2 performs integer division by default, while Python 3 performs floating-point division. * Unicode Support: Python 3 uses Unicode by default, while Python 2 uses ASCII. * xrange: Python 2 uses xrange for iteration, while Python 3 uses range. * Error Handling: Python 3 uses raise as a function, while Python 2 uses raise as a statement.

Reasons to Dislike Python

Question: What are some reasons to dislike Python?

Eucledian Distance

The Euclidean distance is a measure of the straight-line distance between two points in a two-dimensional space. To calculate the Euclidean distance between two points (x1, y1) and (x2, y2) in a Cartesian coordinate system, you can use the formula:

d = √((x2 - x1)^2 + (y2 - y1)^2)

Given the points (2, 3) and (10, 8), you can calculate the Euclidean distance as follows:

  1. Coordinates:

    • Point 1: (x1, y1) = (2, 3)
    • Point 2: (x2, y2) = (10, 8)
  2. Euclidean Distance Calculation:

    • Substitute the coordinates into the formula:
      d = √((10 - 2)^2 + (8 - 3)^2)
      d = √(8^2 + 5^2)
      d = √(64 + 25)
      d = √89
      
    • in python:
  3. Final Result:

    • The Euclidean distance between the points (2, 3) and (10, 8) is √89, which is approximately 9.43.

Therefore, the Euclidean distance between the points (2, 3) and (10, 8) in a two-dimensional space is approximately 9.43 units. This distance represents the length of the straight line connecting the two points on the coordinate plane.

QEMU

QEMU is a virtual machine manager that can be used to run virtual machines on a Linux host. It is a very powerful tool that can be used to emulate a wide variety of hardware. It is also the basis for many other virtual machine managers, such as KVM and VirtualBox.

Trouble Shooting

QEMU is not starting sudo service qemu restart

Spatial / GIS

Data Types

Vector Features Vector data is made up of vertices (points) and paths (direction between connected points).

  • points - XY coordinates
    • [-168.12,78.54]
    • used when features are too small to be a polygon
  • lines - a set of connected points (vertices)
    • [[-168.12,78.54], [-169.12,79.54], [-170.12,80.54]]
  • polygons - a set of connected points (vertices) that start and end at the same point
    • [[-168.12,78.54], [-169.12,79.54], [-170.12,80.54], [-168.12,78.54]]
    • used for features that have an area

Raster Features Raster data is made up of pixels (grid cells). Pixels are usually regularly spaced and square, but don't have to be. Each pixel (grid cell) has its own value(s) or class(es).

  • discrete - each grid cell has a distinct value or category.
    • usually represented by an integer
  • continuous - each grid cell has a value that shows the change from a fixed registration point
    • used for elevation, temperature, or an aerial photograph
    • e.g., elevation uses sea level as a registration point and each cell shows change from sea level

Computer Science Staples

Data Types

Strings

JS

    var string = 'I am a string';

Py

  string = 'I am a string'

Numbers

JS

    var number = 18;

Py

  number = 18

Arrays

JS

    var array = ['0 index', '1 index', ['multi-dimensional'], '3 index'];

Py

  array = ['0 index', '1 index', '2 index']

Lists

Terminal

Terminal is a program that allows you to interact with your computer using text commands. It is also known as a command line interface (CLI) or console. It is a powerful tool that allows you to do almost anything you can do with a GUI (graphical user interface).

Terminal Commands Cheat Sheet

File Management

  • ls: List directory contents.

    • Usage: ls [options] [directory]
    • Example: ls -l /home/user
  • cd: Change the current directory.

    • Usage: cd [directory]
    • Example: cd /var/www
  • mkdir: Create a new directory.

    • Usage: mkdir [directory]
    • Example: mkdir new_folder
  • rmdir: Remove an empty directory.

    • Usage: rmdir [directory]
    • Example: rmdir old_folder
  • rm: Remove files or directories.

    • Usage: rm [options] [file/directory]
    • Example: rm -rf old_folder
  • cp: Copy files and directories.

    • Usage: cp [options] source destination
    • Example: cp file.txt /home/user/Desktop
  • mv: Move/rename files and directories.

    • Usage: mv [options] source destination
    • Example: mv file.txt new_file.txt

File Viewing & Editing

  • cat: Concatenate and display file content.

    • Usage: cat [file]
    • Example: cat file.txt
  • less: View file content in an interactive way.

    • Usage: less [file]
    • Example: less file.txt
  • nano: Edit files using the nano editor.

    • Usage: nano [file]
    • Example: nano file.txt
  • vi/vim: Edit files using the vi/vim editor.

    • Usage: vi [file]
    • Example: vi file.txt

System Information

  • pwd: Print the name of the current working directory.

    • Usage: pwd
    • Example: pwd
  • whoami: Display the current user.

    • Usage: whoami
    • Example: whoami
  • df: Report file system disk space usage.

    • Usage: df [options]
    • Example: df -h
  • top: Display Linux processes.

    • Usage: top
    • Example: top

Networking

  • ping: Check the network connectivity.

    • Usage: ping [options] destination
    • Example: ping google.com
  • ifconfig: Configure or display network interface parameters.

    • Usage: ifconfig [options]
    • Example: ifconfig
  • ssh: Securely connect to a remote machine.

    • Usage: ssh [user@]hostname
    • Example: ssh user@192.168.1.1
  • scp: Securely copy files between hosts.

    • Usage: scp [options] source destination
    • Example: scp file.txt user@192.168.1.1:/path

File Permissions

  • chmod: Change file access permissions.

    • Usage: chmod [options] mode file
    • Example: chmod 755 script.sh
  • chown: Change file owner and group.

    • Usage: chown [options] owner[:group] file
    • Example: chown user:user file.txt

File Manipulation Commands Cheat Sheet

Creating Files and Directories

  • touch: Create a new empty file.

    • Usage: touch [file]
    • Example: touch newfile.txt
  • mkdir: Create a new directory.

    • Usage: mkdir [directory]
    • Example: mkdir new_directory

Viewing File Content

  • cat: Display the contents of a file.

    • Usage: cat [file]
    • Example: cat file.txt
  • less: View the contents of a file one page at a time.

    • Usage: less [file]
    • Example: less file.txt
  • head: Display the first few lines of a file.

    • Usage: head [options] [file]
    • Example: head -n 5 file.txt
  • tail: Display the last few lines of a file.

    • Usage: tail [options] [file]
    • Example: tail -n 5 file.txt

Editing Files

  • nano: Edit a file using the nano editor.

    • Usage: nano [file]
    • Example: nano file.txt
  • vi/vim: Edit a file using the vi/vim editor.

    • Usage: vi [file]
    • Example: vi file.txt

Copying, Moving, and Renaming Files

  • cp: Copy files or directories.

    • Usage: cp [options] source destination
    • Example: cp file.txt /path/to/destination
  • mv: Move or rename files or directories.

    • Usage: mv [options] source destination
    • Example: mv file.txt newfile.txt

Convert File Formats

for file in *.HEIC; do sips -s format jpeg "$file" --out "${file%.*}.jpg"; done

Deleting Files and Directories

  • rm: Remove files or directories.

    • Usage: rm [options] [file/directory]
    • Example: rm file.txt
  • rmdir: Remove empty directories.

    • Usage: rmdir [directory]
    • Example: rmdir empty_directory

File Permissions and Ownership

  • chmod: Change file access permissions.

    • Usage: chmod [options] mode file
    • Example: chmod 755 script.sh
  • chown: Change file owner and group.

    • Usage: chown [options] owner[:group] file
    • Example: chown user:user file.txt

Searching and Sorting Files

  • grep: Search for patterns within files.

    • Usage: grep [options] pattern [file]
    • Example: grep "search text" file.txt
  • sort: Sort lines of text files.

    • Usage: sort [options] [file]
    • Example: sort file.txt
  • find: Search for files in a directory hierarchy.

    • Usage: find [path] [options]
    • Example: find /path/to/search -name "filename"

File Compression and Archiving

  • tar: Archive multiple files into a tarball.

    • Usage: tar [options] [archive-file] [file/directory]
    • Example: tar -cvf archive.tar /path/to/directory
  • gzip: Compress or expand files.

    • Usage: gzip [options] [file]
    • Example: gzip file.txt
  • zip: Package and compress (archive) files.

    • Usage: zip [options] [archive-file] [file]
    • Example: zip archive.zip file.txt

ps

ps is a command that lists the processes running on your computer.

kill

kill is a command that allows you to kill a process. It takes the PID of the process as an argument.

aux

aux is a command that lists all the processes running on your computer.

grep

grep is a command that searches for a pattern in a file. grep -i ignoring case. grep -v prints the lines that do not match the pattern. grep -n along with the line number.

chown

chown is a command that changes the owner of a file or directory. sudo chown ${USER}:${USER} ./ changes the owner of the current directory to the current user.

PATH

PATH is a variable which stores directories that are searched when a command is entered.

echo $PATH prints the PATH variable.

add to PATH

Adding a directory to the PATH expands the number of directories that are searched when a command is entered in the shell from any directory.

export PATH=$PATH:/path/to/directory adds a directory to the PATH variable. export PATH=/path/to/directory:$PATH adds a directory to the beginning of the PATH variable.

iTerm2 Shell Integration Commands

imgcat filename
  Displays the image inline.
imgls
  Shows a directory listing with image thumbnails.
it2api
  Command-line utility to manipulate iTerm2.
it2attention start|stop|fireworks
  Gets your attention.
it2check
  Checks if the terminal is iTerm2.
it2copy [filename]
  Copies to the pasteboard.
it2dl filename
  Downloads the specified file, saving it in your Downloads folder.
it2setcolor ...
  Changes individual color settings or loads a color preset.
it2setkeylabel ...
  Changes Touch Bar function key labels.
it2tip
  iTerm2 usage tips
it2ul
  Uploads a file.
it2universion
  Sets the current unicode version.
it2profile
  Change iTerm2 session profile on the fly.  

Useful Scripts

Convert a .mov to a .mp4: ffmpeg -i "$input_file" -c:v libx264 -crf 23 -preset medium -c:a aac -b:a 128k "$output_file"

Terminology (FDA.gov)

abstraction: The separation of the logical properties of data or function from its implementation in a computer program. See: encapsulation, information hiding, software engineering.

algorithm: (IEEE) (1) A finite set of well-defined rules for the solution of a problem in a finite number of steps. (2) Any sequence of operations for performing a specific task.

analog: Pertaining to data [signals] in the form of continuously variable [wave form] physical quantities; e.g., pressure, resistance, rotation, temperature, voltage. Contrast with digital.

analog-to-digital converter: Input related devices which translate an input device's [sensor] analog signals to the corresponding digital signals needed by the computer. Contrast with DAC [digital-to-analog converter]. See: analog, digital.

analysis: (1) To separate into elemental parts or basic principles so as to determine the nature of the whole. (2) A course of reasoning showing that a certain result is a consequence of assumed premises. (3) (ANSI) The methodical investigation of a problem, and the separation of the problem into smaller related units for further detailed study.

application software: (IEEE) Software designed to fill specific needs of a user; for example, software for navigation, payroll, or process control. Contrast with support software; system software.

architectural design: (IEEE) (1) The process of defining a collection of hardware and software components and their interfaces to establish the framework for the development of a computer system. See: functional design. (2) The result of the process in (1). See: software engineering.

architecture: (IEEE) The organizational structure of a system or component. See: component, module, subprogram, routine.

archival database: (ISO) An historical copy of a database saved at a significant point in time for use in recovery or restoration of the database.

archive: (IEEE) A lasting collection of computer system data or other records that are in long term storage.

archive file: (ISO) A file that is part of a collection of files set aside for later research or verification, for security purposes, for historical or legal purposes, or for backup.

array: (IEEE) An n-dimensional ordered set of data items identified by a single name and one or more indices, so that each element of the set is individually addressable; e.g., a matrix, table, or vector.

asynchronous: Occurring without a regular time relationship, i.e., timing independent.

auxiliary storage: Storage device other than main memory [RAM]; e.g., disks and tapes.

band: Range of frequencies used for transmitting a signal. A band can be identified by the difference between its lower and upper limits, i.e. bandwidth, as well as by its actual lower and upper limits; e.g., a 10 MHz band in the 100 to 110 MHz range.

bandwidth: The transmission capacity of a computer channel, communications line or bus. It is expressed in cycles per second [Hz], and also is often stated in bits or bytes per second. See: band.

baseline: (NIST) A specification or product that has been formally reviewed and agreed upon, that serves as the basis for further development, and that can be changed only through formal change control procedures.

batch: (IEEE) Pertaining to a system or mode of operation in which inputs are collected and processed all at one time, rather than being processed as they arrive, and a job, once started, proceeds to completion without additional input or user interaction. Contrast with conversational, interactive, on-line, real time.

benchmark: A standard against which measurements or comparisons can be made.

bias: A measure of how closely the mean value in a series of replicate measurements approaches the true value. See: accuracy, precision, calibration.

block: (ISO) (1) A string of records, words, or characters that for technical or logical purposes are treated as a unity. (2) A collection of contiguous records that are recorded as a unit, and the units are separated by interblock gaps. (3) A group of bits or digits that are transmitted as a unit and that may be encoded for error-control purposes. (4) In programming languages, a subdivision of a program that serves to group related statements, delimit routines, specify storage allocation, delineate the applicability of labels, or segment parts of the program for other purposes.

bootstrap: (IEEE) A short computer program that is permanently resident or easily loaded into a computer and whose execution brings a larger program, such an operating system or its loader, into memory.

bug: A fault in a program which causes the program to perform in an unintended or unanticipated manner. See: anomaly, defect, error, exception, fault.

coding: (IEEE) (1) In software engineering, the process of expressing a computer program in a programming language. (2) The transforming of logic and data from design specifications (design descriptions) into a programming language. See: implementation.

coding standards: Written procedures describing coding [programming] style conventions specifying rules governing the use of individual constructs provided by the programming language, and naming, formatting, and documentation requirements which prevent programming errors, control complexity and promote understandability of the source code. Syn: development standards, programming standards.

framework: Software that exists within a programming language to make development in any piece of a tech stack faster, easier, or less error-prone.

headless: A software application that can run without a graphical user interface.

programming language: Software that’s built to help developers create instructions that a computer can understand.

tech stack: The unique technologies a team combines (aka stacks) to build custom software.

vector: Multiple meanings: - A phrase to describe a number with a direction. Used by someone who is trying to sound smart. - A vector can also be a one dimensional array of data elements of the same type.

Testing

JavaScript

const sumAB = (a, b) => a + b;

function sumABTest() {
    if (sumAB(5,11) === 16) {
        return 'Pass';
    } else {
        return 'Fail';
    }
}

document.body.innerHTML = sumABTest();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment