Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help to modify the code to pass the test case: # For file with only numbers, you should only return one thing ( i

Please help to modify the code to pass the test case:
# For file with only numbers, you should only return one thing(i.e.the first 10 characters)
# For file with story,you NEED to return two things(i.e. a modified string AND a list)
# you can return multiple values by simply return them separated by commas like the following
# def editor(fname):
#
# return 'modifystring', [a,b,c,d]
# HINT: to call your function for the story.txt file, use the following command
# editor("./data/story.txt")
# return statement should be in the format below
# return 'modifystring', [a,b,c,d]
import re
from collections import Counter
def editor(fname):
with open(fname,'r') as file:
content = file.read().strip()
if content.isdigit():
return content[:10]
else:
words = content.split()
modified_words =[word.capitalize() for word in words]
modified_string =''.join(modified_words)
word_counts = Counter(word.lower() for word in words)
top_five =[word for word, count in word_counts.most_common(5)]
return modified_string, top_five
Test Case 1:
import pytest
from week1 import editor
## dont change anything!!!
def test_story1():
new_text, top_five = editor('data/story.txt')
expected=['the','of','and','with','a']
assert set(top_five)==set(expected)
assert new_text =='''the man and his wife,
talking of the latest armed robbery in the suburb, were distracted by the sight of the little boy's pet cat
effortlessly arriving over the seven-foot wall,
descending first with a rapid bracing of extended forepaws down on the sheer vertical surface,
and then a graceful launch, landing with swishing tail within the property.'''

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Visual Basic6 Database Programming

Authors: John W. Fronckowiak, David J. Helda

1st Edition

ISBN: 0764532545, 978-0764532542

More Books

Students also viewed these Databases questions

Question

a valuing of personal and psychological privacy;

Answered: 1 week ago