Skip to content

Instantly share code, notes, and snippets.

View hirohiro2255's full-sized avatar

Hirokazu Hirono hirohiro2255

View GitHub Profile
@hirohiro2255
hirohiro2255 / foldLeft_for_fun.scala
Created January 31, 2017 10:42
just wanted to write them down for no reason..
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)(_ * _)
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
}
@hirohiro2255
hirohiro2255 / 0_reuse_code.js
Created February 5, 2017 04:44
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@hirohiro2255
hirohiro2255 / palindrome.js
Created February 13, 2017 23:53
palindrome for number
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;
}
@hirohiro2255
hirohiro2255 / reverseString.js
Created June 12, 2017 10:55
reverse a string.
function reverseString(str) {
let reversedStr = str.split('');
reversedStr.reverse();
return reversedStr.join('');
}
@hirohiro2255
hirohiro2255 / factorialize.js
Created June 12, 2017 11:04
function that factorializes a number.
function factorialize(num) {
// ternary operator
return num < 1 ? 1 : num * factorialize(num - 1);
}
@hirohiro2255
hirohiro2255 / palindrome.js
Created June 13, 2017 02:38
palindrome func to compare two strings
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;
@hirohiro2255
hirohiro2255 / longestWord.js
Created June 13, 2017 07:00
find the longest word from array of string
function findLongestWord(str) {
let strArr = str.split(' ');
let longest = '';
strArr.forEach((val) => {
if (val.length > longest.length) {
longest = val;
}
});
return longest.length;
}
@hirohiro2255
hirohiro2255 / titleCase.js
Last active June 15, 2017 05:11
function that gets an array of string and return it as string with each words capitalized
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(' ');
}
@hirohiro2255
hirohiro2255 / confirmEnding.js
Created June 16, 2017 05:44
checks if end of str is identical to the seconds arguments
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;
}
}