Skip to content

Instantly share code, notes, and snippets.

View primaryobjects's full-sized avatar

Kory Becker primaryobjects

View GitHub Profile
@primaryobjects
primaryobjects / 1-sum.js
Last active May 17, 2021 00:56
Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.
/* This solution works by using an array for placeholders of each positive and negative value up to a and b.
We then count the number of items in each array and remove from the oppositie array to get the final value.
If the negative array is larger, we multiple by -1.
This solution works, but is slow.
*/
/**
* @param {number} a
* @param {number} b
* @return {number}
@primaryobjects
primaryobjects / countPrimes.js
Created January 26, 2017 14:47
Count prime numbers up to n.
var isPrime = function(n) {
var result = true;
if (n <= 1) {
result = false;
}
else {
// We only need to check up to the sqrt of the number.
for (var i=2; i*i<=n; i++) {
if (n % i === 0) {
@primaryobjects
primaryobjects / complement.js
Last active January 24, 2017 15:45
Number complement
var findComplement = function(num) {
var result = 0;
// Convert number to binary string.
var bits = num.toString(2);
var complement = '';
// Create opposite binary string for complement.
for (var i=0; i<bits.length; i++) {
complement += bits[i] === '0' ? '1' : '0';
@primaryobjects
primaryobjects / excel-column.js
Created January 24, 2017 14:44
Excel Sheet Column Number
/**
* @param {string} s
* @return {number}
*/
var titleToNumber = function(s) {
var result = 0;
for (var i=0; i<s.length; i++) {
result *= 26;
result += ((s.charCodeAt(i) - 'A'.charCodeAt(0)) + 1);
@primaryobjects
primaryobjects / lca.js
Created January 24, 2017 14:27
Lowest Common Ancestor of a Binary Search Tree BST
/* Start at root and check if p and q are less, then go left, otherwise go right. */
var lowestCommonAncestor = function(root, p, q) {
if (root.val > p.val && root.val > q.val) {
return lowestCommonAncestor(root.left, p, q);
}
else if (root.val < p.val && root.val < q.val) {
return lowestCommonAncestor(root.right, p, q);
}
else {
return root;
@primaryobjects
primaryobjects / remove-adjacent.js
Created January 24, 2017 14:06
Given an integer x, find one digit who is equal to either of its adjacent digits, and remove it. Demo at http://jsbin.com/fopoxupace/edit?js,console
var removeAdjacent = function(n) {
var result = 0;
var str = n.toString();
var numbers = [];
for (var i=0; i<str.length; i++) {
if (i+1 < str.length) {
var a = str[i];
var b = str[i+1];
@primaryobjects
primaryobjects / find-character.js
Last active January 24, 2017 04:22
Find the different character in two strings. Demo at http://jsbin.com/bucafudumo/edit?js,console
var findCharacter = function(s, t) {
var result;
s = s.toLowerCase();
t = t.toLowerCase();
var hash1 = [];
var hash2 = [];
var length = Math.max(s.length, t.length);
@primaryobjects
primaryobjects / digits.js
Created January 24, 2017 04:00
Return an array individual digits in a number.
var getDigits = function(x) {
var result = [];
while (x) {
result.push(x % 10);
x = Math.floor(x / 10);
}
return result;
}
@primaryobjects
primaryobjects / smallest-replace.js
Created January 24, 2017 03:53
Replace two adjacent digits with larger one to find the smallest number that can be obtained.
var smallestByReplace = function(n) {
var digits = n.toString();
var numbers = [];
for (var i=0; i<digits.length; i++) {
if (i+1 < digits.length) {
var a = digits[i];
var b = digits[i+1];
var max = Math.max(a, b);
@primaryobjects
primaryobjects / repeated-substring.js
Created January 24, 2017 03:31
Repeated Substring Pattern, searching for a repeatable substring within a string.
/**
* @param {string} str
* @return {boolean}
*/
var repeatedSubstringPattern = function(str) {
var result = false;
var sameCount = 1;
for (var i=1; i<str.length; i++) {
if (str[i] === str[0]) {