Question: Rewrite your program from Unit 5 so that it reads strings from the file and builds the list courses instead of defining all the data

Rewrite your program from Unit 5 so that it reads strings from the file and builds the list courses instead of defining all the data as part of your program itself.
The file aspect should be the only thing that you change - the rest of the program logic should remain the same.
Note that when reading from a file, each line contains a newline character,
, which will cause your table to be double spaced. See the string method .strip() in Chapter 9.5 for an easy way of fixing this.
Add your test cases from the program in Unit 5 at the end of the text file.
You can edit the text file with Notepad, or IDLE, or Thonny or any text editor on your system.
#Nikolaus Albers 9/14/2024
#note: this one confused me a bit but I think I got it
def parse_course(course_str):
# Department code and course number are always 5 characters combined (e.g., CS152)
dept_code = course_str[:2] # First two characters are department letters
course_num = course_str[2:5] # Next three characters are course number
# Remaining string contains title and enrollment
# We find where the enrollment starts by detecting where numbers start appearing at the end
rest = course_str[5:]
i = len(rest)-1
# Move backward in the string to find where non-numeric characters end
while i >=0 and rest[i].isdigit():
i -=1
# Title goes until the last non-digit character
title = rest[:i +1].strip()
# Enrollment is the remaining part
enrollment = int(rest[i +1:])
return (dept_code, course_num, title, enrollment)
def table1(courses):
print("Table 1")
for course in courses:
dept_code, course_num, title, enrollment = parse_course(course)
print(f"{dept_code}{course_num}")
def table2(courses):
print("
Table 2")
for course in courses:
dept_code, course_num, title, enrollment = parse_course(course)
print(f"{dept_code}{course_num}{title}{enrollment}")
def table3(courses):
print("
Table 3")
total_enrollment =0
for course in courses:
dept_code, course_num, title, enrollment = parse_course(course)
# Truncate title to 20 characters if longer
truncated_title =(title[:20]+'...') if len(title)>20 else title
print(f"{dept_code}{course_num}{truncated_title:<23}{enrollment:>3}")
total_enrollment += enrollment
print(f"Total: {total_enrollment}")
def table4(courses):
print("
Table 4")
# Sort courses by department and course number
courses_sorted = sorted(courses, key=lambda x: (x[:2], x[2:5]))
# Determine the length of the longest title
max_title_length = max(len(parse_course(course)[2]) for course in courses)
for course in courses_sorted:
dept_code, course_num, title, enrollment = parse_course(course)
print(f"{dept_code}{course_num}{title:<{max_title_length}}{enrollment}")
# Main function to execute tables
def main():
courses =[
'CS152Introduction to Python Programming21','CS369Operating Systems Administration8',
'CS352Data Structures19','CS208Discrete Mathematics124','CS319Computer Architecture14',
'MA221Calculus and Analytical Geometry for Majors I12','MA311Linear Algebra7',
'MA150Precalculus Mathematics27','CS335Introduction to Cybersecurity20',
'IS361Data Management Systems22','MG315Advanced Business Statistics6',
'PH100Physics for Poets5' # Additional record to test
]
table1(courses)
table2(courses)
table3(courses)
table4(courses)
# Run the main function to see all the tables
main()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!