Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. def compute_month_stats(data):
  2.  
  3. """
  4.  
  5. function: compute_month_stats
  6.  
  7. Given a list of strings containing weather data for one month,
  8.  
  9. compute summary statistics for that month (maximimum temperature,
  10.  
  11. minimum temperature, number of days with freezing temperatures,
  12.  
  13. total precipitation) and return all this as a tuple.
  14.  
  15. Input parameter:
  16.  
  17. ----------------
  18.  
  19. * data: A list of strings where each string contains one day's
  20.  
  21. weather measurements. All of the data in the list is from a
  22.  
  23. single month. The values are separated by commas, and each day's
  24.  
  25. string has the same number of values. The order of these values
  26.  
  27. is: year, month, day, high temp (Celsius), low temp (Celsius),
  28.  
  29. and precipitation (mm).
  30.  
  31. Output:
  32.  
  33. -------
  34.  
  35. * There is no output to the console from this function.
  36.  
  37. Return value:
  38.  
  39. -------------
  40.  
  41. A tuple with five values is returned. The values are: the month
  42.  
  43. number corresponding to the data, the maximum high temperature,
  44.  
  45. the minimum low temperature, the total precipitation for the
  46.  
  47. month (rounded to one decimal place), and the number of days
  48.  
  49. having freezing or below-freezing temperatures (i.e., 0.0 C or
  50.  
  51. lower).
  52.  
  53. """
  54.  
  55. #declaring all needed variables
  56.  
  57. month=0
  58.  
  59. max_hi_temp=None
  60.  
  61. min_low_temp=None
  62.  
  63. total_precip=0
  64.  
  65. num_days_freezing=0
  66.  
  67. #validating length
  68.  
  69. if len(data)>0:
  70.  
  71. #extracting month from the first record
  72.  
  73. #{splitting by comma, taking element at index 1, converting to int}
  74.  
  75. month=int(data[0].split(',')[1])
  76.  
  77. #looping through each day in data
  78.  
  79. for day in data:
  80.  
  81. #splitting by comma
  82.  
  83. fields=day.split(',')
  84.  
  85. #finding hi temperature (value at index 3, converted to float)
  86.  
  87. hi_temp=float(fields[3])
  88.  
  89. #finding low temperature
  90.  
  91. low_temp=float(fields[4])
  92.  
  93. #finding precipitation
  94.  
  95. precip=float(fields[5])
  96.  
  97. #if max_hi_temp is not initialized or is less than hi_temp
  98.  
  99. #updating max_hi_temp
  100.  
  101. if max_hi_temp==None or max_hi_temp<hi_temp:
  102.  
  103. max_hi_temp=hi_temp
  104.  
  105. # if min_low_temp is not initialized or is greater than low_temp
  106.  
  107. # updating min_low_temp
  108.  
  109. if min_low_temp==None or min_low_temp>low_temp:
  110.  
  111. min_low_temp=low_temp
  112.  
  113. #adding precip to total precipitation
  114.  
  115. total_precip+=precip
  116.  
  117. #if low temp is less than or equal to 0, incrementing num_days_freezing
  118.  
  119. if low_temp<=0:
  120.  
  121. num_days_freezing+=1
  122.  
  123. #returning a tuple containing everything, (total_precip rounded to 2 places)
  124.  
  125. return (month,max_hi_temp,min_low_temp,round(total_precip,2),num_days_freezing)
  126.  
  127. return None # data is empty
  128.  
  129. def main():
  130.  
  131. weather_data = [WEATHER_LIST_01, WEATHER_LIST_02]
  132.  
  133. for month_data in weather_data:
  134.  
  135. stats = compute_month_stats(month_data)
  136.  
  137. for val in stats[:-1]:
  138.  
  139. print(val, ",", sep="", end="")
  140.  
  141. print(stats[-1])
  142.  
  143. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement