Hướng dẫn triplets program in python - chương trình bộ ba trong python

Đưa ra một danh sách các từ, hãy viết một chương trình Python để tạo bộ ba từ danh sách đã cho.

Nội phân chính

  • Làm thế nào để bạn thực hiện một bộ ba trong Python?
  • Làm thế nào để bạn nhận được bộ ba từ một danh sách trong Python?
  • Làm thế nào để bạn tạo ra một mảng triplet?
  • Bộ ba trong mã hóa là gì?

Ví dụ:

Đầu vào: ['Geek', 'for', 'Geek', 'là', 'tốt nhất', 'tài nguyên', 'cho', 'nghiên cứu'] đầu ra: [['geek', 'for', 'geeks' ], ['for', 'geek', 'là'], ['geek', 'là', 'tốt nhất'], ['là', 'tốt nhất', 'tài nguyên'], ['tốt nhất', ' Tài nguyên ',' cho '], [' Tài nguyên ',' cho ',' Nghiên cứu ']]] [‘Geeks’, ‘for’, ‘Geeks’, ‘is’, ‘best’, ‘resource’, ‘for’, ‘study’]
Output:
[[‘Geeks’, ‘for’, ‘Geeks’], [‘for’, ‘Geeks’, ‘is’],
[‘Geeks’, ‘is’, ‘best’], [‘is’, ‘best’, ‘resource’],
[‘best’, ‘resource’, ‘for’], [‘resource’, ‘for’, ‘study’]]

Đầu vào: ['i', 'am', 'paras', 'jain', 'i', 'nghiên cứu', 'từ', 'gfg'] đầu ra: [['i', 'am', 'paras' ], ['am', 'paras', 'jain'], ['paras', 'jain', 'i'], ['jain', 'i', 'nghiên cứu'], ['i', ' Nghiên cứu ',' từ '], [' nghiên cứu ',' từ ',' gfg ']]]] [‘I’, ‘am’, ‘Paras’, ‘Jain’, ‘I’, ‘Study’, ‘From’, ‘GFG’]
Output:
[[‘I’, ‘am’, ‘Paras’], [‘am’, ‘Paras’, ‘Jain’],
[‘Paras’, ‘Jain’, ‘I’], [‘Jain’, ‘I’, ‘Study’],
[‘I’, ‘Study’, ‘From’], [‘Study’, ‘From’, ‘GFG’]]

Hãy cùng xem một số phương pháp để thực hiện nhiệm vụ này.

Phương pháp số 1: Sử dụng danh sách hiểu Using List comprehension

list_of_words = ['I',

[['Geeks', 'for', 'Geeks'], ['for', 'Geeks', 'is'],
 ['Geeks', 'is', 'best'], ['is', 'best', 'resource'],
 ['best', 'resource', 'for'], ['resource', 'for', 'study']]
0____9
[['Geeks', 'for', 'Geeks'], ['for', 'Geeks', 'is'],
 ['Geeks', 'is', 'best'], ['is', 'best', 'resource'],
 ['best', 'resource', 'for'], ['resource', 'for', 'study']]
2

[['Geeks', 'for', 'Geeks'], ['for', 'Geeks', 'is'],
 ['Geeks', 'is', 'best'], ['is', 'best', 'resource'],
 ['best', 'resource', 'for'], ['resource', 'for', 'study']]
6'I',
[['Geeks', 'for', 'Geeks'], ['for', 'Geeks', 'is'],
 ['Geeks', 'is', 'best'], ['is', 'best', 'resource'],
 ['best', 'resource', 'for'], ['resource', 'for', 'study']]
9,
Input : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 10
Output : [(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]

Input : [12, 3, 6, 1, 6, 9], k = 24
Output : [(12, 6, 6), (12, 9, 3)]
1,
Input : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 10
Output : [(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]

Input : [12, 3, 6, 1, 6, 9], k = 24
Output : [(12, 6, 6), (12, 9, 3)]
3
Input : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 10
Output : [(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]

Input : [12, 3, 6, 1, 6, 9], k = 24
Output : [(12, 6, 6), (12, 9, 3)]
4

Input : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 10
Output : [(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]

Input : [12, 3, 6, 1, 6, 9], k = 24
Output : [(12, 6, 6), (12, 9, 3)]
5 =
Input : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 10
Output : [(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]

Input : [12, 3, 6, 1, 6, 9], k = 24
Output : [(12, 6, 6), (12, 9, 3)]
7
Input : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 10
Output : [(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]

Input : [12, 3, 6, 1, 6, 9], k = 24
Output : [(12, 6, 6), (12, 9, 3)]
8
Input : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 10
Output : [(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]

Input : [12, 3, 6, 1, 6, 9], k = 24
Output : [(12, 6, 6), (12, 9, 3)]
9
[(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]
03030

[(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]
1
[(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]
2
[(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]
3
[(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]
4

[(1, 2, 7), (1, 3, 6), (1, 4, 5), (2, 3, 5)]
2
[(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]
6
Input : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 10
Output : [(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]

Input : [12, 3, 6, 1, 6, 9], k = 24
Output : [(12, 6, 6), (12, 9, 3)]
5
[(1, 2, 7), (1, 3, 6), (1, 4, 5), (2, 3, 5)]
5

Output:

[['I', 'am', 'Paras'], ['am', 'Paras', 'Jain'], 
 ['Paras', 'Jain', 'I'], ['Jain', 'I', 'Study'],
 ['I', 'Study', 'DS'], ['Study', 'DS', 'Algo']]

& nbsp; Phương pháp #2: Sử dụng phép lặp
Method #2: Using Iteration

list_of_words = [

[(1, 2, 7), (1, 3, 6), (1, 4, 5), (2, 3, 5)]
9, list_of_words 1,
[(1, 2, 7), (1, 3, 6), (1, 4, 5), (2, 3, 5)]
9________list_of_words 555515

[['Geeks', 'for', 'Geeks'], ['for', 'Geeks', 'is'],
 ['Geeks', 'is', 'best'], ['is', 'best', 'resource'],
 ['best', 'resource', 'for'], ['resource', 'for', 'study']]
6list_of_words 8, =0, list_of_words 1, =4
Input : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 10
Output : [(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]

Input : [12, 3, 6, 1, 6, 9], k = 24
Output : [(12, 6, 6), (12, 9, 3)]
4

=6= =8

=9=

[(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]
7[2

Is

'I'3'I'4= =8

'I'3'I'8

'I'3, 0

Input : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 10
Output : [(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]

Input : [12, 3, 6, 1, 6, 9], k = 24
Output : [(12, 6, 6), (12, 9, 3)]
8 , 2, 3

'I'3, 0

Input : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 10
Output : [(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]

Input : [12, 3, 6, 1, 6, 9], k = 24
Output : [(12, 6, 6), (12, 9, 3)]
8
[(1, 2, 7), (1, 3, 6), (1, 4, 5), (2, 3, 5)]
0, 3

'I'3

[['Geeks', 'for', 'Geeks'], ['for', 'Geeks', 'is'],
 ['Geeks', 'is', 'best'], ['is', 'best', 'resource'],
 ['best', 'resource', 'for'], ['resource', 'for', 'study']]
00

[(1, 2, 7), (1, 3, 6), (1, 4, 5), (2, 3, 5)]
2
[['Geeks', 'for', 'Geeks'], ['for', 'Geeks', 'is'],
 ['Geeks', 'is', 'best'], ['is', 'best', 'resource'],
 ['best', 'resource', 'for'], ['resource', 'for', 'study']]
02

Output:

[['Geeks', 'for', 'Geeks'], ['for', 'Geeks', 'is'],
 ['Geeks', 'is', 'best'], ['is', 'best', 'resource'],
 ['best', 'resource', 'for'], ['resource', 'for', 'study']]

Đưa ra một danh sách các số nguyên, hãy viết một chương trình Python để tìm tất cả các bộ ba tổng hợp lên để đưa ra số nguyên ‘K.

Examples:

Input : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 10
Output : [(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]

Input : [12, 3, 6, 1, 6, 9], k = 24
Output : [(12, 6, 6), (12, 9, 3)]

Cách tiếp cận số 1: ngây thơ (sử dụng tập hợp) Trong phương pháp này, chúng tôi sử dụng hai cho các vòng lặp. Vòng lặp đầu tiên đặt phần tử đầu tiên, một phần khác để kiểm tra xem hai phần tử khác bao gồm tổng đầu tiên lên đến K hay không. Cách tiếp cận này mất độ phức tạp thời gian O (N2). Naive (Using set)
In this approach, we use two for loops. The first loop sets first element, another to check whether other two elements including first sums up to k or not. This approach takes O(n2) time complexity.

[['Geeks', 'for', 'Geeks'], ['for', 'Geeks', 'is'],
 ['Geeks', 'is', 'best'], ['is', 'best', 'resource'],
 ['best', 'resource', 'for'], ['resource', 'for', 'study']]
03
[['Geeks', 'for', 'Geeks'], ['for', 'Geeks', 'is'],
 ['Geeks', 'is', 'best'], ['is', 'best', 'resource'],
 ['best', 'resource', 'for'], ['resource', 'for', 'study']]
04

'I'3

[['Geeks', 'for', 'Geeks'], ['for', 'Geeks', 'is'],
 ['Geeks', 'is', 'best'], ['is', 'best', 'resource'],
 ['best', 'resource', 'for'], ['resource', 'for', 'study']]
06= [8

'I'3

[['Geeks', 'for', 'Geeks'], ['for', 'Geeks', 'is'],
 ['Geeks', 'is', 'best'], ['is', 'best', 'resource'],
 ['best', 'resource', 'for'], ['resource', 'for', 'study']]
10==8

'I'3

[(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]
2
[(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]
3
[(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]
4
[(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]
5
[(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]
6[8,
[(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]
7__122222222

[(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]
1
[['Geeks', 'for', 'Geeks'], ['for', 'Geeks', 'is'],
 ['Geeks', 'is', 'best'], ['is', 'best', 'resource'],
 ['best', 'resource', 'for'], ['resource', 'for', 'study']]
27=
[['Geeks', 'for', 'Geeks'], ['for', 'Geeks', 'is'],
 ['Geeks', 'is', 'best'], ['is', 'best', 'resource'],
 ['best', 'resource', 'for'], ['resource', 'for', 'study']]
29
[['Geeks', 'for', 'Geeks'], ['for', 'Geeks', 'is'],
 ['Geeks', 'is', 'best'], ['is', 'best', 'resource'],
 ['best', 'resource', 'for'], ['resource', 'for', 'study']]
30

[(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]
1
[['Geeks', 'for', 'Geeks'], ['for', 'Geeks', 'is'],
 ['Geeks', 'is', 'best'], ['is', 'best', 'resource'],
 ['best', 'resource', 'for'], ['resource', 'for', 'study']]
32= =8

[(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]
1
[['Geeks', 'for', 'Geeks'], ['for', 'Geeks', 'is'],
 ['Geeks', 'is', 'best'], ['is', 'best', 'resource'],
 ['best', 'resource', 'for'], ['resource', 'for', 'study']]
36

[(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]
1
[['Geeks', 'for', 'Geeks'], ['for', 'Geeks', 'is'],
 ['Geeks', 'is', 'best'], ['is', 'best', 'resource'],
 ['best', 'resource', 'for'], ['resource', 'for', 'study']]
38=
[['Geeks', 'for', 'Geeks'], ['for', 'Geeks', 'is'],
 ['Geeks', 'is', 'best'], ['is', 'best', 'resource'],
 ['best', 'resource', 'for'], ['resource', 'for', 'study']]
40
[(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]
9
[['Geeks', 'for', 'Geeks'], ['for', 'Geeks', 'is'],
 ['Geeks', 'is', 'best'], ['is', 'best', 'resource'],
 ['best', 'resource', 'for'], ['resource', 'for', 'study']]
42

[(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]
1
[(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]
2
[['Geeks', 'for', 'Geeks'], ['for', 'Geeks', 'is'],
 ['Geeks', 'is', 'best'], ['is', 'best', 'resource'],
 ['best', 'resource', 'for'], ['resource', 'for', 'study']]
45
[(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]
4
[(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]
5
[['Geeks', 'for', 'Geeks'], ['for', 'Geeks', 'is'],
 ['Geeks', 'is', 'best'], ['is', 'best', 'resource'],
 ['best', 'resource', 'for'], ['resource', 'for', 'study']]
48
Input : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 10
Output : [(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]

Input : [12, 3, 6, 1, 6, 9], k = 24
Output : [(12, 6, 6), (12, 9, 3)]
8

[['Geeks', 'for', 'Geeks'], ['for', 'Geeks', 'is'],
 ['Geeks', 'is', 'best'], ['is', 'best', 'resource'],
 ['best', 'resource', 'for'], ['resource', 'for', 'study']]
54
[['Geeks', 'for', 'Geeks'], ['for', 'Geeks', 'is'],
 ['Geeks', 'is', 'best'], ['is', 'best', 'resource'],
 ['best', 'resource', 'for'], ['resource', 'for', 'study']]
55
[['Geeks', 'for', 'Geeks'], ['for', 'Geeks', 'is'],
 ['Geeks', 'is', 'best'], ['is', 'best', 'resource'],
 ['best', 'resource', 'for'], ['resource', 'for', 'study']]
56
[(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]
9
[['Geeks', 'for', 'Geeks'], ['for', 'Geeks', 'is'],
 ['Geeks', 'is', 'best'], ['is', 'best', 'resource'],
 ['best', 'resource', 'for'], ['resource', 'for', 'study']]
58
[(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]
4
[['Geeks', 'for', 'Geeks'], ['for', 'Geeks', 'is'],
 ['Geeks', 'is', 'best'], ['is', 'best', 'resource'],
 ['best', 'resource', 'for'], ['resource', 'for', 'study']]
60

[['Geeks', 'for', 'Geeks'], ['for', 'Geeks', 'is'],
 ['Geeks', 'is', 'best'], ['is', 'best', 'resource'],
 ['best', 'resource', 'for'], ['resource', 'for', 'study']]
61
[['Geeks', 'for', 'Geeks'], ['for', 'Geeks', 'is'],
 ['Geeks', 'is', 'best'], ['is', 'best', 'resource'],
 ['best', 'resource', 'for'], ['resource', 'for', 'study']]
06
Input : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 10
Output : [(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]

Input : [12, 3, 6, 1, 6, 9], k = 24
Output : [(12, 6, 6), (12, 9, 3)]
8= , 2

[['Geeks', 'for', 'Geeks'], ['for', 'Geeks', 'is'],
 ['Geeks', 'is', 'best'], ['is', 'best', 'resource'],
 ['best', 'resource', 'for'], ['resource', 'for', 'study']]
61
[['Geeks', 'for', 'Geeks'], ['for', 'Geeks', 'is'],
 ['Geeks', 'is', 'best'], ['is', 'best', 'resource'],
 ['best', 'resource', 'for'], ['resource', 'for', 'study']]
67

[['Geeks', 'for', 'Geeks'], ['for', 'Geeks', 'is'],
 ['Geeks', 'is', 'best'], ['is', 'best', 'resource'],
 ['best', 'resource', 'for'], ['resource', 'for', 'study']]
61
[['Geeks', 'for', 'Geeks'], ['for', 'Geeks', 'is'],
 ['Geeks', 'is', 'best'], ['is', 'best', 'resource'],
 ['best', 'resource', 'for'], ['resource', 'for', 'study']]
69
[(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]
9
[['Geeks', 'for', 'Geeks'], ['for', 'Geeks', 'is'],
 ['Geeks', 'is', 'best'], ['is', 'best', 'resource'],
 ['best', 'resource', 'for'], ['resource', 'for', 'study']]
71

[['Geeks', 'for', 'Geeks'], ['for', 'Geeks', 'is'],
 ['Geeks', 'is', 'best'], ['is', 'best', 'resource'],
 ['best', 'resource', 'for'], ['resource', 'for', 'study']]
61
[['Geeks', 'for', 'Geeks'], ['for', 'Geeks', 'is'],
 ['Geeks', 'is', 'best'], ['is', 'best', 'resource'],
 ['best', 'resource', 'for'], ['resource', 'for', 'study']]
73
[['Geeks', 'for', 'Geeks'], ['for', 'Geeks', 'is'],
 ['Geeks', 'is', 'best'], ['is', 'best', 'resource'],
 ['best', 'resource', 'for'], ['resource', 'for', 'study']]
74
[['Geeks', 'for', 'Geeks'], ['for', 'Geeks', 'is'],
 ['Geeks', 'is', 'best'], ['is', 'best', 'resource'],
 ['best', 'resource', 'for'], ['resource', 'for', 'study']]
75

[['Geeks', 'for', 'Geeks'], ['for', 'Geeks', 'is'],
 ['Geeks', 'is', 'best'], ['is', 'best', 'resource'],
 ['best', 'resource', 'for'], ['resource', 'for', 'study']]
61
[['Geeks', 'for', 'Geeks'], ['for', 'Geeks', 'is'],
 ['Geeks', 'is', 'best'], ['is', 'best', 'resource'],
 ['best', 'resource', 'for'], ['resource', 'for', 'study']]
77
[(1, 2, 7), (1, 3, 6), (1, 4, 5), (2, 3, 5)]
0
[(1, 2, 7), (1, 3, 6), (1, 4, 5), (2, 3, 5)]
5

[['Geeks', 'for', 'Geeks'], ['for', 'Geeks', 'is'],
 ['Geeks', 'is', 'best'], ['is', 'best', 'resource'],
 ['best', 'resource', 'for'], ['resource', 'for', 'study']]
61
[['Geeks', 'for', 'Geeks'], ['for', 'Geeks', 'is'],
 ['Geeks', 'is', 'best'], ['is', 'best', 'resource'],
 ['best', 'resource', 'for'], ['resource', 'for', 'study']]
77, 2
[(1, 2, 7), (1, 3, 6), (1, 4, 5), (2, 3, 5)]
5

[['Geeks', 'for', 'Geeks'], ['for', 'Geeks', 'is'],
 ['Geeks', 'is', 'best'], ['is', 'best', 'resource'],
 ['best', 'resource', 'for'], ['resource', 'for', 'study']]
54
[['Geeks', 'for', 'Geeks'], ['for', 'Geeks', 'is'],
 ['Geeks', 'is', 'best'], ['is', 'best', 'resource'],
 ['best', 'resource', 'for'], ['resource', 'for', 'study']]
85

'I'3

[['Geeks', 'for', 'Geeks'], ['for', 'Geeks', 'is'],
 ['Geeks', 'is', 'best'], ['is', 'best', 'resource'],
 ['best', 'resource', 'for'], ['resource', 'for', 'study']]
87
[['Geeks', 'for', 'Geeks'], ['for', 'Geeks', 'is'],
 ['Geeks', 'is', 'best'], ['is', 'best', 'resource'],
 ['best', 'resource', 'for'], ['resource', 'for', 'study']]
88

[['Geeks', 'for', 'Geeks'], ['for', 'Geeks', 'is'],
 ['Geeks', 'is', 'best'], ['is', 'best', 'resource'],
 ['best', 'resource', 'for'], ['resource', 'for', 'study']]
89= [__992

[['Geeks', 'for', 'Geeks'], ['for', 'Geeks', 'is'],
 ['Geeks', 'is', 'best'], ['is', 'best', 'resource'],
 ['best', 'resource', 'for'], ['resource', 'for', 'study']]
40=
Input : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 10
Output : [(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]

Input : [12, 3, 6, 1, 6, 9], k = 24
Output : [(12, 6, 6), (12, 9, 3)]
10

[(1, 2, 7), (1, 3, 6), (1, 4, 5), (2, 3, 5)]
2
Input : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 10
Output : [(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]

Input : [12, 3, 6, 1, 6, 9], k = 24
Output : [(12, 6, 6), (12, 9, 3)]
16

Output:

[(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]

& NBSP; Cách tiếp cận #2: Sử dụng mô -đun ItertoolSpython Itertools cung cấp chức năng kết hợp (ITEBLE, R). Công cụ này trả về các chuỗi độ dài r của các phần tử từ đầu vào có thể điều chỉnh được. Mỗi lần chúng tôi thực hiện kết hợp 3 yếu tố và kiểm tra xem chúng có tổng hợp lên K hay không.
Approach #2 : Using itertools
Python itertools module provide combination(iterable, r) function. This tool returns the r length subsequences of elements from the input iterable. Every time we make a combination of 3 elements and check if they sums up to k or not.

Input : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 10
Output : [(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]

Input : [12, 3, 6, 1, 6, 9], k = 24
Output : [(12, 6, 6), (12, 9, 3)]
17
Input : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 10
Output : [(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]

Input : [12, 3, 6, 1, 6, 9], k = 24
Output : [(12, 6, 6), (12, 9, 3)]
18
Input : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 10
Output : [(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]

Input : [12, 3, 6, 1, 6, 9], k = 24
Output : [(12, 6, 6), (12, 9, 3)]
19
Input : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 10
Output : [(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]

Input : [12, 3, 6, 1, 6, 9], k = 24
Output : [(12, 6, 6), (12, 9, 3)]
20

[['Geeks', 'for', 'Geeks'], ['for', 'Geeks', 'is'],
 ['Geeks', 'is', 'best'], ['is', 'best', 'resource'],
 ['best', 'resource', 'for'], ['resource', 'for', 'study']]
03
Input : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 10
Output : [(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]

Input : [12, 3, 6, 1, 6, 9], k = 24
Output : [(12, 6, 6), (12, 9, 3)]
22

'I'3

[['Geeks', 'for', 'Geeks'], ['for', 'Geeks', 'is'],
 ['Geeks', 'is', 'best'], ['is', 'best', 'resource'],
 ['best', 'resource', 'for'], ['resource', 'for', 'study']]
03
Input : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 10
Output : [(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]

Input : [12, 3, 6, 1, 6, 9], k = 24
Output : [(12, 6, 6), (12, 9, 3)]
25

[(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]
1
[['Geeks', 'for', 'Geeks'], ['for', 'Geeks', 'is'],
 ['Geeks', 'is', 'best'], ['is', 'best', 'resource'],
 ['best', 'resource', 'for'], ['resource', 'for', 'study']]
87
Input : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 10
Output : [(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]

Input : [12, 3, 6, 1, 6, 9], k = 24
Output : [(12, 6, 6), (12, 9, 3)]
28
Input : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 10
Output : [(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]

Input : [12, 3, 6, 1, 6, 9], k = 24
Output : [(12, 6, 6), (12, 9, 3)]
29=____6
Input : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 10
Output : [(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]

Input : [12, 3, 6, 1, 6, 9], k = 24
Output : [(12, 6, 6), (12, 9, 3)]
32

'I'3

[['Geeks', 'for', 'Geeks'], ['for', 'Geeks', 'is'],
 ['Geeks', 'is', 'best'], ['is', 'best', 'resource'],
 ['best', 'resource', 'for'], ['resource', 'for', 'study']]
87
Input : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 10
Output : [(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]

Input : [12, 3, 6, 1, 6, 9], k = 24
Output : [(12, 6, 6), (12, 9, 3)]
35
[(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]
6
Input : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 10
Output : [(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]

Input : [12, 3, 6, 1, 6, 9], k = 24
Output : [(12, 6, 6), (12, 9, 3)]
37
Input : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 10
Output : [(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]

Input : [12, 3, 6, 1, 6, 9], k = 24
Output : [(12, 6, 6), (12, 9, 3)]
38
Input : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 10
Output : [(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]

Input : [12, 3, 6, 1, 6, 9], k = 24
Output : [(12, 6, 6), (12, 9, 3)]
35
Input : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 10
Output : [(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]

Input : [12, 3, 6, 1, 6, 9], k = 24
Output : [(12, 6, 6), (12, 9, 3)]
40
Input : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 10
Output : [(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]

Input : [12, 3, 6, 1, 6, 9], k = 24
Output : [(12, 6, 6), (12, 9, 3)]
9
Input : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 10
Output : [(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]

Input : [12, 3, 6, 1, 6, 9], k = 24
Output : [(12, 6, 6), (12, 9, 3)]
42

[['Geeks', 'for', 'Geeks'], ['for', 'Geeks', 'is'],
 ['Geeks', 'is', 'best'], ['is', 'best', 'resource'],
 ['best', 'resource', 'for'], ['resource', 'for', 'study']]
89= [__992

[(1, 2, 7), (1, 3, 6), (1, 4, 5), (2, 3, 5)]
2
Input : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 10
Output : [(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]

Input : [12, 3, 6, 1, 6, 9], k = 24
Output : [(12, 6, 6), (12, 9, 3)]
67
Input : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 10
Output : [(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]

Input : [12, 3, 6, 1, 6, 9], k = 24
Output : [(12, 6, 6), (12, 9, 3)]
10
Input : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 10
Output : [(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]

Input : [12, 3, 6, 1, 6, 9], k = 24
Output : [(12, 6, 6), (12, 9, 3)]
69

Output:

[['Geeks', 'for', 'Geeks'], ['for', 'Geeks', 'is'],
 ['Geeks', 'is', 'best'], ['is', 'best', 'resource'],
 ['best', 'resource', 'for'], ['resource', 'for', 'study']]
40=
Input : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 10
Output : [(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]

Input : [12, 3, 6, 1, 6, 9], k = 24
Output : [(12, 6, 6), (12, 9, 3)]
10

How do you make a triplet in Python?

& NBSP; Cách tiếp cận #2: Sử dụng mô -đun ItertoolSpython Itertools cung cấp chức năng kết hợp (ITEBLE, R). Công cụ này trả về các chuỗi độ dài r của các phần tử từ đầu vào có thể điều chỉnh được. Mỗi lần chúng tôi thực hiện kết hợp 3 yếu tố và kiểm tra xem chúng có tổng hợp lên K hay không..

Input : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 10
Output : [(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]

Input : [12, 3, 6, 1, 6, 9], k = 24
Output : [(12, 6, 6), (12, 9, 3)]
17
Input : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 10
Output : [(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]

Input : [12, 3, 6, 1, 6, 9], k = 24
Output : [(12, 6, 6), (12, 9, 3)]
18
Input : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 10
Output : [(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]

Input : [12, 3, 6, 1, 6, 9], k = 24
Output : [(12, 6, 6), (12, 9, 3)]
19
Input : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 10
Output : [(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]

Input : [12, 3, 6, 1, 6, 9], k = 24
Output : [(12, 6, 6), (12, 9, 3)]
20

[['Geeks', 'for', 'Geeks'], ['for', 'Geeks', 'is'],
 ['Geeks', 'is', 'best'], ['is', 'best', 'resource'],
 ['best', 'resource', 'for'], ['resource', 'for', 'study']]
03
Input : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 10
Output : [(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]

Input : [12, 3, 6, 1, 6, 9], k = 24
Output : [(12, 6, 6), (12, 9, 3)]
22

'I'3

[['Geeks', 'for', 'Geeks'], ['for', 'Geeks', 'is'],
 ['Geeks', 'is', 'best'], ['is', 'best', 'resource'],
 ['best', 'resource', 'for'], ['resource', 'for', 'study']]
03
Input : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 10
Output : [(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]

Input : [12, 3, 6, 1, 6, 9], k = 24
Output : [(12, 6, 6), (12, 9, 3)]
25

Làm thế nào để bạn nhận được bộ ba từ một danh sách trong Python?

[(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]
1
[['Geeks', 'for', 'Geeks'], ['for', 'Geeks', 'is'],
 ['Geeks', 'is', 'best'], ['is', 'best', 'resource'],
 ['best', 'resource', 'for'], ['resource', 'for', 'study']]
87
Input : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 10
Output : [(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]

Input : [12, 3, 6, 1, 6, 9], k = 24
Output : [(12, 6, 6), (12, 9, 3)]
28
Input : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 10
Output : [(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]

Input : [12, 3, 6, 1, 6, 9], k = 24
Output : [(12, 6, 6), (12, 9, 3)]
29=____6
Input : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 10
Output : [(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]

Input : [12, 3, 6, 1, 6, 9], k = 24
Output : [(12, 6, 6), (12, 9, 3)]
32

'I'3

[['Geeks', 'for', 'Geeks'], ['for', 'Geeks', 'is'],
 ['Geeks', 'is', 'best'], ['is', 'best', 'resource'],
 ['best', 'resource', 'for'], ['resource', 'for', 'study']]
87
Input : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 10
Output : [(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]

Input : [12, 3, 6, 1, 6, 9], k = 24
Output : [(12, 6, 6), (12, 9, 3)]
35
[(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]
6
Input : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 10
Output : [(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]

Input : [12, 3, 6, 1, 6, 9], k = 24
Output : [(12, 6, 6), (12, 9, 3)]
37
Input : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 10
Output : [(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]

Input : [12, 3, 6, 1, 6, 9], k = 24
Output : [(12, 6, 6), (12, 9, 3)]
38
Input : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 10
Output : [(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]

Input : [12, 3, 6, 1, 6, 9], k = 24
Output : [(12, 6, 6), (12, 9, 3)]
35
Input : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 10
Output : [(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]

Input : [12, 3, 6, 1, 6, 9], k = 24
Output : [(12, 6, 6), (12, 9, 3)]
40
Input : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 10
Output : [(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]

Input : [12, 3, 6, 1, 6, 9], k = 24
Output : [(12, 6, 6), (12, 9, 3)]
9
Input : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 10
Output : [(1, 5, 4), (1, 6, 3), (1, 7, 2), (2, 5, 3)]

Input : [12, 3, 6, 1, 6, 9], k = 24
Output : [(12, 6, 6), (12, 9, 3)]
42

trả lại res ..

Làm thế nào để bạn tạo ra một mảng triplet?

Bộ ba có thể được tìm thấy bằng cách sử dụng kỹ thuật băm ...

Đi qua mảng từ i = 0 đến n - 2 ..

Tạo một bảng băm trống ..

Traverse từ J = I+ 1 đến N -1 ..

sum = mảng [i] + mảng [j].

if (-sum) có mặt trong bảng băm,.

sau đó in mảng [i], mảng [j] và -sum dưới dạng ba lần ..

Khác, chèn ARR [J] trong bảng băm và tiến hành ..

Bộ ba trong mã hóa là gì?

Một mã bộ ba là nơi mỗi codon (trong mã), bao gồm ba, không chồng chéo, nuceoltides.Mã này bị thoái hóa, vì các cặp bazơ ba khác nhau có thể mã cho cùng một axit amin.Ví dụ, cả AAA và AAG đều mã cho lysine.where each codon (within the code), consists of three, nonoverlapping, nuceoltides. The code is degenerate, as different triplet base pairs can code for the same amino acid. For example, AAA and AAG both code for lysine.