194k views
2 votes
Coach Kyle has some top athletes who have the potential to qualify for Olympics. They have been training very had for the 5000 and 10000 meters running competition. The coach has been recording their timings for each 1000 meters. In the practice sessions, each athlete runs a distance in multiples of 1000 meters and the coach records their time for every 1000 meters. Put the following in a text file called timings.txt. This is not a Python program. Just open up a text editor and copy the following to a file. Alice,3:15,2:45,3:30,2:27,3:42 Bob, 2:25,3:15,3:20,2:57,2:42,3:27 Charlie, 2:45,3:25,3:50,2:27,2:52,3:12 David,2:15,3:35,3:10,2:47 Write a main function that calls read_data, determines the min and max, calls get_average, and prints as below. a) Function read_data will read the file and returns a dictionary with the athlete information. b) Function get_average will accept a list of times and return the average time in the format min:sec. c) Use the split function to split the timings into individual elements of a list. d) Be sure to handle exceptions for files and incorrect data. Athlete Min Alice 2:27 Bob 2:25 Charlie 2:27 David 2:15 Max 3:42 3:27 3:50 3:35 Average 3:07 3:01 3:05 2:56

1 Answer

2 votes

Answer:

Check the explanation

Step-by-step explanation:

Coach Kyle is havaing the following data.

Alice 3:15, 2:45, 3:30, 2:27, 3:42

Bob 2:25, 3:15, 3:20, 2:57, 2:42, 3:27

Charlie 2:45, 3:25, 3:50, 2:27, 2:52, 3:12

David 2:15, 3:35, 3:10, 2:47

let us name it as Athlete. txt

now

1. Use a function to read the data from the file.

To read a file, we can use different methods.

file.read( )

If you want to return a string containing all characters in the file, you can

use file. read().

file = open('Athlete. txt', 'r')

print file. read()

Output:

Alice 3:15, 2:45, 3:30, 2:27, 3:42

Bob 2:25, 3:15, 3:20, 2:57, 2:42, 3:27

Charlie 2:45, 3:25, 3:50, 2:27, 2:52, 3:12

David 2:15, 3:35, 3:10, 2:47

We can also specify how many characters the string should return, by using

file.read(n), where "n" determines number of characters.

This reads the first 5 characters of data and returns it as a string.

file = open('Athlete .txt', 'r')

print file.read(5)

Output:

alice

file. readline( )

The readline() function will read from a file line by line (rather than pulling

the entire file in at once).

Use readline() when you want to get the first line of the file, subsequent calls

to readline() will return successive lines.

Basically, it will read a single line from the file and return a string

containing characters up to .

file = open('athlete .txt', 'r')

print file. readline():

2.Implement the logic using Dictionary, List, and String:

One way to create a dictionary is to start with the empty dictionary and add key-value pairs. The empty dictionary is denoted with a pair of curly braces, {}:

Dictionary operations:

The del statement removes a key-value pair from a dictionary. For example, the following dictionary contains the names of various fruits and the number of each fruit in stock:

>>> inventory = {'apples': 430, 'bananas': 312, 'oranges': 525, 'pears': 217}

>>> print(inventory)

{'oranges': 525, 'apples': 430, 'pears': 217, 'bananas': 312}

If someone buys all of the pears, we can remove the entry from the dictionary:

>>> del inventory['pears']

>>> print(inventory)

{'oranges': 525, 'apples': 430, 'bananas': 312}

Or if we’re expecting more pears soon, we might just change the value associated with pears:

>>> inventory['pears'] = 0

>>> print(inventory)

{'oranges': 525, 'apples': 430, 'pears': 0, 'bananas': 312}

The len function also works on dictionaries; it returns the number of key-value pairs:

>>> len(inventory)

4

The in operator returns True if the key appears in the dictionary and False otherwise:

>>> 'pears' in inventory

True

>>> 'blueberries' in inventory

False

This operator can be very useful, since looking up a non-existant key in a dictionary causes a runtime error:

>>> inventory['blueberries']

Traceback (most recent call last):

File "", line 1, in <module>

KeyError: 'blueberries'

>>>

3. Use maketrans and translate functions to replace commas with spaces

text = text. translate(string. maketrans("",""), string. punctuation)

4. Use split function to split the timings into individual elements of a list

>>> Athlete. split(",")

split reverses by splitting a string into a multi-element list. Note that the delimiter (“,”) is stripped out completely; it does not appear in any of the elements of the returned list.

5. Perform necessary calculations to display min, max, and avg timings of each athlete

_min = None

_max = None

_sum = 0

_len = 0

with open('Athlete .txt') as ff:

for line in ff:

val = int(line. strip())

if _min is None or val < _min:

_min = val

if _max is None or val > _max:

_max = val

_sum += val

_len += 1

_avg = float(_sum) / _len

# Print output

print("Min: %s" % _min)

print("Max: %s" % _max)

print("Avg: %s" % _avg)

for index, row in enumerate(rows):

print "In row %s, Avg = %s, Min = %s" % (index, Avg(row), min(row))

for index, column in enumerate(columns):

print "In column %s, Avg = %s, Min = %s" % (index, Avg(column), min(column))

User CHARAFI Saad
by
5.7k points