Skip to content

Instantly share code, notes, and snippets.

View nimamehanian's full-sized avatar
🎧
Focused

Nima Mehanian nimamehanian

🎧
Focused
View GitHub Profile
@nimamehanian
nimamehanian / conway.js
Created August 11, 2020 23:25
Conway Game Of Life
const gen0 = [
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 1, 0, 0],
[0, 0, 0, 1, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
];
// CONWAY GAME OF LIFE — CONSTRAINTS:
// Any live cell with 0 or 1 live neighbors dies by underpopulation.
// Any live cell with 2 or 3 live neighbors stays alive.
@nimamehanian
nimamehanian / makeChange.js
Created February 13, 2018 09:53
Change-making problem
const makeChange = (tier, divisors) => {
if (!tier.length) { return false; }
const nextTier = tier.reduce((t, n) =>
[...t, ...divisors.map(div => n - div)], []
).filter(val => val > -1);
return nextTier.indexOf(0) === -1 ?
makeChange(nextTier, divisors) : true;
};
makeChange([10], [3, 4]);
@nimamehanian
nimamehanian / poisonPlants.js
Created February 13, 2018 09:07
Hacker Rank Problem
const data = [6, 5, 8, 4, 7, 10, 9];
const daysTillPlantDeath = (toxicities, daysElapsed) => {
const livingPlants = toxicities.filter((poisonLevel, idx) => (
idx === 0 || (idx > 0 && poisonLevel <= toxicities[idx - 1])
));
return livingPlants.join('') === toxicities.join('') ?
daysElapsed : daysTillPlantDeath(livingPlants, daysElapsed + 1);
};
const flattenDeep = array => (
array && array.constructor.name === 'Array' ?
array.toString().split(',').map(num => +num) : []
);

Keybase proof

I hereby claim:

  • I am nimamehanian on github.
  • I am nimamehanian (https://keybase.io/nimamehanian) on keybase.
  • I have a public key ASAJNQGxsMuKcxAzb0zoKZOkmklWfLI0xa4C7SND8G1d4go

To claim this, I am signing this object:

@nimamehanian
nimamehanian / splort.js
Last active September 6, 2015 01:18
Toy problem: SplitSort. Given an input string, render the space-separated entries across `n` columns, vertically, and in alphabetical order. (i.e., implement an elementary UNIX `ls -a` command)
// test strings
var str1 = 'the quick brown fox jumps over the lazy dog and this is probably the longest sentence you have ever come across because it just has so many words in it that it just runs on and on needlessly can you tell';
var str2 = 'Oh, what a harmony of abandonment and impulse, of unnatural and yet graceful postures, in that mystical language of limbs, freed from the weight of corporeal matter, marked quantity infused with new substantial form, as if the holy band were struck by an impetuous wind, breath of life, frenzy of delight, rejoicing song of praise miraculously transformed, from the sound that it was, into image.';
var rand = 'Oh, what a harmony of abandonment? and impulse!, of unnatural (and yet graceful) postures, in that mystical language of limbs, freed from the weight of corporeal matter, marked quantity infused with new substantial form; as if the holy band were struck by an impetuous wind, breath of life: frenzy of delight: rejoicing song of praise, miraculously transformed, fro
@nimamehanian
nimamehanian / obligation.js
Created August 16, 2015 02:15
Obligation
var isResolvable = function (val) {
return val && typeof val.success === 'function';
};
var reference = function (data) {
if (isResolvable(data)) { return data; }
return {
success: function (callback) {
return reference(callback(data));
}
@nimamehanian
nimamehanian / largestPalPerfSq.rb
Created May 22, 2013 03:57
Find the largest palindrome (of number_of_digits length) that is also a perfect square. E.g., largest_pal_perf_sq(15) returns 900_075_181_570_009
def palindrome?(text)
return true if text.length <= 1
return false if text[0] != text[text.length - 1]
return palindrome?(text[1..-2])
end
def prepare_range(number_of_digits)
big = ''
sml = '1'
@nimamehanian
nimamehanian / primeNumbers.js
Last active December 15, 2015 09:29
Find a collection of prime numbers between a range. I began with the primeNumbers function and wrote it the way I'd want to use it; go through a range and call prime() on each number. So that meant I had to create a prime() method for all Numbers using their prototype property. This lead me to research what rules define a number to be prime. The…
var primeNumbers = function(from, to) {
var primes = [];
for(var i = from; i <= to; i++){
if(i.prime()){
primes.push(i);
}
}
return primes;
};
@nimamehanian
nimamehanian / stringifyJSON.js
Created March 24, 2013 15:11
Recursive implementation of JSON.stringify
var stringifyJSON = function(object){
// Base case: If not object and is a string,
// wrap in quotes and return.
// If not passed in (i.e., null),
// return empty string.
if(typeof object !== 'object' || object === null){
if(typeof object === 'string'){
object = '"' + object + '"';
}
return String(object);