Compsci 101, Fall 2016
Assignment 3 HOWTO Turtles/Earthquakes

You can snarf the following files to get started:

PART 1: Turtles Requirements

To get full credit you must follow these requirements.
  1. You must put all your code in one Python module named TurtlePicture.py

  2. You must have a function called draw that has no parameters, and that can only create turtles and call other functions.

  3. You must use at least one turtle. Feel free to use more.
  4. You must have the following if __name__ = '__main__': in your program to start it and it should only call draw .
    if __name__ == '__main__':
        # main function to have a turtle draw a picture 
        draw()
    
  5. You must have at least 6 useful functions in your program, besides the function draw.

  6. You must have at least TWO functions that have a for statement ("a loop") that generate different patterns (examples might be a square, a window, a spiral, etc) and are each called and displayed in the picture at least twice. (these 2 functions would be two of the six required functions. )

    Here is example code for a function that draws a flower pattern. Also is a statement that calls the function.

    SINCE I AM GIVING YOU THIS FUNCTION, you can use it if you want, but it does not count as one of the six functions that you are suppose to create! If you modify it in some way, (possibly add colors or fill in), then you can count it as one of your functions.

    def flowerSquare(alex):
        # draw a flower made out of squares
        for i in range(12):      # repeat twelve times
            # draw a square
            alex.forward(50)
            alex.left(90)
            alex.forward(50)
            alex.left(90)
            alex.forward(50)
            alex.left(90)
            alex.forward(50)
            alex.left(90) 
            # move 30 degrees 
            alex.left(30)
            
    flowerSquare(alex)
    

    and here is the picture of the flower pattern:

    Here is a Python file TurtleFlower.py that uses the flowerSquare function and draws a flower.

  7. You must use at least fifteen different turtle methods.

    The textbook has already shown you several turtle methods.

    For example, here are five turtle methods you have probably already used:

     forward
     left
     right
     up
     down
    

    There are many more in Python's Turtle Graphics module.

    Here is the starter file TurtlePicture.py. Only two of the turtle methods below count as the required fifteen different turtle methods, they would be "forward" and "left".

    import turtle               # allows us to use the turtles library                                         
    wn = turtle.Screen()        # creates a graphics window                                                    
    
    def rightAngle(turt):       # This function is a sample, you can delete it 
        # sample function to draw a right angle
        turt.forward(100)
        turt.left(90)
        turt.forward(100)
        
    def draw():
        # create a turtle and draw a right angle
        alex = turtle.Turtle()    # you can rename the turtle if you want
        rightAngle(alex)   
     
    if __name__ == '__main__':
        # main function to have a turtle draw a picture 
        draw() 
    
    
        wn.exitonclick()        # Must be last line in file
    

  8. You must use at least three colors that are displayed in the picture.
  9. You must use at least one polygon that is filled in with a color (not the default color of the canvas).
  10. You must use exitonclick() as the last statement in your program so the canvas will stay up until it is clicked on.
  11. You must have a comment for each function you write describing what that function does, in addition to a comment at the top of the file with your name.

Your grade will be based on how well your program conforms to the standards above and whether it draws a picture using the function named draw.

PART 2 : Earthquake Requirements

We have created two files for you to use. You should read them into your program with the urllib2. The first file has over 8000 lines of data. That's pretty big to use to debug your program. We have also created a small file with just a few lines, the second link below. We suggest you use that file initially for testing your program.

http://www.cs.duke.edu/courses/fall16/compsci101/data/earthquakeDataSept14-2016past30days.txt
http://www.cs.duke.edu/courses/fall16/compsci101/data/earthquakeDataSmallFile.txt

The format of each line in the file is a "$", followed by the magnitude, followed by blank (" "), followed by a dash ("-") followed by a blank, followed by the place where the earthquake happened. For example, here is the small file.

$2.2 - 3km E of The Geysers, California
$1.3 - Quarry Blast - 5km NNW of Lake Elsinore, CA
$0.7 - 10km SSE of Morton, Washington
$1.2 - 5km NE of San Simeon, California
$1.1 - 13km SSW of Circle Hot Springs Station, Alaska
$4.3 - 25km WSW of Arequipa, Peru
$0.2 - 19km ESE of Anza, CA
$4.1 - 8km ESE of Maltignano, Italy
$0.5 - 5km S of Volcano, Hawaii
$1.2 - 8km E of San Martin, California
$4.3 - 5km WNW of Norcia, Italy
$1.0 - 6km NNW of Garnet, CA
$1.2 - 23km SW of Coalinga, California
$3.5 - 5km S of Volcano, Hawaii
$6.0 - 51km N of Moyobamba, Peru
$3.5 - 8km ESE of Norcia, Italy
$0.9 - Quarry Blast - 13km SSE of Home Gardens, CA
$4.3 - 246km S of Severo-Kuril'sk, Russia
$1.3 - 114km NW of Talkeetna, Alaska
$1.9 - 47km W of Anchorage, Alaska
$4.7 - 14km NW of San Isidro, Costa Rica
$3.9 - 12km NW of Faro, Canada
$0.8 - 51km ENE of Cantwell, Alaska
$0.8 - 2km S of Castle Rock, Washington
$4.5 - 277km NW of Visokoi Island, South Georgia and the South Sandwich Islands
$4.3 - Southwest Indian Ridge
$1.5 - 12km WNW of Luxora, Arkansas
$5.2 - 269km NW of Visokoi Island, South Georgia and the South Sandwich Islands
$0.3 - 10km W of Salcha, Alaska
$-0.2 - 7km ESE of Mammoth Lakes, California
$4.6 - 8km W of Amatrice, Italy
$4.8 - 194km ESE of Nikol'skoye, Russia
$5.0 - 264km ESE of Grytviken, South Georgia and the South Sandwich Islands
$4.2 - 186km ESE of Nikol'skoye, Russia
$0.5 - 12km SE of Mammoth Lakes, California
$4.4 - 66km SW of Severo-Kuril'sk, Russia
$1.3 - 39km W of Willow, Alaska
$4.5 - 27km NNW of `Alaqahdari-ye Kiran wa Munjan, Afghanistan

The first line above shows that there was an earthquake of magnitude 2.2 that happened 3km E of The Geysers in California.

In our data file, we did not include all the information about each earthquake, so you will not know the longitude and latitude where it occurred, the data and time, etc.. Our data file only has the magnitude and the name of the place.

To get full credit you must follow these requirements.

  1. You must put all your code in one Python module named QuakeQueries.py. You can snarf this file to get started.
  2. Write a function named fileToList that has one parameter that is a String representing a url. This function will read earthquake data from the URL that is in the format above and return a list of strings in a DIFFERENT FORMAT, where each string is in the new format: magnitude, followed by a blank, a colon and another blank, and then followed by the place of the earthquake.

    NOTE: You will need to modify the fileToList function that is provided in the snarf file to format strings in this new format.

    For the example above, that list would look like (just the first three items in the list are shown):

    ["2.2 : 3km E of The Geysers, California", 
     "1.3 : Quarry Blast - 5km NNW of Lake Elsinore, CA",
     "0.7 : 10km SSE of Morton, Washington", ... ]
    

  3. You should have an if-main line (the first line shown below) to indicate where your program will start. This program should call fileToList first to get the earthquake data into a list. The start of this code is provided in the snarf file.

    if __name__ == '__main__':
        '''
        Starting part of the earthquake program
        Read a file of earthquake data and answer
        several queries about the data.
        '''
        urlstart = "http://www.cs.duke.edu/courses/compsci101/fall16/data/"
        #datafile = "earthquakeDataSept14-2016past30days.txt"
        datafile = "earthquakeDataSmallFile.txt"
        eqList = fileToList(urlstart+datafile)
        
        print "Number of lines in the file is:", len(eqList)
        print
        
        print "First six lines in the list:"
        for num in range(6):
            print eqList[num]  
    
  4. Write a function named printQuakes that has two parameters. One is a list of strings of earthquakes in the format above and the second is a number. If the number is -1, then print each earthquake string in the list, one per line. If the number is 1 or more, then print only that many earthquake strings, starting with the first one in the list. If the number is larger than the number of earthquake strings then print all the earthquake strings.

    In all the cases, print the earthquakes one per line.

    For example, if you call the function with 8 but there are only 4 earthquakes in the list, then just print the 4 earthquakes, one per line. If you call the function with 8 and there are 300 earthquakes in the list, then just print the first 8, one per line. Assume the function is called correctly, that is, it is not called with 0 (makes no sense) or other negative numbers.

  5. Write a function named bigQuakes. This method has two parameters. One is a decimal number and one is a list of earthquake strings in the format above. This method should return a list of earthquake strings whose earthquakes have magnitude equal or greater than the parameter number.
  6. Write a function named locationQuakes. This method has two parameters. One is a String representing a place, and one is a list of earthquake strings in the format above. This method should return a list of earthquake strings whose earthquakes were in this location. Assume the location is the last word in the description of the earthquake location. For example, if place is "Alaska", then this method should return all the earthquake strings that end with Alaska in their location.
  7. Your program should print out the following information about the large earthquake file (though you should test it on the small earthquake file first). Your output should be easy to identify. Don't just print a list of earthquakes, but identify the earthquakes and then print them out. For example you might have: "Here are the first ten earthquakes in the file that happened in Alaska." When it says to print "an earthquake" or "earthquake information" then print out the earthquake string of the magnitude and place of the earthquake. Your program should also work correctly if we test it on a different file in the same format.

    1. Print the number of lines of earthquake data in the file.
    2. Print the first six lines in the list. [This is mostly to check to see if you are reading in the list correctly]
    3. Print the information on the first 10 earthquakes in the file that are from Peru (They would end with the word Peru).
    4. How many earthquakes happened in Italy? (only consider lines that end with the word Italy).
    5. How many earthquakes happened in California (or listed as CA) that are larger than or equal to 2.0? (only consider lines that end in California or CA)
    6. Print the first six earthquakes in the file that have magnitude 4.0 or larger that happened in Italy.
    7. Print the earthquake information about the largest magnitude earthquake.
    8. What is the largest magnitude earthquake that happened in Russia?
    9. Print the first ten earthquakes in the file that happened in Alaska that have magnitude 1.0 or larger.
    10. Print ten random earthquakes that happened in Alaska that have magnitude 1.0 or larger.

  8. You may write additional functions.

  9. You must have a comment for each function you write describing what that function does, in addition to a comment at the top of the file with your name.

Hint: Sample Output

Your output has to be clearly identified. Using the small datafile shown above, here is an example of how the output might be shown:

Number of lines in the file is: 38

First six lines in the list:
2.2 : 3km E of The Geysers, California
1.3 : Quarry Blast - 5km NNW of Lake Elsinore, CA
0.7 : 10km SSE of Morton, Washington
1.2 : 5km NE of San Simeon, California
1.1 : 13km SSW of Circle Hot Springs Station, Alaska
4.3 : 25km WSW of Arequipa, Peru

First ten earthquakes in Peru are:
4.3 : 25km WSW of Arequipa, Peru
6.0 : 51km N of Moyobamba, Peru

Number of earthquakes in Italy is 4

Number of CA quakes 2.0 or higher is: 0
Number of California quakes 2.0 or higher is: 1
Number of quakes 2.0 or higher in California total is: 1

First six earthquakes in Italy greater than 4.0
4.1 : 8km ESE of Maltignano, Italy
4.3 : 5km WNW of Norcia, Italy
4.6 : 8km W of Amatrice, Italy

Largest quake is: 6.0 : 51km N of Moyobamba, Peru

Largest quake in Russia is: 4.8 : 194km ESE of Nikol'skoye, Russia

First Ten quakes from Alaska that are 1.0 or higher
1.1 : 13km SSW of Circle Hot Springs Station, Alaska
1.3 : 114km NW of Talkeetna, Alaska
1.9 : 47km W of Anchorage, Alaska
1.3 : 39km W of Willow, Alaska

Ten random quakes from Alaska that are 1.0 or higher
1.3 : 114km NW of Talkeetna, Alaska
1.1 : 13km SSW of Circle Hot Springs Station, Alaska
1.3 : 39km W of Willow, Alaska
1.9 : 47km W of Anchorage, Alaska

done processing

NOTE: Only the total number of earthquakes from CA and California is required but I chose to include additional information for the separate counts first to make it easier to determine if I was computing the total correctly.

Consider other testing. For example, you could run your program on big quakes in California (CA or California) with quakes 1.0 or bigger and that output should be:

Number of CA quakes 1.0 or higher is: 2
Number of California quakes 1.0 or higher is: 4
Number of quakes 1.0 or higher in California total is: 6

If you do this be sure to change it back to 2.0 before you turn it in and run your output.


Hint: Comparing magnitudes

You'll need to extract/slice the magnitude from the earthquake string and convert it to a number so you can compare it to another number.


Hint: Reading in lines from a URL

Here is sample code for reading all the lines from the nytimes.com website we did earlier in the semester.

import urllib2

f = urllib2.urlopen("http://nytimes.com")
for line in f:
    print line


Hint: Selecting List Elements at Random

There are many ways to choose list elements at random. The simplest one is to shuffle the list, then take a slice of the shuffled list. To use the shuffle function, you must import random. See the PyDev console pasted below for details.

import random
nums = [1,3,5,7,30,52]
nums
Out[4]: [1, 3, 5, 7, 30, 52]
random.shuffle(nums)
nums
Out[6]: [5, 3, 52, 1, 7, 30]
random.shuffle(nums)
nums
Out[8]: [3, 52, 5, 1, 30, 7]
nums[0:3]
Out[9]: [3, 52, 5]


HINT: How to turn in your turtle picture with Ambient Eclipse

First you could turn in your program and picture with websubmit by selecting files.

Here is how you can put your picture into your eclipse folder, and then turn it in with your programs easily.

Suppose your picture is named "image.jpg". Then in Eclipse in your assignment3 folder, create a new file and call it image.jpg

You may get a pop up that talks about "editors available on marketplace", just cancel that.

Now Eclipse knows about the file but it is empty.

Find your image file (on a regular file search or browse and copy it. Then browse to your workspace and the folder for assignment3 and copy the image file onto itself. They have to be the same name for this to work. If one is image.jpg and the other is image.JPG this won't work.

If it recognizes that it is the same file it will ask if you want to replace the old file with this one and yes you do.

Now your image looks like a file in your assignment3 folder. Don't click on it, it is too big to view and will display in a weird format. But now when you do ambient submit, you can choose it as an option.

In general you do not want to copy files into your eclipse folder unless you create a new file first with the same name and then replace it. Each time you create a file in Eclipse, then Eclipse creates additional hidden files with information on your file. If you just copy a file in, it won't have that hidden information and may not appear in Eclipse. By creating a fake file first with the same name, you get the hidden files with info about the file and you can copy in as a replace. That works.

If this is too complicated, then try websubmit which is listed on the assignment web page. There you browse and select files to submit.