Instantly share code, notes, and snippets.

Andreas Basurto nitrohorse

View GitHub Profile
@nitrohorse
nitrohorse / shapes.js
Created Jun 1, 2018
Inheritance using Shapes
View shapes.js
const log = console.log
class Grid {
constructor() {
this.width = 60
this.height = 24
this.grid = new Array(this.height)
for (let row = 0; row < this.height; row++) {
this.grid[row] = new Array(this.width)
@nitrohorse
nitrohorse / hungryRabbit.js
Created May 26, 2018
Determine the number of carrots a rabbit can eat in a garden.
View hungryRabbit.js
/*
Given an NxM matrix representing a garden, each entry contains a number, which is the number of carrots in that plot. A rabbit starts in the middle, or plot closest to middle with the most carrots and always goes up down left or right after eating to the plot with the most carrots. The values of the matrix will represent numbers of carrots available to the rabbit in each square of the garden. If the garden does not have an exact center, the rabbit should start in the square closest to the center with the highest carrot count.
On a given turn, the rabbit will eat the carrots available on the square that it is on, and then move up, down, left, or right, choosing the the square that has the most carrots.If there are no carrots left on any of the adjacent squares, the rabbit will go to sleep.You may assume that the rabbit will never have to choose between two squares with the same number of carrots.
Write a function which takes a garden matrix and returns the number of carrots the rabbit eats. You may
@nitrohorse
nitrohorse / ticketPurchasingWaitingTime.js
Created May 26, 2018
Given a list of ticket buyers with their numbers of desired tickets, determine how long it will take Jesse to purchase his tickets.
View ticketPurchasingWaitingTime.js
/*
A line has formed to buy tickets for a concert. In order to delay a shortage caused by brokers buying large blocks of tickets, venue management has decided to sell only one ticket at a time. Buyers have to to wait through line again if they want to buy more tickets. Jesse is standing in line and has a number of tickets to purchase.
Given a list of ticket buyers with their numbers of desired tickets, determine how long it will take Jesse to purchase his tickets. Jesse's position in line will be stated, and each transaction takes 1 unit of time. For your purposes, no time is spent moving to the back of the line.
Sample Input
5
2
6
3
@nitrohorse
nitrohorse / getMovieTitles.js
Created May 26, 2018
Get a collection of movie titles using native Node.js
View getMovieTitles.js
/*
Given a string `substr`, getMovieTitles() must perform the following tasks:
1. Query https://jsonmock.hackerrank.com/api/movies/search/?Title=substr (replace substr).
2. Initialize the titles array to store total string elements. Store the Title of each movie meeting the search criterion in the titles array.
3. Sort titles in ascending order and return it as your answer.
*/
const https = require('https')
const log = console.log
@nitrohorse
nitrohorse / letterCombinations.js
Created May 26, 2018
Given a digit string, return all possible letter combinations that the number could represent on a T9 dialpad.
View letterCombinations.js
/*
Given a digit string, return all possible letter combinations that the number could represent on a T9 dialpad.
Input: Digit string '23'
Output: [ 'ad', 'ae', 'af', 'bd', 'be', 'bf', 'cd', 'ce', 'cf' ]
https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/
*/
const log = console.log
const getPossibilities = digitString => {
const digitMap = {
@nitrohorse
nitrohorse / balancedBraces.js
Created May 26, 2018
Validate that strings of braces are balanced
View balancedBraces.js
const log = console.log
const topOfStack = stack => {
return stack[stack.length - 1]
}
const isOpening = char => {
return char === '(' || char === '[' || char === '{'
}
@nitrohorse
nitrohorse / numberOfIslands.js
Created May 26, 2018
Count the number of "islands" (groups of 1s) in a 2D matrix.
View numberOfIslands.js
const log = console.log
const dfs = (grid, i, j) => {
if (i < 0 || i >= grid.length
|| j < 0 || j >= grid[0].length
|| grid[i][j] !== 1) {
return // out of bounds or already visited
}
grid[i][j] = 0 // mark as visited
@nitrohorse
nitrohorse / boggleSolver.js
Created May 26, 2018
Print all valid words in a Boggle board represented by a 2D matrix.
View boggleSolver.js
/*
Create an application that prints all the words in a Boggle board represented by a 2D matrix.
* Input: Board represented as 2D matrix.
* Input: Set of valid words.
* Output: Words contained in the boggle board.
* Notes:
* Can’t re-use the same letter for a given word.
* Letters can’t be connected diagonally.
* Print the word every time it appears
* Runnable solution
@nitrohorse
nitrohorse / serializationOfABST.js
Last active May 23, 2018
Serialize and deserialize a binary search tree
View serializationOfABST.js
/*
5
/ \
1 7
/ \ \
0 3 9
/
2
Constraints:
@nitrohorse
nitrohorse / mergeObjects.js
Last active May 11, 2018
Function that merges two objects (implement Object.assign)
View mergeObjects.js
/*
create merge function (implement Object.assign)
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign
- takes two objects and returns one merged object
objects defined as:
- native JS object -> keys/values
- for duplicate keys, overwrite values
follow-up: