Skip to content

Instantly share code, notes, and snippets.

View Aldizh's full-sized avatar

Aldi Zhupani Aldizh

View GitHub Profile
// Function to find triplets with zero sum.
/**
* @param {number[]} arr
* @param {number} n
* @returns {boolean}
*/
function findTriplets(arr, n)
{
@Aldizh
Aldizh / Person.js
Last active May 2, 2020 22:16
Prototype Inheritance example javascript
/*
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
*/
@Aldizh
Aldizh / counter.js
Last active September 20, 2019 20:17
Basic counter that demonstrates javascript closure concept
// 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 {
@Aldizh
Aldizh / mac-php-composer-setup.md
Created March 14, 2017 01:43 — forked from tomysmile/mac-php-composer-setup.md
Setup PHP Composer using Brew
@Aldizh
Aldizh / flattenObject.js
Created September 15, 2017 01:32
Utility function to transform an object to flat key value pairs. It is used to maintain a mapping between an object structure and a general purpose file which could be translated for example.
/*
*
* 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)',
@Aldizh
Aldizh / constructObject.js
Created September 15, 2017 01:42
This is used to convert a flat file into a nested object structure, give the flat file is in proper dot notation.
/*
*
* @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: {
@Aldizh
Aldizh / git_tips.txt
Last active May 2, 2020 01:26
Helpful github tips and commands
# 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
@Aldizh
Aldizh / reduce_for_objects
Last active May 1, 2020 00:38
Utility for reducing array of objects
/*
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"},
@Aldizh
Aldizh / highest_power
Last active May 1, 2020 00:14
Gives you the highest power of x less then provided number n
/*
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);
@Aldizh
Aldizh / cellComplete.js
Created June 27, 2020 14:38
Cell Complete Problem (XOR)
/*
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++) {