Luyện tập câu hỏi phỏng vấn lập trình bằng Python
Kirill Smirnov
Data Science Consultant, Altran
map( )
map( Iterable1, Iterable2, ...)
Các Iterable: [1, 2, 3, 4, 5], [10, 20, 30, 40, 50], ...
map(function(x1, x2, ...), Iterable1, Iterable2, ...)
Các Iterable: [1, 2, 3, 4, 5], [10, 20, 30, 40, 50], ...
1, 10, ... → function(1, 10, ...) → đối tượng mới
2, 20, ... → function(2, 20, ...) → đối tượng mới
3, 30, ... → function(3, 30, ...) → đối tượng mới
4, 40, ... → function(4, 40, ...) → đối tượng mới
5, 50, ... → function(5, 50, ...) → đối tượng mới
nums = [1, 2, 3, 4, 5]
Mục tiêu: lấy [1, 4, 9, 16, 25]
def squared(x):
return x**2
squares = map(squared, nums)
print(squares)
<map object at 0x7fdbe4ab3da0>
squares là iterable
for square in squares:
print(square)
1
4
9
16
25
nums = [1, 2, 3, 4, 5]
Mục tiêu: lấy [1, 4, 9, 16, 25]
def squared(x):
return x**2
squares = map(squared, nums)
print(squares)
<map object at 0x7fdbe4ab3da0>
squares là Iterable
list(squares)
[1, 4, 9, 16, 25]
nums = [1, 2, 3, 4, 5]
Mục tiêu: lấy [1, 4, 9, 16, 25]
def squared(x):
return x**2
squares = map(squared, nums)
print(squares)
<map object at 0x7fdbe4ab3da0>
squares là Iterator
next(squares)
1
next(squares)
4
nums = [1, 2, 3, 4, 5]
Mục tiêu: lấy [1, 4, 9, 16, 25]
def squared(x):
return x**2
squares = map(squared, nums)
list(squares)
[1, 4, 9, 16, 25]
nums = [1, 2, 3, 4, 5]
Mục tiêu: lấy [1, 4, 9, 16, 25]
squares = map(lambda x: x**2, nums)
list(squares)
[1, 4, 9, 16, 25]
nums1 = [1, 2, 3, 4, 5]
nums2 = [10, 20, 30, 40, 50]
Mục tiêu: [1*10, 2*20, 3*30, 4*40, 5*50] = [10, 40, 90, 160, 250]
mult = map(lambda x, y: x*y, nums1, nums2)
list(mult)
[10, 40, 90, 160, 250]
filter( )
filter( Iterable)
Iterable: [1, 2, 3, 4, 5]
filter(function(x), Iterable)
Iterable: [1, 2, 3, 4, 5]
1 → function(1) → True → giữ 1
2 → function(2) → False → loại 2
3 → function(3) → True → giữ 3
4 → function(4) → False → loại 4
5 → function(5) → True → giữ 5
nums = [-3, -2, -1, 0, 1, 2, 3]
Mục tiêu: [1, 2, 3]
def positive(x):
return x > 0
fobj = filter(positive, nums)
print(fobj)
<filter object at 0x7f196d378d68>
fobj là Iterable
for item in fobj:
print(item)
1
2
3
nums = [-3, -2, -1, 0, 1, 2, 3]
Mục tiêu: [1, 2, 3]
def positive(x):
return x > 0
fobj = filter(positive, nums)
print(fobj)
<filter object at 0x7f196d378d68>
fobj là Iterable
list(fobj)
[1, 2, 3]
nums = [-3, -2, -1, 0, 1, 2, 3]
Mục tiêu: [1, 2, 3]
def positive(x):
return x > 0
fobj = filter(positive, nums)
print(fobj)
<filter object at 0x7f196d378d68>
fobj là Iterator
next(fobj)
1
next(fobj)
4
nums = [-3, -2, -1, 0, 1, 2, 3]
Mục tiêu: [1, 2, 3]
def positive(x):
return x > 0
fobj = filter(positive, nums)
list(fobj)
[1, 2, 3]
nums = [-3, -2, -1, 0, 1, 2, 3]
Mục tiêu: [1, 2, 3]
fobj = filter(lambda x: x > 0, nums)
list(fobj)
[1, 2, 3]
from functools import reduce
reduce(function(x, y), Iterable)
Iterable: [1, 2, 3, 4, 5]
[1, 2, 3, 4, 5] → đối tượng mới cùng kiểu với phần tử

from functools import reduce
reduce(function(x, y), Iterable)
Iterable: [1, 2, 3, 4, 5]
[1, 2, 3, 4, 5] → đối tượng mới cùng kiểu với phần tử

from functools import reduce
reduce(function(x, y), Iterable)
Iterable: [1, 2, 3, 4, 5]
[1, 2, 3, 4, 5] → đối tượng mới cùng kiểu với phần tử

from functools import reduce
reduce(function(x, y), Iterable)
Iterable: [1, 2, 3, 4, 5]
[1, 2, 3, 4, 5] → đối tượng mới cùng kiểu với phần tử

from functools import reduce
reduce(function(x, y), Iterable)
Iterable: [1, 2, 3, 4, 5]
[1, 2, 3, 4, 5] → đối tượng mới cùng kiểu với phần tử

nums = [8, 4, 5, 1, 9]
Mục tiêu: 1 - nhỏ nhất
def smallest(x, y):
if x < y:
return x
else:
return y
reduce(smallest, nums)
1
smallest(8, 4) → 4
smallest(4, 5) → 4
smallest(4, 1) → 1
smallest(1, 9) → 1 - kết quả cuối
nums = [8, 1, 4, 2, 9]
Mục tiêu: 1 - nhỏ nhất
def smallest(x, y):
if x < y:
return x
else:
return y
reduce(smallest, nums)
1
nums = [8, 1, 4, 2, 9]
Mục tiêu: 1 - nhỏ nhất
reduce(lambda x, y: x if x < y else y, nums)
1
Luyện tập câu hỏi phỏng vấn lập trình bằng Python