Định nghĩa vấn đề
Tạo một chương trình Python để đảo ngược một câu.
Thuật toán
- Lấy một chuỗi làm đầu vào.
- Chuyển đổi câu thành một danh sách các từ.
- Tham gia danh sách theo thứ tự ngược lại mà cuối cùng là câu đảo ngược.
Chương trình
sentence = "dread it run from it destiny still arrives"
word_list = sentence.split[]
reversed_list = word_list[:: -1]
reversed_sentence = " ".join[reversed_list]
print[reversed_sentence]
Đầu ra
arrives still destiny it from run it dread
Chương trình này có thể được nén thêm.
sentence = "dread it run from it destiny still arrives"
print[" ".join[sentence.split[][::-1]]]
Đầu ra
arrives still destiny it from run it dread
Chương trình này có thể được nén thêm.
sentence = "dread it run from it destiny still arrives"
word_list = sentence.split[]
reversed_list = reversed[word_list]
reversed_sentence = " ".join[reversed_list]
print[reversed_sentence]
Danh sách Python có thể được đảo ngược bằng phương pháp arrives still destiny it from run it dread
2, có thể được sử dụng thay cho arrives still destiny it from run it dread
3 trong chương trình như sau.
arrives still destiny it from run it dread
arrives still destiny it from run it dread
sentence = input["Enter a sentence :"]
print[" ".join[reversed[sentence.split[]]]]
Đầu ra
Enter a sentence :This is an input
input an is This
Chương trình này có thể được nén thêm.
Giải pháp này sử dụng cùng một quy trình nhưng có các phương thức khác nhau để đảo ngược các từ trong chuỗi với phép lặp trợ giúp ngược và mô -đun biểu thức chính quy. Sau đây là các bước của cách tiếp cận của chúng tôi:
Tìm tất cả các từ trong chuỗi với chức năng trợ giúp của hàm re.findall []. & Nbsp;
Lặp qua danh sách theo cách lạc hậu. & Nbsp;
Examples:
Input : str =" geeks quiz practice code" Output : str = code practice quiz geeks Input : str = "my name is laxmi" output : str= laxmi is name my
Tham gia các từ trong một chuỗi với hàm trợ giúp của tham gia [].
Python3
Enter a sentence :This is an input
input an is This
9 Input : str =" geeks quiz practice code" Output : str = code practice quiz geeks Input : str = "my name is laxmi" output : str= laxmi is name my0
arrives still destiny it from run it dread
6 arrives still destiny it from run it dread
7Các
arrives still destiny it from run it dread
0sentence = "dread it run from it destiny still arrives"
word_list = sentence.split[]
reversed_list = reversed[word_list]
reversed_sentence = " ".join[reversed_list]
print[reversed_sentence]
5arrives still destiny it from run it dread
5 sentence = "dread it run from it destiny still arrives"
word_list = sentence.split[]
reversed_list = reversed[word_list]
reversed_sentence = " ".join[reversed_list]
print[reversed_sentence]
2code practice quiz geeks3__
arrives still destiny it from run it dread
0sentence = input["Enter a sentence :"]
print[" ".join[reversed[sentence.split[]]]]
2 sentence = input["Enter a sentence :"]
print[" ".join[reversed[sentence.split[]]]]
3arrives still destiny it from run it dread
2arrives still destiny it from run it dread
3arrives still destiny it from run it dread
4arrives still destiny it from run it dread
5Đầu ra
code practice quiz geeks
Độ phức tạp về thời gian: O [n], trong đó n là độ dài của không gian chuỗi chuỗi O[n], where n is the length of the string
Auxiliary Space: O[n], where n is the length of the string
Chúng ta được cung cấp một chuỗi và chúng ta cần đảo ngược các từ của một chuỗi đã cho? Ví dụ:
Input : str = geeks quiz practice code Output : str = code practice quiz geeks
Vấn đề này có một giải pháp hiện có, vui lòng giới thiệu các từ ngược trong một liên kết chuỗi đã cho. Chúng tôi sẽ giải quyết vấn đề này trong Python. Đưa ra dưới đây là các bước cần tuân thủ để giải quyết vấn đề này.
- Tách từng từ trong một chuỗi đã cho bằng phương thức Split [] của Kiểu dữ liệu chuỗi trong Python.
- Đảo ngược danh sách phân tách từ.
- In các từ của danh sách, ở dạng chuỗi sau khi nối từng từ với không gian bằng phương thức.
Implementation:
Python3
arrives still destiny it from run it dread
6 arrives still destiny it from run it dread
7arrives still destiny it from run it dread
0arrives still destiny it from run it dread
9arrives still destiny it from run it dread
5 sentence = "dread it run from it destiny still arrives"
word_list = sentence.split[]
reversed_list = reversed[word_list]
reversed_sentence = " ".join[reversed_list]
print[reversed_sentence]
1sentence = "dread it run from it destiny still arrives"
word_list = sentence.split[]
reversed_list = reversed[word_list]
reversed_sentence = " ".join[reversed_list]
print[reversed_sentence]
2sentence = "dread it run from it destiny still arrives"
word_list = sentence.split[]
reversed_list = reversed[word_list]
reversed_sentence = " ".join[reversed_list]
print[reversed_sentence]
3arrives still destiny it from run it dread
0sentence = "dread it run from it destiny still arrives"
word_list = sentence.split[]
reversed_list = reversed[word_list]
reversed_sentence = " ".join[reversed_list]
print[reversed_sentence]
5arrives still destiny it from run it dread
5 sentence = "dread it run from it destiny still arrives"
word_list = sentence.split[]
reversed_list = reversed[word_list]
reversed_sentence = " ".join[reversed_list]
print[reversed_sentence]
2sentence = "dread it run from it destiny still arrives"
word_list = sentence.split[]
reversed_list = reversed[word_list]
reversed_sentence = " ".join[reversed_list]
print[reversed_sentence]
8sentence = "dread it run from it destiny still arrives"
word_list = sentence.split[]
reversed_list = reversed[word_list]
reversed_sentence = " ".join[reversed_list]
print[reversed_sentence]
9sentence = input["Enter a sentence :"]
print[" ".join[reversed[sentence.split[]]]]
0arrives still destiny it from run it dread
0sentence = input["Enter a sentence :"]
print[" ".join[reversed[sentence.split[]]]]
2 sentence = input["Enter a sentence :"]
print[" ".join[reversed[sentence.split[]]]]
3sentence = input["Enter a sentence :"]
print[" ".join[reversed[sentence.split[]]]]
4 sentence = input["Enter a sentence :"]
print[" ".join[reversed[sentence.split[]]]]
5arrives still destiny it from run it dread
5arrives still destiny it from run it dread
5 sentence = input["Enter a sentence :"]
print[" ".join[reversed[sentence.split[]]]]
8sentence = input["Enter a sentence :"]
print[" ".join[reversed[sentence.split[]]]]
9arrives still destiny it from run it dread
0Enter a sentence :This is an input
input an is This
1 arrives still destiny it from run it dread
5 Enter a sentence :This is an input
input an is This
3arrives still destiny it from run it dread
0arrives still destiny it from run it dread
2 Enter a sentence :This is an input
input an is This
6Enter a sentence :This is an input
input an is This
1Enter a sentence :This is an input
input an is This
8Đầu ra
code practice quiz geeks
Độ phức tạp về thời gian: O [n], trong đó n là độ dài của không gian chuỗi chuỗi O[n], where n is the length of the string
Auxiliary Space: O[n], where n is the length of the string
Chúng ta được cung cấp một chuỗi và chúng ta cần đảo ngược các từ của một chuỗi đã cho? Ví dụ:
- Vấn đề này có một giải pháp hiện có, vui lòng giới thiệu các từ ngược trong một liên kết chuỗi đã cho. Chúng tôi sẽ giải quyết vấn đề này trong Python. Đưa ra dưới đây là các bước cần tuân thủ để giải quyết vấn đề này.
- Tách từng từ trong một chuỗi đã cho bằng phương thức Split [] của Kiểu dữ liệu chuỗi trong Python.
- Đảo ngược danh sách phân tách từ.
Python3
In các từ của danh sách, ở dạng chuỗi sau khi nối từng từ với không gian bằng phương thức.
arrives still destiny it from run it dread
6 arrives still destiny it from run it dread
7arrives still destiny it from run it dread
0arrives still destiny it from run it dread
9arrives still destiny it from run it dread
5 sentence = "dread it run from it destiny still arrives"
word_list = sentence.split[]
reversed_list = reversed[word_list]
reversed_sentence = " ".join[reversed_list]
print[reversed_sentence]
1sentence = "dread it run from it destiny still arrives"
word_list = sentence.split[]
reversed_list = reversed[word_list]
reversed_sentence = " ".join[reversed_list]
print[reversed_sentence]
2sentence = "dread it run from it destiny still arrives"
word_list = sentence.split[]
reversed_list = reversed[word_list]
reversed_sentence = " ".join[reversed_list]
print[reversed_sentence]
3arrives still destiny it from run it dread
0sentence = "dread it run from it destiny still arrives"
word_list = sentence.split[]
reversed_list = reversed[word_list]
reversed_sentence = " ".join[reversed_list]
print[reversed_sentence]
5arrives still destiny it from run it dread
5 sentence = "dread it run from it destiny still arrives"
word_list = sentence.split[]
reversed_list = reversed[word_list]
reversed_sentence = " ".join[reversed_list]
print[reversed_sentence]
2sentence = "dread it run from it destiny still arrives"
word_list = sentence.split[]
reversed_list = reversed[word_list]
reversed_sentence = " ".join[reversed_list]
print[reversed_sentence]
8sentence = "dread it run from it destiny still arrives"
word_list = sentence.split[]
reversed_list = reversed[word_list]
reversed_sentence = " ".join[reversed_list]
print[reversed_sentence]
9sentence = input["Enter a sentence :"]
print[" ".join[reversed[sentence.split[]]]]
0arrives still destiny it from run it dread
0sentence = input["Enter a sentence :"]
print[" ".join[reversed[sentence.split[]]]]
2 sentence = input["Enter a sentence :"]
print[" ".join[reversed[sentence.split[]]]]
3sentence = input["Enter a sentence :"]
print[" ".join[reversed[sentence.split[]]]]
4 sentence = input["Enter a sentence :"]
print[" ".join[reversed[sentence.split[]]]]
5arrives still destiny it from run it dread
5arrives still destiny it from run it dread
5 sentence = input["Enter a sentence :"]
print[" ".join[reversed[sentence.split[]]]]
8sentence = input["Enter a sentence :"]
print[" ".join[reversed[sentence.split[]]]]
9arrives still destiny it from run it dread
0Enter a sentence :This is an input
input an is This
1 arrives still destiny it from run it dread
5 Enter a sentence :This is an input
input an is This
3arrives still destiny it from run it dread
0arrives still destiny it from run it dread
2 Enter a sentence :This is an input
input an is This
6Enter a sentence :This is an input
input an is This
1Enter a sentence :This is an input
input an is This
8Đầu ra
code practice quiz geeks
Giải pháp này sử dụng cùng một quy trình nhưng có các phương thức khác nhau để đảo ngược các từ trong chuỗi với phép lặp trợ giúp ngược và mô -đun biểu thức chính quy. Sau đây là các bước của cách tiếp cận của chúng tôi:Shashank Mishra [Gullu]. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks.