Skip to content

Instantly share code, notes, and snippets.

View ambergkim's full-sized avatar

Amber Kim ambergkim

View GitHub Profile
class Grid {
// determine number of rows and columns.
constructor(rows, columns) {
this.rows = rows;
this.columns = columns;
}
// get the neighbors helper
getNeighbors(row, column) {
@ambergkim
ambergkim / getNeighbors.js
Created July 19, 2018 18:26
get neighbor coordinates from 4 sides and 4 corners
class Grid {
constructor(rows, columns) {
this.rows = rows;
this.columns = columns;
}
getNeighbors(row, column) {
let neighbors = [];
if (!this.isValidCoord(row, column)) {
@ambergkim
ambergkim / phone-book-search.js
Created July 19, 2018 17:50
Given a list of objects with names and phone numbers and a person's name to search for, return phone number of the LAST person w/ that name
// Example Input 1:
let phoneBook = [{name: 'Ann', number: "123-456-7890"}, {name: 'John', number: "234-567-8901"}, {name: 'John', number: "345-678-9012"} ];
// Phone book is alphabetized
// ON Soln
// container for match {}
// loop through the array
// find match, save match to container
// return match.number
@ambergkim
ambergkim / remove-duplicates2.js
Last active March 2, 2019 22:52
O(1) storage
// Storage O(1).
function removeDupes(arr) {
// sort the array
arr = arr.sort((a, b) => {
return a - b;
});
// pointer 1
p1 = 0;
// O(N)
function remDupes(arr) {
// have we seen this?
let hash = [];
// pointer 1
p = -1;
// establish i as pointer 2
@ambergkim
ambergkim / find-duplicates.js
Created July 14, 2018 01:12
takes in 2 arrays. find what is the same for both.
// only list duplicates once
// return array of duplicates
// helper findInstances(array)
// obj
// loop through array
// if no key in obj, create key
// return obj
// main findSimilar(arr1, arr2)
@ambergkim
ambergkim / division-from-scratch-v2.js
Created July 14, 2018 01:05
JavaScript division with division operator
// Input: int, divInt
// Output: int
// helper
// return whole sets & remainder
// input: int, divInt
// output: [sets, remainder]
// main
// fin str
@ambergkim
ambergkim / palindrome.js
Created July 14, 2018 00:55
check if a string is a palindrome
// Input: str
// Output: true or false
// Example: madam -> 5 length, odd
// Example: wowwow -> 6 length, even
// Example: hello -> fail, odd
// Example: hi -> fail, even
// isPalindrome(str)
// midpoint str/2 ceil -1
@ambergkim
ambergkim / division-from-scratch.js
Created July 14, 2018 00:49
Create a function to divide integers w/o using javascript division.
// Inputs: int, divideByInt
// Output: Int
// Sets
// Current Set
// Loop through int
// if current < divide
// current ++
@ambergkim
ambergkim / convert-int-roman.js
Last active July 11, 2018 19:28
Converting Integers to Roman Numerals
// Roman Numerals
// I, II, III, IV, V, VI, VII, VIII, IX, X
// X, XX, XX, XL, L, LX, LXX, LXXX, XC, C
// C, CC, CCC, CD, D, DC, DCC, DCCC, CM, M
// EXAMPLES:
// 2018: MM X VIII
// 199: C XC IX
// Input: Integer