Skip to content

Instantly share code, notes, and snippets.

View primaryobjects's full-sized avatar

Kory Becker primaryobjects

View GitHub Profile
@primaryobjects
primaryobjects / reverse-number.js
Created March 2, 2017 03:19
Reverse the digits in a number. Reverse a number without converting to a string.
var reverseInt = function(num)
{
var result=0;
while (num > 0)
{
result = (result * 10) + (num % 10);
num = Math.floor(num / 10);
}
@primaryobjects
primaryobjects / next-greater-element.js
Created March 2, 2017 02:54
Find the next greater element of an array of numbers within a second array.
/**
* @param {number[]} findNums
* @param {number[]} nums
* @return {number[]}
*/
var nextGreaterElement = function(findNums, nums) {
var result = [];
var hash = {};
for (var i=0; i<findNums.length; i++) {
@primaryobjects
primaryobjects / keyboard-row.js
Last active July 14, 2020 13:18
Find words on a single row of the keyboard. Solution uses a hash of keyboard keys assigned to rows.
/**
* @param {string[]} words
* @return {string[]}
*/
var findWords = function(words) {
var result = [];
var keyboard = {'q':1, 'w':1, 'e':1, 'r':1, 't':1, 'y':1, 'u':1, 'i':1, 'o':1, 'p':1,
'a':2, 's':2, 'd':2, 'f':2, 'g':2, 'h':2, 'j':2, 'k':2, 'l':2,
'z':3, 'x':3, 'c':3, 'v':3, 'b':3, 'n':3, 'm':3
@primaryobjects
primaryobjects / minheap.js
Last active February 28, 2017 05:17
Min Heap, Min-heap, Binary Heap implementation
function BinaryHeap(scoreFunction){
this.content = [];
this.scoreFunction = scoreFunction;
}
BinaryHeap.prototype = {
push: function(element) {
// Add the new element to the end of the array.
this.content.push(element);
// Allow it to bubble up.
@primaryobjects
primaryobjects / radio.java
Last active September 3, 2018 15:18
Hackerland Radio Transmitters, find number of transmitters so houses are within range.
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
@primaryobjects
primaryobjects / delete-duplicate-letters.js
Created February 28, 2017 01:57
Alternating Characters - delete duplicate letters next to each other in a string so only alternating characters remain.
function processData(input) {
var lines = input.split('\n');
for (var i=1; i<lines.length; i++) {
var str = lines[i];
var deleteCount = 0;
for (var j=1; j<str.length; j++) {
// Is a duplicate to the left of this position?
if (str[j-1] === str[j]) {
// Delete.
@primaryobjects
primaryobjects / gem-elements.js
Created February 28, 2017 00:58
Gem Elements, find letters in each string that exist in all strings.
function processData(input) {
var lines = input.split('\n');
var n = parseInt(lines[0]);
var gems = {};
var hash = {};
for (var i=1; i<lines.length; i++) {
var str = lines[i];
for (var j=0; j<str.length; j++) {
@primaryobjects
primaryobjects / sherlock-array.js
Created February 27, 2017 02:22
Sherlock and Array, find an index in an array where sum of left and right of index equal the same value.
function processData(input) {
var lines = input.split('\n');
for (var i=2; i<lines.length; i+=2) {
var arr = lines[i].split(' ').map(function(x) { return parseInt(x); });
if (arr.length > 1) {
var sum = 0;
// First, get the sum of the entire array.
for (var j=0; j<arr.length; j++) {
@primaryobjects
primaryobjects / funny-string.js
Created February 27, 2017 02:09
Detect if a string is funny by the difference between each 2 letters equaling the same as the difference between the reverse string 2 letters.
function processData(input) {
var lines = input.split('\n');
for (var i=1; i<lines.length; i++) {
var str = lines[i];
for (var j=1; j<str.length; j++) {
var diff1 = Math.abs(str.charCodeAt(j) - str.charCodeAt(j-1));
var diff2 = Math.abs(str.charCodeAt(str.length - j) - str.charCodeAt(str.length - 1 - j));
if (diff1 !== diff2) {
break;
@primaryobjects
primaryobjects / sos.js
Created February 27, 2017 01:56
Mars Exploration - determine how many letters of Sami's SOS have been changed by radiation.
process.stdin.resume();
process.stdin.setEncoding('ascii');
var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;
process.stdin.on('data', function (data) {
input_stdin += data;
});