Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement