Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

*PYTHON 3 PROGRAM* Displaying a Timeline From a File For this problem, you will develop two small functions that work together: one creates a Python

*PYTHON 3 PROGRAM*

Displaying a Timeline From a File

For this problem, you will develop two small functions that work together: one creates a Python dictionary from the contents of a text file, and the other displays the contents of that dictionary in a particular way.

Start by completing the loadEvents() function, which takes a string argument representing the name of a (plain text) data file. Each line of the file has the following format:

start-time end-time

label where start-time and end-time are integer values between 0 and 47 (inclusive), and start-time is always less than or equal to

end-time. label may consist of one or more words. All fields are separated by exactly one space. For example:

23 32 Meeting with Mary Brown

For each line in the file,

loadEvents()

should create a dictionary item where the key is equal to the label and the value consists of a two-element list with the start-time and end-time values.

For example,

23 32 Meeting with Mary Brown

the event above would produce a key-value pair of the form:

"Meeting with Mary Brown" : [23, 32]

image text in transcribed

After processing the entire file, loadEvents() should return the dictionary that it created. Next, complete the timeline() function, which takes a dictionary as its only argument. This function displays each event from the dictionary, one per line, in any order. The first 48 columns of the line represent the duration of the day, broken up into half-hour increments. Columns that do not fall within the time span of the event (that is, between start-time and end-time) are filled with spaces; columns that are occupied by the event should be filled with asterisks instead. A short distance after the end of the 48 columns, print the event's label.

For example, given the event data:

image text in transcribed

*Here is the required test skeleton that correlates to the programs in the question*

# Complete the functions that follow for this assignment

def loadEvents(filename): events = {} # ADD YOUR CODE HERE return events

def timeline(events): # ADD YOUR CODE HERE return

# DO NOT modify or remove the code below! We will use it for testing.

if __name__ == "__main__":

# Problem 2: Displaying a Timeline From a File fn = input("Enter the name of the timeline data file: ") e = loadEvents(fn) print("Dictionary of events:", e) print() print("Your timeline is:") timeline(e) print()

HINT: If you used split() to divide up the fields of the line, then you may want to use the corresponding join) function to recreate the original event name. In order to omit the starting and ending times from the rejoined string, use the index [2:] to skip the first two elements of your list from split For example, if you previously wrote something like pieces = line.split() you can extract and reconstruct the original event label using code like: label = " " . pieces [ 2 : ]

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_2

Step: 3

blur-text-image_3

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

Fundamentals Of Database Management Systems

Authors: Mark L. Gillenson

2nd Edition

0470624701, 978-0470624708

More Books

Students also viewed these Databases questions

Question

x-3+1, x23 Let f(x) = -*+3, * Answered: 1 week ago

Answered: 1 week ago

Question

Recommend the key methods to improve service productivity.

Answered: 1 week ago