In the lectures about lists we have seen some Python code examples that involve the processing of lists containing weather statistics. The original source of the values shown in the slides was taken from part of the Environment Canada website that serves up historical data:
http://climate.weather.gc.ca/historical_data/search_historic_data_e.html
Data can be provied by that website using the Comma Separated Value
(CSV) format which is stored in text files normally using a CSV
suffix. We will not work directly with such files in this part of
the assignment, but instead will work with pre-packaged
strings.
In the code cell below are:
* two global variables which each corresponding to a list of
strings, ```WEATHER_LIST_01``` and ```WEATHER_LIST_02```. Each list
contains the comma-separated values for a single month's weather
measurements;
* a nearly empty function called ```compute_month_stats()``` which
you are to complete for this part of the assignment; and
* a ```main()``` function that calls
```compute_month_stats()```.
The docstring comment for ```compute_month_stats()``` describes the format of each string, and also describes what the function must compute. Note that the function which you are to complete ***must not produce output to the console*** (*i.e.*, must not call ```print()```). If the function is correctly written, the output of the program when ```WEATHER_LIST_01``` is given as the argument to the function will be:
```
4,23.1,-8.0,91.0,14
5,26.3,4.1,1.8,0
```
As part of your solution you will need to use string-to-list functions such as ```split()```, as well as ```for``` loops and various kinds of ```if``` statements (and many other kinds of Python elements, too). You may use code from lecture slides but you **must** include a comment indicating which slides were used.
And here are some additional important notes:
1. Please remember that strings are not integers or floats. You may
need to call ```int()```, ```float()```, and ```str()``` as
appropriate.
1. The data from Environment Canada is not necessarily always
complete as equipment at measurement stations often malfunctions.
Data provided to you has been cleaned up such that you need not
worry about handling errors in the data. That is, the keen-eyed
among you will notice that there may be fewer strings in a list
than there are days in a month. ***Therefore your solution must not
make any assumptions about the number of days in a month.***
1. ***You are not permitted to ```import``` modules into your
solution (*i.e.*, submitted solutions may not use ```re```,
```pandas```, ```itertools```, etc.). The only exception is the
```sys``` module.***
WEATHER_LIST_01=['2017,04,01,4.7,-5.4,0.0',
'2017,04,02,0.6,-2.5,5.6', '2017,04,03,2.2,-2.9,0.0',
'2017,04,04,6.8,-3.7,0.0', '2017,04,05,2.8,-1.3,0.0',
'2017,04,06,6.8,-0.1,18.4', '2017,04,07,11.6,2.7,27.3',
'2017,04,08,7.5,1.1,0.0', '2017,04,09,9.1,1.2,0.0',
'2017,04,10,11.3,0.3,0.0', '2017,04,11,23.1,3.5,0.0',
'2017,04,12,14.2,1.7,0.0', '2017,04,13,14.5,2.0,0.0',
'2017,04,14,11.1,1.7,0.0', '2017,04,15,15.0,1.9,0.0',
'2017,04,16,17.9,3.1,0.0', '2017,04,17,10.9,-1.2,0.2',
'2017,04,18,5.3,-6.2,0.0', '2017,04,19,5.0,-8.0,0.0',
'2017,04,20,2.7,-3.3,0.0', '2017,04,21,10.6,-2.8,0.0',
'2017,04,22,6.0,-3.9,5.5', '2017,04,23,4.9,-0.1,3.4',
'2017,04,24,14.9,0.4,0.0', '2017,04,25,12.6,-2.5,0.0',
'2017,04,26,10.8,3.9,9.4', '2017,04,27,15.3,9.6,19.0',
'2017,04,28,15.6,8.5,2.2', '2017,04,29,20.4,5.8,0.0',
'2017,04,30,11.3,3.4,0.0']
WEATHER_LIST_02=['2015,05,01,18.0,9.0,0.0',
'2015,05,02,16.0,4.1,0.0', '2015,05,04,17.6,5.3,0.0',
'2015,05,05,16.2,6.1,0.4', '2015,05,07,19.6,4.5,0.0',
'2015,05,08,22.8,5.5,0.0', '2015,05,09,22.5,7.0,0.0',
'2015,05,10,18.8,8.1,0.0', '2015,05,11,16.8,10.3,0.0',
'2015,05,12,17.5,10.8,0.0', '2015,05,13,16.8,10.5,0.0',
'2015,05,14,16.8,9.2,1.2', '2015,05,15,19.9,7.3,0.0',
'2015,05,16,18.5,10.6,0.0', '2015,05,17,19.2,9.6,0.0',
'2015,05,18,21.3,9.2,0.0', '2015,05,19,22.9,9.2,0.0',
'2015,05,20,22.4,7.9,0.0', '2015,05,21,22.3,8.3,0.0',
'2015,05,22,18.9,9.8,0.2', '2015,05,23,18.5,8.8,0.0',
'2015,05,24,18.5,9.4,0.0', '2015,05,25,15.6,8.1,0.0',
'2015,05,26,19.7,9.4,0.0', '2015,05,27,21.4,9.0,0.0',
'2015,05,28,23.5,9.1,0.0', '2015,05,29,26.3,9.9,0.0',
'2015,05,30,22.5,8.8,0.0', '2015,05,31,20.5,7.7,0.0']
def compute_month_stats(data):
"""
function: compute_month_stats
Given a list of strings containing weather data for one
month,
compute summary statistics for that month (maximimum
temperature,
minimum temperature, number of days with freezing
temperatures,
total precipitation) and return all this as a tuple.
Input parameter:
----------------
* data: A list of strings where each string contains one
day's
weather measurements. All of the data in the list is from a
single month. The values are separated by commas, and each
day's
string has the same number of values. The order of these
values
is: year, month, day, high temp (Celsius), low temp
(Celsius),
and precipitation (mm).
Output:
-------
* There is no output to the console from this function.
Return value:
-------------
A tuple with five values is returned. The values are: the
month
number corresponding to the data, the maximum high
temperature,
the minimum low temperature, the total precipitation for the
month (rounded to one decimal place), and the number of days
having freezing or below-freezing temperatures (i.e., 0.0 C
or
lower).
"""
return(0,0,0,0,0)
def main():
weather_data = [WEATHER_LIST_01, WEATHER_LIST_02]
for month_data in weather_data:
stats = compute_month_stats(month_data)
for val in stats[:-1]:
print(val, ",", sep="", end="")
print(stats[-1])
main()
Output:
4,23.1,-8.0,91.0,14 5,26.3,4.1,1.8,0
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
#code
WEATHER_LIST_01 = ['2017,04,01,4.7,-5.4,0.0', '2017,04,02,0.6,-2.5,5.6', '2017,04,03,2.2,-2.9,0.0',
'2017,04,04,6.8,-3.7,0.0', '2017,04,05,2.8,-1.3,0.0', '2017,04,06,6.8,-0.1,18.4',
'2017,04,07,11.6,2.7,27.3', '2017,04,08,7.5,1.1,0.0', '2017,04,09,9.1,1.2,0.0',
'2017,04,10,11.3,0.3,0.0', '2017,04,11,23.1,3.5,0.0', '2017,04,12,14.2,1.7,0.0',
'2017,04,13,14.5,2.0,0.0', '2017,04,14,11.1,1.7,0.0', '2017,04,15,15.0,1.9,0.0',
'2017,04,16,17.9,3.1,0.0', '2017,04,17,10.9,-1.2,0.2', '2017,04,18,5.3,-6.2,0.0',
'2017,04,19,5.0,-8.0,0.0', '2017,04,20,2.7,-3.3,0.0', '2017,04,21,10.6,-2.8,0.0',
'2017,04,22,6.0,-3.9,5.5', '2017,04,23,4.9,-0.1,3.4', '2017,04,24,14.9,0.4,0.0',
'2017,04,25,12.6,-2.5,0.0', '2017,04,26,10.8,3.9,9.4', '2017,04,27,15.3,9.6,19.0',
'2017,04,28,15.6,8.5,2.2', '2017,04,29,20.4,5.8,0.0', '2017,04,30,11.3,3.4,0.0']
WEATHER_LIST_02 = ['2015,05,01,18.0,9.0,0.0', '2015,05,02,16.0,4.1,0.0', '2015,05,04,17.6,5.3,0.0',
'2015,05,05,16.2,6.1,0.4', '2015,05,07,19.6,4.5,0.0', '2015,05,08,22.8,5.5,0.0',
'2015,05,09,22.5,7.0,0.0', '2015,05,10,18.8,8.1,0.0', '2015,05,11,16.8,10.3,0.0',
'2015,05,12,17.5,10.8,0.0', '2015,05,13,16.8,10.5,0.0', '2015,05,14,16.8,9.2,1.2',
'2015,05,15,19.9,7.3,0.0', '2015,05,16,18.5,10.6,0.0', '2015,05,17,19.2,9.6,0.0',
'2015,05,18,21.3,9.2,0.0', '2015,05,19,22.9,9.2,0.0', '2015,05,20,22.4,7.9,0.0',
'2015,05,21,22.3,8.3,0.0', '2015,05,22,18.9,9.8,0.2', '2015,05,23,18.5,8.8,0.0',
'2015,05,24,18.5,9.4,0.0', '2015,05,25,15.6,8.1,0.0', '2015,05,26,19.7,9.4,0.0',
'2015,05,27,21.4,9.0,0.0', '2015,05,28,23.5,9.1,0.0', '2015,05,29,26.3,9.9,0.0',
'2015,05,30,22.5,8.8,0.0', '2015,05,31,20.5,7.7,0.0']
def compute_month_stats(data):
"""
function: compute_month_stats
Given a list of strings containing weather data for one month,
compute summary statistics for that month (maximimum temperature,
minimum temperature, number of days with freezing temperatures,
total precipitation) and return all this as a tuple.
Input parameter:
----------------
* data: A list of strings where each string contains one day's
weather measurements. All of the data in the list is from a
single month. The values are separated by commas, and each day's
string has the same number of values. The order of these values
is: year, month, day, high temp (Celsius), low temp (Celsius),
and precipitation (mm).
Output:
-------
* There is no output to the console from this function.
Return value:
-------------
A tuple with five values is returned. The values are: the month
number corresponding to the data, the maximum high temperature,
the minimum low temperature, the total precipitation for the
month (rounded to one decimal place), and the number of days
having freezing or below-freezing temperatures (i.e., 0.0 C or
lower).
"""
#declaring all needed variables
month=0
max_hi_temp=None
min_low_temp=None
total_precip=0
num_days_freezing=0
#validating length
if len(data)>0:
#extracting month from the first record
#{splitting by comma, taking element at index 1, converting to int}
month=int(data[0].split(',')[1])
#looping through each day in data
for day in data:
#splitting by comma
fields=day.split(',')
#finding hi temperature (value at index 3, converted to float)
hi_temp=float(fields[3])
#finding low temperature
low_temp=float(fields[4])
#finding precipitation
precip=float(fields[5])
#if max_hi_temp is not initialized or is less than hi_temp
#updating max_hi_temp
if max_hi_temp==None or max_hi_temp<hi_temp:
max_hi_temp=hi_temp
# if min_low_temp is not initialized or is greater than low_temp
# updating min_low_temp
if min_low_temp==None or min_low_temp>low_temp:
min_low_temp=low_temp
#adding precip to total precipitation
total_precip+=precip
#if low temp is less than or equal to 0, incrementing num_days_freezing
if low_temp<=0:
num_days_freezing+=1
#returning a tuple containing everything, (total_precip rounded to 2 places)
return (month,max_hi_temp,min_low_temp,round(total_precip,2),num_days_freezing)
return None # data is empty
def main():
weather_data = [WEATHER_LIST_01, WEATHER_LIST_02]
for month_data in weather_data:
stats = compute_month_stats(month_data)
for val in stats[:-1]:
print(val, ",", sep="", end="")
print(stats[-1])
main()
#output
4,23.1,-8.0,91.0,14
5,26.3,4.1,1.8,0
In the lectures about lists we have seen some Python code examples that involve the processing of...
Python code You will need to make use of the random library for this homework. It is pretty simple to use. You can use the following methods: random.choice(list) returns a single item from the list, tuple or string at random. random.sample(list.o - returns a new list of n items randomly chosen from the list, tuple or string random shufflellist)randomly reorders all of the items in the list and changes the list itself. The list must be a mutable object so...
Hello! I have this python Homework due tonight that I don't know how to do. Here is a document with the data in the csv file, as I didn't know how to share it https://docs.google.com/document/d/1bDJVR2MqWKInvw5u0r3fOG3-CBmu3BEiPZwlaq_CShQ/edit?usp=sharing Activity #3: On the class website is a CSV file containing weather data from Coulter Field (in Bryan) for 3 years (1 day is missing for some reason!); the data was taken from Weather Underground (wunderground.com). There are different versions of the file for Windows...
need help finiding slope and placement for the farenheit in the eaquation for a java.util.Scanner COMP163 Greensboro Weather Trends There has been significant debate about global warming. Instead of answering the big question, your program is to determine if (and by how much) Greensboro has been warming for the past few decades. For this assignment you are to write a program that reads a file created with data from the National Climatic Data Center to determine the average annual minimum...
can someone please help me write a python code for this urgently, asap Question: Write a Python function, minmp, that is passed a file name and a metal alloy's formula unit structure*". The file will contain metal elements and their properties. The function will return a tuple of the element from the formula with the lowest melting point and that melting point Write a second function, molform, that will be called by the first function, to take the metal alloy's...
Using C++, create a doubly linked list data structure that stores strings. At a minimum, you must have a List class that contains the list functionality (including an insert function) and a linkable object ("link node") class. For convenience, you may include the data directly or the data may be external to the link node, connected with a reference. You may use an inner class for the LinkNode and/or include the LinkNode class with the List class file if you...
Help needed with Python 3: Dictionaries and Sets. The problem is one that asks the user to create a completed program that prompts the user for the name of a data file, and reads the contents of that data file, then creates variables in a form suitable for computing the answer to some questions. The format should be in the same manner as the attached .py file. All subsequent assignment details (.py and .csv files) can be found at this...
Python coding exercise: please include comments Goal #1: import financial data given into your program provided to you as a CSV formatted text file Use the following data for testing (the following is only a sample of the data; there are over 4000 rows of data): *Note: The data you will read in is linear by date (but, non-contiguous due to holidays and weekends,) reflecting a timeline of stock performance in chronological order; however your program should run through the...
(Python) Write a program called sales.py that uses a list to hold the sums of the sales for each month. The list will have 12 items with index 0 corresponds with month of “Jan”, index 1 with month “Feb”, etc. o Write a function called Convert() which is passed a string which is a month name (ex. “Jan”) and returns the matching index (ex. 0). Hint – use a list of strings to hold the months in the correct order,...
+ Run C Code IMPORTANT: • Run the following code cell to create the input file, biostats.csv, which you will be using later. 74, In [ ]: N %%file biostats.csv Name, Sex, Age, Alex, M, 41, Bert, M, 42, Dave, M, 39, Elly, F, 30, Fran, F, 33, Jake, M, F, Luke, M, 34, F Myra, M, M, 38, Ruth, F, 28, 22 22 323 47 47, Height, Weight 170 200 167 70 115 143 139 280 98 75, 350...
Information About This Project In the realm of database processing, a flat file is a text file that holds a table of records. Here is the data file that is used in this project. The data is converted to comma separated values ( CSV ) to allow easy reading into an array. Table: Consultants ID LName Fee Specialty 101 Roberts 3500 Media 102 Peters 2700 Accounting 103 Paul 1600 Media 104 Michael 2300 Web Design...