Difference between revisions of "CSC111 Lab 10 Addendum 2014"
(One intermediate revision by the same user not shown) | |||
Line 37: | Line 37: | ||
("Moon", 3476 ) ] | ("Moon", 3476 ) ] | ||
− | def | + | def __str__( self ): |
+ | # returns a string version of the object | ||
justNames = [ name for name,diameter in self._list ] | justNames = [ name for name,diameter in self._list ] | ||
return ", ".join( justNames ) | return ", ".join( justNames ) | ||
def addNewPlanetFromInput( self ): | def addNewPlanetFromInput( self ): | ||
+ | # gets information from the keyboard and adds it to the list | ||
name = input( "What is the name of the new planet? " ) | name = input( "What is the name of the new planet? " ) | ||
name = name.strip().capitalize() | name = name.strip().capitalize() | ||
+ | |||
+ | # make sure we don't already have this planet | ||
justNames = [ name for name,diameter in self._list ] | justNames = [ name for name,diameter in self._list ] | ||
if name in justNames: | if name in justNames: | ||
Line 49: | Line 53: | ||
return | return | ||
diameter = int( input( "What is the diameter of this planet? " ) ) | diameter = int( input( "What is the diameter of this planet? " ) ) | ||
+ | |||
+ | # add new planet to the list | ||
self._list.append( (name, diameter) ) | self._list.append( (name, diameter) ) | ||
def getNoOfPlanets( self ): | def getNoOfPlanets( self ): | ||
+ | # returns the number of planets held in the list | ||
return len( self._list ) | return len( self._list ) | ||
def isEmpty( self ): | def isEmpty( self ): | ||
+ | # returns True if the list is empty, False otherwise. | ||
return len( self._list )==0 | return len( self._list )==0 | ||
def getSmallest( self ): | def getSmallest( self ): | ||
+ | # returns the smallest planet or None if empty list | ||
if self.getNoOfPlanets() == 0: | if self.getNoOfPlanets() == 0: | ||
return None | return None | ||
Line 67: | Line 76: | ||
def getLargest( self ): | def getLargest( self ): | ||
+ | # returns the largest planet, or None if empty list | ||
if self.getNoOfPlanets() == 0: | if self.getNoOfPlanets() == 0: | ||
return None | return None |
Latest revision as of 07:17, 10 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 planet names (not the sizes)
#
# 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 __str__( self ):
# returns a string version of the object
justNames = [ name for name,diameter in self._list ]
return ", ".join( justNames )
def addNewPlanetFromInput( self ):
# gets information from the keyboard and adds it to the list
name = input( "What is the name of the new planet? " )
name = name.strip().capitalize()
# make sure we don't already have this planet
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? " ) )
# add new planet to the list
self._list.append( (name, diameter) )
def getNoOfPlanets( self ):
# returns the number of planets held in the list
return len( self._list )
def isEmpty( self ):
# returns True if the list is empty, False otherwise.
return len( self._list )==0
def getSmallest( self ):
# returns the smallest planet or None if empty list
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 ):
# returns the largest planet, or None if empty list
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]