Skip to content

Instantly share code, notes, and snippets.

View vaidehijoshi's full-sized avatar

Vaidehi Joshi vaidehijoshi

View GitHub Profile
@vaidehijoshi
vaidehijoshi / clean-up-branches.md
Last active July 1, 2020 21:51
clean up branches

To delete local branches which have alread been merged into master:

$ git branch --merged master | grep -v "\* master" | xargs -n 1 git branch -d

Omit the master branch argument to remove local branches which have already been merged into the current HEAD:

$ git branch --merged | grep -v "\*" | xargs -n 1 git branch -d

To delete all local branches except for master:

$ git branch | grep -v "master" | xargs git branch -D

// Using Esprima, a ECMAScript parser written in ECMAScript
// var esprima = require('esprima');
// var program = 'const answer = 42';
// Syntax
{
"type": "Program",
"body": [
{
"type": "VariableDeclaration",
> fibonacci(3)
> calculating 1
> calculating 1
> calculating 0
> also calculating 0
> calculating 1
> calculating 0
> calculating 1
> calculating 1
// Calculates the nth number in the Fibonacci sequence.
// For example, fibonacci(6) will return the 6th number
// in the sequence, or the number 8.
function fibonacci(n) {
if (n < 2) {
// If the nth number is less
// than 2, return it directly.
console.log('calculating ' + n);
return n;
var array = [9,4,1,7,9,1,2,0]
countingSort(array, 0, 9)
> (10) [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
> (10) [1, 2, 1, 0, 1, 0, 0, 1, 0, 2]
> item at index 0 is: 9
> moving item 0 to correct location
> item at index 1 is: 4
> moving item 1 to correct location
> item at index 2 is: 1
// Notice that we needed to know the max/min value in order to use counting sort at all!
function countingSort(array, minimumValue, maximumValue) {
var i;
var z = 0;
var count = [];
// Count the instances of each element.
for (i = minimumValue; i <= maximumValue; i++) {
count[i] = 0;
}
function heapify(heap, i, max) {
var index, leftChild, righChild;
while(i < max) {
index = i;
leftChild = 2*i + 1;
righChild = leftChild + 1;
if (leftChild < max && heap[leftChild] > heap[index]) {
function buildMaxHeap(array) {
var i;
i = array.length / 2 - 1;
i = Math.floor(i);
// Build a max heap out of
// all array elements passed in.
while (i >= 0) {
heapify(array, i, array.length);
i -= 1;
function heapSort(array) {
// Build our max heap.
buildMaxHeap(array);
// Find last element.
lastElement = array.length - 1;
// Continue heap sorting until we have
// just one element left in the array.
while(lastElement > 0) {
function swap(array, firstItemIndex, lastItemInde) {
var tmp = array[firstItemIndex];
// Swap first and last items in the array.
array[firstItemIndex] = array[lastItemInde];
array[lastItemInde] = tmp;
}