Viết chương trình Python để tìm các số chia hết cho mười chín hoặc mười ba từ danh sách các số bằng Lambda
Giải pháp mẫu
Mã Python
nums = [19, 65, 57, 39, 152, 639, 121, 44, 90, 190]
print["Orginal list:"]
print[nums]
result = list[filter[lambda x: [x % 19 == 0 or x % 13 == 0], nums]]
print["\nNumbers of the above list divisible by nineteen or thirteen:"]
print[result]
Đầu ra mẫu
Orginal list: [19, 65, 57, 39, 152, 639, 121, 44, 90, 190] Numbers of the above list divisible by nineteen or thirteen: [19, 65, 57, 39, 152, 190]
Trực quan hóa việc thực thi mã Python
Công cụ sau đây trực quan hóa những gì máy tính đang làm từng bước khi nó thực thi chương trình nói trên
//Để in các số chia hết cho cả 3 và 7 từ 1 đến 100
print["Các số chia hết cho 3 và 7 từ 1 đến 100 là. "]
cho tôi trong phạm vi [1.100].
if [i % 3 == 0 và i % 7 == 0].
in[i]
Trong chương trình bên dưới, chúng tôi đã sử dụng hàm ẩn danh [lambda] bên trong hàm có sẵn filter[]
để tìm tất cả các số chia hết cho 13 trong danh sách
Mã nguồn
# Take a list of numbers
my_list = [12, 65, 54, 39, 102, 339, 221,]
# use anonymous function to filter
result = list[filter[lambda x: [x % 13 == 0], my_list]]
# display the result
print["Numbers divisible by 13 are",result]
đầu ra
Numbers divisible by 13 are [65, 39, 221]
Tìm hiểu thêm về filter[] tại Python filter[]
Cách tiếp cận. Ví dụ: giả sử N = 20 làm giới hạn, thì chương trình sẽ in tất cả các số nhỏ hơn 20 chia hết cho cả 3 và 5. Đối với điều này, hãy chia mỗi số từ 0 đến N cho cả 3 và 5 và kiểm tra phần còn lại của chúng. Nếu phần còn lại là 0 trong cả hai trường hợp thì chỉ cần in số đó
Dưới đây là việc thực hiện.
Python3
# Python program to print all the numbers
# divisible by 3 and 5 for a given number
________số 8
# Result function with N
Orginal list: [19, 65, 57, 39, 152, 639, 121, 44, 90, 190] Numbers of the above list divisible by nineteen or thirteen: [19, 65, 57, 39, 152, 190]0
Orginal list: [19, 65, 57, 39, 152, 639, 121, 44, 90, 190] Numbers of the above list divisible by nineteen or thirteen: [19, 65, 57, 39, 152, 190]1
Orginal list: [19, 65, 57, 39, 152, 639, 121, 44, 90, 190] Numbers of the above list divisible by nineteen or thirteen: [19, 65, 57, 39, 152, 190]2
Orginal list: [19, 65, 57, 39, 152, 639, 121, 44, 90, 190] Numbers of the above list divisible by nineteen or thirteen: [19, 65, 57, 39, 152, 190]3____14
Orginal list: [19, 65, 57, 39, 152, 639, 121, 44, 90, 190] Numbers of the above list divisible by nineteen or thirteen: [19, 65, 57, 39, 152, 190]3
# Python program to print all the numbers
1 # Python program to print all the numbers
2# Python program to print all the numbers
3 # Python program to print all the numbers
4# Python program to print all the numbers
5# Python program to print all the numbers
6
# Python program to print all the numbers
7# Python program to print all the numbers
8
# Python program to print all the numbers
7# divisible by 3 and 5 for a given number
0 # Python program to print all the numbers
2# divisible by 3 and 5 for a given number
2 # divisible by 3 and 5 for a given number
3 # divisible by 3 and 5 for a given number
4# divisible by 3 and 5 for a given number
4 # divisible by 3 and 5 for a given number
6 # divisible by 3 and 5 for a given number
7 # Python program to print all the numbers
2# divisible by 3 and 5 for a given number
2
0 # divisible by 3 and 5 for a given number
4# divisible by 3 and 5 for a given number
4 # divisible by 3 and 5 for a given number
6
4
5
6
7
8
9# Result function with N
0 # Result function with N
1# Result function with N
2# divisible by 3 and 5 for a given number
4 # Result function with N
4
# Result function with N
5
# Python program to print all the numbers
7# Result function with N
7____84
5
Orginal list: [19, 65, 57, 39, 152, 639, 121, 44, 90, 190] Numbers of the above list divisible by nineteen or thirteen: [19, 65, 57, 39, 152, 190]00
________số 8
Orginal list: [19, 65, 57, 39, 152, 639, 121, 44, 90, 190] Numbers of the above list divisible by nineteen or thirteen: [19, 65, 57, 39, 152, 190]02
# divisible by 3 and 5 for a given number
0
Orginal list: [19, 65, 57, 39, 152, 639, 121, 44, 90, 190] Numbers of the above list divisible by nineteen or thirteen: [19, 65, 57, 39, 152, 190]04
# divisible by 3 and 5 for a given number
4# divisible by 3 and 5 for a given number
4 Orginal list: [19, 65, 57, 39, 152, 639, 121, 44, 90, 190] Numbers of the above list divisible by nineteen or thirteen: [19, 65, 57, 39, 152, 190]07
4Orginal list: [19, 65, 57, 39, 152, 639, 121, 44, 90, 190] Numbers of the above list divisible by nineteen or thirteen: [19, 65, 57, 39, 152, 190]2
Orginal list: [19, 65, 57, 39, 152, 639, 121, 44, 90, 190] Numbers of the above list divisible by nineteen or thirteen: [19, 65, 57, 39, 152, 190]3____111
Orginal list: [19, 65, 57, 39, 152, 639, 121, 44, 90, 190] Numbers of the above list divisible by nineteen or thirteen: [19, 65, 57, 39, 152, 190]3____113
# divisible by 3 and 5 for a given number
4 Orginal list: [19, 65, 57, 39, 152, 639, 121, 44, 90, 190] Numbers of the above list divisible by nineteen or thirteen: [19, 65, 57, 39, 152, 190]15
Orginal list: [19, 65, 57, 39, 152, 639, 121, 44, 90, 190] Numbers of the above list divisible by nineteen or thirteen: [19, 65, 57, 39, 152, 190]2
Orginal list: [19, 65, 57, 39, 152, 639, 121, 44, 90, 190] Numbers of the above list divisible by nineteen or thirteen: [19, 65, 57, 39, 152, 190]3____118
Orginal list: [19, 65, 57, 39, 152, 639, 121, 44, 90, 190] Numbers of the above list divisible by nineteen or thirteen: [19, 65, 57, 39, 152, 190]3____120
Độ phức tạp về thời gian. O[N]
Không gian phụ. Ô[1]
Phương pháp thay thế. Điều này cũng có thể được thực hiện bằng cách kiểm tra xem số đó có chia hết cho 15 hay không, vì BCNN của 3 và 5 là 15 và bất kỳ số nào chia hết cho 15 cũng chia hết cho 3 và 5 và ngược lại.
Python3
Orginal list: [19, 65, 57, 39, 152, 639, 121, 44, 90, 190] Numbers of the above list divisible by nineteen or thirteen: [19, 65, 57, 39, 152, 190]21
Orginal list: [19, 65, 57, 39, 152, 639, 121, 44, 90, 190] Numbers of the above list divisible by nineteen or thirteen: [19, 65, 57, 39, 152, 190]22
Orginal list: [19, 65, 57, 39, 152, 639, 121, 44, 90, 190] Numbers of the above list divisible by nineteen or thirteen: [19, 65, 57, 39, 152, 190]23
# divisible by 3 and 5 for a given number
4Orginal list: [19, 65, 57, 39, 152, 639, 121, 44, 90, 190] Numbers of the above list divisible by nineteen or thirteen: [19, 65, 57, 39, 152, 190]25
# Python program to print all the numbers
1
Orginal list: [19, 65, 57, 39, 152, 639, 121, 44, 90, 190] Numbers of the above list divisible by nineteen or thirteen: [19, 65, 57, 39, 152, 190]27
# Python program to print all the numbers
3 # Python program to print all the numbers
4
7# divisible by 3 and 5 for a given number
6Orginal list: [19, 65, 57, 39, 152, 639, 121, 44, 90, 190] Numbers of the above list divisible by nineteen or thirteen: [19, 65, 57, 39, 152, 190]32
Orginal list: [19, 65, 57, 39, 152, 639, 121, 44, 90, 190] Numbers of the above list divisible by nineteen or thirteen: [19, 65, 57, 39, 152, 190]33
Orginal list: [19, 65, 57, 39, 152, 639, 121, 44, 90, 190] Numbers of the above list divisible by nineteen or thirteen: [19, 65, 57, 39, 152, 190]34
Orginal list: [19, 65, 57, 39, 152, 639, 121, 44, 90, 190] Numbers of the above list divisible by nineteen or thirteen: [19, 65, 57, 39, 152, 190]33
# divisible by 3 and 5 for a given number
0 Orginal list: [19, 65, 57, 39, 152, 639, 121, 44, 90, 190] Numbers of the above list divisible by nineteen or thirteen: [19, 65, 57, 39, 152, 190]27
# divisible by 3 and 5 for a given number
2Orginal list: [19, 65, 57, 39, 152, 639, 121, 44, 90, 190] Numbers of the above list divisible by nineteen or thirteen: [19, 65, 57, 39, 152, 190]39
# divisible by 3 and 5 for a given number
4# divisible by 3 and 5 for a given number
4# divisible by 3 and 5 for a given number
6
4Orginal list: [19, 65, 57, 39, 152, 639, 121, 44, 90, 190] Numbers of the above list divisible by nineteen or thirteen: [19, 65, 57, 39, 152, 190]3______86
Orginal list: [19, 65, 57, 39, 152, 639, 121, 44, 90, 190] Numbers of the above list divisible by nineteen or thirteen: [19, 65, 57, 39, 152, 190]46
# divisible by 3 and 5 for a given number
4# Result function with N
1Orginal list: [19, 65, 57, 39, 152, 639, 121, 44, 90, 190] Numbers of the above list divisible by nineteen or thirteen: [19, 65, 57, 39, 152, 190]49
Đầu ra
0 15 30 45
Độ phức tạp về thời gian. O[n]
Không gian phụ. Ô[1]
Mời các bạn tham khảo bài viết đầy đủ về Chương trình in các số chia hết cho 3 và 5 cho một số cho trước để biết thêm chi tiết