Skip to content

Instantly share code, notes, and snippets.

View primaryobjects's full-sized avatar

Kory Becker primaryobjects

View GitHub Profile
@primaryobjects
primaryobjects / utopian-tree.js
Created February 27, 2017 01:12
The Utopian Tree goes through 2 cycles of growth every year. Each spring, it doubles in height. Each summer, its height increases by 1 meter.
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;
});
@primaryobjects
primaryobjects / missing.js
Last active February 26, 2017 20:34
Find missing numbers between two arrays. Uses a hash with increment and decrement to keep value at 0 for matching pairs. In the end, use absolute value to get any remainder of non matches.
function processData(input) {
var lines = input.split('\n');
var arr1 = lines[1].split(' ').map(function(x) { return parseInt(x); });
var arr2 = lines[3].split(' ').map(function(x) { return parseInt(x); });
var hash = {};
var length = Math.max(arr1.length, arr2.length);
for (var i=0; i<length; i++) {
if (i < arr1.length) {
hash[arr1[i]] = hash[arr1[i]] ? hash[arr1[i]] + 1 : 1;
@primaryobjects
primaryobjects / match-pairs.js
Last active February 26, 2017 20:15
Sock Merchant - match pairs of socks by integer color. Match pairs of integers, return number of pairs. Using a hash.
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;
});
@primaryobjects
primaryobjects / queue-two-stacks.js
Last active February 26, 2017 20:03
Build a queue from two stacks. Queue from 2 stacks. Queue from two 2 arrays. One array is head (reverse of tail), one is tail.
function processData(input) {
var lines = input.split('\n');
var head = [];
var tail = [];
for (var i=1; i<lines.length; i++) {
var parts = lines[i].split(' ');
var command = parseInt(parts[0]);
var param = parts.length > 1 ? parseInt(parts[1]) : null;
@primaryobjects
primaryobjects / huffman-decode.java
Created February 26, 2017 19:40
Huffman decoding a tree.
void decode(String S, Node root)
{
if (S.length() == 0) return;
Node pointer = root;
while (pointer.left != null || pointer.right != null) {
int x = Character.getNumericValue(S.charAt(0));
if (x == 0) {
pointer = pointer.left;
@primaryobjects
primaryobjects / merge-linked-list.java
Created February 26, 2017 19:37
Merge two linked lists in ascending order.
/*
Merge two linked lists
head pointer input could be NULL as well for empty list
Node is defined as
class Node {
int data;
Node next;
}
*/
@primaryobjects
primaryobjects / linked-list-runner.java
Created February 26, 2017 19:15
Get the nth element from the end of a linked list. Uses a runner method to find the nth element from the tail of a linked list.
/*
Get Nth element from the end in a linked list of integers
Number of elements in the list will always be greater than N.
Node is defined as
class Node {
int data;
Node next;
}
*/
@primaryobjects
primaryobjects / linked-list-compare.java
Created February 26, 2017 19:03
Compare two linked lists. Compare values of 2 linked lists for equality.
int CompareLists(Node headA, Node headB) {
int result = 1;
Node current1 = headA;
Node current2 = headB;
while (current1 != null && current2 != null && current1.data == current2.data) {
current1 = current1.next;
current2 = current2.next;
}
@primaryobjects
primaryobjects / reverse-linked-list-iterative.java
Last active February 26, 2017 15:45
Reverse a linked list in Java and Python.
Node Reverse(Node head) {
if (head == null) {
return head;
}
Node curr = head;
Node prev = head;
Node next = head.next;
while (next != null) {
@primaryobjects
primaryobjects / max-heap.js
Created February 26, 2017 15:32
Max Heap implementation.
/* Heap implementation. A heap is a fully formed binary tree.
Elements are added to fill the bottom level evenly on left and right.
For max-heaps, parent values must be greater than or equal to their children.
For min-heaps, parent values must be less than or equal to their children.
*/
function add(root, value) {
if (!root) {
root = { val: value };
}
else {