This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # | |
| # [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') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Definition for a binary tree node. | |
| * function TreeNode(val) { | |
| * this.val = val; | |
| * this.left = this.right = null; | |
| * } | |
| */ | |
| /** | |
| * @param {TreeNode} root | |
| * @return {number[]} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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--; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* 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 = {}; | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @param {number} n | |
| * @return {boolean} | |
| */ | |
| var canWinNim = function(n) { | |
| return (n % 4 === 0) ? false : true; | |
| } |