Lambda Flashcards

1
Q

print((lambda a: a+10)(5))

A

15

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

print((lambda a,b:a*b)(5,6))

A

30

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

x = lambda a, b, c: a + b + c

print(x(5, 6, 2))

A

13

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

print(list(map(lambda x: x.capitalize(), [‘cat’, ‘dog’, ‘cow’])))

A

[‘Cat’, ‘Dog’, ‘Cow’]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

print(list(map(lambda x: x[::-1], [‘cat’, ‘dog’, ‘cow’])))

A

[‘tac’, ‘god’, ‘woc’]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

print(list(map(lambda x: x.replace(‘o’,’a’), [‘cat’, ‘dog’, ‘cow’])))

A

[‘cat’, ‘dag’, ‘caw’]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
#There are spaces to the left and right of cat, dog, and cow
print(list(map(lambda x: x.strip(), [' cat ', ' dog  ', ' cow '])))
A

[‘cat’, ‘dog’, ‘cow’]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

print(list(map(lambda x: x.endswith(‘g’), [‘cat’, ‘dog’, ‘cow’])))

A

[False, True, False]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

print(list(map(lambda x: x.isalpha(), [‘cat’, ‘dog’, ‘cow’])))

A

[True, True, True]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

print(list(map(lambda x: x.count(‘o’), [‘cat’, ‘dog’, ‘cow’])))

A

[0, 1, 1]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

print(list(map(lambda x: len(x), (‘cat’, ‘banana’, ‘cherry’))))

A

[3, 6, 6]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

avar = lambda x: x != 2 and x != 4

print(list(filter(avar, range(6))))

A

[0, 1, 3, 5]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

avar = lambda x: x < 4 or x > 8

print(list(filter(avar, range(11))))

A

[0, 1, 2, 3, 9, 10]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

print(list(filter(lambda x: x%2 == 0, range(11))))

A

[0, 2, 4, 6, 8, 10]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

student_tuples = [(‘john’, ‘A’, 15),(‘jane’, ‘B’, 12),(‘dave’, ‘B’, 10),]
print(sorted(student_tuples, key=lambda student: student[2]))

A
#sorts 10, 12, then 15
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

student_tuples = [(‘john’, ‘A’, 15),(‘jane’, ‘B’, 12),(‘dave’, ‘B’, 10),]
print(sorted(student_tuples, key=lambda student: student[0]))

A
#sorts dave, jane, then john
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
17
Q

lst = [(‘candy’,’30’,’100’), (‘apple’,’10’,’200’), (‘baby’,’20’,’300’)]
lst.sort(key=lambda x:x[1])
print(lst)

A
#sorts 10, 20, then 30
[('apple', '10', '200'), ('baby', '20', '300'), ('candy', '30', '100')]