Question 1: Read the given CSV file and create corresponding ship objects. Good News: This portion is already working to read the file and create Ship objects, although the objects are not being...

1 answer below »


Question 1: Read the given CSV file and create corresponding ship objects.



Good News: This portion is already working to read the file and create
Ship
objects, although the objects are not being stored (yet) in any data structure.



[2 Points] Modfiy this code so that these
Ship
objects are stored in a dictionary that you can use to manipulate them. In the dictionary, use the
shipname
as the key and the
Ship
object as the value.





Question 2:



[3 points] Which of these ships has medicine, and how much does each have?


Write some code to print out the names of ships that have medicine, along with the amount each has.



[3 points] Looking across all of these ships, what is the total amount of medicine onboard?


Have your code also print out the total amount of medicine across these ships.






Question 3:



Puerto Cabezas is located at (14.01996° N, 83.38492° W).



[6 points] Write code to calculate how far is each ship (in great circle distance, nautical miles) from Puerto Cabezas?


(Be explicit in your output, reporting ships by namein alphabetical order, and including their distance.)





Question 4:



[6 points] Which 5 ships are closest to Puerto Cabezas? Write some code to print their name and distance, ordered from closest to farthest.





Question 5:



Assuming that each ship heads for Puerto Cabezas immediately (starting at previously defined "start time") at its maximum speed, at what time will it arrive?



[7 points] Write a function called
get_arrival_timethat takes three arguments



  • theShipobject

  • aCoordrepresenting the port location of Puerto Cabezas

  • adatetimeobject representing the start time of travel


and returns adatetimeobject representing its arrival time.



[3 points] Then use this function to print the arrival time for each of the ships.(Be explicit in your output.) This function must run without error.





Question 6:



[10 points] In what order will these ships arrive?


Write some code to sort and print them by their arrival time.





Question 7:



Local officials in Puerto Cabezas need to know what ships can arrive by (Midnight) 0000 GMT Wednesday 18 November 2020.



[20 points] Write some Python code to determine which ships will have arrived by this time. Print them out.





Question 8:



[20 points] How much of each cargo type will have "arrived" into port by this time? Write some Python code to report the aggregate amount for each cargo type.





Question 9:



If priority is defined simply by the total amount of cargo on board (all units treated equally), with larger cargos representing higher priority, then in what order will these ships be offloaded and depart?



[25 points] Write some Python code to list these ships, in order.





Question 10:



If priority is defined in terms of the quantity of food (first), fuel (second), and supplies (third), with larger quantities representing higher priority, then in what order will these ships be offloaded and depart?



[30 points] Write some Python code to list these ships, in order.


Hint: You should compare quantity of food first (independent of fuel or supplies). Only if there is a tie in food, should you compare fuel. Only if there is a tie in fuel, should you compare supplies. How can you do this easily? Think tuples...


Answered Same DayJul 29, 2021

Answer To: Question 1: Read the given CSV file and create corresponding ship objects. Good News: This portion...

Pratap answered on Jul 30 2021
153 Votes
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
- Summer 2021 - Assignment 2
"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### >> Put your name here >> "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### The Scenario: \n",
"The date is November 17, 2020. Hurricane Iota has just slammed into an already storm-ravaged Nicaragua as a Category 4 storm. The landfall was just 15 miles south of where Hurricane Eta hit two weeks earlier. Eta left thousands displaced and do
zens killed in Central America.\n",
"\n",
"The storm has created an urgent need for humanitarian aid in the city of Puerto Cabezas. Unfortunately, the storm has destroyed the local airport and blocked all incoming roads, making it accessible only by sea.\n",
"\n",
"Fortunately, there are a number of ships in the vicinity, each of which has a varied inventory of cargo. The most relevant cargo are (1) food, (2) medicine, (3) supplies, (4) equipment, and (5) fuel. Not every ship has every commodity, and the quantities of each are different.\n",
"\n",
"The file ``nearby_ships_20201117.csv`` contains information about these ships, specifically:\n",
"* ship name\n",
"* current position (lat, lon)\n",
"* max speed (in knots)\n",
"* cargo contents\n",
"\n",
"**Puerto Cabezas** is located at (14.01996° N, 83.38492° W).\n",
"\n",
"![midterm_map_1a_small.png](attachment:midterm_map_1a_small.png)\n",
"\n",
"The **start time** (at which this file was generated, and for the purposes of this assignment) is **1200 GMT Tuesday 17 November 2020**. (Note: \"start\" is NOT the current time at which you are taking this exam!)`\n",
"\n",
"You will write Python code to answer several questions about the overall scenario and the capability of these ships to provide relief."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**The Code:** You have been provided with two Python classes that you will use to perform your analysis.\n",
"* ``Coord`` -- exactly as used in Lab 1 Assignment, defined below\n",
"* ``Ship`` -- a new class, defined below\n",
"\n",
"**Note: you are not allowed to use the Pandas library on this exam.**"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# Here is the first of two class definitions that you will use.\n",
"# This code is taken directly from the Lab 1 assignement.\n",
"# You do NOT need to change anything here; just load it.\n",
"import math \n",
"\n",
"\n",
"class Coord:\n",
" '''An improved class to represent lat/lon values.'''\n",
" \n",
" def __init__(self,lat,lon):\n",
" self.lat = float(lat) # make sure it's a float\n",
" self.lon = float(lon)\n",
" \n",
" # Follows the specification described in the Aviation Formulary v1.46\n",
" # by Ed Williams (originally at http://williams.best.vwh.net/avform.htm)\n",
" def dist_to(self, other):\n",
" lat1 = Coord.deg2rad(self.lat)\n",
" lon1 = Coord.deg2rad(self.lon)\n",
" lat2 = Coord.deg2rad(other.lat)\n",
" lon2 = Coord.deg2rad(other.lon)\n",
" \n",
" # there are two implementations of this function.\n",
" # implementation #1:\n",
" #dist_rad = math.acos(math.sin(lat1) * math.sin(lat2) \n",
" # + math.cos(lat1) * math.cos(lat2) * math.cos(lon1-lon2))\n",
"\n",
" # implementation #2: (less subject to numerical error for short distances)\n",
" dist_rad=2*math.asin(math.sqrt((math.sin((lat1-lat2)/2))**2 +\n",
" math.cos(lat1)*math.cos(lat2)*(math.sin((lon1-lon2)/2))**2))\n",
"\n",
" return Coord.rad2nm(dist_rad)\n",
" \n",
"\n",
" def __str__(self):\n",
" return \"(%f,%f)\" % (self.lat,self.lon)\n",
" \n",
" def __repr__(self):\n",
" return \"Coord(%f,%f)\" % (self.lat,self.lon) \n",
"\n",
" def deg2rad(degrees):\n",
" '''Converts degrees (in decimal) to radians.'''\n",
" return (math.pi/180)*degrees\n",
"\n",
" def rad2nm(radians):\n",
" '''Converts a distance in radians to a distance in nautical miles.'''\n",
" return ((180*60)/math.pi)*radians\n",
" \n",
"# End of class Coord"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# Here is the second of two class definitions for this exam.\n",
"# You may add additional methods to this class (if you wish), \n",
"# but YOU SHOULD NOT CHANGE THE init METHOD.\n",
"\n",
"import datetime\n",
"\n",
"class Ship:\n",
" '''A simple class to represent a ship and its cargo.'''\n",
" \n",
" def __init__(self,name,loc,max_speed,cargo_items):\n",
" '''builds and returns a complete Ship object'''\n",
" self.name = name # assumed a String\n",
" self.loc = loc # assumed a Coordinate\n",
" self.max_speed = max_speed # assumed a float\n",
" \n",
" self.cargo = {} # a dictionary\n",
" # cargo_items is assumed to be a list of strings, each of the form \"key=value\"\n",
" # where \"key\" is a string, and \"value\" can be converted to a numerical value\n",
" for item in cargo_items:\n",
" if item.strip(): # if something other than whitespace\n",
" key,value = item.split('=')\n",
" self.cargo[key] = float(value)\n",
" # end of __init__\n",
"\n",
" def get_cargo(self,cargo_name):\n",
" '''returns the quantity of named cargo from dictionary if exists, or None otherwise'''\n",
" return self.cargo.get(cargo_name)\n",
" \n",
" def __str__(self):\n",
" return \"Ship %s %s %.1f %s\" % (self.name,self.loc, self.max_speed, self.cargo)\n",
"\n",
"# end of class Ship"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Question 1: Read the given CSV file and create corresponding ship objects. \n",
"\n",
"#### Good News: This portion is already working to read the file and create ``Ship`` objects, although the objects are not being stored (yet) in any data structure.\n",
"\n",
"#### [2 Points] Modfiy this code so that these ``Ship`` objects are stored in a dictionary that you can use to manipulate them. In the dictionary, use the ``shipname`` as the key and the ``Ship`` object as the value."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"import csv\n",
"\n",
"f = open(\"nearby_ships_20201117.csv\",\"r\")\n",
"reader = csv.reader(f)\n",
"\n",
"ship_dict = dict()\n",
"for row in reader:\n",
" shipname = row[0]\n",
" coord = Coord(row[1],row[2])\n",
" speed = float(row[3])\n",
" next_ship = Ship(shipname,coord,speed,row[4:])\n",
" ship_dict[shipname] = next_ship\n",
" \n",
"f.close()"
]
},
{
"cell_type": "markdown",
...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here