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 height = (node) => node ? Math.max(height(node.left), height(node.right)) + 1 : 0 | |
| // That's all folks! |
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
| return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 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 height (node) { | |
| if (!node) { | |
| return 0 | |
| } | |
| var leftHeight = height(node.left) | |
| var rightHeight = height(node.right) | |
| if (leftHeight > rightHeight) { | |
| return leftHeight + 1 // Adding 1 for considering node's height | |
| } else { | |
| return rightHeight + 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
| const rootNode = { | |
| left: anotherNode, | |
| right: yetAnotherNode, | |
| value: 5, | |
| } |
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 curry = (fn, arr = []) => (...params) => (arr.length + params.length >= fn.length) ? fn(...[...arr, ...params]) : curry(fn, [...arr, ...params]) | |
| /* Testing with a sum function */ | |
| const sum = curry(function (a, b, c, d, e) { | |
| return a + b + c + d + e | |
| }) | |
| console.log(sum(1, 2, 3, 4, 5)) // 15 | |
| console.log(sum()()(1)()(2)()(3, 4)(5)) // 15 | |
| console.log(sum(1, 2, 3)()()()(4, 5)) // 15 |
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
| import random | |
| num = random.randrange(0, 10) | |
| while True: | |
| str = input("guess between 0 to 9 or type exit ") | |
| if str == "exit": | |
| break | |
| if not(str.isalnum()): | |
| print("not a valid input ") | |
| continue |
NewerOlder