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
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.
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]))