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
| def sum(list: List[Int]): Int = list.foldLeft(0)((a, b) => a + b) | |
| def sum(list: List[Int]) = list.foldLeft(0)(_ + _) | |
| def multiply(list: List[Int]) = list.foldLeft(1)((a, b) => a * b) | |
| def multiply(list: List[Int]) = list.foldLeft(1)(_ * _) |
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
| def combination(x: Int, y: Int): Int = { | |
| assert(x > y) | |
| (x to (y + 1) by -1).product / (((x to y by -1).size - 1) to 1 by -1).product | |
| } | |
| def permutation(x: Int, y: Int): Int = { | |
| assert(x > y) | |
| (x to y + 1).product | |
| } |
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
| // Use Gists to store code you would like to remember later on | |
| console.log(window); // log the "window" object to the console |
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
| function palindrome(num) { | |
| if (num.toString().split("").reverse().join("") === num.toString()) { | |
| if ((num).toString(2) === (num).toString(2).split("").reverse().join("")) { | |
| if ((num).toString(16) === (num).toString(16).split("").reverse().join("")) { | |
| return true; | |
| } | |
| } | |
| } | |
| return false; | |
| } |
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
| function reverseString(str) { | |
| let reversedStr = str.split(''); | |
| reversedStr.reverse(); | |
| return reversedStr.join(''); | |
| } |
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
| function factorialize(num) { | |
| // ternary operator | |
| return num < 1 ? 1 : num * factorialize(num - 1); | |
| } |
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
| function palindrome(str) { | |
| const formattedStr = (str) => { | |
| if (typeof str === 'string') { | |
| str = str.toLowerCase(); | |
| str = str.replace(/\s+/g, ''); | |
| // assumes there are several that should be removed. | |
| str = str.replace(/[!"#$%&'()\*\+\-\.,\/:;<=>?@\[\\\]^_`{|}~]/g, ''); | |
| return str !== undefined ? str : null; | |
| } else { | |
| return null; |
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
| function findLongestWord(str) { | |
| let strArr = str.split(' '); | |
| let longest = ''; | |
| strArr.forEach((val) => { | |
| if (val.length > longest.length) { | |
| longest = val; | |
| } | |
| }); | |
| return longest.length; | |
| } |
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
| function titleCase(str) { | |
| let strArr = str.toLowerCase().split(' '); | |
| let result = [] | |
| strArr.forEach((str) => { | |
| result.push(str.charAt(0).toUpperCase() + str.slice(1, str.length)); | |
| }); | |
| return result.join(' '); | |
| } |
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
| function confirmEnding(str, target) { | |
| if (str.indexOf(' ') === -1) { | |
| // turn str into array if str is one word. | |
| return str.split('')[str.length - 1] === target; | |
| } else { | |
| // turn str into an array if str has more than 1 word and split by blank. | |
| let words = str.split(' '); | |
| return words[words.length - 1] === target || words[words.length - 1].match(target) !== null; | |
| } | |
| } |
OlderNewer