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 |
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
| 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
| 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
| 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
| 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
| const somefunc = string => string | |
| .split(/* Something */) | |
| .map(/* Something */) | |
| .filter(/* Something */) |
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 positiveSum = arr => { | |
| let sum = 0 | |
| let i = 0 | |
| let ii = arr.length | |
| for (i = 0; i < ii; ++i) { | |
| if (arr[i] > 0) { | |
| sum += arr[i] | |
| } | |
| } | |
| } |
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 flatten = arr => arr | |
| .reduce((a, b) => a.concat(b)) | |
| // Execution | |
| flatten([ | |
| [1, 2, 3], | |
| [4, 5], | |
| [6] | |
| ]) |
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
| // Reverse only the letters of words in a sentence | |
| // A helper function to reverse words | |
| const reverseWord = str => str.split('').reverse().join('') | |
| // Actual function | |
| const reverseLettersInSentence = sentence => sentence | |
| .split(' ') | |
| .map(reverseWord) | |
| .join(' ') |
OlderNewer