Skip to content

Instantly share code, notes, and snippets.

@matharoo
matharoo / PriorityQueue.js
Created September 23, 2019 16:28
Priority Queue implementation in Javascript
/**
* A priority queue stores a list of items but each can have a numeric priority value.
* Items with a higher priority are dequeued before items with a lower priority.
* Implemented as a hash of arrays where the hash keys are priority values.
*/
function PriorityQueue() { //removed unused size variable
this.store = {}; // keys are priorities, values are arrays of elements
this.count = 0;
// either keep all the functions inside Priority Queue or outside.
// only keep properties of object inside constructor and define the Object properties on the prototype function so that its easy to read and follow the code.
@matharoo
matharoo / peak.js
Created October 20, 2019 20:56
In an array of ints, return the index such that the sum of the elements to the right of that index equals the sum of the elements to the left of that index
function peak(arr) {
let leftsum = 0;
let rightsum = 0;
mid = Math.floor(arr.length / 2)
const hash = {}
while (mid > 0) {
leftsum = 0;
rightsum = 0;
for (x = mid - 1; x >= 0; x--) {
leftsum += arr[x]
@matharoo
matharoo / sudoku-validator.js
Created October 21, 2019 05:34
A function that accepts a 2D array representing a Sudoku board, and returns true if it is a valid solution, or false otherwise. The cells of the sudoku board may also contain 0's, which will represent empty cells. Boards containing one or more zeroes are considered to be invalid solutions.
function validSolution(board) {
let valid = true;
const testvaliditiy = (arr) => {
let set = new Set(arr);
if (arr.includes(0) || [...set].length !== 9) {
return false;
} else {
return true;
}
}
@matharoo
matharoo / async-await.js
Created October 22, 2019 20:21
Using async/await in javascript, transforming promise to chain to async/await
function callApi(url){
return new Promise((resolve,reject) => {
console.log(`calling API: ${url}`)
if(url==='twitter'|| url=='facebook'){
resolve(`connected to ${url}`)
}
else{
reject(`Sorry unsupported API : ${url}`)
}
});
@matharoo
matharoo / builder-pattern.js
Created October 22, 2019 23:10
Builder pattern in JS
class Address{
constructor(street,city,country){
this.street = street;
this.city = city;
this.country = country;
}
}
class User{
@matharoo
matharoo / sortByName.js
Last active October 23, 2019 23:58
convert 0-999 to their alphabet forms, sort them and return back number form.. not the best implementation needs some refactoring.
function sortByName(arr) {
let ones= ['zero','one','two','three','four','five','six','seven','eight','nine','ten','eleven','twelve','thirteen','fourteen','fifteen','sixteen','seventeen','eighteen','nineteen'];
let tens = ['','','twenty','thirty','fourty','fifty','sixty','seventy','eighty','ninety']
let alphanums = {}
arr.forEach((v,i)=>{
let numstring = String(v)
numstring =numstring.split('').reverse('').join('');
let len= numstring.length;
let str=''
let x= 0;
@matharoo
matharoo / domainName.js
Created November 6, 2019 03:08
Extract the domain name from a URL ... just the domainName extractor before .[coms,nets...]
function domainName(url){
if(url.search('//')>0){
url = url.slice(url.search('//')+2);
}
if(url.search('www')>=0){
url = url.slice(url.search('www')+4);
}
return url.slice(0,url.indexOf('.'));
}
@matharoo
matharoo / debug.sh
Created November 13, 2019 16:50
debugging using -x in bash
#!/bin/bash
var1='A'
var2='B'
my_function () {
local var1='C'
var2='D'
echo "Inside function: var1: $var1, var2: $var2"
}
@matharoo
matharoo / mynode-deployment.yaml
Created October 19, 2020 04:07
mynode-app deployment.yaml example
apiVersion: apps/v1
kind: Deployment
metadata:
name: mynode-app
namespace: nodejs
spec:
replicas: 2
selector:
matchLabels:
app: mynode-app
@matharoo
matharoo / mynode-service.yaml
Created October 19, 2020 04:08
mynode-app service.yaml using Loadbalancer example
apiVersion: v1
kind: Service
metadata:
name: mynode-app
namespace: nodejs
spec:
selector:
app: mynode-app
ports:
- port: 80