Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write Python expressions corresponding to these statements: 4.22 Write a function month() that takes a number between 1 and 12 as input and returns the

Write Python expressions corresponding to these statements:

4.22 Write a function month() that takes a number between 1 and 12 as input and returns the three-character abbreviation of the corresponding month. Do this without using an if statement, just string operations. Hint: Use a string to store the abbreviations in order.

>>> month(1)

'Jan'

>>> month(11)

'Nov'

4.23 Write a function average() that takes no input but requests that the user enter a sentence. Your function should return the average length of a word in the sentence.

>>> average()

Enter a sentence: A sample sentence

5.0

4.25 Write function vowelCount() that takes a string as input and counts and prints the number of occurrences of vowels in the string.

>>> vowelCount('Le Tour de France')

a, e, i, o, and u appear, respectively, 1, 3, 0, 1, 1 times.

4.28 Implement function links() that takes as input the name of an HTML file (as a string) and returns the number of hyperlinks in that file. To do this you will assume that each hyperlink appears in an anchor tag. You also need to know that every anchor tag ends with the substring <\a>. Test your code on HTML file twolinks.html or any HTML file downloaded from the web into the folder where your program is.

>>> links('twolinks.html')

2

4.29 Write a function stats() that takes one input argument: the name of a text file. The function should print, on the screen, the number of lines, words, and characters in the file; your function should open the file only once.

>>>stats('example.txt')

line count: 3

word count: 20

character count: 98

4.30 Implement function distribution() that takes as input the name of a file (as a string). This one-line file will contain letter grades separated by blanks. Your function should print the distribution of grades, as shown.

>>> distribution('grades.txt')

6 students got A

2 students got A-

3 students got B+

2 students got B

2 students got B-

4 students got C

1 student got C-

2 students got F

4.31 Implement function duplicate() that takes as input a string and the name of a file in the current directory and returns True if the file contains duplicate words and False otherwise.

>>> duplicate('Duplicates.txt')

True

>>> duplicate('noDuplicates.txt')

False

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

Database Processing Fundamentals Design

Authors: Marion Donnie Dutton Don F. Seaman

14th Edition Globel Edition

1292107634, 978-1292107639

More Books

Students also viewed these Databases questions