Is there no better way of writing this code without a double for loop as this will have a high time complexity of O(n^2) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Is there no better way of writing this code without a double for loop as this will have a high time complexity of O(n^2)

To make this code more efficient

4th Feb 2022, 8:56 AM
Adewale Isaac
Adewale Isaac - avatar
2 Answers
+ 3
You want to read book titles from a file and create an abbreviation of each title by joining the first letter of every word in the title. Is that correct? Given that each title is on a separate line you could read the file line by line instead of all at once. That way you wouldn't have to loop through the book titles later. Instead you could do this: Read a title (i.e. a line). Create abbreviation. Repeat.
4th Feb 2022, 1:54 PM
Simon Sauter
Simon Sauter - avatar
0
file = open("/usercode/files/books.txt", "r") books = file.readlines() file.close() print("\n".join(["".join([words[0] for words in book.split()]) for book in books]))
4th Feb 2022, 8:56 AM
Adewale Isaac
Adewale Isaac - avatar