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 gcd(m, n) { return n ? gcd(n, m % n) : m; } |
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
| // CalcAgeAt calculates the age at the given time. | |
| func CalcAgeAt(t time.Time, birthDay time.Time) int { | |
| age := t.Year() - birthDay.Year() | |
| if t.YearDay() < birthDay.YearDay() { | |
| age-- | |
| } | |
| return age | |
| } |
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
| // Kahan-Babuska algorithm | |
| export const sum = (x: number[]) => { | |
| if (x.length === 0) return 0 | |
| let res = x[0], c = 0, t = 0 | |
| for (let i = 1; i < x.length; i++) { | |
| t = res + x[i] | |
| if (Math.abs(res) >= Math.abs(x[i])) { | |
| c += res - t + x[i] | |
| } else { | |
| c += x[i] - t + res |
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
| const grades = ['小1', '小2', '小3', '小4', '小5', '小6', '中1', '中2', '中3']; | |
| // 誕生日からat時点での学年を算出 | |
| const birthdayToSchoolGrade = (birthday, at) => { | |
| const d = new Date(at); | |
| d.setMonth(d.getMonth() - 3); | |
| d.setMonth(3); | |
| d.setDate(1); | |
| const bd = new Date(birthday); | |
| const dNum = Number(d.toISOString().substring(0, 10).replaceAll('-', '')); |
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
| const memoize = (func) => { | |
| const memo = new Map() | |
| return (...args) => { | |
| const key = JSON.stringify(args) | |
| if (!memo.has(key)) memo.set(key, func(...args)) | |
| return memo.get(key) | |
| } | |
| } | |
| /* |
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
| #!/bin/bash | |
| FROM_USER=user1 | |
| FROM_HOST=host1 | |
| FROM_DB=database1 | |
| FROM_TBL=table1 | |
| TO_USER=user2 | |
| TO_HOST=host2 | |
| TO_DB=database2 |
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
| func Repeat(s string, n uint) string { | |
| switch n { | |
| case 0: | |
| return "" | |
| case 1: | |
| return s | |
| } | |
| max := len(s) * int(n) | |
| var b strings.Builder |
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 sharpeRatio(array, interest = 0) { | |
| if (array.length < 3) return { mean: 0, sd: 0, sharpeRatio: 0 }; | |
| let mean = 0, sqsum = 0, l = array.length; | |
| for (let n = 1; n < l; n++) { | |
| const changeRate = (array[n] - array[n - 1]) / array[n - 1]; | |
| const x = changeRate - mean; | |
| mean += x / n; | |
| sqsum += (n - 1) * x * x / n; | |
| } | |
| const sd = Math.sqrt(sqsum / (l - 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
| // t期運用時点における運用総額をs(t)とする。 | |
| // 初期金額をα,各期の積立金額をβ,期待リターンをR(= 1 + r)とすると,各期の運用総額sは,期間tの関数S(t)として次の式で表される。 | |
| // | |
| // S(t) = (α * R^t) + β * Σ(k = 0, t - 1) R^k | |
| // | |
| // ※各期の計算は以下のようになる | |
| // | |
| // S(t = 0) = α | |
| // S(t = 1) = α * R + β = (α * R^1) + (β * R^0) | |
| // S(t = 2) = (α * R + β) * R + β = (α * R^2) + (β * R^1 + β * R^0) |
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 debounce(fun, wait = 0) { | |
| let id = null; | |
| return () => { | |
| if (id !== null) clearTimeout(id) | |
| id = setTimeout(() => { | |
| fun(); | |
| id = null; | |
| }, wait); | |
| }; | |
| } |
NewerOlder