Skip to content

Instantly share code, notes, and snippets.

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
@pratyushcrd
pratyushcrd / curry.js
Created August 14, 2017 07:41
One line curry function in Javascript ES6
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
const rootNode = {
left: anotherNode,
right: yetAnotherNode,
value: 5,
}
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
return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1
const height = (node) => node ? Math.max(height(node.left), height(node.right)) + 1 : 0
// That's all folks!
const somefunc = string => string
.split(/* Something */)
.map(/* Something */)
.filter(/* Something */)
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]
}
}
}
const flatten = arr => arr
.reduce((a, b) => a.concat(b))
// Execution
flatten([
[1, 2, 3],
[4, 5],
[6]
])
// 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(' ')