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 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 for an easy way of fixing this.
Add your test cases from the program in Unit 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
#note: this one confused me a bit but I think I got it
def parsecoursecoursestr:
# Department code and course number are always characters combined eg CS
deptcode coursestr: # First two characters are department letters
coursenum coursestr: # 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 coursestr:
i lenrest
# Move backward in the string to find where nonnumeric characters end
while i and restiisdigit:
i
# Title goes until the last nondigit character
title rest:i strip
# Enrollment is the remaining part
enrollment intresti :
return deptcode, coursenum, title, enrollment
def tablecourses:
printTable
for course in courses:
deptcode, coursenum, title, enrollment parsecoursecourse
printfdeptcodecoursenum
def tablecourses:
print
Table
for course in courses:
deptcode, coursenum, title, enrollment parsecoursecourse
printfdeptcodecoursenumtitleenrollment
def tablecourses:
print
Table
totalenrollment
for course in courses:
deptcode, coursenum, title, enrollment parsecoursecourse
# Truncate title to characters if longer
truncatedtitle title: if lentitle else title
printfdeptcodecoursenumtruncatedtitle:enrollment:
totalenrollment enrollment
printfTotal: totalenrollment
def tablecourses:
print
Table
# Sort courses by department and course number
coursessorted sortedcourses keylambda x: x: x:
# Determine the length of the longest title
maxtitlelength maxlenparsecoursecourse for course in courses
for course in coursessorted:
deptcode, coursenum, title, enrollment parsecoursecourse
printfdeptcodecoursenumtitle:maxtitlelengthenrollment
# Main function to execute tables
def main:
courses
CSIntroduction to Python ProgrammingCSOperating Systems Administration
CSData StructuresCSDiscrete MathematicsCSComputer Architecture
MACalculus and Analytical Geometry for Majors IMALinear Algebra
MAPrecalculus MathematicsCSIntroduction to Cybersecurity
ISData Management SystemsMGAdvanced Business Statistics
PHPhysics for Poets # Additional record to test
tablecourses
tablecourses
tablecourses
tablecourses
# 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
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
