Difference between revisions of "CSC111 Homework 10 Solution"
(Created page with '--~~~~ ---- <source lang="python"> """ hw10a.py Kristina Fedorenko Edited by D. Thiebaut 111c-aq Creates a class to keep information about a student. The class keeps track of …') |
|||
Line 166: | Line 166: | ||
</source> | </source> | ||
+ | |||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | [[Category:CSC111]][[Category:Python]][[Category:Homework]] |
Revision as of 15:13, 19 April 2010
--D. Thiebaut 19:12, 19 April 2010 (UTC)
"""
hw10a.py
Kristina Fedorenko
Edited by D. Thiebaut
111c-aq
Creates a class to keep information about a student.
The class keeps track of the student's name, account number,
password, and email address.
Object issued from this class have methods to
- print the student information (everything we know about a student)
- create a new student with all the information
- get the name of a student
- get the last name of the student <<<==== (new)
- set the name of the student
- get the account number
- set the account number
- get the password
- get the email
- set the email of the student
"""
import os
#--------------------------------------------------------
# original list of students
#--------------------------------------------------------
text1="""
111c-ca, Liz Cloud, xxyzzyK, lcloud@email.smith.edu
111c-cb, Fiona Poory, ahlhla, fpoory@email.smith.edu
111c-ck, Alex Doe, aluoiid, alexDoe@email.smith.edu
"""
class Student:
"""A class containing student's info"""
def __init__(self, account, name, passw, email):
"""constructor"""
self.account = account
self.name = name
self.passw = passw
self.email = email
def printAll(self):
"""prints out all contained info, with headers"""
print "account: %-6s name: %-13s pw: %-7s email: %s" %\
(self.account, self.name, self.passw, self.email)
def getEmail(self):
"""returns student's email"""
return self.email
def setEmail(self, newEmail):
"""changes student's email"""
self.email = newEmail
def getName(self):
"""returns student's name"""
return self.name
def setName(self, newName):
"""changes student's name"""
self.name = newName
def getLastName(self):
"""returns student's last name"""
lastname = self.name
lastname = lastname.split()[1]
return lastname
#--------------------------------------------------------
# parseInput: you are not allowed to change the
# code of this function
#--------------------------------------------------------
def parseInput( text ):
""" parses the original text and creates a list of student objects"""
lines = text.split( "\n" )
List = []
for line in lines:
words = line.split( ',' )
if len( words )==4:
(account, name, passw, email) = words
#--- create a new object and add it to the list of objects ---
List.append( Student( account.strip(), name.strip(),\
passw.strip(), email.strip() ) )
return List
#--------------------------------------------------------
# students_Sort: sort students alphabetically,
# by their last name.
# (Note from DT: we haven't see List comprehension
# yet, but the code below gives you an *example* of
# how to create a list out of another list very quickly!)
#--------------------------------------------------------
def students_Sort(List):
NewL = [ (stu.getLastName(), stu ) for stu in List ]
NewL.sort()
return [ stu for n, stu in NewL ]
#--------------------------------------------------------
# main
#--------------------------------------------------------
def main():
""" creates a list of students, displays the list, changes the email
address to reflect new change """
#--- create a list of student objects---
List = parseInput( text )
#--- display the information for all the students in the list ---
print "Original List"
for stu in List:
stu.printAll()
#--- Smith college decided to change its email address
#--- from "email.smith.edu" to "smithcollege.edu",
#--- so all the email addresses currently on record for students
#--- have to change...
#--- change all email addresses ---
for stu in List:
email = stu.getEmail().replace( "email.smith.edu", "smithcollege.edu" )
stu.setEmail( email )
#--- show the list again ---
print "\n\nNew List after change of email address"
for stu in List:
stu.printAll()
#--- user Fiona Poory has changed her last name to Litmus...
# her record must be changed ---
for stu in List:
if stu.getName().find( "Fiona" )!= -1 and\
stu.getName().find( "Poory" )!=-1:
stu.setName( "Fiona Litmus" )
email = stu.getEmail()
email = email.replace( "fpoory", "flitmus" )
stu.setEmail( email )
#--- show the list again ---
print "\n\nNew List after change of name"
for stu in List:
stu.printAll()
#--- a new student has just joined Smith ---
List.append( Student( "111c-bz", "Rachel Maddow",\
"msnbcms", "rmaddow@smithcollege.edu" ) )
#--- show the list again ---
print "\n\nNew List after addition of student"
for stu in List:
stu.printAll()
SortedStu = students_Sort(List)
print "\n\nStudents sorted alphabetically"
for stu in SortedStu:
stu.printAll()
main()