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

+13

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

48 Answers

New Answer

+22

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()

+23

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()

+13

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

+10

file = open("/usercode/files/books.txt", "r") for x in file.readlines(): x = x.strip('\n') a = len(x) b = x[0] print(str(b) + str(a)) file.close()

+7

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 ✌️

+7

Tharyabha Manek - you are kidding us, right? sorry for my comment. the code you did is working. but only exactly for the file as it is. As soon as we have moore books or less books it will crash. this is called hard-coded. I am real sure that you are able to do it in a way, that is working for all number of books in the file.

+6

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 😊

+6

here is my attempt for this exercise: for line in open("/usercode/files/books.txt", "r"): print(f'{line[0]}{len(line.strip())}')

+5

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 😊

+5

Here was my solution, for posterity ;) file = open("/usercode/files/books.txt", "r") #your code goes here listed = file.readlines() relisted = [] for element in listed: relisted.append(element.strip()) for line in relisted: print(line[0] + str(len(line))) file.close()

+4

Richard Villarreal yes

+4

file = open("/usercode/files/books.txt", "r") for line in file: line = line.strip("\n") print(line[0] + str(len(line))) file.close() This one is the easiest

+3

Proven way: https://code.sololearn.com/cPeBWF4tkD8w/?ref=app

+2

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?

+2

Hi eveyone file = open("/usercode/files/books.txt", "r") for line in file.readlines(): print(str(line[0])+str(len(line.strip()))) file.close()

+2

file = open("/usercode/files/books.txt", "r") for line in file: ti=line.replace('\n','') count=len(title) if t in line: print(f'{title[0]}{count}') file.close()

+1

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

+1

Hi Everyone..... 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()

+1

file = open("/usercode/files/books.txt", "r") #your code goes here for line in file: print(line[0]+ str(len(line.strip('\n')))) file.close()

+1

file=open("/user code/files/book.txt","r") ist=[] for i in file: ist.append(i.rstrip("\n")) for i in ist: print(i [0]+str(len(i))) file.close()