Difference between revisions of "CSC111 Lab 10 Addendum 2014"
(Created page with "--~~~~ ---- This code is part of the CSC111 Lab 10 pages. <br /> <source lang="python"> class PlanetList: def __init__( self ): self._li...") |
|||
Line 5: | Line 5: | ||
<br /> | <br /> | ||
<source lang="python"> | <source lang="python"> | ||
+ | # Planets.py | ||
+ | # D. Thiebaut | ||
+ | # This program contains a class PlanetList that holds a collection of planets along | ||
+ | # with their sizes. | ||
+ | # To create an object of this type, simply use the default constructor, as follows: | ||
+ | # | ||
+ | # planets = PlanetList() | ||
+ | # | ||
+ | # The methods supported are: | ||
+ | # | ||
+ | # toString(): returns a string containing all the planets | ||
+ | # addNewPlanetFromInput(): no arguments needed. The method prompts the user for | ||
+ | # a planet name and diameter (in km). | ||
+ | # getNoOfPlanets(): returns the number of planets | ||
+ | # isEmpty(): returns True if the list is empty, False otherwise | ||
+ | # getSmallest(): returns the name of the planet with the smallest diameter | ||
+ | # getLargest(): returns the name of the planet with the largest diameter | ||
+ | # | ||
+ | # ------------------------------------------------------------------------------------------------------ | ||
class PlanetList: | class PlanetList: |
Revision as of 08:34, 8 April 2014
--D. Thiebaut (talk) 09:29, 8 April 2014 (EDT)
This code is part of the CSC111 Lab 10 pages.
# Planets.py
# D. Thiebaut
# This program contains a class PlanetList that holds a collection of planets along
# with their sizes.
# To create an object of this type, simply use the default constructor, as follows:
#
# planets = PlanetList()
#
# The methods supported are:
#
# toString(): returns a string containing all the planets
# addNewPlanetFromInput(): no arguments needed. The method prompts the user for
# a planet name and diameter (in km).
# getNoOfPlanets(): returns the number of planets
# isEmpty(): returns True if the list is empty, False otherwise
# getSmallest(): returns the name of the planet with the smallest diameter
# getLargest(): returns the name of the planet with the largest diameter
#
# ------------------------------------------------------------------------------------------------------
class PlanetList:
def __init__( self ):
self._list = [("Earth", 12756 ),
("Venus", 12104 ),
("Mercury", 4880 ),
("Moon", 3476 ) ]
def toString( self ):
justNames = [ name for name,diameter in self._list ]
return ", ".join( justNames )
def addNewPlanetFromInput( self ):
name = input( "What is the name of the new planet? " )
name = name.strip().capitalize()
justNames = [ name for name,diameter in self._list ]
if name in justNames:
print( "This planet is alread in the list!" )
return
diameter = int( input( "What is the diameter of this planet? " ) )
self._list.append( (name, diameter) )
def getNoOfPlanets( self ):
return len( self._list )
def isEmpty( self ):
return len( self._list )==0
def getSmallest( self ):
if self.getNoOfPlanets() == 0:
return None
listOrderedBySize = [ (diam, name) for name, diam in self._list ]
listOrderedBySize.sort()
# return the name (2nd argument) of the first in list
return listOrderedBySize[0][1]
def getLargest( self ):
if self.getNoOfPlanets() == 0:
return None
listOrderedBySize = [ (diam, name) for name, diam in self._list ]
listOrderedBySize.sort()
listOrderedBySize.reverse()
# return the name (2nd argument) of the first in list
return listOrderedBySize[0][1]