CSC111 Lab 10 Addendum 2014

From dftwiki3
Jump to: navigation, search

--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]