Simpler way? [SOLVED] | SoloLearn: Learn to code for FREE!


Simpler way? [SOLVED]

I just completed a code coach problem but I feel like I am over complicating it. Is there a simpler way to accomplish the task than how I have? PROBLEM: You have been asked to make a special book categorization program, which assigns each book a special code based on its title. The code is equal to the first letter of the book, followed by the number of characters in the title. For example, for the book "Harry Potter", the code would be: H12, as it contains 12 characters (including the space). You are provided a books.txt file, which includes the book titles, each one written on a separate line. Read the title one by one and output the code for each book on a separate line. For example, if the books.txt file contains: Some book Another book Your program should output: S9 A12 Recall the readlines() method, which returns a list containing the lines of the file. Also, remember that all lines, except the last one, contain a \n at the end, which should not be included in the character count. MY SOLUTION: file = open("/usercode/files/books.txt", "r") #your code goes here fileLines = [] for lines in file: fileLines.append(lines) length = 1 for n in fileLines: if length == len(fileLines): print(n[0] + str(len(n))) else: print(n[0] + str(len(n)-1)) length += 1 file.close() I am using len(n) - 1 because there is an extra space (“ “) on all of the lines except for the last line. I opted not to use the readlines() method, but maybe I should have? Thank you for the help!

10/27/2020 7:36:04 PM

Richard Villarreal

12 Answers

New Answer


I think the problem specifically wants you to use readlines, so Lothar solution is better, but this works too... --- file = open("/usercode/files/books.txt", "r") for line in file: title=line.replace('\n','') count=len(title) print(f'{title[0]}{count}') file.close()


Usually there are many ways to solve a single problem. Simpler way doesn't always mean more clear. Choose the one that works for you ✌️


A short way to do it is like this: file = open("/usercode/files/books.txt", "r") for line in file.readlines(): print(f'{line[0]}{len(line.strip())}') file.close()


Instead of dealing with \n we can simply remove it with the help of strip() file = open("/usercode/files/books.txt", "r") #your code goes here books = file.readlines() books = [x.strip() for x in books] for book in books: print(book[0]+str(len(book))) file.close() Hope It Helps You 😊


Richard Villarreal strip function just remove trailing and leading characters. Like "hello World".strip("hed") >>>llo Worl #h,e,d are removed from starting and ending By default strip function remove whitespace which includes \n(newline) "Harry Potter\n".strip() >>>Harry Potter Hope It Helps You 😊


Yup I did this way ,just used readlines() similar to what your fileLines list holds after looping over lines in file, and then checked if last character in line contains "\n" or not , for lines in file.readlines(): if lines[-1]=="\n": print(lines[0]+str(len(lines)-1)) else: print(lines[0]+str(len(lines)))


Richard Villarreal yes


Proven way:


Awesome! Thank you for the reply. I have not seen a negative number in the brackets before and just want to make sure I understand. lines[-1] checks the last character of the current line?


Hacker Badshah I have not encountered the strip function yet. What exactly does it strip from the books list?


VB s kbkzb. Kgs pzvso l. Dk bsbvs nviz qox dkhnz kz nnj ???????


Thank you all for the input. I appreciate the help!