-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreversestringbymultithreading.py
More file actions
33 lines (25 loc) · 1011 Bytes
/
reversestringbymultithreading.py
File metadata and controls
33 lines (25 loc) · 1011 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import threading
def reverse_string(string):
return string[::-1]
def reverse_paragraph(paragraph):
# Split the paragraph into a list of strings
strings = paragraph.split()
# Define a list to hold the results
results = [''] * len(strings)
# Define a function that reverses a single string and stores the result in the results list
def reverse_string_and_store_result(i):
results[i] = reverse_string(strings[i])
# Create a thread for each string and start them all simultaneously
threads = []
for i in range(len(strings)):
thread = threading.Thread(target=reverse_string_and_store_result, args=(i,))
threads.append(thread)
thread.start()
# Wait for all threads to complete before returning the results
for thread in threads:
thread.join()
# Join the reversed strings back into a paragraph
return ' '.join(results)
paragraph =input()
reversed_paragraph = reverse_paragraph(paragraph)
print(reversed_paragraph)