Skip to content

Instantly share code, notes, and snippets.

View primaryobjects's full-sized avatar

Kory Becker primaryobjects

View GitHub Profile
@primaryobjects
primaryobjects / panagram.js
Created February 22, 2017 02:36
Panagram detection in a string (using all letters of the alphabet).
function processData(input) {
var hash = {};
for (var i=0; i<input.length; i++) {
var ch = input.charCodeAt(i);
if (ch >= 65 && ch <= 90) {
ch += 32; // lowercase
}
// Only consider alphabet.
@primaryobjects
primaryobjects / matching-brackets.js
Created February 21, 2017 23:43
Balanced Brackets, Matching Brackets using a stack.
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 / max.js
Created February 20, 2017 22:17
Maximum Element in a stack challenge.
function processData(input) {
//Enter your code here
var lines = input.split('\n');
var stack = [];
var max = 0;
for (var i=1; i<lines.length; i++) {
var parts = lines[i].split(' ').map(function(x) { return parseInt(x); });
var command = parts[0];
var value = parts.length > 1 ? parts[1] : null;
// Find the smallest decreasing number in an array, before increasing values.
var findMinBeforeIncrease = function(arr) {
var result = null;
var current = 0;
for (var i=1; i<arr.length; i++) {
if (arr[i] < arr[i-1]) {
current = arr[i];
}
else {
@primaryobjects
primaryobjects / longest-unique-substring.js
Created February 16, 2017 20:15
Finding the longest substring with unique characters in a string. http://jsbin.com/lowinac/edit?js,console
var longestUnique = function(s) {
var result = '';
var hash = {};
var current = '';
for (var i=0; i<s.length; i++) {
hash[s[i]] = hash[s[i]] ? hash[s[i]] + 1 : 1;
if (hash[s[i]] > 1) {
// Terminate string here.
@primaryobjects
primaryobjects / prime.js
Created February 16, 2017 20:02
Display prime numbers from 1 to 100.
for (var counter = 0; counter <= 100; counter++) {
var notPrime = false;
for (var i = 2; i <= counter; i++) {
if (counter % i === 0 && i !== counter) {
notPrime = true;
}
}
if (notPrime === false) {
@primaryobjects
primaryobjects / breadth-first-search.js
Created February 16, 2017 19:52
Basic breadth first search of a tree, example. Finding the first index of an element in a tree. Demo at http://jsbin.com/bufuwub/edit?js,console
var addNode = function(val, head) {
var node = { val: val, children: [] };
if (head) {
head.children.push(node);
}
return node;
}
@primaryobjects
primaryobjects / dollartize.js
Created February 16, 2017 19:32
Convert a number into a comma-separated dollar amount. Demo at http://jsbin.com/qipude/edit?js,console
var dollartize = function(num) {
var result = '';
var numStr = num + '';
var count = 0;
for (var i=numStr.length - 1; i>=0; i--) {
if (count++ % 3 === 0) {
result = ',' + result;
}
@primaryobjects
primaryobjects / fib.go
Last active February 16, 2017 15:32
Fibonacci written in Go. My first Go program lol.
package main
import "fmt"
func fibn(n int) int {
result := 0
if n == 0 {
result = 0
} else if n == 1 {
@primaryobjects
primaryobjects / binarytree.js
Last active June 27, 2017 02:54
Binary tree, insert, exists, preorder, postorder, inorder traversal.
var insert = function(value, root) {
if (!root) {
// Create a new root.
root = { val: value };
}
else {
var current = root;
while (current) {
if (value < current.val) {
if (!current.left) {