Skip to content

Instantly share code, notes, and snippets.

View primaryobjects's full-sized avatar

Kory Becker primaryobjects

View GitHub Profile
#
# [2017-02-06] Challenge #302 [Easy] Spelling with Chemistry
# Using the periodic table of elements, convert text into elementized words.
# https://www.reddit.com/r/dailyprogrammer/comments/5seexn/20170206_challenge_302_easy_spelling_with/
#
# by Kory Becker
# http://primaryobjects.com
#
data <- read.csv('https://gist.githubusercontent.com/primaryobjects/46edcc29e7b6dac3ea84a17995707678/raw/b361d2bd9952016a3481c0220f8d9fef8963299e/elements.tsv', sep='\t')
Element Symbol Z Atomic Weight c
Actinium Ac 89 (227) 1.1
Aluminum Al 13 26.9815 1.5
Americium Am 95 (243) 1.3
Antimony Sb 51 121.75 1.9
Argon Ar 18 39.948
Arsenic As 33 74.9216 2.0
Astatine At 85 (210) 2.2
Barium Ba 56 137 0.9
Berkelium Bk 97 (247) 1.3
@primaryobjects
primaryobjects / postOrderTraversal.js
Created February 13, 2017 22:15
Postorder traversal of a binary tree.
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {number[]}
var insertionSort = function(arr) {
if (arr && arr.length) {
for (var i=1; i<arr.length; i++) {
var temp = arr[i];
var j = i - 1;
while (j >= 0 && arr[j] > temp) {
// Swap positions, bumping the values up.
arr[j+1] = arr[j];
j--;
var selectionSort = function(arr) {
if (arr && arr.length) {
for (var i=0; i<arr.length - 1; i++) {
// Start at position i.
var minIndex = i;
// Find the next smallest value.
for (var j=i+1; j<arr.length; j++) {
if (arr[j] < arr[minIndex]) {
// Found a new minimum, remember its index.
@primaryobjects
primaryobjects / longestPalindrome.js
Created February 8, 2017 18:21
Longest palindrome - Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters.
/**
* @param {string} s
* @return {number}
*/
var longestPalindrome = function(s) {
var result = 0;
var hash = {};
for (var i=0; i<s.length; i++) {
// Build a count of the letters.
@primaryobjects
primaryobjects / 1-greatestDifference.js
Created February 8, 2017 16:13
Find the greatest difference between any two numbers within an array. Demo at http://jsbin.com/duxefuqira/edit?js,console
var greatestDifference = function(arr) {
/* Approach 1: O(n^2), Find all permutations of numbers and subtract to get the difference. */
var result = 0;
var max = 0;
for (var i=0; i<arr.length; i++) {
for (var j=i+1; j<arr.length; j++) {
var a = arr[i];
var b = arr[j];
@primaryobjects
primaryobjects / ransomNote.js
Created February 8, 2017 14:43
Ransom Note: Write a function that will return true if a ransom note can be constructed from the magazine letters. i.e., a substring exists within second string.
/* Approach: O(n), Count all of the letters in each string, adding and subtracting from the counts as we go along.
If a count goes negative, mark it as a possible differing letter in a second hash.
If a count goes to 0, delete it as a possible differing letter (since it has balanced out the counts with the other string).
When we're done, there should be no entries in the negatives hash (meaning the ransom note could be fully constructed from the available letters. i.e., the substring exists within the second string.)
The same solution is used in "Find the Difference" (find the differing letter in two strings) https://gist.github.com/primaryobjects/7655cb19be5cd43aa8d1a340f2041725
*/
/**
* @param {string} ransomNote
* @param {string} magazine
@primaryobjects
primaryobjects / 1-findTheDifference.js
Created February 8, 2017 02:19
Find the Difference - find the differing letter in two strings.
/* Approach 1: O(n), Count all of the letters in each string, adding and subtracting from the counts as we go along.
If a count goes negative, mark it as a possible differing letter in a second hash.
If a count goes to 0, delete it as a possible differing letter (since it has balanced out the counts with the other string).
When we're done, there should be only 1 remaining entry in the negatives hash. That's our answer. */
var findTheDifference = function(s, t) {
var result = '';
var hash = {};
var negatives = {};
@primaryobjects
primaryobjects / 1-nim.js
Created February 7, 2017 19:52
Nim Game - can you win at the game of nim, given a set number of stones?
/**
* @param {number} n
* @return {boolean}
*/
var canWinNim = function(n) {
return (n % 4 === 0) ? false : true;
}