Skip to content

Instantly share code, notes, and snippets.

# using lambda function inside filter function
drinks = ['beer', 'beer', 'milk', 'beer']
print(list(filter(lambda drink: (drink == 'beer'), drinks)))
# output: ['beer', 'beer', 'beer']
# 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 November 8, 2022 16:33
create grill function receive 1 argument
# 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
# global scope
outer = "eggshell"
def egg():
# enclosing scope
middle = "egg white"
def inner_egg():
# local scope
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.");
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.");
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]
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]
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))
np.random.rand(2,4)
# Output
# array([[0.62998756, 0.59149609, 0.57944416, 0.485989 ],
# [0.72946946, 0.47362045, 0.14221045, 0.27405028]])