Skip to content

Instantly share code, notes, and snippets.

View ali-k-h's full-sized avatar

Ali Kosar Hosseinkhani ali-k-h

  • Los Angeles, CA
View GitHub Profile
@ali-k-h
ali-k-h / singleton_es6.js
Created May 26, 2017 20:16
Singleton Implementation with ES6 ECMAScript 2016
//from http://2ality.com/2011/04/singleton-pattern-in-javascript-not.html
var namespace = {
_singleton:null,
get singleton(){
if(!this._singleton){
this._singleton = {
amethod:function(){
console.log('I am single')
}
@ali-k-h
ali-k-h / shufflecard.js
Last active October 2, 2017 06:46
Shuffle a deck of card
function shuffleCards(){
let cards = [];
let n=51;
while(n>=0){
cards[n] = n;
n--;
};
for(let i=0; i<cards.length; i++){
let rand = Math.floor(Math.random()*(51-i)) + 1;
@ali-k-h
ali-k-h / spiralMatrixPrint.js
Created May 25, 2017 19:43
Print a matrix in a spiral order JS6
function spiralMatrix(m, n){
var colS = 0,
colE = n-1,
rowS = 0,
rowE = n-1;
while(colS <= colE && rowS <= rowE){
let c = colS,
r = rowS;
for(c=colS; c<=colE;c++)
console.log(m[r][c])
@ali-k-h
ali-k-h / quick_sort_es6.js
Last active May 26, 2017 20:17
Quick Sort ES6 ECMAScript 6
function partition(a, start, end){
function swap(_a, i, j){
var tmp = _a[i]
_a[i] = _a[j]
_a[j] = tmp
}
var pivot = a[end]
var pIndex = start
for(let i=start; i<end; i++){
@ali-k-h
ali-k-h / binary_search_es6.js
Last active May 26, 2017 20:18
Binary Search ES6 ECMAScript 6
function binarySearchIterative(a,n,data){
var high = n
var low = 0
while(1){
let mid = Math.abs((low + high)/2)
if(a[mid] === data) return mid
if(data < a[mid]) high = mid-1
else low = mid + 1
}
return -1
@ali-k-h
ali-k-h / merge_sort_es6.js
Last active June 27, 2017 18:26
Merge Sort implementation ES6 ECMAScript 6
function mergeSort(a, low, high){
if(!a || !a.length || a.length < 2) return a
if(low === undefined) low = 0
if(high === undefined) high = a.length -1
if(low >=high) return a
let mid = Math.floor((low + high)/2)
mergeSort(a, low, mid)
mergeSort(a, mid + 1, high)
merge(a,low,high,mid)
return a
@ali-k-h
ali-k-h / binarry_tree_js6.js
Last active May 17, 2017 22:11
Implementing Binarry Tree Data Structure in JS6 ECMAScript 6
class BinarryTree{
constructor(root, left, right){
this.root = root || null
this.left = left || null
this.right = right || null
}
levelOrderTree(t){
var q = []
q.unshift(t)
while (q.length>0) {
@ali-k-h
ali-k-h / queue_circular_array_es6.js
Last active May 26, 2017 20:19
Queue implementation with circular array in ES6 ECMAScript 6
class Q {
constructor(capacity){
this.front = this.rear = -1
this.capacity = capacity
this.arr = []
}
isFull(){
return (this.rear + 1) % this.capacity === this.front
}
@ali-k-h
ali-k-h / stack_reverse_es6.js
Last active May 16, 2017 19:57
JS6 ECMAScript 6: reverse the elements of a stack using only push and pop
function reveseStack(s){
if(s.length === 0) return
var data = s.pop()
console.log('reveseStack -> ' + data)
reveseStack(s)
instertAtBottom(s, data)
}
function instertAtBottom (s, data){
console.log('instertAtBottom -> ' + data)
@ali-k-h
ali-k-h / linkedlist_ES6.js
Last active May 26, 2017 20:19
Linked list implementation in ES6 ECMAScript 6
class LinkedList{
constructor(){
this.head = null
this.tail = null
this.count = 0
}
insertAtBegining(data){
const node = {
data:data,