0

Title Encoder

I want to know how different people code the same problem. PROBLEM LINK: https://www.sololearn.com/learning/eom-project/1158/1066 PROBLEM: You are given a file named "books.txt" with book titles, each on a separate line. To encode the book titles you need to take the first letters of each word in the title and combine them. For example, for the book title "Game of Thrones" the encoded version should be "GoT". Complete the program to read the book title from the file and output the encoded versions, each on a new line. would u please show your codes for the above problem

25th Mar 2021, 7:45 AM
Mitta Mukesh Kumar
Mitta Mukesh Kumar - avatar
23 ответов
+ 25
file = open("/usercode/files/books.txt", "r") for x in file: title=x.split() for a in title: print(a[0],end='') print() file.close()
16th Apr 2021, 2:27 PM
Sarah Koziol
Sarah Koziol - avatar
+ 3
file = open("/usercode/files/books.txt", "r") for line in file.readlines(): title = line.split() a = "" for c in title: a += c[0] print (a) file.close ()
27th May 2021, 1:26 PM
Christian Baroni
Christian Baroni - avatar
+ 3
f = open(file, 'r') # do not use f.readlines() # that will include new line in the list (bullsh*t) c = f.read().split() # use this to get a clear list :P # get result print("".join([x[0] for x in c])) # always call close method if you are # not using context manager "with..." f.close()
29th Sep 2021, 4:03 PM
_] | [\]
_] | [\] - avatar
+ 2
Mitta Mukesh Kumar Just correct me if I am wrong you wanted to know how others solve this problem in different way it means you don't have any problem in your code then use your activity feed or create code Q&A is to ask programming related questions, code queries hope you understood.
25th Mar 2021, 10:09 AM
R🍁🇮🇳
R🍁🇮🇳 - avatar
+ 2
I have tried something which is quite similar to others. It runs successfully but if anyone has any suggestions on that... It would be great help. file = open("/usercode/files/books.txt", "r") #your code goes here for i in file.readlines(): title = "" for j in i.split(): title += (j[0]) print(title) file.close()
20th Feb 2022, 6:37 PM
Karan Modi
Karan Modi - avatar
+ 2
https://code.sololearn.com/cP3jc80rk9Tr/?ref=app with open("/usercode/files/books.txt", "r") as file: for each_line in file: split_each_title = each_line.split() #split_each_title is a list for each_word in split_each_title: first_letter_of_each_word = each_word[0] print (first_letter_of_each_word,end="") #combine all the first letters of each word (in each title:for loop) print() #To seperate each_line by print("\n")
19th Jun 2022, 11:08 AM
HE Miaomiao
+ 1
file = open("/usercode/files/books.txt", "r") for i in file: splits=i.split() for x in splits: print(x[0])
27th Jul 2021, 6:44 PM
Kasymbekov Ulan Akylovich
Kasymbekov Ulan Akylovich - avatar
25th Mar 2021, 7:45 AM
Mitta Mukesh Kumar
Mitta Mukesh Kumar - avatar
0
No, but if you post your attempt we can help you fix it Mitta Mukesh Kumar not able to get into problem link. Whats the problem?
25th Mar 2021, 7:46 AM
Slick
Slick - avatar
0
this question it have to be something wrong with it this is my code file = open("books.txt") for line in file: if len(line) < 2: continue buffer = "" words = line.split() for word in words: buffer += word[0] print (buffer) file.close() this works for me, if i create a file on my desktop, with same books name, and even be careful in case of some extra '\n' on this file, but it does not work, i think there is something wrong with that file, the first answer on this thread uses "/usercode/files/books.txt" as path, where on that question says that the books.txt file is on /usercode/files/books.txt???????? EDIT: now i use this same code an change the file path, and now it works
26th May 2021, 2:25 PM
Guillermo Plasencia
Guillermo Plasencia - avatar
0
file = open("/usercode/files/books.txt", "r") c = file.readlines() for line in c: x=line.split() for i in x: print(i[0]) file.close()
12th Aug 2021, 10:53 PM
Laston
Laston - avatar
0
Here is what i did and it worked: (with comments for more clarification ) file = open("/usercode/files/books.txt", "r") list=file.readlines() #this is a list called "list" that takes all the lines read from the file ( list[0]=>first line;list[1]=>2nd line ...and so on) for line in list: # for loop to treat each line in the list "list" words=line.split() # split() make a list of words of the corresponding line for i in words: # we treat each word in the line print(i[0]) #we output just the first letter of each word (a string is considered as a list it can be accessed using indexes) file.close() # Finally,it's highly recommanded to close the file
19th Sep 2021, 9:55 PM
Hasnae BOUHMADY
Hasnae BOUHMADY - avatar
0
file = open("/usercode/files/books.txt", "r") for x in file: title=x.split() for a in title: print(a[0],end='') print() file.close()
15th Oct 2021, 4:22 PM
michael
michael - avatar
0
file = open("/usercode/files/books.txt", "r") for x in file: title=x.split() for a in title: print(a[0],end='') print() file.close()
23rd Dec 2021, 2:34 AM
Md. Rakibul Islam
Md. Rakibul Islam - avatar
0
def getfirst(lst): str = '' for i in lst: str += i[0] print(str) file = open("/usercode/files/books.txt", "r") #your code goes here lines = file.readlines() for i in lines: getfirst(i.split()) file.close()
25th Feb 2022, 1:50 PM
MatejSmida
0
Hey,you can try this: file = open("/usercode/files/books.txt", "r") for line in file.readlines(): no = list(line.split(" ")) for i in no: print(i[0]) print("\n") #your code goes here file.close()
13th May 2022, 8:24 AM
Abdul khadar D Jamadar
Abdul khadar D Jamadar - avatar
0
file = open("/usercode/files/books.txt", "r") lines=file.readlines() for i in range(len(lines)): line=lines[i].split() for j in line: print(j[0:1],end="") #your code goes here file.close()
15th May 2022, 6:23 PM
Harsha Sai Ganesh Pabolu
0
abbr = "" for i in file.readlines(): title = i.split(" ") words = 0 for j in title: length = len(title) abbr += title[words][0] if words < length: words+=1 else: words = length abbr += "\n" print(abbr) here's a solution without any shortcuts
13th Jul 2022, 7:16 AM
Andrew G
0
file = open("/usercode/files/books.txt", "r") for line in file: words = line.split() i = "" for word in words: i += word[0] print(i) file.close()
22nd Dec 2022, 5:47 AM
Juan David Ortiz Guevara
Juan David Ortiz Guevara - avatar
- 1
For better understanding file = open("/usercode/files/books.txt", "r") for i in file.readlines(): x=i.split() a="" i=0 for y in x: if i==0: a+=y[i] else: break print (a) #your code goes here file.close()
30th Jul 2021, 1:03 PM
HARSHITH JK
HARSHITH JK - avatar