Computer Science, asked by youngs12344pchf4s, 5 months ago


Write a function that counts and display the number of 5 letter words in a
text file “Sample.txt"

Answers

Answered by allysia
9

Answer:

It goes like:

\\\tt de f  \ count(): \\\tt {\qquad f\ =ope n("Sample.txt",'r')} \\\tt {\qquad count=0 } \\\tt {\qquad for\ line\ in\ f: } \\\ttt {\qquad {\qquad words=line.split()}} \\\tt {\qquad {\qquad for\  i \ in \ words: }} \\\tt {\qquad {\qquad {\qquad if \ len(i)==5:}}} \\\tt {\qquad {\qquad {\qquad {\qquad print(i)}}}} \\\tt {\qquad {\qquad {\qquad {\qquad count+=1 }}}} \\\tt {\qquad print("The\ number\ of\ 5\ lettered\ words\ is", count)}

Answered by jai696
3

\huge\red{\mid{\fbox{\tt{Using\: Python\: 3}}\mid}}

def count(file):

with open(file) as f:

words = [word for line in f for word in line.split()]

five_letter_words = [word for word in words if len(word) == 5]

print("\n".join(five_letter_words))

return f"No of 5 letter words is {len(five_letter_words)}"

print(count("sample.txt"))

\large\mathsf\color{lightgreen}useful?\: \color{white}\longrightarrow\: \color{orange}brainliest!

Similar questions