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
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()
+ 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 ()
+ 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()
+ 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.
+ 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()
+ 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")
+ 1
file = open("/usercode/files/books.txt", "r")
for i in file:
splits=i.split()
for x in splits:
print(x[0])
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?
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
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()
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
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()
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()
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()
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()
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()
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
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()
- 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()