Your assignment is to take a list of strings representing (Department code, course number, title, enrollment) and
Question:
Your assignment is to take a list of strings representing (Department code, course number, title, enrollment) and print them in nice tables, one per line. Data looks like
'CS152Introduction to Python Programming21'
or
'CS352Data Structures19'
The department code and course number are always the same size, but the course titles contain different numbers of words and are different lengths. The enrollment numbers will always be between 1 and 999. Unfortunately, when the data was generated, the programmer forgot to add a space between each of the components, so you will have to deal with that in your program.
Look at the string methods in section 9.5 for how to complete this. You may want to look on the web for some additional examples. You do not have to use the .format() method in order to accomplish this lab, nor do you need any techniques from Chapter 10.
For Table 1, print just the department code and course number, separated by a space
Table 1
CS 152
CS 369
...
MG 315
For Table 2, print all the data in the string, with a single space between each piece.
Table 2
CS 152 Introduction to Python Programming 21
CS 369 Operating Systems Administration 8
CS 352 Data Structures 19
CS 208 Discrete Mathematics 124
CS 319 Computer Architecture 14
MA 221 Calculus and Analytical Geometry for Majors I 12
MA 311 Linear Algebra 7
MA 150 Precalculus Mathematics 27
CS 335 Introduction to Cybersecurity 20
IS 361 Data Management Systems 22
MG 315 Advanced Business Statistics 6
For Table3, truncate the titles to the number of characters shown, but watch out for short titles. Make sure the enrollments all line up as shown. Add up all the enrollments and print the total.
Table 3
CS 152 Introduction to Pyth 21
CS 369 Operating Systems Ad 8
CS 352 Data Structures 19
CS 208 Discrete Mathematics 124
CS 319 Computer Architectur 14
MA 221 Calculus and Analyti 12
MA 311 Linear Algebra 7
MA 150 Precalculus Mathemat 27
CS 335 Introduction to Cybe 20
IS 361 Data Management Syst 22
MG 315 Advanced Business St 6
Total: 280
For Table 4, sort the list (this is one statement in Python), and print the complete title, but make sure all the enrollments line up as shown.
Table 4
CS152 Introduction to Python Programming 21
CS208 Discrete Mathematics 124
CS319 Computer Architecture 14
CS335 Introduction to Cybersecurity 20
CS352 Data Structures 19
CS369 Operating Systems Administration 8
IS361 Data Management Systems 22
MA150 Precalculus Mathematics 27
MA221 Calculus and Analytical Geometry for Majors I 12
MA311 Linear Algebra 7
MG315 Advanced Business Statistics 6
For Table 4, make sure your program actually prints the table based on the length of the longest title. You will have to write code to find the length of the longest title, then use that number. For instance, if the data only had CS 208 and MA 311, the table would look like
CS 208 Discrete Mathematics 24
MA 311 Linear Algebra 7
Once your program works with the provided data,
a) add a record to the list to demonstrate your code still computes a total correctly,
b) add a second record to the list to demonstrate your code still sorts the list correctly,
c) add a third record to the list to show that your program always creates a neat table, no matter how long or short the titles are.
I HAVE CODED THIS BUT CAN"T FIGURE OUT HOW TO FORMAT TABLE 4 BASED ON LENGTH OF TITLE. ANY ASSISTANCE WOULD BE HELPFUL
def BreakData(courses):
#List with data in parts
lis = []
# Maximum length of course title
max_len = 0
for acourse in courses:
#checking enrollment, check for 3, 2, 1 the enrollment lies between 1-999
if acourse[-3].isdigit():
enrollment = acourse[-3:]
elif acourse[-2].isdigit():
enrollment = acourse[-2:]
else:
enrollment = acourse[-1]
# Index till which there is course title
ind = len(acourse)-len(enrollment)
#Appending list
#Using slicing, gives the character at index 0,1 ie index <2
lis.append((acourse[:2], acourse[2:5], acourse[5:ind], enrollment))
max_len = max(max_len, len(acourse[5:ind]))
return lis, max_len
def table1(courses):
print("\nTable 1\n")
for acourse in courses:
#Printing department code and course number
print(acourse[:2], acourse[2:5])
print("\n")
def table2(courses):
print("\nTable 2\n")
# Calling BreakData function for divided data
courses, max_len = BreakData(courses)
for acourse in courses:
# Printing allspace seperated data
print(acourse[0], acourse[1], acourse[2], acourse[3])
def table3(courses):
print("\nTable 3\n")
# Calling BreakData function for divided data
courses, max_len = BreakData(courses)
total = 0
for acourse in courses:
#Truncating the course tite, keeping 20 characters
#If length is small, adding spaces
name = (acourse[2] + " "*20)[:20]
total += int(acourse[3])
print('{:<0s}{:>4s}{:>21s}{:>4s}'.format(acourse[0], acourse[1], name, acourse[3]))
#Adding spaces to print total
tot = " "*21 + "Total:" + " "*0
print(tot, str(total))
print("\n")
def table4(courses):
print("\nTable 4\n")
#Sorting list
courses.sort()
# Calling BreakData function for divided data and max_length of course title
courses, max_len = BreakData(courses)
for acourse in courses:
#Adding max_length spaces to make all course title length equa1=l
name = (acourse[2] + " "*max_len)[:max_len]
print('{:>0s}{:>4s}{:>46s}{:>4s}'.format(acourse[0], acourse[1], name, acourse[3]))
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']
#Printing all tables
table1(courses)
table2(courses)
table3(courses)
table4(courses)
#a) add a record to the list to demonstrate your code still computes a total correctly
courses.append('CS377Digital Forensics17')
#b) add a second record to the list to demonstrate your code still sorts the list correctly,
courses.append('CS225Programming Concepts55')
#c) add a third record to the list to show that your program always creates a neat table, no matter how long or short the titles are.
courses.append('MA360Modern Geometries6')
#Printing all tables after adding values
table1(courses)
table2(courses)
table3(courses)
table4(courses)
#Calling main function
main()