Skip to content

Instantly share code, notes, and snippets.

View lambda-2.py
# using lambda function inside filter function
drinks = ['beer', 'beer', 'milk', 'beer']
print(list(filter(lambda drink: (drink == 'beer'), drinks)))
# output: ['beer', 'beer', 'beer']
View lambda-2.py
# using lambda function inside filter function
drinks = ['beer', 'beer', 'milk', 'beer']
print(list(filter(lambda drink: (drink == 'beer'), drinks)))
# output: ['beer', 'beer', 'beer']
@werterzz
werterzz / lambda-1.py
Created Nov 8, 2022
create grill function receive 1 argument
View lambda-1.py
# create grill function receive 1 argument
grill = lambda food: "grilled " + food
print(grill('beef'))
# output: grilled beef
# create cook function receive 2 arguments
cook = lambda cook, food: cook + " " + food
print(cook("boil", "egg"))
# output: boil egg
View python-scope-explain.py
# global scope
outer = "eggshell"
def egg():
# enclosing scope
middle = "egg white"
def inner_egg():
# local scope
View coverage-metrics-2.js
const doYouLikeDog = (answer) => {
return answer === true ? "you like dogs." : answer === false ? "you don't like dogs." : "you didn't answer my question."
}
test('pass true on doYouLikeDog()', () => {
expect(doYouLikeDog(true)).toBe("you like dogs.");
});
test('pass false on doYouLikeDog()', () => {
expect(doYouLikeDog(false)).toBe("you don't like dogs.");
View coverage-metrics-1.js
const doYouLikeDog = (answer) => { // cover
if (answer === true) return "you like dogs." // cover
else if (answer === false) return "you don't like dogs." // cover
else return "you didn't answer my question." // not cover
} // cover
//-----------------------------------
test('pass true on doYouLikeDog()', () => {
expect(doYouLikeDog(true)).toBe("you like dogs.");
View convert-numpy-array-to-list.py
npArrData = np.array([1, 2, 3])
listData = npArrData.tolist()
print("npArrData: ", type(npArrData), ": ", npArrData)
print("listData:", type(listData), ": ", listData)
# Output
# npArrData: <class 'numpy.ndarray'> : [1 2 3]
# listData: <class 'list'> : [1, 2, 3]
View convert-list-to-numpy-array.py
listData = [1, 2, 3]
npArrData = np.asarray(listData)
print("listData:", type(listData), ": ", listData)
print("npArrData: ", type(npArrData), ": ", npArrData)
# Output
# listData: <class 'list'> : [1, 2, 3]
# npArrData: <class 'numpy.ndarray'> : [1 2 3]
View numpy-multi.py
floatArr = np.random.rand(2, 4)
intArr = np.random.randint(100, size=(2, 4))
print("floatArr: \n" + str(floatArr))
print("\nintArr: \n" + str(intArr))
multArr = np.multiply(floatArr, intArr)
print("\nmultArr: \n" + str(multArr))
View numpy-zero-to-one.py
np.random.rand(2,4)
# Output
# array([[0.62998756, 0.59149609, 0.57944416, 0.485989 ],
# [0.72946946, 0.47362045, 0.14221045, 0.27405028]])