- Algorithms
- Classifications
- Data Structures
- Git
- HTML
- JavaScript
- Machine Learning
- PSQL
- Python
- QEMU
- Spatical Data
- Staples
- Terminal
- Terminology
- Testing
TODO:
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;
};
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;
}
};
}
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.
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.
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.
Stopping Criteria Stop splitting once all decision stumps are classified OR out of features to split on
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('');
}
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
}
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;
}
}
}
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;
}
}
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;
}
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);
}
}
function factorial(n) {
if (n === 0) {
return 1;
} else {
return n * factorial(n - 1);
}
}
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)
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
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;
}
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;
}
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;
}
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;
}
}
}
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];
}
// 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;
};
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]));
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;
}
Lists are mutable sequences, typically used to store collections of homogeneous items.
fruits = ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana']
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 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 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()
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;
};
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:
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;
};
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;
};
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();
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()
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.
x = 10
int
, float
, str
, bool
, list
, tuple
, dict
if x < 10:
print("Less than 10")
elif x > 10:
print("Greater than 10")
else:
print("Equals 10")
for i in range(10):
print(i)
while x < 10:
print(x)
x += 1
def my_function(param1, param2):
return param1 + param2
class MyClass:
def __init__(self, param):
self.param = param
def method(self):
return self.param
import math
from math import pi
try:
x = 1 / 0
except ZeroDivisionError as e:
print(e)
with open('file.txt', 'r') as file:
data = file.read()
squares = [x**2 for x in range(10)]
f = lambda x: x**2
numbers = [1, 2, 3, 4, 5]
squares = list(map(lambda x: x**2, numbers))
evens = list(filter(lambda x: x % 2 == 0, numbers))
def my_generator(n):
for i in range(n):
yield i**2
let x = 10
number
, string
, boolean
, array
, object
, function
if (x < 10) {
console.log("Less than 10");
} else if (x > 10) {
console.log("Greater than 10");
} else {
console.log("Equals 10");
}
for (let i = 0; i < 10; i++) {
console.log(i);
}
function myFunction(param1, param2) {
return param1 + param2;
}
class MyClass {
constructor(param) {
this.param = param;
}
method() {
return this.param;
}
}
import math from 'math';
try {
x = 1 / 0;
} catch (e) {
console.log(e);
}
const myPromise = new Promise((resolve, reject) => {
if (success) {
resolve('Success');
} else {
reject('Failure');
}
});
async function myAsyncFunction() {
let result = await promise;
console.log(result);
}
const numbers = [1, 2, 3, 4, 5];
const squares = numbers.map(x => x**2);
const evens = numbers.filter(x => x % 2 === 0);
const f = x => x**2;
const { param1, param2 } = obj;
const arr1 = [1, 2, 3];
const arr2 = [...arr1, 4, 5];
const name = 'Alice';
const greeting = `Hello, ${name}!`;
document.getElementById('myButton').addEventListener('click', () => {
console.log('Button clicked');
});
document.getElementById('myId').innerHTML = 'Hello, World!';
fetch('https://api.example.com/data')
.then(response => response.json())
.then(data => console.log(data));
const myPromise = new Promise((resolve, reject) => {
if (success) {
resolve('Success');
} else {
reject('Failure');
}
});
async function myAsyncFunction() {
let result = await promise;
console.log(result);
}
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.
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>
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">
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);
const sumAB = (a, b) => a + b;
function sumABTest() {
if (sumAB(5,11) === 16) {
return 'Pass';
} else {
return 'Fail';
}
}
document.body.innerHTML = sumABTest();
// 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 };
Source: https://colab.research.google.com/drive/13beu02DtqZM3zoMpYjvO25fLXXGCl5tN?usp=sharing A series of questions to help pick an algorithm for a problem.
Questions
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
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.
CIDR (Classless Inter-Domain Routing): A method for allocating IP addresses and routing Internet Protocol packets.
CNAME (Canonical Name Record): A type of DNS record that maps an alias name to a true or canonical domain name.
www.example.com
to the canonical name example.com
.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.
DNS (Domain Name System): A hierarchical and decentralized naming system for computers, services, or other resources connected to the Internet or a private network.
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.
www.ecotrust.org
is a domain name for Ecotrust's website.IP Address (Internet Protocol Address): A numerical label assigned to each device connected to a computer network that uses the Internet Protocol for communication.
ISP (Internet Service Provider): An organization that provides services for accessing, using, or participating in the Internet.
00:1A:C2:7B:00:47
.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.
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.
https://
in the URL.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
.
* 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
* 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
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)
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()
py - Python script pyc - Compiled Python script pyd - Python script with C extensions pyo - Optimized Python script pyw - Python script without console window
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
try:
# Code that may raise an exception
except Exception as e:
# Handle the exception
* 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
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 (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
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 []
* 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.
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)
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.
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
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]
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
Question: What is the output of the following code?
list = ['1', ‘2', '3', '4', '5']
print (list[12:])
Answer: []
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.
Question: What are some reasons to dislike Python?
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:
Coordinates:
(x1, y1) = (2, 3)
(x2, y2) = (10, 8)
Euclidean Distance Calculation:
d = √((10 - 2)^2 + (8 - 3)^2)
d = √(8^2 + 5^2)
d = √(64 + 25)
d = √89
Final Result:
(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 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.
QEMU is not starting
sudo service qemu restart
Vector Features Vector data is made up of vertices (points) and paths (direction between connected points).
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).
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).
ls: List directory contents.
ls [options] [directory]
ls -l /home/user
cd: Change the current directory.
cd [directory]
cd /var/www
mkdir: Create a new directory.
mkdir [directory]
mkdir new_folder
rmdir: Remove an empty directory.
rmdir [directory]
rmdir old_folder
rm: Remove files or directories.
rm [options] [file/directory]
rm -rf old_folder
cp: Copy files and directories.
cp [options] source destination
cp file.txt /home/user/Desktop
mv: Move/rename files and directories.
mv [options] source destination
mv file.txt new_file.txt
cat: Concatenate and display file content.
cat [file]
cat file.txt
less: View file content in an interactive way.
less [file]
less file.txt
nano: Edit files using the nano editor.
nano [file]
nano file.txt
vi/vim: Edit files using the vi/vim editor.
vi [file]
vi file.txt
pwd: Print the name of the current working directory.
pwd
pwd
whoami: Display the current user.
whoami
whoami
df: Report file system disk space usage.
df [options]
df -h
top: Display Linux processes.
top
top
ping: Check the network connectivity.
ping [options] destination
ping google.com
ifconfig: Configure or display network interface parameters.
ifconfig [options]
ifconfig
ssh: Securely connect to a remote machine.
ssh [user@]hostname
ssh user@192.168.1.1
scp: Securely copy files between hosts.
scp [options] source destination
scp file.txt user@192.168.1.1:/path
chmod: Change file access permissions.
chmod [options] mode file
chmod 755 script.sh
chown: Change file owner and group.
chown [options] owner[:group] file
chown user:user file.txt
touch: Create a new empty file.
touch [file]
touch newfile.txt
mkdir: Create a new directory.
mkdir [directory]
mkdir new_directory
cat: Display the contents of a file.
cat [file]
cat file.txt
less: View the contents of a file one page at a time.
less [file]
less file.txt
head: Display the first few lines of a file.
head [options] [file]
head -n 5 file.txt
tail: Display the last few lines of a file.
tail [options] [file]
tail -n 5 file.txt
nano: Edit a file using the nano editor.
nano [file]
nano file.txt
vi/vim: Edit a file using the vi/vim editor.
vi [file]
vi file.txt
cp: Copy files or directories.
cp [options] source destination
cp file.txt /path/to/destination
mv: Move or rename files or directories.
mv [options] source destination
mv file.txt newfile.txt
for file in *.HEIC; do sips -s format jpeg "$file" --out "${file%.*}.jpg"; done
rm: Remove files or directories.
rm [options] [file/directory]
rm file.txt
rmdir: Remove empty directories.
rmdir [directory]
rmdir empty_directory
chmod: Change file access permissions.
chmod [options] mode file
chmod 755 script.sh
chown: Change file owner and group.
chown [options] owner[:group] file
chown user:user file.txt
grep: Search for patterns within files.
grep [options] pattern [file]
grep "search text" file.txt
sort: Sort lines of text files.
sort [options] [file]
sort file.txt
find: Search for files in a directory hierarchy.
find [path] [options]
find /path/to/search -name "filename"
tar: Archive multiple files into a tarball.
tar [options] [archive-file] [file/directory]
tar -cvf archive.tar /path/to/directory
gzip: Compress or expand files.
gzip [options] [file]
gzip file.txt
zip: Package and compress (archive) files.
zip [options] [archive-file] [file]
zip archive.zip file.txt
ps
is a command that lists the processes running on your computer.
kill
is a command that allows you to kill a process. It takes the PID of the process as an argument.
aux
is a command that lists all the processes running on your computer.
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
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 is a variable which stores directories that are searched when a command is entered.
echo $PATH
prints the PATH variable.
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.
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.
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"
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.