This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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']; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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++) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Definition for a binary tree node. | |
* function TreeNode(val) { | |
* this.val = val; | |
* this.left = this.right = null; | |
* } | |
*/ | |
/** | |
* @param {TreeNode} root | |
* @param {number} k |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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. */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Node(val){ | |
this.value = val; | |
this.left = null; | |
this.right = null; | |
} | |
function BinaryTree(){ | |
this.root = null; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; |