Skip to content

Instantly share code, notes, and snippets.

View alexitaylor's full-sized avatar
🚀
building cool things

Alexi Taylor alexitaylor

🚀
building cool things
View GitHub Profile
/**
* @param {string} s
* @return {string}
*/
var frequencySort = function(s) {
var characters = {};
for (var i = 0; i < s.length; i++) {
if (s[i] in characters) {
characters[s[i]]++;
} else {
// Prompt: https://gist.github.com/tim-hr/697af278700fcf12014eb36d932ad7c4
var messageBus = {
subscribers: [],
subscribe: function(payload) {
this.subscribers.push(payload);
},
publish: function(payload) {
this.subscribers.forEach(function(sub){
if (validTopic(payload.topic, sub.topic) && payload.channel === sub.channel) {
/**
* Created by alexi on 5/30/17.
*/
// Given an array of characters, return the array with every vowel doubled. For example:
var input = ['w','h','a','t',' ','o','n',' ','e','a','r','t','h',' ','a','r','e',' ','y','o','u',' ','t','a','l','k','i','n','g',' ','a','b','o','u','t','?'];
var expectedOutput = ['w','h','a','a','t',' ','o','o','n',' ','e','e','a','a','r','t','h',' ','a','a','r','e','e',' ','y','o','o','u','u',' ','t','a','a','l','k','i','i','n','g',' ','a','a','b','o','o','u','u','t','?'];
var vowelDoubler = function(array){
var vowels = ['a', 'e', 'i', 'o', 'u'];
@alexitaylor
alexitaylor / productOfArrayExceptSelf.js
Last active May 25, 2017 16:48
Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i]. Solve it without division and in O(n). For example, given [1,2,3,4], return [24,12,8,6].
/**
* @param {number[]} nums
* @return {number[]}
*/
var productExceptSelf = function(nums) {
var result = [];
var right = 1;
var left = 1;
for (var j = 0; j <= nums.length - 1; j++) {
@alexitaylor
alexitaylor / kthSmallest.js
Created May 22, 2017 16:57
Kth Smallest Element in a BST
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @param {number} k
/*
Tree has path to target sum?
You are given a binary tree whose nodes all have integer values (both positive and negative).
Given some target sum (say, 14), return true if there is any path starting from the root and ending in a leaf, such that adding up all the values along the path equals the given sum.
*/
const BinaryTree = function(value) {
this.value = value;
this.left = null;
@alexitaylor
alexitaylor / Cardinaldirection.js
Last active April 29, 2017 01:22
Cardinal Direction: get cardinal direction from degrees
//given "0-360" returns the nearest cardinal direction "N/NE/E/SE/S/SW/W/NW/N"
function getCardinal (angle) {
if (angle >= 0 && angle <= 22 || angle >= 338 && angle <= 360)
return "N";
if (angle >= 23 && angle <= 67)
return "NE";
if (angle >= 68 && angle <= 112)
return "E";
if (angle >= 113 && angle <= 157)
return "SE";
/* Prompt: tackling floating-point imprecision with the CashAmount class
In programming languages such as JavaScript, 0.1 + 0.2 does not equal 0.3. This is true in Ruby, Python, etc. The imprecision is inherent in floating-point arithmetic, it isn't because JavaScript itself is wonky.
These tiny errors can add up and cause actual wrong answers over time if you're not careful. They also make it harder to unit-test your work, since you have to compare within ranges rather than comparing to exact expected values.
To deal with this specifically within a monetary context, let's make a class called CashAmount that accepts double values (e.g., 14.72) and will never suffer from the aforementioned precision problems. A CashAmount represents a collection of bills and coins, such as you might find in your wallet, purse, or a cash register.
Note: you can do this by converting to pennies for all denominations so you are always working with integers, then converting back to a two-decimal float as needed. */
function Node(val){
this.value = val;
this.left = null;
this.right = null;
}
function BinaryTree(){
this.root = null;
}
if (numRows === 0) {
return [];
}
var triangle = [[1]];
for(var i = 0; i < numRows - 1; i++ ) {
var row = [1];
for(var j = 1; j < triangle[i].length; j++) {
row[j] = triangle[i][j] + triangle[i][j-1];