開発環境
- macOS Sierra - Apple (OS)
- Emacs (Text Editor)
- JavaScript (プログラミング言語)
- Node.js, Safari(JavaScript エンジン)
- Learning JavaScript [邦訳](参考書籍)
Think Perl 6: How to Think Like a Computer Scientist (Laurent Rosenfeld(著)、Allen B. Downey(著)、Oreilly & Associates Inc)の Part 1(Starting with the basics)、Chapter 5(Fruitful Subroutines)の Exercises. を JavaScript で取り組んでみる。
Exercises.
コード(Emacs)
HTML5
<pre id="output0"></pre> <button id="run0">run</button> <button id="clear0">clear</button> <script src="sample1.js"></script>
JavaScript
let btn0 = document.querySelector('#run0'), btn1 = document.querySelector('#clear0'), pre0 = document.querySelector('#output0'), p = (x) => pre0.textContent += x + '\n', range = (start, end, step=1) => { let result = []; for (let i = start; i < end; i += step) { result.push(i); } return result; }; let ack = (m, n) => { if (m === 0) { return n + 1; } if (m > 0 && n === 0) { return ack(m - 1, 1); } return ack(m - 1, ack(m, n - 1)); }; let firstL = (word) => word.substring(0, 1), lastL = (word) => word.substring(word.length - 1), middleL = (word) => word.substring(1, word.length - 1); let isPalindrome = (word) => word.length <= 1 ? true : firstL(word) === lastL(word) ? isPalindrome(middleL(word)) : false; let isPowerOf = (a, b) => a === 1 ? true : a % b === 0 && isPowerOf(Math.floor(a / b), b); let gcd = (a, b) => b === 0 ? a : gcd(b, a % b); let output = () => { p('2.'); range(1, 4) .forEach((m) => range(1, 5) .forEach((n) => p(`ack(${m}, ${n}) => ${ack(m, n)}`))); p('3-3'); p(isPalindrome('')); p(isPalindrome('a')); p(!isPalindrome('ab')); p(isPalindrome('aba')); p(!isPalindrome('abc')); p(isPalindrome('abba')); p(!isPalindrome('abca')); p('4.'); range(1, 21) .forEach((a) => range(2, 4) .forEach((b) => { if (isPowerOf(a, b)) { p(`${a} is power of ${b}`); } else { p(`${a} is not power of ${b}`); } })); p('5.'); range(2, 11) .forEach((a) => range(2, 11) .forEach((b) => p(`(${a}, ${b}) = ${gcd(a, b)}`))); }; btn0.onclick = output; btn1.onclick = () => pre0.textContent = ''; output();
2. ack(1, 1) => 3 ack(1, 2) => 4 ack(1, 3) => 5 ack(1, 4) => 6 ack(2, 1) => 5 ack(2, 2) => 7 ack(2, 3) => 9 ack(2, 4) => 11 ack(3, 1) => 13 ack(3, 2) => 29 ack(3, 3) => 61 ack(3, 4) => 125 3-3 true true true true true true true 4. 1 is power of 2 1 is power of 3 2 is power of 2 2 is not power of 3 3 is not power of 2 3 is power of 3 4 is power of 2 4 is not power of 3 5 is not power of 2 5 is not power of 3 6 is not power of 2 6 is not power of 3 7 is not power of 2 7 is not power of 3 8 is power of 2 8 is not power of 3 9 is not power of 2 9 is power of 3 10 is not power of 2 10 is not power of 3 11 is not power of 2 11 is not power of 3 12 is not power of 2 12 is not power of 3 13 is not power of 2 13 is not power of 3 14 is not power of 2 14 is not power of 3 15 is not power of 2 15 is not power of 3 16 is power of 2 16 is not power of 3 17 is not power of 2 17 is not power of 3 18 is not power of 2 18 is not power of 3 19 is not power of 2 19 is not power of 3 20 is not power of 2 20 is not power of 3 5. (2, 2) = 2 (2, 3) = 1 (2, 4) = 2 (2, 5) = 1 (2, 6) = 2 (2, 7) = 1 (2, 8) = 2 (2, 9) = 1 (2, 10) = 2 (3, 2) = 1 (3, 3) = 3 (3, 4) = 1 (3, 5) = 1 (3, 6) = 3 (3, 7) = 1 (3, 8) = 1 (3, 9) = 3 (3, 10) = 1 (4, 2) = 2 (4, 3) = 1 (4, 4) = 4 (4, 5) = 1 (4, 6) = 2 (4, 7) = 1 (4, 8) = 4 (4, 9) = 1 (4, 10) = 2 (5, 2) = 1 (5, 3) = 1 (5, 4) = 1 (5, 5) = 5 (5, 6) = 1 (5, 7) = 1 (5, 8) = 1 (5, 9) = 1 (5, 10) = 5 (6, 2) = 2 (6, 3) = 3 (6, 4) = 2 (6, 5) = 1 (6, 6) = 6 (6, 7) = 1 (6, 8) = 2 (6, 9) = 3 (6, 10) = 2 (7, 2) = 1 (7, 3) = 1 (7, 4) = 1 (7, 5) = 1 (7, 6) = 1 (7, 7) = 7 (7, 8) = 1 (7, 9) = 1 (7, 10) = 1 (8, 2) = 2 (8, 3) = 1 (8, 4) = 4 (8, 5) = 1 (8, 6) = 2 (8, 7) = 1 (8, 8) = 8 (8, 9) = 1 (8, 10) = 2 (9, 2) = 1 (9, 3) = 3 (9, 4) = 1 (9, 5) = 1 (9, 6) = 3 (9, 7) = 1 (9, 8) = 1 (9, 9) = 9 (9, 10) = 1 (10, 2) = 2 (10, 3) = 1 (10, 4) = 2 (10, 5) = 5 (10, 6) = 2 (10, 7) = 1 (10, 8) = 2 (10, 9) = 1 (10, 10) = 10
0 コメント:
コメントを投稿