Skip to content

Instantly share code, notes, and snippets.

View Aldizh's full-sized avatar

Aldi Zhupani Aldizh

View GitHub Profile
@Aldizh
Aldizh / string_compression.txt
Last active September 28, 2023 15:20
Compress/Decompress a string input using a reversible formula
// Write a compression and decompression algorithm that results in a smalller string
// You can assume all the input parameters are letters of the alphabet
function isNumber(char) {
return !Number.isNaN(parseInt(char)) && typeof parseInt(char) === 'number'
}
// str is in this format, number then letter => "2a4f7s154v..."
function decompress(str) {
if (!str) return str
@Aldizh
Aldizh / bowling.js
Created September 8, 2021 04:39
Bowling game calculator
// Installed npm packages: jquery underscore request express
// jade shelljs passport http sys lodash async mocha chai sinon
// sinon-chai moment connect validator restify ejs ws co when
// helmet wrench brain mustache should backbone forever debug jsdom
/*
Please write a function to score a game of bowling,
special scoring considerations
======================================
@Aldizh
Aldizh / matchingRects.js
Created January 25, 2021 16:32
Pairs of similar rectangles
/*
Given a 2D array A[][2] of size N (1 N 103), where A[i][0] and A[i][1] denotes the length and breadth of rectangle i respectively.
Two rectangle i and j where (i < j) are similar if the ratio of their length and breadth is equal
A[i][0] / A[i][1] = A[j][0] / A[j][1]
Input : A[][2] = {{4, 8}, {15, 30}, {3, 6}, {10, 20}}
Output: 6
@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++) {
@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 / 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 / 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 / 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 / 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 / mac-php-composer-setup.md
Created March 14, 2017 01:43 — forked from tomysmile/mac-php-composer-setup.md
Setup PHP Composer using Brew