Firstly install Brew on your MAC
- ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Then install PHP
- brew update
- brew tap homebrew/dupes
- brew tap homebrew/php
- brew install php56
// Function to find triplets with zero sum. | |
/** | |
* @param {number[]} arr | |
* @param {number} n | |
* @returns {boolean} | |
*/ | |
function findTriplets(arr, n) | |
{ |
/* | |
Let us start with tehse two definitions: | |
Class Variables — Declared inside the class definition (but outside any of the instance methods). They are not tied to any particular object of the class, hence shared across all the objects of the class. Modifying a class variable affects all objects instance at the same time. | |
Instance Variable — Declared inside the constructor method of class (the __init__ method). They are tied to the particular object instance of the class, hence the contents of an instance variable are completely independent from one object instance to the other. | |
Every function created in javascript inherits from Object | |
which internally consists of a constructor function and __proto__ | |
__proto__ allows for sharing across instances | |
*/ |
// In the function below changeBy is a private function | |
// whereas increment and decrement are public and hence | |
// can be invoked outside the Counter's scope | |
function Counter(initialCounter) { | |
var privateCounter = initialCounter || 0; | |
function changeBy(val) { | |
privateCounter += val; | |
} | |
return { |
Firstly install Brew on your MAC
Then install PHP
/* | |
* | |
* Flattens an object given a nested object structure | |
* @function flattenObject | |
* @param {Object} obj - The object whose values and keys you want to map over. | |
* @return {String} - A json string with key-value pairs. | |
* @example | |
* const original = { | |
* address: { | |
* 'addressLine1': 'Address (Line 1)', |
/* | |
* | |
* @function constructObject | |
* @param {file} file in dot notation ('a.b.c':'Hey', 'd.e.g': 'you') | |
* @return {Object} - a nested object. | |
* @example | |
* const object = constructObject(file) | |
* Returns the following: | |
* a: { | |
* b: { |
# Whenever i start coding i like to start with this command to clean up the repo and find out where we are | |
$ git fetch --prune origin && git status | |
# Merging is usually a good strategy as it allows for better tracking and dividing up the work | |
# Github offers a squash && merge option now on the PRs, but lets consider this: | |
# You want to test the feature branch locally before merging it and if there are any conflicts you want to resolve and test | |
$ git checkout master | |
$ git merge --squash feature_branch | |
$ git reset origin/master (Optional if we want to unstage all files that differ from origin master) | |
$ git add --all |
/* | |
Problem: Given an array of objects parse those objects | |
to form meaningful key value pairs | |
*/ | |
// Input | |
const dataSource = [ | |
{id: 48, code: "firstName", description: "John"}, | |
{id: 49, code: "lastName", description: "Smith"}, | |
{id: 49, code: "occupation", description: "Agent"}, |
/* | |
Problem, find the highest power less than a number n | |
Iterative solution: O(n) | |
Logarithmic solution: O(logn) | |
Bitwise solution: O(logn) | |
*/ | |
var highestPower = function(n, x) { | |
var res = 0; | |
for (var i = n; i >= 1; i--){ | |
var pow = Math.pow(x, i); |
/* | |
There is a colony of 8 cells arranged in a straight line where each day every cell competes with its adjacent cells(neighbour). Each day, for each cell, if its neighbours are both active or both inactive, the cell becomes inactive the next day,. otherwise itbecomes active the next day. | |
Assumptions: The two cells on the ends have single adjacent cell, so the other adjacent cell can be assumsed to be always inactive. Even after updating the cell state. consider its pervious state for updating the state of other cells. Update the cell informationof allcells simultaneously. | |
Write a fuction cellCompete which takes takes one 8 element array of integers cells representing the current state of 8 cells and one integer days representing te number of days to simulate. An integer value of 1 represents an active cell and value of 0 represents an inactive cell. | |
*/ | |
function changeState(states) { | |
let temp = [] | |
const len = states.length | |
const virtualCell = 0 | |
for (var i = 0; i < len; i++) { |