Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need to rewrite this program so that it reads strings from a . txt file and builds the list courses instead of defining all

I need to rewrite this program so that it reads strings from a .txt file and builds the list courses instead of defining all the data as part of your program itself. The text file is called courses.txtThe file aspect should be the only thing that you change - the rest of the program logic should remain the same.
#table 1
def table1(courses):
print("Table 1")
total =0
for course in courses:
data = course.split()
length = len(data)
print(data[0]+""+ data[1])
total += int(data[length-1])
print("
total: ", total)
#table2
def table2(courses):
print("Table 2")
for course in courses:
dept_code = course[:2]
course_info = course[2:]
print(f"{dept_code}{course_info}")
def table3(courses):
print("Table 3")
total_enrollment =0
for course in courses:
data = course.split()
dept_code = data[0]
course_num = data[1]
enrollment = int(data[-1])
title ="".join(data[2:-1])
# Truncate title to a max length, e.g.,22 characters
max_title_length =22
if len(title)> max_title_length:
title = title[:max_title_length]
print(f"{dept_code}{course_num}{title.ljust(max_title_length)}{enrollment:3d}")
total_enrollment += enrollment
print("Total enrollment:", total_enrollment)
def table4(courses):
print("Table 4")
# Find the length of the longest title
max_title_length = max(len("".join(course.split()[2:-1])) for course in courses)
# Sort courses by title
courses_sorted = sorted(courses, key=lambda x: "".join(x.split()[2:-1]))
total_enrollment =0
for course in courses_sorted:
data = course.split()
dept_code = data[0]
course_num = data[1]
enrollment = int(data[-1])
title ="".join(data[2:-1])
print(f"{dept_code}{course_num}{title.ljust(max_title_length)}{enrollment:3d}")
total_enrollment += enrollment
print("Total enrollment:", total_enrollment)
def main():
courses =[]
courses =['CS 152 Introduction to Python Programming 21',
'CS 369 Operating Systems Adminstration 8',
'CS 365 Computer Networking 19',
'CS 208 Discrete Mathmatics 124',
'CS 319 Computer Architecture 14',
'MA 221 Calculus and Analytical Geometry for Majors I 12',
'MA 311 Linear Algebra 7',
'MA 150 Precalculus Mathmatics 27',
'CS 335 Introduction to Cybersecurity 20',
'IS 361 Data Management Systems 22',
'MG 315 Advanced Business Statistics 6']
courses.append('IT 745 Information Technology 78')
courses.append('CS 316 Cybersecurity Administration 24')
courses.append('CS 130 Developing the User Experience 15)
table1(courses)
table2(courses)
table3(courses)
table4(courses)
#calling main function
main()

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

Logidata+ Deductive Databases With Complex Objects Lncs 701

Authors: Paolo Atzeni

1st Edition

354056974X, 978-3540569749

More Books

Students also viewed these Databases questions

Question

=+9-1 Explain why psychologists are concerned with human biology.

Answered: 1 week ago