Compsci 101, Fall 2017
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 use at least one turtle. Feel free to use more.
  3. You must have the following if __name__ = '__main__': in your program to start.
    if __name__ == '__main__':
        # main function to have a turtle draw a picture 
        draw()
    
  4. You must have at least 6 useful functions in your program, besides the function draw.

  5. 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 and uses a for loop. The last line of the code is an example of calling this 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.

  6. You must use at least ten 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 ten 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
    

  7. You must use at least three colors that are displayed in the picture.
  8. You must use at least one polygon that is filled in with a color (not the default color of the canvas).
  9. You must use exitonclick() as the last statement in your program so the canvas will stay up until it is clicked on.
  10. 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.

But the main thing is to have some fun in creating your picture!

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 9000 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. You could also create your own test file.

http://www.cs.duke.edu/courses/fall17/compsci101/data/earthqDataAug14-Sep13-2017.txt   # large file, 30 days of data
http://www.cs.duke.edu/courses/fall17/compsci101/data/earthqDataSmallSep2017.txt       # small sample file    

The format of each line in the file is the magnitude, followed by a "%", followed by the type of earthquake, followed by "$", followed by the location. For example, here is the small file.

1.3%earthquake$81km SSW of Kobuk, Alaska
1.92%earthquake$37km SW of Challis, Idaho
1.5%earthquake$74km NNW of Ester, Alaska
2.3%earthquake$30km SE of Yerington, Nevada
1.45%earthquake$6km NE of Aguanga, CA
2.3%earthquake$16km SSE of Fort Irwin, CA
1.7%other event$113km NW of Redoubt Volcano, Alaska
1.95%earthquake$6km W of Volcano, Hawaii
0.03%earthquake$12km NNE of West Yellowstone, Montana
1.19%earthquake$32km WSW of Frenchtown, Montana
0.99%earthquake$36km ENE of Coso Junction, CA
1.32%earthquake$6km WNW of The Geysers, California
1.39%earthquake$14km ESE of Lincoln, Montana
1.43%earthquake$16km ENE of Ocotillo, CA
1.1%earthquake$64km ENE of Cape Yakataga, Alaska
1.85%earthquake$10km ENE of Pahala, Hawaii
-0.62%earthquake$33km W of Lincoln, Montana
0.48%earthquake$20km ESE of Anza, CA
1.07%earthquake$14km ESE of Lincoln, Montana
3.2%earthquake$70km WNW of Skagway, Alaska
4.3%earthquake$47km S of San Francisco del Mar, Mexico
1.06%earthquake$5km W of Imperial, CA
5.1%earthquake$28km N of Subtanjalla, Peru
2.3%earthquake$12km SSW of Guanica, Puerto Rico
3.8%earthquake$94km NE of Chirikof Island, Alaska
2.18%earthquake$26km E of Honaunau-Napoopoo, Hawaii
4.4%earthquake$67km S of San Francisco del Mar, Mexico
4.6%earthquake$120km SW of Huarmey, Peru
4.2%earthquake$12km NNW of Juliaca, Peru
2.4%earthquake$45km SSE of Dillingham, Alaska
4.3%earthquake$South of the Fiji Islands
1.02%earthquake$8km E of Tijuana, B.C., MX
2.9%earthquake$3km NE of Barahona, Puerto Rico
4%earthquake$82km SSW of Paredon, Mexico
1.09%earthquake$5km NNW of The Geysers, California
2.5%earthquake$13km ESE of Soda Springs, Idaho
0.55%earthquake$9km NE of Aguanga, CA
4.5%earthquake$41km SW of Pijijiapan, Mexico
4.4%earthquake$17km NNE of Jayune, Peru
2.59%earthquake$22km N of Luquillo, Puerto Rico
4.2%earthquake$246km ESE of Chirikof Island, Alaska
2.65%earthquake$71km N of Tierras Nuevas Poniente, Puerto Rico
1.7%earthquake$33km E of Sutton-Alpine, Alaska
5.2%earthquake$103km SSW of Paredon, Mexico
1.38%earthquake$14km SE of Lincoln, Montana
3.05%earthquake$70km N of Tierras Nuevas Poniente, Puerto Rico
1.3%earthquake$68km WNW of Valdez, Alaska
4.1%earthquake$80km SW of Paredon, Mexico
3.05%earthquake$70km N of Tierras Nuevas Poniente, Puerto Rico
4.6%earthquake$33km S of Layo, Peru

The first line above shows that there was an earthquake of magnitude 1.3 that happened 81km SSW of Kobuk, Alaska.

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, the type or category of earthquake (such as "earthquake" or "quarry blast") and the name of the place where the earthquake happened.

Note that magnitudes can be NEGATIVE Numbers. There is an example above with -0.62 magnitude from 33km W of Lincoln, Montana.

To get full credit you must follow these requirements.

  1. You must put all your code in one Python module named QueriesQuakes.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: type of earthquake (such as earthquake or quarry blast), followed by a comma and a blank, the magnitude, followed by a comma and a blank, and then followed by the place where the earthquake took place.

    NOTE: This function fileToList has already been started for you. You will see it when you snarf the starter files. Currently this function reads in lines of data and puts them in a list. You will need to modify this function to format strings to a different format before putting them in the list.

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

     ["earthquake, 1.3, 81km SSW of Kobuk, Alaska", 
     "earthquake, 1.9, 37km SW of Challis, Idaho", 
     "earthquake, 1.5, 74km NNW of Ester, Alaska", 
     ...
     ] 
    

  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.

    
    
  4. Write a function named printQuakes that has two parameters. One is a list of strings of earthquake data in the format above (an example string in the list might be: "earthquake, 1.3, 81km SSW of Kobuk, Alaska"). 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 getParts. This method has one parameter that is a string representing one line of earthquake data in the format: type of earthquake (such as earthquake or quarry blast), followed by a comma and a blank, the magnitude, followed by a comma and a blank, and then followed by the place where the earthquake took place.

    This function returns a list of three items, 1) the magnitude as a float, 2) the type of earthquake as a string, and 3) the location of the earthquake as a string.

    For example, the call getParts("earthquake, 1.3, 81km SSW of Kobuk, Alaska") would return the list

    [1.3, "earthquake", "81km SSW of Kobuk, Alaska"]

    You should find this function useful to call whenever you need a part of a line of earthquake data, such as you need the magnitude, or you need the type of the activity (such as earthquake).

  6. 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 (an example string in the list might be: "earthquake, 1.3, 81km SSW of Kobuk, Alaska"). This method should return a list of earthquake strings whose earthquakes have magnitude equal or greater than the parameter number.
  7. 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 (an example string in the list might be: "earthquake, 1.3, 81km SSW of Kobuk, Alaska").. 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.
  8. Your program should print out the following information about the large earthquake file (though you should test it on the small earthquake file first). Whenever you print out the information for one earthquake, it should be in the comma format described above with the earthquake type first, followed by a comma and a blank, then the magnitude, followed by a comma and a blank, and then followed by the location of the earthquake.

    If you write a function that returns a list of earthquake strings, then you can just call PrintQuakes to print them.

    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 Puerto Rico", and then follow that with one line for each earthquake. Your program should also work correctly if we test it on a different file in the same format.

    Information to print:

    1. Print the number of lines in the file and also the number of lines that are categorized as earthquake.
      (HINT: YOU MAY WANT TO WRITE A FUNCTION numberOfType to calculate this. Pass in the earthquake list and a type such as "earthquake" and then return the number that are categorized as "earthquake". It would be helpful to call the getParts function)
    2. Print the first ten lines in the list. [This is mostly to check to see if you are reading in the list correctly]. Call printQuakes to do this.
    3. Print the information on the first 10 lines in the list that are from Puerto Rico (Their strings would end with Puerto Rico).
    4. How many earthquakes happened in Mexico? (only consider lines that end with the word Mexico).
    5. How many earthquakes happened in California that are 1.0 or greater but less than 3.0? (only consider lines that end with California)
    6. Print the first five earthquakes in the file that have magnitude 3.0 or larger that happened in Alaska. (only consider lines that end in Alaska)
    7. Print the different types of earthquake categories that are in the file ("earthquake" is one type of activity, there are others). You may want to write a function to calculate this.
    8. Print the earthquake information about the largest magnitude earthquake.
    9. What is the largest magnitude earthquake that happened in Hawaii? (only consider lines that end in Hawaii)
    10. Print ten random earthquakes that happened in Peru (only consider lines that end in Peru)

  9. You may write additional functions, however, you should be able to answer all of these questions by using the functions named above.

  10. 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:

Information about Earthquakes from the 30 days
leading up to September 13, 2017.

Number of lines in the file is: 50

Number of lines categorized as earthquakes in the file is:  49

First ten lines in the file:
earthquake, 1.3, 81km SSW of Kobuk, Alaska
earthquake, 1.92, 37km SW of Challis, Idaho
earthquake, 1.5, 74km NNW of Ester, Alaska
earthquake, 2.3, 30km SE of Yerington, Nevada
earthquake, 1.45, 6km NE of Aguanga, CA
earthquake, 2.3, 16km SSE of Fort Irwin, CA
other event, 1.7, 113km NW of Redoubt Volcano, Alaska
earthquake, 1.95, 6km W of Volcano, Hawaii
earthquake, 0.03, 12km NNE of West Yellowstone, Montana
earthquake, 1.19, 32km WSW of Frenchtown, Montana

First ten earthquakes in Puerto Rico are:
earthquake, 2.3, 12km SSW of Guanica, Puerto Rico
earthquake, 2.9, 3km NE of Barahona, Puerto Rico
earthquake, 2.59, 22km N of Luquillo, Puerto Rico
earthquake, 2.65, 71km N of Tierras Nuevas Poniente, Puerto Rico
earthquake, 3.05, 70km N of Tierras Nuevas Poniente, Puerto Rico
earthquake, 3.05, 70km N of Tierras Nuevas Poniente, Puerto Rico

Number of earthquakes in Mexico is 6

Number of California quakes 1.0 or higher and smaller than  3.0  is  2

First five earthquakes in Alaska  3.0  or greater are 
earthquake, 3.2, 70km WNW of Skagway, Alaska
earthquake, 3.8, 94km NE of Chirikof Island, Alaska
earthquake, 4.2, 246km ESE of Chirikof Island, Alaska

Types of activity:
earthquake
other event

Largest quake is: earthquake, 5.2, 103km SSW of Paredon, Mexico

Largest quake in Hawaii is: earthquake, 2.18, 26km E of Honaunau-Napoopoo,
Hawaii

Ten random quakes from Peru
earthquake, 4.2, 12km NNW of Juliaca, Peru
earthquake, 5.1, 28km N of Subtanjalla, Peru
earthquake, 4.4, 17km NNE of Jayune, Peru
earthquake, 4.6, 120km SW of Huarmey, Peru
earthquake, 4.6, 33km S of Layo, Peru

done processing

REMEMBER, once you have this output correct you need to run your program on the large data file to turn in.

If we ask for the max and there is a tie, then any of the answers that tie can be given as a correct answer.


Hint: Comparing magnitudes

You'll need to convert the magnitude 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]


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

If you want to include a picture of your turtle program, below describes how to do this. THIS IS OPTIONAL, IT IS NOT REQUIRED. It is worth 0 points.

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.