CS5 Programming Assignment 5

CS8 Programming Assignment 5

Description

This programming assignment is a practice on generating Python dictionaries and reading files. You will develop programs that read from a text file named stars.txt, and create and return a dictionary data structure, and then perform certain computations over this dictionary.

Requirements

I am providing a text file stars.txt containing 148 lines. Each line has the name of a star, such as Alnilam, followed up by its distance in light years to the Solar System, such as 1340, and its apparent visual magnitude (AVM), such as 1.70. AVM is a number which is a measure of a star's brightness; a smaller AVM indicates that the star is brighter. The star file is available here: stars.txt. For example, the line about the star Alnilam is as follows:
Alnilam                    1340        1.7
which is the the central star in Orion's belt. It is at a distance of 1340 light years from us and has AVM 1.70. Another example the line is
Bellatrix                   245       1.64
means that the star Bellatrix, which is again in the constellation Orion, is 245 light years away, and has AVM 1.64. Thus, Bellatrix is slightly brighter than Alnilam. The stars given in the file have their AVM between 0 and 3. Your programs should work correctly for any input file which is formatted like stars.txt, but it may have fewer or larger number of stars than your test file. However, you may assume all stars will have their AVM between 0 and 3.
  1. Write a function named makeStarDict(filename) which takes the string filename (such as "stars.txt") as the name of file, and reads the file (which is a star file just like stars.txt, and returns a dictionary with keys = star names (as strings) in the file, and values = as the list [distance, AVM] of values (as floating-point numbers). Therefore, construct your dictionary so that starDict[starName] is the list [distance, AVM], and thus, starDict[starName][0] would be that star's distance and starDict[starName][1] would be its AVM. As an example, when you run your function in the IDLE interactive session
    >>> starDict = makeStarDict("stars.txt")
    >>> starDict["Alnilam"]
    [1340.0, 1.7]
    >>> starDict["Alnilam"][0]
    1340.0
    >>> starDict["Alnilam"][1]
    1.7
    
  2. Write a function named starDistance(dist1, dist2, starDict) which takes two floating-point numbers dist1 and dist2 (with dist1 < dist2) and returns a sorted list of the names of all stars in starDict whose distance is in the range dist1 < distance < dist2. An example:
    >>> mylist = starDistance(500, 530, starDict)
    >>> mylist
    ['Al-Kab', 'Beta-Lup', 'Lesath', 'Pi-Sco']
    
  3. Write a function named starAVM(avm1, avm2, starDict) which takes two floating-point numbers avm1 and avm2 (with avm1 < avm2) and returns a sorted list of the names of all stars in starDict whose AVM is in the range avm1 < AVM < avm2. An example:
    >>> mylist = starAVM(1.0, 1.2, starDict)
    >>> mylist
    ['Fomalhaut', 'Pollux', 'Spica']
    
  4. Write a function named starMean(avm1, avm2, starDict) which takes two distinct AVM values avm1 and avm2 (with avm1 < avm2), and returns the average distance of all stars whose AVM is in the range avm1 < AVM < avm2. An example:
    >>> avg = starMean(1.0, 1.2, starDict)
    >>> avg
    103.0
    

What to Submit

Functions to be submitted:
  makeStarDict(filename)
  starDistance(dist1, dist2, starDict)
  starAVM(avm1, avm2, starDict)
  starMean(avm1, avm2, starDict)

Write all 4 functions in a Python text file named pa5.py, and submit your file using the Dropbox link. Submit only the functions; no test programs outside the functions are needed. Also, you may write and include in your pa5.py file as many helper functions as you want.

Important: Add your full name and email address as a comment in the first line of your file, such as:
# Alice Smartpants (smartpants@umail.ucsb.edu)


cs8 web page