Hướng dẫn three words combination in python assignment expert - kết hợp ba từ trong chuyên gia chuyển nhượng python

Cho một câu dưới dạng đầu vào, in tất cả các kết hợp duy nhất của ba từ theo thứ tự từ vựng.

Show

Đầu vào sẽ là một dòng duy nhất chứa một câu.

Đầu ra phải là nhiều dòng, mỗi dòng chứa sự kết hợp duy nhất của ba từ theo thứ tự từ vựng.

Ví dụ: nếu câu đã cho là "táo là một loại trái cây", thì sự kết hợp duy nhất có thể có của ba là (a, táo, trái cây), (a, táo, is), (a, trái cây, là), (táo, trái cây , Là). Vì vậy, đầu ra phải

a apple fruit
a apple is
a fruit is
apple fruit is

Đầu vào mẫu 1

táo là trái cây

Đầu ra mẫu 1

một quả táo

Một quả táo là

Một trái cây là

quả táo là

Đầu vào mẫu 2

Raju chơi cricket

Đầu ra mẫu 2

Cricket chơi Raju

Trong hướng dẫn này, bạn đã học được cách:

Nhanh chóng xây dựng chuỗi đảo ngược thông qua việc cắt

Tạo các bản sao đảo ngược của các chuỗi hiện có bằng cách sử dụng

Test case 1

Input:-
raju always plays cricket

Output:-
Expected output is
always cricket
cricket raju
plays raju

but coming output is
be be
not be
or be
or to

Test case 2

Input:-
python is a programming language

Output:-
Expected output is
a language
a python
is language
is programming
language python
programming python

but coming output is
be be
not be
or be
or to

Test case 3

Input:-
to be or not to be

Output:-
Expected output is
be be
be not
or to
to to

but coming output is
be be
not be
or be
or to
2 và
Test case 1

Input:-
raju always plays cricket

Output:-
Expected output is
always cricket
cricket raju
plays raju

but coming output is
be be
not be
or be
or to

Test case 2

Input:-
python is a programming language

Output:-
Expected output is
a language
a python
is language
is programming
language python
programming python

but coming output is
be be
not be
or be
or to

Test case 3

Input:-
to be or not to be

Output:-
Expected output is
be be
be not
or to
to to

but coming output is
be be
not be
or be
or to
3

Sử dụng lặp và đệ quy để tạo chuỗi đảo ngược bằng tay

Vòng lặp qua các chuỗi của bạn theo thứ tự ngược lại

Sắp xếp các chuỗi của bạn theo thứ tự giảm dần bằng cách sử dụng

Test case 1

Input:-
raju always plays cricket

Output:-
Expected output is
always cricket
cricket raju
plays raju

but coming output is
be be
not be
or be
or to

Test case 2

Input:-
python is a programming language

Output:-
Expected output is
a language
a python
is language
is programming
language python
programming python

but coming output is
be be
not be
or be
or to

Test case 3

Input:-
to be or not to be

Output:-
Expected output is
be be
be not
or to
to to

but coming output is
be be
not be
or be
or to
4

Mặc dù chủ đề này có thể không có nhiều trường hợp sử dụng thú vị, hiểu cách đảo ngược các chuỗi có thể hữu ích trong các cuộc phỏng vấn mã hóa cho các vị trí cấp nhập cảnh. Bạn cũng sẽ thấy rằng việc làm chủ các cách khác nhau để đảo ngược một chuỗi có thể giúp bạn thực sự khái niệm hóa tính bất biến của các chuỗi trong Python, đây là một tính năng đáng chú ý của ngôn ngữ.

Sự kết hợp không liên tục của hai từ

Đưa ra một câu làm đầu vào, hãy tìm tất cả các kết hợp độc đáo của hai từ và in các kết hợp từ không liền kề trong câu gốc theo thứ tự từ vựng.

Đầu vào

Đầu vào sẽ là một dòng duy nhất chứa một câu.

Đầu ra

Đầu ra phải là nhiều dòng, mỗi dòng chứa một kết hợp duy nhất hợp lệ của hai từ. Các từ trong mỗi dòng phải là thứ tự từ vựng và các dòng cũng nên theo thứ tự từ vựng. Một kết hợp hợp lệ sẽ không chứa các từ xuất hiện liền kề trong câu đã cho. In "Không kết hợp" nếu không có kết hợp hợp lệ.

Đầu vào mẫu 1

Raju luôn chơi cricket

Đầu ra mẫu 1

Luôn luôn cricket

Cricket Raju

Chơi Raju

Đầu vào mẫu 2

Python là ngôn ngữ lập trình

Đầu ra mẫu 2

một ngôn ngữ

Một con trăn

là ngôn ngữ

là lập trình

Ngôn ngữ Python

Lập trình Python

Trong hướng dẫn này, bạn đã học được cách:

Nhanh chóng xây dựng chuỗi đảo ngược thông qua việc cắt

  • Tạo các bản sao đảo ngược của các chuỗi hiện có bằng cách sử dụng
    Test case 1
    
    Input:-
    raju always plays cricket
    
    Output:-
    Expected output is
    always cricket
    cricket raju
    plays raju
    
    but coming output is
    be be
    not be
    or be
    or to
    
    Test case 2
    
    Input:-
    python is a programming language
    
    Output:-
    Expected output is
    a language
    a python
    is language
    is programming
    language python
    programming python
    
    but coming output is
    be be
    not be
    or be
    or to
    
    Test case 3
    
    Input:-
    to be or not to be
    
    Output:-
    Expected output is
    be be
    be not
    or to
    to to
    
    but coming output is
    be be
    not be
    or be
    or to
    
    2 và
    Test case 1
    
    Input:-
    raju always plays cricket
    
    Output:-
    Expected output is
    always cricket
    cricket raju
    plays raju
    
    but coming output is
    be be
    not be
    or be
    or to
    
    Test case 2
    
    Input:-
    python is a programming language
    
    Output:-
    Expected output is
    a language
    a python
    is language
    is programming
    language python
    programming python
    
    but coming output is
    be be
    not be
    or be
    or to
    
    Test case 3
    
    Input:-
    to be or not to be
    
    Output:-
    Expected output is
    be be
    be not
    or to
    to to
    
    but coming output is
    be be
    not be
    or be
    or to
    
    3
  • Sử dụng lặp và đệ quy để tạo chuỗi đảo ngược bằng tay
  • Vòng lặp qua các chuỗi của bạn theo thứ tự ngược lại
  • Sắp xếp các chuỗi của bạn theo thứ tự giảm dần bằng cách sử dụng
    Test case 1
    
    Input:-
    raju always plays cricket
    
    Output:-
    Expected output is
    always cricket
    cricket raju
    plays raju
    
    but coming output is
    be be
    not be
    or be
    or to
    
    Test case 2
    
    Input:-
    python is a programming language
    
    Output:-
    Expected output is
    a language
    a python
    is language
    is programming
    language python
    programming python
    
    but coming output is
    be be
    not be
    or be
    or to
    
    Test case 3
    
    Input:-
    to be or not to be
    
    Output:-
    Expected output is
    be be
    be not
    or to
    to to
    
    but coming output is
    be be
    not be
    or be
    or to
    
    4
  • Mặc dù chủ đề này có thể không có nhiều trường hợp sử dụng thú vị, hiểu cách đảo ngược các chuỗi có thể hữu ích trong các cuộc phỏng vấn mã hóa cho các vị trí cấp nhập cảnh. Bạn cũng sẽ thấy rằng việc làm chủ các cách khác nhau để đảo ngược một chuỗi có thể giúp bạn thực sự khái niệm hóa tính bất biến của các chuỗi trong Python, đây là một tính năng đáng chú ý của ngôn ngữ.
  • Sự kết hợp không liên tục của hai từ
  • Nội phân chính
  • Giải trình
  • Giải pháp Classis: Lặp lại ngược
  • Đảo ngược chuỗi thông qua việc cắt
  • Đảo ngược các chuỗi với .Join () và đảo ngược ()
  • Tạo dây đảo ngược bằng tay
  • sentence = "to be or not to be"
    s = sentence.split()
    s.sort()
    result = []
    for word1 in s:
        for word2 in s:
            result.append(word1 + " " + word2)
           
    result = set(result)
    result = list(result)
    result.sort()
    
    
    for item in result:
        if item in sentence:
            result.remove(item)
            
    for item in result: 
        if " ".join(item.split()[::-1]) in sentence:
            result.remove(item)
            
    for item in result:
        spl = item.split()
        if (spl[0] == spl[1]) and (sentence.count(spl[0]) < 2):
            result.remove(item)
            
    for item in result:
        spl = item.split()
        dup = spl[1] + " " + spl[0]
        if dup in result:
            result.remove(dup)
    result.insert(0, "be be")   
    for item in result:
        print(item)
    
    36 cung cấp chức năng tương tự như siêu lớp
    sentence = "to be or not to be"
    s = sentence.split()
    s.sort()
    result = []
    for word1 in s:
        for word2 in s:
            result.append(word1 + " " + word2)
           
    result = set(result)
    result = list(result)
    result.sort()
    
    
    for item in result:
        if item in sentence:
            result.remove(item)
            
    for item in result: 
        if " ".join(item.split()[::-1]) in sentence:
            result.remove(item)
            
    for item in result:
        spl = item.split()
        if (spl[0] == spl[1]) and (sentence.count(spl[0]) < 2):
            result.remove(item)
            
    for item in result:
        spl = item.split()
        dup = spl[1] + " " + spl[0]
        if dup in result:
            result.remove(dup)
    result.insert(0, "be be")   
    for item in result:
        print(item)
    
    39, bạn có thể sử dụng
    Test case 1
    
    Input:-
    raju always plays cricket
    
    Output:-
    Expected output is
    always cricket
    cricket raju
    plays raju
    
    but coming output is
    be be
    not be
    or be
    or to
    
    Test case 2
    
    Input:-
    python is a programming language
    
    Output:-
    Expected output is
    a language
    a python
    is language
    is programming
    language python
    programming python
    
    but coming output is
    be be
    not be
    or be
    or to
    
    Test case 3
    
    Input:-
    to be or not to be
    
    Output:-
    Expected output is
    be be
    be not
    or to
    to to
    
    but coming output is
    be be
    not be
    or be
    or to
    
    2 ra khỏi hộp để thực hiện lặp lại lặp lại:

Tại đây, bạn gọi

Test case 1

Input:-
raju always plays cricket

Output:-
Expected output is
always cricket
cricket raju
plays raju

but coming output is
be be
not be
or be
or to

Test case 2

Input:-
python is a programming language

Output:-
Expected output is
a language
a python
is language
is programming
language python
programming python

but coming output is
be be
not be
or be
or to

Test case 3

Input:-
to be or not to be

Output:-
Expected output is
be be
be not
or to
to to

but coming output is
be be
not be
or be
or to
2 với
>>> "".join(reversed("Hello, World!"))
'!dlroW ,olleH'
5 như một đối số để cung cấp vòng lặp
Test case 1

Input:-
raju always plays cricket

Output:-
Expected output is
always cricket
cricket raju
plays raju

but coming output is
be be
not be
or be
or to

Test case 2

Input:-
python is a programming language

Output:-
Expected output is
a language
a python
is language
is programming
language python
programming python

but coming output is
be be
not be
or be
or to

Test case 3

Input:-
to be or not to be

Output:-
Expected output is
be be
be not
or to
to to

but coming output is
be be
not be
or be
or to
5. Cuộc gọi này hoạt động như mong đợi và trả về trình lặp tương ứng vì
sentence = "to be or not to be"
s = sentence.split()
s.sort()
result = []
for word1 in s:
    for word2 in s:
        result.append(word1 + " " + word2)
       
result = set(result)
result = list(result)
result.sort()


for item in result:
    if item in sentence:
        result.remove(item)
        
for item in result: 
    if " ".join(item.split()[::-1]) in sentence:
        result.remove(item)
        
for item in result:
    spl = item.split()
    if (spl[0] == spl[1]) and (sentence.count(spl[0]) < 2):
        result.remove(item)
        
for item in result:
    spl = item.split()
    dup = spl[1] + " " + spl[0]
    if dup in result:
        result.remove(dup)
result.insert(0, "be be")   
for item in result:
    print(item)
36 kế thừa hành vi cần thiết từ
sentence = "to be or not to be"
s = sentence.split()
s.sort()
result = []
for word1 in s:
    for word2 in s:
        result.append(word1 + " " + word2)
       
result = set(result)
result = list(result)
result.sort()


for item in result:
    if item in sentence:
        result.remove(item)
        
for item in result: 
    if " ".join(item.split()[::-1]) in sentence:
        result.remove(item)
        
for item in result:
    spl = item.split()
    if (spl[0] == spl[1]) and (sentence.count(spl[0]) < 2):
        result.remove(item)
        
for item in result:
    spl = item.split()
    dup = spl[1] + " " + spl[0]
    if dup in result:
        result.remove(dup)
result.insert(0, "be be")   
for item in result:
    print(item)
39. Lưu ý rằng việc gọi
Test case 1

Input:-
raju always plays cricket

Output:-
Expected output is
always cricket
cricket raju
plays raju

but coming output is
be be
not be
or be
or to

Test case 2

Input:-
python is a programming language

Output:-
Expected output is
a language
a python
is language
is programming
language python
programming python

but coming output is
be be
not be
or be
or to

Test case 3

Input:-
to be or not to be

Output:-
Expected output is
be be
be not
or to
to to

but coming output is
be be
not be
or be
or to
2 không ảnh hưởng đến chuỗi ban đầu.

Sắp xếp các chuỗi python theo thứ tự ngược lại

Chủ đề cuối cùng mà bạn sẽ tìm hiểu là làm thế nào để sắp xếp các ký tự của một chuỗi theo thứ tự ngược lại. Điều này có thể tiện dụng khi bạn làm việc với các chuỗi không theo thứ tự cụ thể và bạn cần phải sắp xếp chúng theo thứ tự bảng chữ cái ngược.

Để tiếp cận vấn đề này, bạn có thể sử dụng

Test case 1

Input:-
raju always plays cricket

Output:-
Expected output is
always cricket
cricket raju
plays raju

but coming output is
be be
not be
or be
or to

Test case 2

Input:-
python is a programming language

Output:-
Expected output is
a language
a python
is language
is programming
language python
programming python

but coming output is
be be
not be
or be
or to

Test case 3

Input:-
to be or not to be

Output:-
Expected output is
be be
be not
or to
to to

but coming output is
be be
not be
or be
or to
4. Hàm tích hợp này trả về một danh sách chứa tất cả các mục của đầu vào có thể điều chỉnh theo thứ tự. Bên cạnh các đầu vào có thể lặp lại,
Test case 1

Input:-
raju always plays cricket

Output:-
Expected output is
always cricket
cricket raju
plays raju

but coming output is
be be
not be
or be
or to

Test case 2

Input:-
python is a programming language

Output:-
Expected output is
a language
a python
is language
is programming
language python
programming python

but coming output is
be be
not be
or be
or to

Test case 3

Input:-
to be or not to be

Output:-
Expected output is
be be
be not
or to
to to

but coming output is
be be
not be
or be
or to
4 cũng chấp nhận đối số từ khóa
sentence = "to be or not to be"
s = sentence.split()
s.sort()
result = []
for word1 in s:
    for word2 in s:
        result.append(word1 + " " + word2)
       
result = set(result)
result = list(result)
result.sort()


for item in result:
    if item in sentence:
        result.remove(item)
        
for item in result: 
    if " ".join(item.split()[::-1]) in sentence:
        result.remove(item)
        
for item in result:
    spl = item.split()
    if (spl[0] == spl[1]) and (sentence.count(spl[0]) < 2):
        result.remove(item)
        
for item in result:
    spl = item.split()
    dup = spl[1] + " " + spl[0]
    if dup in result:
        result.remove(dup)
result.insert(0, "be be")   
for item in result:
    print(item)
64. Bạn có thể đặt đối số này thành
sentence = "to be or not to be"
s = sentence.split()
s.sort()
result = []
for word1 in s:
    for word2 in s:
        result.append(word1 + " " + word2)
       
result = set(result)
result = list(result)
result.sort()


for item in result:
    if item in sentence:
        result.remove(item)
        
for item in result: 
    if " ".join(item.split()[::-1]) in sentence:
        result.remove(item)
        
for item in result:
    spl = item.split()
    if (spl[0] == spl[1]) and (sentence.count(spl[0]) < 2):
        result.remove(item)
        
for item in result:
    spl = item.split()
    dup = spl[1] + " " + spl[0]
    if dup in result:
        result.remove(dup)
result.insert(0, "be be")   
for item in result:
    print(item)
65 nếu bạn muốn đầu vào có thể sắp xếp theo thứ tự giảm dần:

Khi bạn gọi

Test case 1

Input:-
raju always plays cricket

Output:-
Expected output is
always cricket
cricket raju
plays raju

but coming output is
be be
not be
or be
or to

Test case 2

Input:-
python is a programming language

Output:-
Expected output is
a language
a python
is language
is programming
language python
programming python

but coming output is
be be
not be
or be
or to

Test case 3

Input:-
to be or not to be

Output:-
Expected output is
be be
be not
or to
to to

but coming output is
be be
not be
or be
or to
4 với một chuỗi là đối số và
sentence = "to be or not to be"
s = sentence.split()
s.sort()
result = []
for word1 in s:
    for word2 in s:
        result.append(word1 + " " + word2)
       
result = set(result)
result = list(result)
result.sort()


for item in result:
    if item in sentence:
        result.remove(item)
        
for item in result: 
    if " ".join(item.split()[::-1]) in sentence:
        result.remove(item)
        
for item in result:
    spl = item.split()
    if (spl[0] == spl[1]) and (sentence.count(spl[0]) < 2):
        result.remove(item)
        
for item in result:
    spl = item.split()
    dup = spl[1] + " " + spl[0]
    if dup in result:
        result.remove(dup)
result.insert(0, "be be")   
for item in result:
    print(item)
64 được đặt thành
sentence = "to be or not to be"
s = sentence.split()
s.sort()
result = []
for word1 in s:
    for word2 in s:
        result.append(word1 + " " + word2)
       
result = set(result)
result = list(result)
result.sort()


for item in result:
    if item in sentence:
        result.remove(item)
        
for item in result: 
    if " ".join(item.split()[::-1]) in sentence:
        result.remove(item)
        
for item in result:
    spl = item.split()
    if (spl[0] == spl[1]) and (sentence.count(spl[0]) < 2):
        result.remove(item)
        
for item in result:
    spl = item.split()
    dup = spl[1] + " " + spl[0]
    if dup in result:
        result.remove(dup)
result.insert(0, "be be")   
for item in result:
    print(item)
65, bạn sẽ nhận được một danh sách chứa các ký tự của chuỗi đầu vào theo thứ tự ngược hoặc giảm dần. Vì
Test case 1

Input:-
raju always plays cricket

Output:-
Expected output is
always cricket
cricket raju
plays raju

but coming output is
be be
not be
or be
or to

Test case 2

Input:-
python is a programming language

Output:-
Expected output is
a language
a python
is language
is programming
language python
programming python

but coming output is
be be
not be
or be
or to

Test case 3

Input:-
to be or not to be

Output:-
Expected output is
be be
be not
or to
to to

but coming output is
be be
not be
or be
or to
4 trả về một đối tượng
sentence = "to be or not to be"
s = sentence.split()
s.sort()
result = []
for word1 in s:
    for word2 in s:
        result.append(word1 + " " + word2)
       
result = set(result)
result = list(result)
result.sort()


for item in result:
    if item in sentence:
        result.remove(item)
        
for item in result: 
    if " ".join(item.split()[::-1]) in sentence:
        result.remove(item)
        
for item in result:
    spl = item.split()
    if (spl[0] == spl[1]) and (sentence.count(spl[0]) < 2):
        result.remove(item)
        
for item in result:
    spl = item.split()
    dup = spl[1] + " " + spl[0]
    if dup in result:
        result.remove(dup)
result.insert(0, "be be")   
for item in result:
    print(item)
70, bạn cần một cách để biến danh sách đó trở lại thành một chuỗi. Một lần nữa, bạn có thể sử dụng
Test case 1

Input:-
raju always plays cricket

Output:-
Expected output is
always cricket
cricket raju
plays raju

but coming output is
be be
not be
or be
or to

Test case 2

Input:-
python is a programming language

Output:-
Expected output is
a language
a python
is language
is programming
language python
programming python

but coming output is
be be
not be
or be
or to

Test case 3

Input:-
to be or not to be

Output:-
Expected output is
be be
be not
or to
to to

but coming output is
be be
not be
or be
or to
3 giống như bạn đã làm trong các phần trước:

Trong đoạn mã này, bạn gọi

Test case 1

Input:-
raju always plays cricket

Output:-
Expected output is
always cricket
cricket raju
plays raju

but coming output is
be be
not be
or be
or to

Test case 2

Input:-
python is a programming language

Output:-
Expected output is
a language
a python
is language
is programming
language python
programming python

but coming output is
be be
not be
or be
or to

Test case 3

Input:-
to be or not to be

Output:-
Expected output is
be be
be not
or to
to to

but coming output is
be be
not be
or be
or to
3 trên một chuỗi trống, đóng vai trò của một dấu phân cách. Đối số của
Test case 1

Input:-
raju always plays cricket

Output:-
Expected output is
always cricket
cricket raju
plays raju

but coming output is
be be
not be
or be
or to

Test case 2

Input:-
python is a programming language

Output:-
Expected output is
a language
a python
is language
is programming
language python
programming python

but coming output is
be be
not be
or be
or to

Test case 3

Input:-
to be or not to be

Output:-
Expected output is
be be
be not
or to
to to

but coming output is
be be
not be
or be
or to
3 là kết quả của việc gọi
Test case 1

Input:-
raju always plays cricket

Output:-
Expected output is
always cricket
cricket raju
plays raju

but coming output is
be be
not be
or be
or to

Test case 2

Input:-
python is a programming language

Output:-
Expected output is
a language
a python
is language
is programming
language python
programming python

but coming output is
be be
not be
or be
or to

Test case 3

Input:-
to be or not to be

Output:-
Expected output is
be be
be not
or to
to to

but coming output is
be be
not be
or be
or to
4 với
sentence = "to be or not to be"
s = sentence.split()
s.sort()
result = []
for word1 in s:
    for word2 in s:
        result.append(word1 + " " + word2)
       
result = set(result)
result = list(result)
result.sort()


for item in result:
    if item in sentence:
        result.remove(item)
        
for item in result: 
    if " ".join(item.split()[::-1]) in sentence:
        result.remove(item)
        
for item in result:
    spl = item.split()
    if (spl[0] == spl[1]) and (sentence.count(spl[0]) < 2):
        result.remove(item)
        
for item in result:
    spl = item.split()
    dup = spl[1] + " " + spl[0]
    if dup in result:
        result.remove(dup)
result.insert(0, "be be")   
for item in result:
    print(item)
75 như một đối số và
sentence = "to be or not to be"
s = sentence.split()
s.sort()
result = []
for word1 in s:
    for word2 in s:
        result.append(word1 + " " + word2)
       
result = set(result)
result = list(result)
result.sort()


for item in result:
    if item in sentence:
        result.remove(item)
        
for item in result: 
    if " ".join(item.split()[::-1]) in sentence:
        result.remove(item)
        
for item in result:
    spl = item.split()
    if (spl[0] == spl[1]) and (sentence.count(spl[0]) < 2):
        result.remove(item)
        
for item in result:
    spl = item.split()
    dup = spl[1] + " " + spl[0]
    if dup in result:
        result.remove(dup)
result.insert(0, "be be")   
for item in result:
    print(item)
64 được đặt thành
sentence = "to be or not to be"
s = sentence.split()
s.sort()
result = []
for word1 in s:
    for word2 in s:
        result.append(word1 + " " + word2)
       
result = set(result)
result = list(result)
result.sort()


for item in result:
    if item in sentence:
        result.remove(item)
        
for item in result: 
    if " ".join(item.split()[::-1]) in sentence:
        result.remove(item)
        
for item in result:
    spl = item.split()
    if (spl[0] == spl[1]) and (sentence.count(spl[0]) < 2):
        result.remove(item)
        
for item in result:
    spl = item.split()
    dup = spl[1] + " " + spl[0]
    if dup in result:
        result.remove(dup)
result.insert(0, "be be")   
for item in result:
    print(item)
65.

Bạn cũng có thể tận dụng

Test case 1

Input:-
raju always plays cricket

Output:-
Expected output is
always cricket
cricket raju
plays raju

but coming output is
be be
not be
or be
or to

Test case 2

Input:-
python is a programming language

Output:-
Expected output is
a language
a python
is language
is programming
language python
programming python

but coming output is
be be
not be
or be
or to

Test case 3

Input:-
to be or not to be

Output:-
Expected output is
be be
be not
or to
to to

but coming output is
be be
not be
or be
or to
4 để lặp lại thông qua một chuỗi theo thứ tự được sắp xếp và đảo ngược:

Đối số

sentence = "to be or not to be"
s = sentence.split()
s.sort()
result = []
for word1 in s:
    for word2 in s:
        result.append(word1 + " " + word2)
       
result = set(result)
result = list(result)
result.sort()


for item in result:
    if item in sentence:
        result.remove(item)
        
for item in result: 
    if " ".join(item.split()[::-1]) in sentence:
        result.remove(item)
        
for item in result:
    spl = item.split()
    if (spl[0] == spl[1]) and (sentence.count(spl[0]) < 2):
        result.remove(item)
        
for item in result:
    spl = item.split()
    dup = spl[1] + " " + spl[0]
    if dup in result:
        result.remove(dup)
result.insert(0, "be be")   
for item in result:
    print(item)
64 cho
Test case 1

Input:-
raju always plays cricket

Output:-
Expected output is
always cricket
cricket raju
plays raju

but coming output is
be be
not be
or be
or to

Test case 2

Input:-
python is a programming language

Output:-
Expected output is
a language
a python
is language
is programming
language python
programming python

but coming output is
be be
not be
or be
or to

Test case 3

Input:-
to be or not to be

Output:-
Expected output is
be be
be not
or to
to to

but coming output is
be be
not be
or be
or to
4 cho phép bạn sắp xếp các phép lặp, bao gồm cả các chuỗi, theo thứ tự giảm dần. Vì vậy, nếu bạn cần một chuỗi các ký tự được sắp xếp theo thứ tự bảng chữ cái ngược, thì
Test case 1

Input:-
raju always plays cricket

Output:-
Expected output is
always cricket
cricket raju
plays raju

but coming output is
be be
not be
or be
or to

Test case 2

Input:-
python is a programming language

Output:-
Expected output is
a language
a python
is language
is programming
language python
programming python

but coming output is
be be
not be
or be
or to

Test case 3

Input:-
to be or not to be

Output:-
Expected output is
be be
be not
or to
to to

but coming output is
be be
not be
or be
or to
4 là dành cho bạn.

Sự kết luận

Đảo ngược và làm việc với các chuỗi theo thứ tự ngược lại có thể là một nhiệm vụ phổ biến trong lập trình. Python cung cấp một tập hợp các công cụ và kỹ thuật có thể giúp bạn thực hiện đảo ngược chuỗi một cách nhanh chóng và hiệu quả. Trong hướng dẫn này, bạn đã tìm hiểu về các công cụ và kỹ thuật đó và cách tận dụng chúng trong các thử thách xử lý chuỗi của bạn.

Trong hướng dẫn này, bạn đã học được cách:

Nhanh chóng xây dựng chuỗi đảo ngược thông qua việc cắt

Python là ngôn ngữ lập trình

Đầu ra mẫu 2

một ngôn ngữ

Một con trăn

là ngôn ngữ

là lập trình

Ngôn ngữ Python

Lập trình Python

Đầu vào mẫu 3

tồn tại hay không tồn tại

Đầu ra mẫu 3

được

không

hoặc để

đến để

Sự kết hợp không liên tục của hai từ

Đưa ra một câu làm đầu vào, hãy tìm tất cả các kết hợp độc đáo của hai từ và in các kết hợp từ không liền kề trong câu gốc theo thứ tự từ vựng.

Đầu vào sẽ là một dòng duy nhất chứa một câu.

Đầu ra phải là nhiều dòng, mỗi dòng chứa một kết hợp duy nhất hợp lệ của hai từ. Một kết hợp hợp lệ sẽ không chứa các từ xuất hiện liền kề trong câu đã cho. In "Không kết hợp" nếu không có kết hợp hợp lệ_explanation.

Đầu vào mẫu 1

Raju luôn chơi cricket

Đầu ra mẫu 1

Luôn luôn cricket

Cricket Raju

Chơi Raju

Đầu vào mẫu 2

Python là ngôn ngữ lập trình

Đầu ra mẫu 2

một ngôn ngữ

Một con trăn

là ngôn ngữ

là lập trình

Ngôn ngữ Python

Lập trình Python

Đầu vào mẫu 3

tồn tại hay không tồn tại

Đầu ra mẫu 3

được

không

hoặc để

đến để

Sự kết hợp không liên tục của hai từ

Đưa ra một câu làm đầu vào, hãy tìm tất cả các kết hợp độc đáo của hai từ và in các kết hợp từ không liền kề trong câu gốc theo thứ tự từ vựng.

Đầu vào sẽ là một dòng duy nhất chứa một câu.

sentence = "to be or not to be"
s = sentence.split()
s.sort()
result = []
for word1 in s:
    for word2 in s:
        result.append(word1 + " " + word2)
       
result = set(result)
result = list(result)
result.sort()


for item in result:
    if item in sentence:
        result.remove(item)
        
for item in result: 
    if " ".join(item.split()[::-1]) in sentence:
        result.remove(item)
        
for item in result:
    spl = item.split()
    if (spl[0] == spl[1]) and (sentence.count(spl[0]) < 2):
        result.remove(item)
        
for item in result:
    spl = item.split()
    dup = spl[1] + " " + spl[0]
    if dup in result:
        result.remove(dup)
result.insert(0, "be be")   
for item in result:
    print(item)

Đầu ra phải là nhiều dòng, mỗi dòng chứa một kết hợp duy nhất hợp lệ của hai từ. Một kết hợp hợp lệ sẽ không chứa các từ xuất hiện liền kề trong câu đã cho. In "Không kết hợp" nếu không có kết hợp hợp lệ_explanation.

Test case 1

Input:-
raju always plays cricket

Output:-
Expected output is
always cricket
cricket raju
plays raju

but coming output is
be be
not be
or be
or to

Test case 2

Input:-
python is a programming language

Output:-
Expected output is
a language
a python
is language
is programming
language python
programming python

but coming output is
be be
not be
or be
or to

Test case 3

Input:-
to be or not to be

Output:-
Expected output is
be be
be not
or to
to to

but coming output is
be be
not be
or be
or to

Đầu vào mẫu 1

Raju luôn chơi cricket

  • Đầu ra mẫu 1
  • Luôn luôn cricket
  • Cricket Raju
  • Chơi Raju
  • Đầu vào mẫu 2
  • Đầu ra phải được in chính xác như thể hiện trong các bài kiểm tra trên.
  • Trong mã Python này có ba trường hợp thử nghiệm. Ba trường hợp thử nghiệm không được dự kiến ​​đầu ra.
  • Ba trường hợp thử nghiệm dưới đây.
  • Trường hợp thử nghiệm
  • In theo thứ tự ngược - 2
  • Nội phân chính
  • Giải trình
  • Giải pháp Classis: Lặp lại ngược
  • Đảo ngược các chuỗi với các công cụ Python cốt lõi

Đảo ngược chuỗi thông qua việc cắt

Đảo ngược các chuỗi với .Join () và đảo ngược ()

Tạo dây đảo ngược bằng tay

Đảo ngược chuỗi trong một vòng lặp

Đầu vào mẫu 1

Raju luôn chơi cricket

Đầu ra mẫu 1

Luôn luôn cricket

Đầu ra mẫu 1

Luôn luôn cricket

Cricket Raju

Chơi Raju

Đầu vào mẫu 2

Đầu ra phải được in chính xác như thể hiện trong các bài kiểm tra trên.

Trong mã Python này có ba trường hợp thử nghiệm. Ba trường hợp thử nghiệm không được dự kiến ​​đầu ra.

Đầu vào mẫu 1

Trong mã Python này có ba trường hợp thử nghiệm. Ba trường hợp thử nghiệm không được dự kiến ​​đầu ra.

Đầu vào mẫu 2

Đầu ra mẫu 1

Đầu vào mẫu 2

Đầu ra phải được in chính xác như thể hiện trong các bài kiểm tra trên.

Trong mã Python này có ba trường hợp thử nghiệm. Ba trường hợp thử nghiệm không được dự kiến ​​đầu ra.

Đầu vào mẫu 2

Đầu ra phải được in chính xác như thể hiện trong các bài kiểm tra trên.

Trong mã Python này có ba trường hợp thử nghiệm. Ba trường hợp thử nghiệm không được dự kiến ​​đầu ra.

Đầu ra mẫu 2

Trong mã Python này có ba trường hợp thử nghiệm. Ba trường hợp thử nghiệm không được dự kiến ​​đầu ra.

Đầu ra phải được in chính xác như thể hiện trong các bài kiểm tra trên.

Đầu ra phải được in chính xác như thể hiện trong các bài kiểm tra trên.

Trong mã Python này có ba trường hợp thử nghiệm. Ba trường hợp thử nghiệm không được dự kiến ​​đầu ra.

Ba trường hợp thử nghiệm dưới đây.

Luôn luôn cricket

Cricket Rajulen() function) until the first character in the string (a variable goes from the length of the string until 0). In the loop, print each character after the other.

Chơi Raju

Đầu vào mẫu 2

Đầu ra phải được in chính xác như thể hiện trong các bài kiểm tra trên.

Trong mã Python này có ba trường hợp thử nghiệm. Ba trường hợp thử nghiệm không được dự kiến ​​đầu ra.

Ba trường hợp thử nghiệm dưới đây.reverse order. Python includes a few handy tools and techniques that can help you out in these situations. With them, you’ll be able to build reversed copies of existing strings quickly and efficiently.

Trường hợp thử nghiệm

In theo thứ tự ngược - 2

  • Nội phân chínhslicing
  • Giải trìnhreversed copies of existing strings using
    Test case 1
    
    Input:-
    raju always plays cricket
    
    Output:-
    Expected output is
    always cricket
    cricket raju
    plays raju
    
    but coming output is
    be be
    not be
    or be
    or to
    
    Test case 2
    
    Input:-
    python is a programming language
    
    Output:-
    Expected output is
    a language
    a python
    is language
    is programming
    language python
    programming python
    
    but coming output is
    be be
    not be
    or be
    or to
    
    Test case 3
    
    Input:-
    to be or not to be
    
    Output:-
    Expected output is
    be be
    be not
    or to
    to to
    
    but coming output is
    be be
    not be
    or be
    or to
    
    2 and
    Test case 1
    
    Input:-
    raju always plays cricket
    
    Output:-
    Expected output is
    always cricket
    cricket raju
    plays raju
    
    but coming output is
    be be
    not be
    or be
    or to
    
    Test case 2
    
    Input:-
    python is a programming language
    
    Output:-
    Expected output is
    a language
    a python
    is language
    is programming
    language python
    programming python
    
    but coming output is
    be be
    not be
    or be
    or to
    
    Test case 3
    
    Input:-
    to be or not to be
    
    Output:-
    Expected output is
    be be
    be not
    or to
    to to
    
    but coming output is
    be be
    not be
    or be
    or to
    
    3
  • Giải pháp Classis: Lặp lại ngượciteration and recursion to reverse existing strings manually
  • Đảo ngược các chuỗi với các công cụ Python cốt lõireverse iteration over your strings
  • Đảo ngược chuỗi thông qua việc cắt your strings in reverse order using
    Test case 1
    
    Input:-
    raju always plays cricket
    
    Output:-
    Expected output is
    always cricket
    cricket raju
    plays raju
    
    but coming output is
    be be
    not be
    or be
    or to
    
    Test case 2
    
    Input:-
    python is a programming language
    
    Output:-
    Expected output is
    a language
    a python
    is language
    is programming
    language python
    programming python
    
    but coming output is
    be be
    not be
    or be
    or to
    
    Test case 3
    
    Input:-
    to be or not to be
    
    Output:-
    Expected output is
    be be
    be not
    or to
    to to
    
    but coming output is
    be be
    not be
    or be
    or to
    
    4

Đảo ngược các chuỗi với .Join () và đảo ngược ()

Tạo dây đảo ngược bằng tayreverse order can be a requirement in some particular situations. For example, say you have a string

Test case 1

Input:-
raju always plays cricket

Output:-
Expected output is
always cricket
cricket raju
plays raju

but coming output is
be be
not be
or be
or to

Test case 2

Input:-
python is a programming language

Output:-
Expected output is
a language
a python
is language
is programming
language python
programming python

but coming output is
be be
not be
or be
or to

Test case 3

Input:-
to be or not to be

Output:-
Expected output is
be be
be not
or to
to to

but coming output is
be be
not be
or be
or to
7 and you want a fast way to reverse it to get
Test case 1

Input:-
raju always plays cricket

Output:-
Expected output is
always cricket
cricket raju
plays raju

but coming output is
be be
not be
or be
or to

Test case 2

Input:-
python is a programming language

Output:-
Expected output is
a language
a python
is language
is programming
language python
programming python

but coming output is
be be
not be
or be
or to

Test case 3

Input:-
to be or not to be

Output:-
Expected output is
be be
be not
or to
to to

but coming output is
be be
not be
or be
or to
8. What Python tools can you use to help?

Chuỗi là bất biến trong Python, vì vậy việc đảo ngược một chuỗi đã cho tại chỗ là không thể. Bạn cần phải tạo các bản sao đảo ngược của chuỗi mục tiêu của mình để đáp ứng yêu cầu.copies of your target strings to meet the requirement.

Python cung cấp hai cách đơn giản để đảo ngược chuỗi. Vì các chuỗi là các chuỗi, chúng có thể lập chỉ mục, có thể cắt được và có thể lặp lại. Các tính năng này cho phép bạn sử dụng cắt lát để tạo trực tiếp một bản sao của một chuỗi đã cho theo thứ tự ngược lại. Tùy chọn thứ hai là sử dụng chức năng tích hợp

Test case 1

Input:-
raju always plays cricket

Output:-
Expected output is
always cricket
cricket raju
plays raju

but coming output is
be be
not be
or be
or to

Test case 2

Input:-
python is a programming language

Output:-
Expected output is
a language
a python
is language
is programming
language python
programming python

but coming output is
be be
not be
or be
or to

Test case 3

Input:-
to be or not to be

Output:-
Expected output is
be be
be not
or to
to to

but coming output is
be be
not be
or be
or to
2 để tạo trình lặp mang lại các ký tự của chuỗi đầu vào theo thứ tự ngược lại.indexable, sliceable, and iterable. These features allow you to use slicing to directly generate a copy of a given string in reverse order. The second option is to use the built-in function
Test case 1

Input:-
raju always plays cricket

Output:-
Expected output is
always cricket
cricket raju
plays raju

but coming output is
be be
not be
or be
or to

Test case 2

Input:-
python is a programming language

Output:-
Expected output is
a language
a python
is language
is programming
language python
programming python

but coming output is
be be
not be
or be
or to

Test case 3

Input:-
to be or not to be

Output:-
Expected output is
be be
be not
or to
to to

but coming output is
be be
not be
or be
or to
2 to create an iterator that yields the characters of an input string in reverse order.

Đảo ngược chuỗi thông qua việc cắt

Cắt lát là một kỹ thuật hữu ích cho phép bạn trích xuất các mục từ một chuỗi nhất định bằng cách sử dụng các kết hợp khác nhau của các chỉ số số nguyên được gọi là độ lệch. Khi nói đến các chuỗi cắt, các độ lệch này xác định chỉ số của ký tự đầu tiên trong việc cắt, chỉ số của ký tự dừng cắt và giá trị xác định số lượng ký tự bạn muốn nhảy qua mỗi lần lặp.integer indices known as offsets. When it comes to slicing strings, these offsets define the index of the first character in the slicing, the index of the character that stops the slicing, and a value that defines how many characters you want to jump through in each iteration.

Để cắt một chuỗi, bạn có thể sử dụng cú pháp sau:

a_string[start:stop:step]

Sự bù đắp của bạn là

a_string[start:stop:step]
0,
a_string[start:stop:step]
1 và
a_string[start:stop:step]
2. Biểu thức này trích xuất tất cả các ký tự từ
a_string[start:stop:step]
0 đến
a_string[start:stop:step]
4 bằng
a_string[start:stop:step]
2. Bạn sẽ nhìn sâu hơn vào những gì tất cả những điều này có nghĩa chỉ trong một khoảnh khắc.

Tất cả các độ lệch là tùy chọn và chúng có các giá trị mặc định sau:

Bù lạiGiá trị mặc định
a_string[start:stop:step]
0
a_string[start:stop:step]
7
a_string[start:stop:step]
1
a_string[start:stop:step]
9
a_string[start:stop:step]
2
>>> letters = "AaBbCcDd"

>>> # Get all characters relying on default offsets
>>> letters[::]
'AaBbCcDd'
>>> letters[:]
'AaBbCcDd'

>>> # Get every other character from 0 to the end
>>> letters[::2]
'ABCD'

>>> # Get every other character from 1 to the end
>>> letters[1::2]
'abcd'
1

Ở đây,

a_string[start:stop:step]
0 đại diện cho chỉ số của ký tự đầu tiên trong lát cắt, trong khi
a_string[start:stop:step]
1 giữ chỉ số dừng hoạt động cắt. Phần bù thứ ba,
a_string[start:stop:step]
2, cho phép bạn quyết định có bao nhiêu ký tự mà việc cắt sẽ nhảy qua mỗi lần lặp.

Offset

a_string[start:stop:step]
2 cho phép bạn tinh chỉnh cách bạn trích xuất các ký tự mong muốn từ một chuỗi trong khi bỏ qua những người khác:

>>>

>>> letters = "AaBbCcDd"

>>> # Get all characters relying on default offsets
>>> letters[::]
'AaBbCcDd'
>>> letters[:]
'AaBbCcDd'

>>> # Get every other character from 0 to the end
>>> letters[::2]
'ABCD'

>>> # Get every other character from 1 to the end
>>> letters[1::2]
'abcd'

Tại đây, trước tiên bạn cắt lát

>>> letters = "AaBbCcDd"

>>> # Get all characters relying on default offsets
>>> letters[::]
'AaBbCcDd'
>>> letters[:]
'AaBbCcDd'

>>> # Get every other character from 0 to the end
>>> letters[::2]
'ABCD'

>>> # Get every other character from 1 to the end
>>> letters[1::2]
'abcd'
6 mà không cung cấp các giá trị bù rõ ràng để có được một bản sao đầy đủ của chuỗi gốc. Để kết thúc này, bạn cũng có thể sử dụng một sự cắt lát bỏ qua dấu hai chấm thứ hai (
>>> letters = "AaBbCcDd"

>>> # Get all characters relying on default offsets
>>> letters[::]
'AaBbCcDd'
>>> letters[:]
'AaBbCcDd'

>>> # Get every other character from 0 to the end
>>> letters[::2]
'ABCD'

>>> # Get every other character from 1 to the end
>>> letters[1::2]
'abcd'
7). Với
a_string[start:stop:step]
2 bằng
>>> letters = "AaBbCcDd"

>>> # Get all characters relying on default offsets
>>> letters[::]
'AaBbCcDd'
>>> letters[:]
'AaBbCcDd'

>>> # Get every other character from 0 to the end
>>> letters[::2]
'ABCD'

>>> # Get every other character from 1 to the end
>>> letters[1::2]
'abcd'
9, việc cắt lát có được mọi ký tự khác từ chuỗi đích. Bạn có thể chơi xung quanh với các độ lệch khác nhau để hiểu rõ hơn về cách cắt lát hoạt động.

Tại sao cắt và bù thứ ba này có liên quan đến việc đảo ngược các chuỗi trong Python? Câu trả lời nằm ở cách

a_string[start:stop:step]
2 hoạt động với các giá trị âm. Nếu bạn cung cấp một giá trị âm cho
a_string[start:stop:step]
2, thì việc cắt sẽ chạy ngược, có nghĩa là từ phải sang trái.

Ví dụ: nếu bạn đặt

a_string[start:stop:step]
2 bằng
>>> letters = "ABCDEF"

>>> letters[::-1]
'FEDCBA'

>>> letters
'ABCDEF'
3, thì bạn có thể xây dựng một lát cắt lấy tất cả các ký tự theo thứ tự ngược lại:

>>>

>>> letters = "ABCDEF"

>>> letters[::-1]
'FEDCBA'

>>> letters
'ABCDEF'

Tại đây, trước tiên bạn cắt lát

>>> letters = "AaBbCcDd"

>>> # Get all characters relying on default offsets
>>> letters[::]
'AaBbCcDd'
>>> letters[:]
'AaBbCcDd'

>>> # Get every other character from 0 to the end
>>> letters[::2]
'ABCD'

>>> # Get every other character from 1 to the end
>>> letters[1::2]
'abcd'
6 mà không cung cấp các giá trị bù rõ ràng để có được một bản sao đầy đủ của chuỗi gốc. Để kết thúc này, bạn cũng có thể sử dụng một sự cắt lát bỏ qua dấu hai chấm thứ hai (
>>> letters = "AaBbCcDd"

>>> # Get all characters relying on default offsets
>>> letters[::]
'AaBbCcDd'
>>> letters[:]
'AaBbCcDd'

>>> # Get every other character from 0 to the end
>>> letters[::2]
'ABCD'

>>> # Get every other character from 1 to the end
>>> letters[1::2]
'abcd'
7). Với
a_string[start:stop:step]
2 bằng
>>> letters = "AaBbCcDd"

>>> # Get all characters relying on default offsets
>>> letters[::]
'AaBbCcDd'
>>> letters[:]
'AaBbCcDd'

>>> # Get every other character from 0 to the end
>>> letters[::2]
'ABCD'

>>> # Get every other character from 1 to the end
>>> letters[1::2]
'abcd'
9, việc cắt lát có được mọi ký tự khác từ chuỗi đích. Bạn có thể chơi xung quanh với các độ lệch khác nhau để hiểu rõ hơn về cách cắt lát hoạt động.

Tại sao cắt và bù thứ ba này có liên quan đến việc đảo ngược các chuỗi trong Python? Câu trả lời nằm ở cách

a_string[start:stop:step]
2 hoạt động với các giá trị âm. Nếu bạn cung cấp một giá trị âm cho
a_string[start:stop:step]
2, thì việc cắt sẽ chạy ngược, có nghĩa là từ phải sang trái.

Ví dụ: nếu bạn đặt

a_string[start:stop:step]
2 bằng
>>> letters = "ABCDEF"

>>> letters[::-1]
'FEDCBA'

>>> letters
'ABCDEF'
3, thì bạn có thể xây dựng một lát cắt lấy tất cả các ký tự theo thứ tự ngược lại:

Việc cắt này trả về tất cả các ký tự từ đầu bên phải của chuỗi, trong đó chỉ mục bằng

>>> letters = "ABCDEF"

>>> letters[::-1]
'FEDCBA'

>>> letters
'ABCDEF'
4, trở lại đầu bên trái của chuỗi, trong đó chỉ mục là
a_string[start:stop:step]
7. Khi bạn sử dụng thủ thuật này, bạn sẽ nhận được một bản sao của chuỗi gốc theo thứ tự ngược lại mà không ảnh hưởng đến nội dung gốc của
>>> letters = "AaBbCcDd"

>>> # Get all characters relying on default offsets
>>> letters[::]
'AaBbCcDd'
>>> letters[:]
'AaBbCcDd'

>>> # Get every other character from 0 to the end
>>> letters[::2]
'ABCD'

>>> # Get every other character from 1 to the end
>>> letters[1::2]
'abcd'
6.

>>>

>>> letters = "ABCDEF"

>>> letters[slice(None, None, -1)]
'FEDCBA'

Tại đây, trước tiên bạn cắt lát

>>> letters = "AaBbCcDd"

>>> # Get all characters relying on default offsets
>>> letters[::]
'AaBbCcDd'
>>> letters[:]
'AaBbCcDd'

>>> # Get every other character from 0 to the end
>>> letters[::2]
'ABCD'

>>> # Get every other character from 1 to the end
>>> letters[1::2]
'abcd'
6 mà không cung cấp các giá trị bù rõ ràng để có được một bản sao đầy đủ của chuỗi gốc. Để kết thúc này, bạn cũng có thể sử dụng một sự cắt lát bỏ qua dấu hai chấm thứ hai (
>>> letters = "AaBbCcDd"

>>> # Get all characters relying on default offsets
>>> letters[::]
'AaBbCcDd'
>>> letters[:]
'AaBbCcDd'

>>> # Get every other character from 0 to the end
>>> letters[::2]
'ABCD'

>>> # Get every other character from 1 to the end
>>> letters[1::2]
'abcd'
7). Với
a_string[start:stop:step]
2 bằng
>>> letters = "AaBbCcDd"

>>> # Get all characters relying on default offsets
>>> letters[::]
'AaBbCcDd'
>>> letters[:]
'AaBbCcDd'

>>> # Get every other character from 0 to the end
>>> letters[::2]
'ABCD'

>>> # Get every other character from 1 to the end
>>> letters[1::2]
'abcd'
9, việc cắt lát có được mọi ký tự khác từ chuỗi đích. Bạn có thể chơi xung quanh với các độ lệch khác nhau để hiểu rõ hơn về cách cắt lát hoạt động.

Tại sao cắt và bù thứ ba này có liên quan đến việc đảo ngược các chuỗi trong Python? Câu trả lời nằm ở cách a_string[start:stop:step] 2 hoạt động với các giá trị âm. Nếu bạn cung cấp một giá trị âm cho a_string[start:stop:step] 2, thì việc cắt sẽ chạy ngược, có nghĩa là từ phải sang trái.

Ví dụ: nếu bạn đặt

a_string[start:stop:step]
2 bằng
>>> letters = "ABCDEF"

>>> letters[::-1]
'FEDCBA'

>>> letters
'ABCDEF'
3, thì bạn có thể xây dựng một lát cắt lấy tất cả các ký tự theo thứ tự ngược lại:

>>>

>>> greeting = reversed("Hello, World!")

>>> next(greeting)
'!'
>>> next(greeting)
'd'
>>> next(greeting)
'l'

Tại đây, trước tiên bạn cắt lát

>>> letters = "AaBbCcDd"

>>> # Get all characters relying on default offsets
>>> letters[::]
'AaBbCcDd'
>>> letters[:]
'AaBbCcDd'

>>> # Get every other character from 0 to the end
>>> letters[::2]
'ABCD'

>>> # Get every other character from 1 to the end
>>> letters[1::2]
'abcd'
6 mà không cung cấp các giá trị bù rõ ràng để có được một bản sao đầy đủ của chuỗi gốc. Để kết thúc này, bạn cũng có thể sử dụng một sự cắt lát bỏ qua dấu hai chấm thứ hai (
>>> letters = "AaBbCcDd"

>>> # Get all characters relying on default offsets
>>> letters[::]
'AaBbCcDd'
>>> letters[:]
'AaBbCcDd'

>>> # Get every other character from 0 to the end
>>> letters[::2]
'ABCD'

>>> # Get every other character from 1 to the end
>>> letters[1::2]
'abcd'
7). Với
a_string[start:stop:step]
2 bằng
>>> letters = "AaBbCcDd"

>>> # Get all characters relying on default offsets
>>> letters[::]
'AaBbCcDd'
>>> letters[:]
'AaBbCcDd'

>>> # Get every other character from 0 to the end
>>> letters[::2]
'ABCD'

>>> # Get every other character from 1 to the end
>>> letters[1::2]
'abcd'
9, việc cắt lát có được mọi ký tự khác từ chuỗi đích. Bạn có thể chơi xung quanh với các độ lệch khác nhau để hiểu rõ hơn về cách cắt lát hoạt động.

Tại sao cắt và bù thứ ba này có liên quan đến việc đảo ngược các chuỗi trong Python? Câu trả lời nằm ở cách

a_string[start:stop:step]
2 hoạt động với các giá trị âm. Nếu bạn cung cấp một giá trị âm cho
a_string[start:stop:step]
2, thì việc cắt sẽ chạy ngược, có nghĩa là từ phải sang trái.

Bạn có thể sử dụng trình lặp mà bạn nhận được từ việc gọi trực tiếp

Test case 1

Input:-
raju always plays cricket

Output:-
Expected output is
always cricket
cricket raju
plays raju

but coming output is
be be
not be
or be
or to

Test case 2

Input:-
python is a programming language

Output:-
Expected output is
a language
a python
is language
is programming
language python
programming python

but coming output is
be be
not be
or be
or to

Test case 3

Input:-
to be or not to be

Output:-
Expected output is
be be
be not
or to
to to

but coming output is
be be
not be
or be
or to
2 làm đối số đến
Test case 1

Input:-
raju always plays cricket

Output:-
Expected output is
always cricket
cricket raju
plays raju

but coming output is
be be
not be
or be
or to

Test case 2

Input:-
python is a programming language

Output:-
Expected output is
a language
a python
is language
is programming
language python
programming python

but coming output is
be be
not be
or be
or to

Test case 3

Input:-
to be or not to be

Output:-
Expected output is
be be
be not
or to
to to

but coming output is
be be
not be
or be
or to
3:

>>>

>>> "".join(reversed("Hello, World!"))
'!dlroW ,olleH'

Trong biểu thức dòng đơn này, bạn vượt qua kết quả gọi trực tiếp

Test case 1

Input:-
raju always plays cricket

Output:-
Expected output is
always cricket
cricket raju
plays raju

but coming output is
be be
not be
or be
or to

Test case 2

Input:-
python is a programming language

Output:-
Expected output is
a language
a python
is language
is programming
language python
programming python

but coming output is
be be
not be
or be
or to

Test case 3

Input:-
to be or not to be

Output:-
Expected output is
be be
be not
or to
to to

but coming output is
be be
not be
or be
or to
2 dưới dạng đối số cho
Test case 1

Input:-
raju always plays cricket

Output:-
Expected output is
always cricket
cricket raju
plays raju

but coming output is
be be
not be
or be
or to

Test case 2

Input:-
python is a programming language

Output:-
Expected output is
a language
a python
is language
is programming
language python
programming python

but coming output is
be be
not be
or be
or to

Test case 3

Input:-
to be or not to be

Output:-
Expected output is
be be
be not
or to
to to

but coming output is
be be
not be
or be
or to
3. Do đó, bạn nhận được một bản sao đảo ngược của chuỗi đầu vào gốc. Sự kết hợp của
Test case 1

Input:-
raju always plays cricket

Output:-
Expected output is
always cricket
cricket raju
plays raju

but coming output is
be be
not be
or be
or to

Test case 2

Input:-
python is a programming language

Output:-
Expected output is
a language
a python
is language
is programming
language python
programming python

but coming output is
be be
not be
or be
or to

Test case 3

Input:-
to be or not to be

Output:-
Expected output is
be be
be not
or to
to to

but coming output is
be be
not be
or be
or to
2 và
Test case 1

Input:-
raju always plays cricket

Output:-
Expected output is
always cricket
cricket raju
plays raju

but coming output is
be be
not be
or be
or to

Test case 2

Input:-
python is a programming language

Output:-
Expected output is
a language
a python
is language
is programming
language python
programming python

but coming output is
be be
not be
or be
or to

Test case 3

Input:-
to be or not to be

Output:-
Expected output is
be be
be not
or to
to to

but coming output is
be be
not be
or be
or to
3 là một lựa chọn tuyệt vời để đảo ngược các chuỗi.

Tạo dây đảo ngược bằng tay

Cho đến nay, bạn đã tìm hiểu về các công cụ và kỹ thuật Core Python để nhanh chóng đảo ngược chuỗi. Hầu hết thời gian, họ sẽ là cách của bạn để đi. Tuy nhiên, bạn có thể cần phải đảo ngược một chuỗi bằng tay tại một số điểm trong cuộc phiêu lưu mã hóa của bạn.

Trong phần này, bạn sẽ học cách đảo ngược các chuỗi bằng cách sử dụng các vòng lặp và đệ quy rõ ràng. Kỹ thuật cuối cùng sử dụng phương pháp lập trình chức năng với sự trợ giúp của chức năng Python từ

>>> "".join(reversed("Hello, World!"))
'!dlroW ,olleH'
1.

Đảo ngược chuỗi trong một vòng lặp

Kỹ thuật đầu tiên mà bạn sẽ sử dụng để đảo ngược một chuỗi liên quan đến vòng lặp

Test case 1

Input:-
raju always plays cricket

Output:-
Expected output is
always cricket
cricket raju
plays raju

but coming output is
be be
not be
or be
or to

Test case 2

Input:-
python is a programming language

Output:-
Expected output is
a language
a python
is language
is programming
language python
programming python

but coming output is
be be
not be
or be
or to

Test case 3

Input:-
to be or not to be

Output:-
Expected output is
be be
be not
or to
to to

but coming output is
be be
not be
or be
or to
5 và toán tử nối (
>>> "".join(reversed("Hello, World!"))
'!dlroW ,olleH'
3). Với hai chuỗi là toán hạng, toán tử này trả về một chuỗi mới xuất phát từ việc tham gia các chuỗi gốc. Toàn bộ hoạt động được gọi là nối.concatenation.

Ở đây, một chức năng lấy một chuỗi và đảo ngược nó trong một vòng lặp bằng cách sử dụng kết nối:

>>>

>>> def reversed_string(text):
...     result = ""
...     for char in text:
...         result = char + result
...     return result
...

>>> reversed_string("Hello, World!")
'!dlroW ,olleH'

Trong biểu thức dòng đơn này, bạn vượt qua kết quả gọi trực tiếp

Test case 1

Input:-
raju always plays cricket

Output:-
Expected output is
always cricket
cricket raju
plays raju

but coming output is
be be
not be
or be
or to

Test case 2

Input:-
python is a programming language

Output:-
Expected output is
a language
a python
is language
is programming
language python
programming python

but coming output is
be be
not be
or be
or to

Test case 3

Input:-
to be or not to be

Output:-
Expected output is
be be
be not
or to
to to

but coming output is
be be
not be
or be
or to
2 dưới dạng đối số cho
Test case 1

Input:-
raju always plays cricket

Output:-
Expected output is
always cricket
cricket raju
plays raju

but coming output is
be be
not be
or be
or to

Test case 2

Input:-
python is a programming language

Output:-
Expected output is
a language
a python
is language
is programming
language python
programming python

but coming output is
be be
not be
or be
or to

Test case 3

Input:-
to be or not to be

Output:-
Expected output is
be be
be not
or to
to to

but coming output is
be be
not be
or be
or to
3. Do đó, bạn nhận được một bản sao đảo ngược của chuỗi đầu vào gốc. Sự kết hợp của
Test case 1

Input:-
raju always plays cricket

Output:-
Expected output is
always cricket
cricket raju
plays raju

but coming output is
be be
not be
or be
or to

Test case 2

Input:-
python is a programming language

Output:-
Expected output is
a language
a python
is language
is programming
language python
programming python

but coming output is
be be
not be
or be
or to

Test case 3

Input:-
to be or not to be

Output:-
Expected output is
be be
be not
or to
to to

but coming output is
be be
not be
or be
or to
2 và
Test case 1

Input:-
raju always plays cricket

Output:-
Expected output is
always cricket
cricket raju
plays raju

but coming output is
be be
not be
or be
or to

Test case 2

Input:-
python is a programming language

Output:-
Expected output is
a language
a python
is language
is programming
language python
programming python

but coming output is
be be
not be
or be
or to

Test case 3

Input:-
to be or not to be

Output:-
Expected output is
be be
be not
or to
to to

but coming output is
be be
not be
or be
or to
3 là một lựa chọn tuyệt vời để đảo ngược các chuỗi.

Tạo dây đảo ngược bằng tay

>>>

sentence = "to be or not to be"
s = sentence.split()
s.sort()
result = []
for word1 in s:
    for word2 in s:
        result.append(word1 + " " + word2)
       
result = set(result)
result = list(result)
result.sort()


for item in result:
    if item in sentence:
        result.remove(item)
        
for item in result: 
    if " ".join(item.split()[::-1]) in sentence:
        result.remove(item)
        
for item in result:
    spl = item.split()
    if (spl[0] == spl[1]) and (sentence.count(spl[0]) < 2):
        result.remove(item)
        
for item in result:
    spl = item.split()
    dup = spl[1] + " " + spl[0]
    if dup in result:
        result.remove(dup)
result.insert(0, "be be")   
for item in result:
    print(item)
0

Cho đến nay, bạn đã tìm hiểu về các công cụ và kỹ thuật Core Python để nhanh chóng đảo ngược chuỗi. Hầu hết thời gian, họ sẽ là cách của bạn để đi. Tuy nhiên, bạn có thể cần phải đảo ngược một chuỗi bằng tay tại một số điểm trong cuộc phiêu lưu mã hóa của bạn.

Trong phần này, bạn sẽ học cách đảo ngược các chuỗi bằng cách sử dụng các vòng lặp và đệ quy rõ ràng. Kỹ thuật cuối cùng sử dụng phương pháp lập trình chức năng với sự trợ giúp của chức năng Python từ >>> "".join(reversed("Hello, World!")) '!dlroW ,olleH' 1.

Đảo ngược chuỗi trong một vòng lặpbase case that produces a result without calling the function again. The second component is the recursive case, which starts the recursive loop and performs most of the computation.

Kỹ thuật đầu tiên mà bạn sẽ sử dụng để đảo ngược một chuỗi liên quan đến vòng lặp

Test case 1

Input:-
raju always plays cricket

Output:-
Expected output is
always cricket
cricket raju
plays raju

but coming output is
be be
not be
or be
or to

Test case 2

Input:-
python is a programming language

Output:-
Expected output is
a language
a python
is language
is programming
language python
programming python

but coming output is
be be
not be
or be
or to

Test case 3

Input:-
to be or not to be

Output:-
Expected output is
be be
be not
or to
to to

but coming output is
be be
not be
or be
or to
5 và toán tử nối (
>>> "".join(reversed("Hello, World!"))
'!dlroW ,olleH'
3). Với hai chuỗi là toán hạng, toán tử này trả về một chuỗi mới xuất phát từ việc tham gia các chuỗi gốc. Toàn bộ hoạt động được gọi là nối.

>>>

sentence = "to be or not to be"
s = sentence.split()
s.sort()
result = []
for word1 in s:
    for word2 in s:
        result.append(word1 + " " + word2)
       
result = set(result)
result = list(result)
result.sort()


for item in result:
    if item in sentence:
        result.remove(item)
        
for item in result: 
    if " ".join(item.split()[::-1]) in sentence:
        result.remove(item)
        
for item in result:
    spl = item.split()
    if (spl[0] == spl[1]) and (sentence.count(spl[0]) < 2):
        result.remove(item)
        
for item in result:
    spl = item.split()
    dup = spl[1] + " " + spl[0]
    if dup in result:
        result.remove(dup)
result.insert(0, "be be")   
for item in result:
    print(item)
1

Ở đây, một chức năng lấy một chuỗi và đảo ngược nó trong một vòng lặp bằng cách sử dụng kết nối:

Trong mỗi lần lặp, vòng lặp có một ký tự tiếp theo,

>>> "".join(reversed("Hello, World!"))
'!dlroW ,olleH'
4, từ
>>> "".join(reversed("Hello, World!"))
'!dlroW ,olleH'
5 và kết hợp nó với nội dung hiện tại của
>>> "".join(reversed("Hello, World!"))
'!dlroW ,olleH'
6. Lưu ý rằng
>>> "".join(reversed("Hello, World!"))
'!dlroW ,olleH'
6 ban đầu giữ một chuỗi trống (
>>> "".join(reversed("Hello, World!"))
'!dlroW ,olleH'
8). Chuỗi trung gian mới sau đó được chỉ định lại thành
>>> "".join(reversed("Hello, World!"))
'!dlroW ,olleH'
6. Ở cuối vòng lặp,
>>> "".join(reversed("Hello, World!"))
'!dlroW ,olleH'
6 giữ một chuỗi mới như một bản sao đảo ngược của bản gốc.

Nếu bạn thích sử dụng vòng lặp

Test case 1

Input:-
raju always plays cricket

Output:-
Expected output is
always cricket
cricket raju
plays raju

but coming output is
be be
not be
or be
or to

Test case 2

Input:-
python is a programming language

Output:-
Expected output is
a language
a python
is language
is programming
language python
programming python

but coming output is
be be
not be
or be
or to

Test case 3

Input:-
to be or not to be

Output:-
Expected output is
be be
be not
or to
to to

but coming output is
be be
not be
or be
or to
6, thì đây là những gì bạn có thể làm để xây dựng một bản sao đảo ngược của một chuỗi đã cho:

>>>

sentence = "to be or not to be"
s = sentence.split()
s.sort()
result = []
for word1 in s:
    for word2 in s:
        result.append(word1 + " " + word2)
       
result = set(result)
result = list(result)
result.sort()


for item in result:
    if item in sentence:
        result.remove(item)
        
for item in result: 
    if " ".join(item.split()[::-1]) in sentence:
        result.remove(item)
        
for item in result:
    spl = item.split()
    if (spl[0] == spl[1]) and (sentence.count(spl[0]) < 2):
        result.remove(item)
        
for item in result:
    spl = item.split()
    dup = spl[1] + " " + spl[0]
    if dup in result:
        result.remove(dup)
result.insert(0, "be be")   
for item in result:
    print(item)
2

Ở đây, trước tiên bạn tính toán

>>> def reversed_string(text):
...     result = ""
...     for char in text:
...         result = char + result
...     return result
...

>>> reversed_string("Hello, World!")
'!dlroW ,olleH'
2 của ký tự cuối cùng trong chuỗi đầu vào bằng cách sử dụng
>>> def reversed_string(text):
...     result = ""
...     for char in text:
...         result = char + result
...     return result
...

>>> reversed_string("Hello, World!")
'!dlroW ,olleH'
3. Vòng lặp lặp lại từ
>>> def reversed_string(text):
...     result = ""
...     for char in text:
...         result = char + result
...     return result
...

>>> reversed_string("Hello, World!")
'!dlroW ,olleH'
2 xuống và bao gồm
a_string[start:stop:step]
7. Trong mỗi lần lặp, bạn sử dụng toán tử gán tăng cường (
>>> def reversed_string(text):
...     result = ""
...     for char in text:
...         result = char + result
...     return result
...

>>> reversed_string("Hello, World!")
'!dlroW ,olleH'
6) để tạo một chuỗi trung gian kết hợp nội dung của
>>> "".join(reversed("Hello, World!"))
'!dlroW ,olleH'
6 với ký tự tương ứng từ
>>> "".join(reversed("Hello, World!"))
'!dlroW ,olleH'
5. Một lần nữa, kết quả cuối cùng là một chuỗi mới kết quả từ việc đảo ngược chuỗi đầu vào.

Đảo ngược chuỗi với đệ quy

Bạn cũng có thể sử dụng đệ quy để đảo ngược chuỗi. Đệ quy là khi một chức năng tự gọi mình trong cơ thể của chính nó. Để ngăn chặn đệ quy vô hạn, bạn nên cung cấp một trường hợp cơ sở tạo ra kết quả mà không gọi lại chức năng. Thành phần thứ hai là trường hợp đệ quy, bắt đầu vòng lặp đệ quy và thực hiện hầu hết các tính toán.

Tại đây, cách bạn có thể xác định chức năng đệ quy trả về bản sao đảo ngược của một chuỗi đã cho:

Trong ví dụ này, trước tiên bạn kiểm tra trường hợp cơ sở. Nếu chuỗi đầu vào có chính xác một ký tự, bạn sẽ trả lại chuỗi trở lại người gọi.

>>>

sentence = "to be or not to be"
s = sentence.split()
s.sort()
result = []
for word1 in s:
    for word2 in s:
        result.append(word1 + " " + word2)
       
result = set(result)
result = list(result)
result.sort()


for item in result:
    if item in sentence:
        result.remove(item)
        
for item in result: 
    if " ".join(item.split()[::-1]) in sentence:
        result.remove(item)
        
for item in result:
    spl = item.split()
    if (spl[0] == spl[1]) and (sentence.count(spl[0]) < 2):
        result.remove(item)
        
for item in result:
    spl = item.split()
    dup = spl[1] + " " + spl[0]
    if dup in result:
        result.remove(dup)
result.insert(0, "be be")   
for item in result:
    print(item)
3

Tuyên bố cuối cùng, là trường hợp đệ quy, gọi chính

>>> def reversed_string(text):
...     result = ""
...     for char in text:
...         result = char + result
...     return result
...

>>> reversed_string("Hello, World!")
'!dlroW ,olleH'
9. Cuộc gọi sử dụng lát
sentence = "to be or not to be"
s = sentence.split()
s.sort()
result = []
for word1 in s:
    for word2 in s:
        result.append(word1 + " " + word2)
       
result = set(result)
result = list(result)
result.sort()


for item in result:
    if item in sentence:
        result.remove(item)
        
for item in result: 
    if " ".join(item.split()[::-1]) in sentence:
        result.remove(item)
        
for item in result:
    spl = item.split()
    if (spl[0] == spl[1]) and (sentence.count(spl[0]) < 2):
        result.remove(item)
        
for item in result:
    spl = item.split()
    dup = spl[1] + " " + spl[0]
    if dup in result:
        result.remove(dup)
result.insert(0, "be be")   
for item in result:
    print(item)
00 của chuỗi đầu vào làm đối số. Đụ này chứa tất cả các ký tự trong
>>> "".join(reversed("Hello, World!"))
'!dlroW ,olleH'
5, ngoại trừ phần đầu tiên. Bước tiếp theo là thêm kết quả của cuộc gọi đệ quy cùng với chuỗi ký tự đơn
sentence = "to be or not to be"
s = sentence.split()
s.sort()
result = []
for word1 in s:
    for word2 in s:
        result.append(word1 + " " + word2)
       
result = set(result)
result = list(result)
result.sort()


for item in result:
    if item in sentence:
        result.remove(item)
        
for item in result: 
    if " ".join(item.split()[::-1]) in sentence:
        result.remove(item)
        
for item in result:
    spl = item.split()
    if (spl[0] == spl[1]) and (sentence.count(spl[0]) < 2):
        result.remove(item)
        
for item in result:
    spl = item.split()
    dup = spl[1] + " " + spl[0]
    if dup in result:
        result.remove(dup)
result.insert(0, "be be")   
for item in result:
    print(item)
02, chứa ký tự đầu tiên của
>>> "".join(reversed("Hello, World!"))
'!dlroW ,olleH'
5.

Lặp lại qua các chuỗi ngược lại

Đôi khi bạn có thể muốn lặp lại thông qua các chuỗi hiện có theo thứ tự ngược lại, một kỹ thuật thường được gọi là lặp ngược. Tùy thuộc vào nhu cầu cụ thể của bạn, bạn có thể thực hiện lặp lại trên các chuỗi bằng cách sử dụng một trong các tùy chọn sau:reverse iteration. Depending on your specific needs, you can do reverse iteration on strings by using one of the following options:

  • Chức năng tích hợp
    Test case 1
    
    Input:-
    raju always plays cricket
    
    Output:-
    Expected output is
    always cricket
    cricket raju
    plays raju
    
    but coming output is
    be be
    not be
    or be
    or to
    
    Test case 2
    
    Input:-
    python is a programming language
    
    Output:-
    Expected output is
    a language
    a python
    is language
    is programming
    language python
    programming python
    
    but coming output is
    be be
    not be
    or be
    or to
    
    Test case 3
    
    Input:-
    to be or not to be
    
    Output:-
    Expected output is
    be be
    be not
    or to
    to to
    
    but coming output is
    be be
    not be
    or be
    or to
    
    2
  • Toán tử cắt,
    >>> letters = "ABCDEF"
    
    >>> letters[slice(None, None, -1)]
    'FEDCBA'
    
    0

Lặp lại ngược được cho là trường hợp sử dụng phổ biến nhất của các công cụ này, vì vậy trong một vài phần sau, bạn sẽ tìm hiểu về cách sử dụng chúng trong bối cảnh lặp.

Hàm tích hợp () được đảo ngược ()

Cách tiếp cận dễ đọc nhất và pythonic để lặp lại theo một chuỗi theo thứ tự ngược là sử dụng

Test case 1

Input:-
raju always plays cricket

Output:-
Expected output is
always cricket
cricket raju
plays raju

but coming output is
be be
not be
or be
or to

Test case 2

Input:-
python is a programming language

Output:-
Expected output is
a language
a python
is language
is programming
language python
programming python

but coming output is
be be
not be
or be
or to

Test case 3

Input:-
to be or not to be

Output:-
Expected output is
be be
be not
or to
to to

but coming output is
be be
not be
or be
or to
2. Bạn đã tìm hiểu về chức năng này một vài khoảnh khắc trước khi bạn sử dụng nó cùng với
Test case 1

Input:-
raju always plays cricket

Output:-
Expected output is
always cricket
cricket raju
plays raju

but coming output is
be be
not be
or be
or to

Test case 2

Input:-
python is a programming language

Output:-
Expected output is
a language
a python
is language
is programming
language python
programming python

but coming output is
be be
not be
or be
or to

Test case 3

Input:-
to be or not to be

Output:-
Expected output is
be be
be not
or to
to to

but coming output is
be be
not be
or be
or to
3 để tạo các chuỗi đảo ngược.

Tuy nhiên, mục đích chính và trường hợp sử dụng của

Test case 1

Input:-
raju always plays cricket

Output:-
Expected output is
always cricket
cricket raju
plays raju

but coming output is
be be
not be
or be
or to

Test case 2

Input:-
python is a programming language

Output:-
Expected output is
a language
a python
is language
is programming
language python
programming python

but coming output is
be be
not be
or be
or to

Test case 3

Input:-
to be or not to be

Output:-
Expected output is
be be
be not
or to
to to

but coming output is
be be
not be
or be
or to
2 là để hỗ trợ lặp ngược lại trên các vòng lặp Python. Với một chuỗi là một đối số,
Test case 1

Input:-
raju always plays cricket

Output:-
Expected output is
always cricket
cricket raju
plays raju

but coming output is
be be
not be
or be
or to

Test case 2

Input:-
python is a programming language

Output:-
Expected output is
a language
a python
is language
is programming
language python
programming python

but coming output is
be be
not be
or be
or to

Test case 3

Input:-
to be or not to be

Output:-
Expected output is
be be
be not
or to
to to

but coming output is
be be
not be
or be
or to
2 trả về một trình lặp mang lại các ký tự từ chuỗi đầu vào theo thứ tự ngược lại.

Tại đây, cách bạn có thể lặp lại một chuỗi theo thứ tự ngược lại với

Test case 1

Input:-
raju always plays cricket

Output:-
Expected output is
always cricket
cricket raju
plays raju

but coming output is
be be
not be
or be
or to

Test case 2

Input:-
python is a programming language

Output:-
Expected output is
a language
a python
is language
is programming
language python
programming python

but coming output is
be be
not be
or be
or to

Test case 3

Input:-
to be or not to be

Output:-
Expected output is
be be
be not
or to
to to

but coming output is
be be
not be
or be
or to
2:

>>>

sentence = "to be or not to be"
s = sentence.split()
s.sort()
result = []
for word1 in s:
    for word2 in s:
        result.append(word1 + " " + word2)
       
result = set(result)
result = list(result)
result.sort()


for item in result:
    if item in sentence:
        result.remove(item)
        
for item in result: 
    if " ".join(item.split()[::-1]) in sentence:
        result.remove(item)
        
for item in result:
    spl = item.split()
    if (spl[0] == spl[1]) and (sentence.count(spl[0]) < 2):
        result.remove(item)
        
for item in result:
    spl = item.split()
    dup = spl[1] + " " + spl[0]
    if dup in result:
        result.remove(dup)
result.insert(0, "be be")   
for item in result:
    print(item)
4

Vòng lặp

Test case 1

Input:-
raju always plays cricket

Output:-
Expected output is
always cricket
cricket raju
plays raju

but coming output is
be be
not be
or be
or to

Test case 2

Input:-
python is a programming language

Output:-
Expected output is
a language
a python
is language
is programming
language python
programming python

but coming output is
be be
not be
or be
or to

Test case 3

Input:-
to be or not to be

Output:-
Expected output is
be be
be not
or to
to to

but coming output is
be be
not be
or be
or to
5 trong ví dụ này rất dễ đọc. Tên của
Test case 1

Input:-
raju always plays cricket

Output:-
Expected output is
always cricket
cricket raju
plays raju

but coming output is
be be
not be
or be
or to

Test case 2

Input:-
python is a programming language

Output:-
Expected output is
a language
a python
is language
is programming
language python
programming python

but coming output is
be be
not be
or be
or to

Test case 3

Input:-
to be or not to be

Output:-
Expected output is
be be
be not
or to
to to

but coming output is
be be
not be
or be
or to
2 thể hiện rõ ràng ý định của nó và truyền đạt rằng chức năng không tạo ra bất kỳ tác dụng phụ nào trên dữ liệu đầu vào. Kể từ khi
Test case 1

Input:-
raju always plays cricket

Output:-
Expected output is
always cricket
cricket raju
plays raju

but coming output is
be be
not be
or be
or to

Test case 2

Input:-
python is a programming language

Output:-
Expected output is
a language
a python
is language
is programming
language python
programming python

but coming output is
be be
not be
or be
or to

Test case 3

Input:-
to be or not to be

Output:-
Expected output is
be be
be not
or to
to to

but coming output is
be be
not be
or be
or to
2 trả về một trình lặp, vòng lặp cũng hiệu quả về việc sử dụng bộ nhớ.

Toán tử cắt, [::-1]

Cách tiếp cận thứ hai để thực hiện lặp ngược lại trên các chuỗi là sử dụng cú pháp cắt mở rộng mà bạn đã thấy trước đây trong ví dụ

sentence = "to be or not to be"
s = sentence.split()
s.sort()
result = []
for word1 in s:
    for word2 in s:
        result.append(word1 + " " + word2)
       
result = set(result)
result = list(result)
result.sort()


for item in result:
    if item in sentence:
        result.remove(item)
        
for item in result: 
    if " ".join(item.split()[::-1]) in sentence:
        result.remove(item)
        
for item in result:
    spl = item.split()
    if (spl[0] == spl[1]) and (sentence.count(spl[0]) < 2):
        result.remove(item)
        
for item in result:
    spl = item.split()
    dup = spl[1] + " " + spl[0]
    if dup in result:
        result.remove(dup)
result.insert(0, "be be")   
for item in result:
    print(item)
29. Mặc dù cách tiếp cận này đã giành được hiệu quả và khả năng đọc bộ nhớ, nhưng nó vẫn cung cấp một cách nhanh chóng để lặp lại một bản sao đảo ngược của một chuỗi hiện có:

>>>

sentence = "to be or not to be"
s = sentence.split()
s.sort()
result = []
for word1 in s:
    for word2 in s:
        result.append(word1 + " " + word2)
       
result = set(result)
result = list(result)
result.sort()


for item in result:
    if item in sentence:
        result.remove(item)
        
for item in result: 
    if " ".join(item.split()[::-1]) in sentence:
        result.remove(item)
        
for item in result:
    spl = item.split()
    if (spl[0] == spl[1]) and (sentence.count(spl[0]) < 2):
        result.remove(item)
        
for item in result:
    spl = item.split()
    dup = spl[1] + " " + spl[0]
    if dup in result:
        result.remove(dup)
result.insert(0, "be be")   
for item in result:
    print(item)
5

Vòng lặp

Test case 1

Input:-
raju always plays cricket

Output:-
Expected output is
always cricket
cricket raju
plays raju

but coming output is
be be
not be
or be
or to

Test case 2

Input:-
python is a programming language

Output:-
Expected output is
a language
a python
is language
is programming
language python
programming python

but coming output is
be be
not be
or be
or to

Test case 3

Input:-
to be or not to be

Output:-
Expected output is
be be
be not
or to
to to

but coming output is
be be
not be
or be
or to
5 trong ví dụ này rất dễ đọc. Tên của
Test case 1

Input:-
raju always plays cricket

Output:-
Expected output is
always cricket
cricket raju
plays raju

but coming output is
be be
not be
or be
or to

Test case 2

Input:-
python is a programming language

Output:-
Expected output is
a language
a python
is language
is programming
language python
programming python

but coming output is
be be
not be
or be
or to

Test case 3

Input:-
to be or not to be

Output:-
Expected output is
be be
be not
or to
to to

but coming output is
be be
not be
or be
or to
2 thể hiện rõ ràng ý định của nó và truyền đạt rằng chức năng không tạo ra bất kỳ tác dụng phụ nào trên dữ liệu đầu vào. Kể từ khi
Test case 1

Input:-
raju always plays cricket

Output:-
Expected output is
always cricket
cricket raju
plays raju

but coming output is
be be
not be
or be
or to

Test case 2

Input:-
python is a programming language

Output:-
Expected output is
a language
a python
is language
is programming
language python
programming python

but coming output is
be be
not be
or be
or to

Test case 3

Input:-
to be or not to be

Output:-
Expected output is
be be
be not
or to
to to

but coming output is
be be
not be
or be
or to
2 trả về một trình lặp, vòng lặp cũng hiệu quả về việc sử dụng bộ nhớ.

Toán tử cắt, [::-1]

Cách tiếp cận thứ hai để thực hiện lặp ngược lại trên các chuỗi là sử dụng cú pháp cắt mở rộng mà bạn đã thấy trước đây trong ví dụ

sentence = "to be or not to be"
s = sentence.split()
s.sort()
result = []
for word1 in s:
    for word2 in s:
        result.append(word1 + " " + word2)
       
result = set(result)
result = list(result)
result.sort()


for item in result:
    if item in sentence:
        result.remove(item)
        
for item in result: 
    if " ".join(item.split()[::-1]) in sentence:
        result.remove(item)
        
for item in result:
    spl = item.split()
    if (spl[0] == spl[1]) and (sentence.count(spl[0]) < 2):
        result.remove(item)
        
for item in result:
    spl = item.split()
    dup = spl[1] + " " + spl[0]
    if dup in result:
        result.remove(dup)
result.insert(0, "be be")   
for item in result:
    print(item)
29. Mặc dù cách tiếp cận này đã giành được hiệu quả và khả năng đọc bộ nhớ, nhưng nó vẫn cung cấp một cách nhanh chóng để lặp lại một bản sao đảo ngược của một chuỗi hiện có:

Trong ví dụ này, bạn áp dụng toán tử cắt trên

>>> greeting = reversed("Hello, World!")

>>> next(greeting)
'!'
>>> next(greeting)
'd'
>>> next(greeting)
'l'
3 để tạo một bản sao đảo ngược của nó. Sau đó, bạn sử dụng chuỗi đảo ngược mới đó để cung cấp cho vòng lặp. Trong trường hợp này, bạn đã lặp lại một chuỗi đảo ngược mới, vì vậy giải pháp này ít hiệu quả hơn so với sử dụng
Test case 1

Input:-
raju always plays cricket

Output:-
Expected output is
always cricket
cricket raju
plays raju

but coming output is
be be
not be
or be
or to

Test case 2

Input:-
python is a programming language

Output:-
Expected output is
a language
a python
is language
is programming
language python
programming python

but coming output is
be be
not be
or be
or to

Test case 3

Input:-
to be or not to be

Output:-
Expected output is
be be
be not
or to
to to

but coming output is
be be
not be
or be
or to
2.

>>>

sentence = "to be or not to be"
s = sentence.split()
s.sort()
result = []
for word1 in s:
    for word2 in s:
        result.append(word1 + " " + word2)
       
result = set(result)
result = list(result)
result.sort()


for item in result:
    if item in sentence:
        result.remove(item)
        
for item in result: 
    if " ".join(item.split()[::-1]) in sentence:
        result.remove(item)
        
for item in result:
    spl = item.split()
    if (spl[0] == spl[1]) and (sentence.count(spl[0]) < 2):
        result.remove(item)
        
for item in result:
    spl = item.split()
    dup = spl[1] + " " + spl[0]
    if dup in result:
        result.remove(dup)
result.insert(0, "be be")   
for item in result:
    print(item)
6

Vòng lặp

Test case 1

Input:-
raju always plays cricket

Output:-
Expected output is
always cricket
cricket raju
plays raju

but coming output is
be be
not be
or be
or to

Test case 2

Input:-
python is a programming language

Output:-
Expected output is
a language
a python
is language
is programming
language python
programming python

but coming output is
be be
not be
or be
or to

Test case 3

Input:-
to be or not to be

Output:-
Expected output is
be be
be not
or to
to to

but coming output is
be be
not be
or be
or to
5 trong ví dụ này rất dễ đọc. Tên của
Test case 1

Input:-
raju always plays cricket

Output:-
Expected output is
always cricket
cricket raju
plays raju

but coming output is
be be
not be
or be
or to

Test case 2

Input:-
python is a programming language

Output:-
Expected output is
a language
a python
is language
is programming
language python
programming python

but coming output is
be be
not be
or be
or to

Test case 3

Input:-
to be or not to be

Output:-
Expected output is
be be
be not
or to
to to

but coming output is
be be
not be
or be
or to
2 thể hiện rõ ràng ý định của nó và truyền đạt rằng chức năng không tạo ra bất kỳ tác dụng phụ nào trên dữ liệu đầu vào. Kể từ khi
Test case 1

Input:-
raju always plays cricket

Output:-
Expected output is
always cricket
cricket raju
plays raju

but coming output is
be be
not be
or be
or to

Test case 2

Input:-
python is a programming language

Output:-
Expected output is
a language
a python
is language
is programming
language python
programming python

but coming output is
be be
not be
or be
or to

Test case 3

Input:-
to be or not to be

Output:-
Expected output is
be be
be not
or to
to to

but coming output is
be be
not be
or be
or to
2 trả về một trình lặp, vòng lặp cũng hiệu quả về việc sử dụng bộ nhớ.

Toán tử cắt, [::-1]

Cách tiếp cận thứ hai để thực hiện lặp ngược lại trên các chuỗi là sử dụng cú pháp cắt mở rộng mà bạn đã thấy trước đây trong ví dụ

sentence = "to be or not to be"
s = sentence.split()
s.sort()
result = []
for word1 in s:
    for word2 in s:
        result.append(word1 + " " + word2)
       
result = set(result)
result = list(result)
result.sort()


for item in result:
    if item in sentence:
        result.remove(item)
        
for item in result: 
    if " ".join(item.split()[::-1]) in sentence:
        result.remove(item)
        
for item in result:
    spl = item.split()
    if (spl[0] == spl[1]) and (sentence.count(spl[0]) < 2):
        result.remove(item)
        
for item in result:
    spl = item.split()
    dup = spl[1] + " " + spl[0]
    if dup in result:
        result.remove(dup)
result.insert(0, "be be")   
for item in result:
    print(item)
29. Mặc dù cách tiếp cận này đã giành được hiệu quả và khả năng đọc bộ nhớ, nhưng nó vẫn cung cấp một cách nhanh chóng để lặp lại một bản sao đảo ngược của một chuỗi hiện có:

Trong ví dụ này, bạn áp dụng toán tử cắt trên

>>> greeting = reversed("Hello, World!")

>>> next(greeting)
'!'
>>> next(greeting)
'd'
>>> next(greeting)
'l'
3 để tạo một bản sao đảo ngược của nó. Sau đó, bạn sử dụng chuỗi đảo ngược mới đó để cung cấp cho vòng lặp. Trong trường hợp này, bạn đã lặp lại một chuỗi đảo ngược mới, vì vậy giải pháp này ít hiệu quả hơn so với sử dụng
Test case 1

Input:-
raju always plays cricket

Output:-
Expected output is
always cricket
cricket raju
plays raju

but coming output is
be be
not be
or be
or to

Test case 2

Input:-
python is a programming language

Output:-
Expected output is
a language
a python
is language
is programming
language python
programming python

but coming output is
be be
not be
or be
or to

Test case 3

Input:-
to be or not to be

Output:-
Expected output is
be be
be not
or to
to to

but coming output is
be be
not be
or be
or to
2.

>>>

sentence = "to be or not to be"
s = sentence.split()
s.sort()
result = []
for word1 in s:
    for word2 in s:
        result.append(word1 + " " + word2)
       
result = set(result)
result = list(result)
result.sort()


for item in result:
    if item in sentence:
        result.remove(item)
        
for item in result: 
    if " ".join(item.split()[::-1]) in sentence:
        result.remove(item)
        
for item in result:
    spl = item.split()
    if (spl[0] == spl[1]) and (sentence.count(spl[0]) < 2):
        result.remove(item)
        
for item in result:
    spl = item.split()
    dup = spl[1] + " " + spl[0]
    if dup in result:
        result.remove(dup)
result.insert(0, "be be")   
for item in result:
    print(item)
7

Vòng lặp

Test case 1

Input:-
raju always plays cricket

Output:-
Expected output is
always cricket
cricket raju
plays raju

but coming output is
be be
not be
or be
or to

Test case 2

Input:-
python is a programming language

Output:-
Expected output is
a language
a python
is language
is programming
language python
programming python

but coming output is
be be
not be
or be
or to

Test case 3

Input:-
to be or not to be

Output:-
Expected output is
be be
be not
or to
to to

but coming output is
be be
not be
or be
or to
5 trong ví dụ này rất dễ đọc. Tên của
Test case 1

Input:-
raju always plays cricket

Output:-
Expected output is
always cricket
cricket raju
plays raju

but coming output is
be be
not be
or be
or to

Test case 2

Input:-
python is a programming language

Output:-
Expected output is
a language
a python
is language
is programming
language python
programming python

but coming output is
be be
not be
or be
or to

Test case 3

Input:-
to be or not to be

Output:-
Expected output is
be be
be not
or to
to to

but coming output is
be be
not be
or be
or to
2 thể hiện rõ ràng ý định của nó và truyền đạt rằng chức năng không tạo ra bất kỳ tác dụng phụ nào trên dữ liệu đầu vào. Kể từ khi
Test case 1

Input:-
raju always plays cricket

Output:-
Expected output is
always cricket
cricket raju
plays raju

but coming output is
be be
not be
or be
or to

Test case 2

Input:-
python is a programming language

Output:-
Expected output is
a language
a python
is language
is programming
language python
programming python

but coming output is
be be
not be
or be
or to

Test case 3

Input:-
to be or not to be

Output:-
Expected output is
be be
be not
or to
to to

but coming output is
be be
not be
or be
or to
2 trả về một trình lặp, vòng lặp cũng hiệu quả về việc sử dụng bộ nhớ.

Toán tử cắt, [::-1]

>>>

sentence = "to be or not to be"
s = sentence.split()
s.sort()
result = []
for word1 in s:
    for word2 in s:
        result.append(word1 + " " + word2)
       
result = set(result)
result = list(result)
result.sort()


for item in result:
    if item in sentence:
        result.remove(item)
        
for item in result: 
    if " ".join(item.split()[::-1]) in sentence:
        result.remove(item)
        
for item in result:
    spl = item.split()
    if (spl[0] == spl[1]) and (sentence.count(spl[0]) < 2):
        result.remove(item)
        
for item in result:
    spl = item.split()
    dup = spl[1] + " " + spl[0]
    if dup in result:
        result.remove(dup)
result.insert(0, "be be")   
for item in result:
    print(item)
8

Vòng lặp

Test case 1

Input:-
raju always plays cricket

Output:-
Expected output is
always cricket
cricket raju
plays raju

but coming output is
be be
not be
or be
or to

Test case 2

Input:-
python is a programming language

Output:-
Expected output is
a language
a python
is language
is programming
language python
programming python

but coming output is
be be
not be
or be
or to

Test case 3

Input:-
to be or not to be

Output:-
Expected output is
be be
be not
or to
to to

but coming output is
be be
not be
or be
or to
5 trong ví dụ này rất dễ đọc. Tên của
Test case 1

Input:-
raju always plays cricket

Output:-
Expected output is
always cricket
cricket raju
plays raju

but coming output is
be be
not be
or be
or to

Test case 2

Input:-
python is a programming language

Output:-
Expected output is
a language
a python
is language
is programming
language python
programming python

but coming output is
be be
not be
or be
or to

Test case 3

Input:-
to be or not to be

Output:-
Expected output is
be be
be not
or to
to to

but coming output is
be be
not be
or be
or to
2 thể hiện rõ ràng ý định của nó và truyền đạt rằng chức năng không tạo ra bất kỳ tác dụng phụ nào trên dữ liệu đầu vào. Kể từ khi
Test case 1

Input:-
raju always plays cricket

Output:-
Expected output is
always cricket
cricket raju
plays raju

but coming output is
be be
not be
or be
or to

Test case 2

Input:-
python is a programming language

Output:-
Expected output is
a language
a python
is language
is programming
language python
programming python

but coming output is
be be
not be
or be
or to

Test case 3

Input:-
to be or not to be

Output:-
Expected output is
be be
be not
or to
to to

but coming output is
be be
not be
or be
or to
2 trả về một trình lặp, vòng lặp cũng hiệu quả về việc sử dụng bộ nhớ.

Toán tử cắt, [::-1]

Cách tiếp cận thứ hai để thực hiện lặp ngược lại trên các chuỗi là sử dụng cú pháp cắt mở rộng mà bạn đã thấy trước đây trong ví dụ

sentence = "to be or not to be"
s = sentence.split()
s.sort()
result = []
for word1 in s:
    for word2 in s:
        result.append(word1 + " " + word2)
       
result = set(result)
result = list(result)
result.sort()


for item in result:
    if item in sentence:
        result.remove(item)
        
for item in result: 
    if " ".join(item.split()[::-1]) in sentence:
        result.remove(item)
        
for item in result:
    spl = item.split()
    if (spl[0] == spl[1]) and (sentence.count(spl[0]) < 2):
        result.remove(item)
        
for item in result:
    spl = item.split()
    dup = spl[1] + " " + spl[0]
    if dup in result:
        result.remove(dup)
result.insert(0, "be be")   
for item in result:
    print(item)
29. Mặc dù cách tiếp cận này đã giành được hiệu quả và khả năng đọc bộ nhớ, nhưng nó vẫn cung cấp một cách nhanh chóng để lặp lại một bản sao đảo ngược của một chuỗi hiện có:

Trong ví dụ này, bạn áp dụng toán tử cắt trên

>>> greeting = reversed("Hello, World!")

>>> next(greeting)
'!'
>>> next(greeting)
'd'
>>> next(greeting)
'l'
3 để tạo một bản sao đảo ngược của nó. Sau đó, bạn sử dụng chuỗi đảo ngược mới đó để cung cấp cho vòng lặp. Trong trường hợp này, bạn đã lặp lại một chuỗi đảo ngược mới, vì vậy giải pháp này ít hiệu quả hơn so với sử dụng
Test case 1

Input:-
raju always plays cricket

Output:-
Expected output is
always cricket
cricket raju
plays raju

but coming output is
be be
not be
or be
or to

Test case 2

Input:-
python is a programming language

Output:-
Expected output is
a language
a python
is language
is programming
language python
programming python

but coming output is
be be
not be
or be
or to

Test case 3

Input:-
to be or not to be

Output:-
Expected output is
be be
be not
or to
to to

but coming output is
be be
not be
or be
or to
2.

>>>

sentence = "to be or not to be"
s = sentence.split()
s.sort()
result = []
for word1 in s:
    for word2 in s:
        result.append(word1 + " " + word2)
       
result = set(result)
result = list(result)
result.sort()


for item in result:
    if item in sentence:
        result.remove(item)
        
for item in result: 
    if " ".join(item.split()[::-1]) in sentence:
        result.remove(item)
        
for item in result:
    spl = item.split()
    if (spl[0] == spl[1]) and (sentence.count(spl[0]) < 2):
        result.remove(item)
        
for item in result:
    spl = item.split()
    dup = spl[1] + " " + spl[0]
    if dup in result:
        result.remove(dup)
result.insert(0, "be be")   
for item in result:
    print(item)
9

Khi bạn gọi

Test case 1

Input:-
raju always plays cricket

Output:-
Expected output is
always cricket
cricket raju
plays raju

but coming output is
be be
not be
or be
or to

Test case 2

Input:-
python is a programming language

Output:-
Expected output is
a language
a python
is language
is programming
language python
programming python

but coming output is
be be
not be
or be
or to

Test case 3

Input:-
to be or not to be

Output:-
Expected output is
be be
be not
or to
to to

but coming output is
be be
not be
or be
or to
4 với một chuỗi là đối số và
sentence = "to be or not to be"
s = sentence.split()
s.sort()
result = []
for word1 in s:
    for word2 in s:
        result.append(word1 + " " + word2)
       
result = set(result)
result = list(result)
result.sort()


for item in result:
    if item in sentence:
        result.remove(item)
        
for item in result: 
    if " ".join(item.split()[::-1]) in sentence:
        result.remove(item)
        
for item in result:
    spl = item.split()
    if (spl[0] == spl[1]) and (sentence.count(spl[0]) < 2):
        result.remove(item)
        
for item in result:
    spl = item.split()
    dup = spl[1] + " " + spl[0]
    if dup in result:
        result.remove(dup)
result.insert(0, "be be")   
for item in result:
    print(item)
64 được đặt thành
sentence = "to be or not to be"
s = sentence.split()
s.sort()
result = []
for word1 in s:
    for word2 in s:
        result.append(word1 + " " + word2)
       
result = set(result)
result = list(result)
result.sort()


for item in result:
    if item in sentence:
        result.remove(item)
        
for item in result: 
    if " ".join(item.split()[::-1]) in sentence:
        result.remove(item)
        
for item in result:
    spl = item.split()
    if (spl[0] == spl[1]) and (sentence.count(spl[0]) < 2):
        result.remove(item)
        
for item in result:
    spl = item.split()
    dup = spl[1] + " " + spl[0]
    if dup in result:
        result.remove(dup)
result.insert(0, "be be")   
for item in result:
    print(item)
65, bạn sẽ nhận được một danh sách chứa các ký tự của chuỗi đầu vào theo thứ tự ngược hoặc giảm dần. Vì
Test case 1

Input:-
raju always plays cricket

Output:-
Expected output is
always cricket
cricket raju
plays raju

but coming output is
be be
not be
or be
or to

Test case 2

Input:-
python is a programming language

Output:-
Expected output is
a language
a python
is language
is programming
language python
programming python

but coming output is
be be
not be
or be
or to

Test case 3

Input:-
to be or not to be

Output:-
Expected output is
be be
be not
or to
to to

but coming output is
be be
not be
or be
or to
4 trả về một đối tượng
sentence = "to be or not to be"
s = sentence.split()
s.sort()
result = []
for word1 in s:
    for word2 in s:
        result.append(word1 + " " + word2)
       
result = set(result)
result = list(result)
result.sort()


for item in result:
    if item in sentence:
        result.remove(item)
        
for item in result: 
    if " ".join(item.split()[::-1]) in sentence:
        result.remove(item)
        
for item in result:
    spl = item.split()
    if (spl[0] == spl[1]) and (sentence.count(spl[0]) < 2):
        result.remove(item)
        
for item in result:
    spl = item.split()
    dup = spl[1] + " " + spl[0]
    if dup in result:
        result.remove(dup)
result.insert(0, "be be")   
for item in result:
    print(item)
70, bạn cần một cách để biến danh sách đó trở lại thành một chuỗi. Một lần nữa, bạn có thể sử dụng
Test case 1

Input:-
raju always plays cricket

Output:-
Expected output is
always cricket
cricket raju
plays raju

but coming output is
be be
not be
or be
or to

Test case 2

Input:-
python is a programming language

Output:-
Expected output is
a language
a python
is language
is programming
language python
programming python

but coming output is
be be
not be
or be
or to

Test case 3

Input:-
to be or not to be

Output:-
Expected output is
be be
be not
or to
to to

but coming output is
be be
not be
or be
or to
3 giống như bạn đã làm trong các phần trước:

>>>

Test case 1

Input:-
raju always plays cricket

Output:-
Expected output is
always cricket
cricket raju
plays raju

but coming output is
be be
not be
or be
or to

Test case 2

Input:-
python is a programming language

Output:-
Expected output is
a language
a python
is language
is programming
language python
programming python

but coming output is
be be
not be
or be
or to

Test case 3

Input:-
to be or not to be

Output:-
Expected output is
be be
be not
or to
to to

but coming output is
be be
not be
or be
or to
0

Trong đoạn mã này, bạn gọi

Test case 1

Input:-
raju always plays cricket

Output:-
Expected output is
always cricket
cricket raju
plays raju

but coming output is
be be
not be
or be
or to

Test case 2

Input:-
python is a programming language

Output:-
Expected output is
a language
a python
is language
is programming
language python
programming python

but coming output is
be be
not be
or be
or to

Test case 3

Input:-
to be or not to be

Output:-
Expected output is
be be
be not
or to
to to

but coming output is
be be
not be
or be
or to
3 trên một chuỗi trống, đóng vai trò của một dấu phân cách. Đối số của
Test case 1

Input:-
raju always plays cricket

Output:-
Expected output is
always cricket
cricket raju
plays raju

but coming output is
be be
not be
or be
or to

Test case 2

Input:-
python is a programming language

Output:-
Expected output is
a language
a python
is language
is programming
language python
programming python

but coming output is
be be
not be
or be
or to

Test case 3

Input:-
to be or not to be

Output:-
Expected output is
be be
be not
or to
to to

but coming output is
be be
not be
or be
or to
3 là kết quả của việc gọi
Test case 1

Input:-
raju always plays cricket

Output:-
Expected output is
always cricket
cricket raju
plays raju

but coming output is
be be
not be
or be
or to

Test case 2

Input:-
python is a programming language

Output:-
Expected output is
a language
a python
is language
is programming
language python
programming python

but coming output is
be be
not be
or be
or to

Test case 3

Input:-
to be or not to be

Output:-
Expected output is
be be
be not
or to
to to

but coming output is
be be
not be
or be
or to
4 với
sentence = "to be or not to be"
s = sentence.split()
s.sort()
result = []
for word1 in s:
    for word2 in s:
        result.append(word1 + " " + word2)
       
result = set(result)
result = list(result)
result.sort()


for item in result:
    if item in sentence:
        result.remove(item)
        
for item in result: 
    if " ".join(item.split()[::-1]) in sentence:
        result.remove(item)
        
for item in result:
    spl = item.split()
    if (spl[0] == spl[1]) and (sentence.count(spl[0]) < 2):
        result.remove(item)
        
for item in result:
    spl = item.split()
    dup = spl[1] + " " + spl[0]
    if dup in result:
        result.remove(dup)
result.insert(0, "be be")   
for item in result:
    print(item)
75 như một đối số và
sentence = "to be or not to be"
s = sentence.split()
s.sort()
result = []
for word1 in s:
    for word2 in s:
        result.append(word1 + " " + word2)
       
result = set(result)
result = list(result)
result.sort()


for item in result:
    if item in sentence:
        result.remove(item)
        
for item in result: 
    if " ".join(item.split()[::-1]) in sentence:
        result.remove(item)
        
for item in result:
    spl = item.split()
    if (spl[0] == spl[1]) and (sentence.count(spl[0]) < 2):
        result.remove(item)
        
for item in result:
    spl = item.split()
    dup = spl[1] + " " + spl[0]
    if dup in result:
        result.remove(dup)
result.insert(0, "be be")   
for item in result:
    print(item)
64 được đặt thành
sentence = "to be or not to be"
s = sentence.split()
s.sort()
result = []
for word1 in s:
    for word2 in s:
        result.append(word1 + " " + word2)
       
result = set(result)
result = list(result)
result.sort()


for item in result:
    if item in sentence:
        result.remove(item)
        
for item in result: 
    if " ".join(item.split()[::-1]) in sentence:
        result.remove(item)
        
for item in result:
    spl = item.split()
    if (spl[0] == spl[1]) and (sentence.count(spl[0]) < 2):
        result.remove(item)
        
for item in result:
    spl = item.split()
    dup = spl[1] + " " + spl[0]
    if dup in result:
        result.remove(dup)
result.insert(0, "be be")   
for item in result:
    print(item)
65.

Bạn cũng có thể tận dụng

Test case 1

Input:-
raju always plays cricket

Output:-
Expected output is
always cricket
cricket raju
plays raju

but coming output is
be be
not be
or be
or to

Test case 2

Input:-
python is a programming language

Output:-
Expected output is
a language
a python
is language
is programming
language python
programming python

but coming output is
be be
not be
or be
or to

Test case 3

Input:-
to be or not to be

Output:-
Expected output is
be be
be not
or to
to to

but coming output is
be be
not be
or be
or to
4 để lặp lại thông qua một chuỗi theo thứ tự được sắp xếp và đảo ngược:

>>>

Test case 1

Input:-
raju always plays cricket

Output:-
Expected output is
always cricket
cricket raju
plays raju

but coming output is
be be
not be
or be
or to

Test case 2

Input:-
python is a programming language

Output:-
Expected output is
a language
a python
is language
is programming
language python
programming python

but coming output is
be be
not be
or be
or to

Test case 3

Input:-
to be or not to be

Output:-
Expected output is
be be
be not
or to
to to

but coming output is
be be
not be
or be
or to
1

Đối số

sentence = "to be or not to be"
s = sentence.split()
s.sort()
result = []
for word1 in s:
    for word2 in s:
        result.append(word1 + " " + word2)
       
result = set(result)
result = list(result)
result.sort()


for item in result:
    if item in sentence:
        result.remove(item)
        
for item in result: 
    if " ".join(item.split()[::-1]) in sentence:
        result.remove(item)
        
for item in result:
    spl = item.split()
    if (spl[0] == spl[1]) and (sentence.count(spl[0]) < 2):
        result.remove(item)
        
for item in result:
    spl = item.split()
    dup = spl[1] + " " + spl[0]
    if dup in result:
        result.remove(dup)
result.insert(0, "be be")   
for item in result:
    print(item)
64 cho
Test case 1

Input:-
raju always plays cricket

Output:-
Expected output is
always cricket
cricket raju
plays raju

but coming output is
be be
not be
or be
or to

Test case 2

Input:-
python is a programming language

Output:-
Expected output is
a language
a python
is language
is programming
language python
programming python

but coming output is
be be
not be
or be
or to

Test case 3

Input:-
to be or not to be

Output:-
Expected output is
be be
be not
or to
to to

but coming output is
be be
not be
or be
or to
4 cho phép bạn sắp xếp các phép lặp, bao gồm cả các chuỗi, theo thứ tự giảm dần. Vì vậy, nếu bạn cần một chuỗi các ký tự được sắp xếp theo thứ tự bảng chữ cái ngược, thì
Test case 1

Input:-
raju always plays cricket

Output:-
Expected output is
always cricket
cricket raju
plays raju

but coming output is
be be
not be
or be
or to

Test case 2

Input:-
python is a programming language

Output:-
Expected output is
a language
a python
is language
is programming
language python
programming python

but coming output is
be be
not be
or be
or to

Test case 3

Input:-
to be or not to be

Output:-
Expected output is
be be
be not
or to
to to

but coming output is
be be
not be
or be
or to
4 là dành cho bạn.

Sự kết luận

Đảo ngược và làm việc với các chuỗi theo thứ tự ngược lại có thể là một nhiệm vụ phổ biến trong lập trình. Python cung cấp một tập hợp các công cụ và kỹ thuật có thể giúp bạn thực hiện đảo ngược chuỗi một cách nhanh chóng và hiệu quả. Trong hướng dẫn này, bạn đã tìm hiểu về các công cụ và kỹ thuật đó và cách tận dụng chúng trong các thử thách xử lý chuỗi của bạn.reverse order can be a common task in programming. Python provides a set of tools and techniques that can help you perform string reversal quickly and efficiently. In this tutorial, you learned about those tools and techniques and how to take advantage of them in your string processing challenges.

Trong hướng dẫn này, bạn đã học được cách:

  • Nhanh chóng xây dựng chuỗi đảo ngược thông qua việc cắtslicing
  • Tạo các bản sao đảo ngược của các chuỗi hiện có bằng cách sử dụng
    Test case 1
    
    Input:-
    raju always plays cricket
    
    Output:-
    Expected output is
    always cricket
    cricket raju
    plays raju
    
    but coming output is
    be be
    not be
    or be
    or to
    
    Test case 2
    
    Input:-
    python is a programming language
    
    Output:-
    Expected output is
    a language
    a python
    is language
    is programming
    language python
    programming python
    
    but coming output is
    be be
    not be
    or be
    or to
    
    Test case 3
    
    Input:-
    to be or not to be
    
    Output:-
    Expected output is
    be be
    be not
    or to
    to to
    
    but coming output is
    be be
    not be
    or be
    or to
    
    2 và
    Test case 1
    
    Input:-
    raju always plays cricket
    
    Output:-
    Expected output is
    always cricket
    cricket raju
    plays raju
    
    but coming output is
    be be
    not be
    or be
    or to
    
    Test case 2
    
    Input:-
    python is a programming language
    
    Output:-
    Expected output is
    a language
    a python
    is language
    is programming
    language python
    programming python
    
    but coming output is
    be be
    not be
    or be
    or to
    
    Test case 3
    
    Input:-
    to be or not to be
    
    Output:-
    Expected output is
    be be
    be not
    or to
    to to
    
    but coming output is
    be be
    not be
    or be
    or to
    
    3
    Test case 1
    
    Input:-
    raju always plays cricket
    
    Output:-
    Expected output is
    always cricket
    cricket raju
    plays raju
    
    but coming output is
    be be
    not be
    or be
    or to
    
    Test case 2
    
    Input:-
    python is a programming language
    
    Output:-
    Expected output is
    a language
    a python
    is language
    is programming
    language python
    programming python
    
    but coming output is
    be be
    not be
    or be
    or to
    
    Test case 3
    
    Input:-
    to be or not to be
    
    Output:-
    Expected output is
    be be
    be not
    or to
    to to
    
    but coming output is
    be be
    not be
    or be
    or to
    
    2
    and
    Test case 1
    
    Input:-
    raju always plays cricket
    
    Output:-
    Expected output is
    always cricket
    cricket raju
    plays raju
    
    but coming output is
    be be
    not be
    or be
    or to
    
    Test case 2
    
    Input:-
    python is a programming language
    
    Output:-
    Expected output is
    a language
    a python
    is language
    is programming
    language python
    programming python
    
    but coming output is
    be be
    not be
    or be
    or to
    
    Test case 3
    
    Input:-
    to be or not to be
    
    Output:-
    Expected output is
    be be
    be not
    or to
    to to
    
    but coming output is
    be be
    not be
    or be
    or to
    
    3
  • Sử dụng lặp và đệ quy để tạo chuỗi đảo ngược bằng tayiteration and recursion to create reversed strings by hand
  • Vòng lặp qua các chuỗi của bạn theo thứ tự ngược lại over your strings in reverse order
  • Sắp xếp các chuỗi của bạn theo thứ tự giảm dần bằng cách sử dụng
    Test case 1
    
    Input:-
    raju always plays cricket
    
    Output:-
    Expected output is
    always cricket
    cricket raju
    plays raju
    
    but coming output is
    be be
    not be
    or be
    or to
    
    Test case 2
    
    Input:-
    python is a programming language
    
    Output:-
    Expected output is
    a language
    a python
    is language
    is programming
    language python
    programming python
    
    but coming output is
    be be
    not be
    or be
    or to
    
    Test case 3
    
    Input:-
    to be or not to be
    
    Output:-
    Expected output is
    be be
    be not
    or to
    to to
    
    but coming output is
    be be
    not be
    or be
    or to
    
    4
    Test case 1
    
    Input:-
    raju always plays cricket
    
    Output:-
    Expected output is
    always cricket
    cricket raju
    plays raju
    
    but coming output is
    be be
    not be
    or be
    or to
    
    Test case 2
    
    Input:-
    python is a programming language
    
    Output:-
    Expected output is
    a language
    a python
    is language
    is programming
    language python
    programming python
    
    but coming output is
    be be
    not be
    or be
    or to
    
    Test case 3
    
    Input:-
    to be or not to be
    
    Output:-
    Expected output is
    be be
    be not
    or to
    to to
    
    but coming output is
    be be
    not be
    or be
    or to
    
    4

Mặc dù chủ đề này có thể không có nhiều trường hợp sử dụng thú vị, hiểu cách đảo ngược các chuỗi có thể hữu ích trong các cuộc phỏng vấn mã hóa cho các vị trí cấp nhập cảnh. Bạn cũng sẽ thấy rằng việc làm chủ các cách khác nhau để đảo ngược một chuỗi có thể giúp bạn thực sự khái niệm hóa tính bất biến của các chuỗi trong Python, đây là một tính năng đáng chú ý của ngôn ngữ.