Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.24 KB | None | 0 0
  1. '''
  2. Task 3
  3. a) make table to hold time of highest and lowest tides eg
  4.    [’Wednesday 17 January’,22.37,0.06,4.52,0.70]
  5.    [’Thursday 18 January’,23.08,0.05,5.23,0.73]
  6. b) Compute the average time (in hours since midnight) that
  7.    the highest and lowest tides occur at over the full
  8.    month of data values eg.
  9. Over the full period, on average the lowest and highest tides
  10. occurred at XX.XX and YY.YY hours after midnight
  11. '''
  12.  
  13. tides = open("Tides.txt")
  14. tides_data = []
  15. empty_list = []
  16.  
  17.  
  18. '''all functions here'''
  19. #makes a list of all the data and stores it into tides_data
  20. for line in tides:
  21.     line = line.replace(" meters", "")
  22.     line = line.rstrip()
  23.     line = line.split(",")
  24.     line[1] = float(line[1])
  25.     line[2] = float(line[2])
  26.     empty_list = line
  27.     tides_data.append(empty_list[:])
  28.  
  29. #function that returns list of lists of max and min of tides_list
  30. #and the time:
  31. def max_min(my_list):
  32.  
  33.     values = []     #list for each day
  34.     daily_data = [] #main list of all days
  35.     current_day = my_list[0][0]
  36.     current_low = my_list[0][2]
  37.     current_high = my_list[0][2]
  38.     current_time_low = my_list[0][1]
  39.     current_time_high = my_list[0][1]
  40.  
  41.     for index, value in enumerate(my_list):
  42.  
  43.         #resets day and values when day changes
  44.         if value[0] != current_day:
  45.             values = [current_day, current_time_low, current_low, current_time_high, current_high]
  46.             daily_data.append(values[:])
  47.             current_day = value[0]
  48.             current_low = value[2]
  49.             current_high = value[2]
  50.             current_time_low = value[1]
  51.             current_time_high = value[1]
  52.        
  53.         #if day does not change, finds min and max tides/times
  54.         elif value[2] < current_low:
  55.             current_low = value[2]
  56.             current_time_low = value[1]
  57.  
  58.             if value[2] > current_high:
  59.                 current_high = value[2]  
  60.                 current_time_high = value[1]
  61.  
  62.     return daily_data
  63.  
  64. #function that takes as
  65. # input:    a list of lists (LIST),
  66. #           inner_index of -> LIST[outer_index][inner_index
  67. # output:   new list with all values list[][n]
  68. def new_list(list_of_lists, index):
  69.     new_list = []
  70.     for inside_list_index, inside_list in enumerate(list_of_lists):
  71.         new_list.append(inside_list[index])
  72.     return new_list
  73.  
  74.  
  75.  
  76.  
  77. '''calling functions'''
  78.  
  79. #makes list of only dates, high/low tides, and times
  80. new_tides_data = max_min(tides_data)
  81.  
  82. #creates a list with the    low tide times (index 1)
  83. #                           low tide (index 2)
  84. #                           high tide times (index 3)
  85. #                           high tide (index 4)
  86. low_tide_times =    new_list(new_tides_data, 1)
  87. low_tide =          new_list(new_tides_data, 2)
  88. high_tide_times =   new_list(new_tides_data, 3)
  89. high_tide =         new_list(new_tides_data, 4)
  90.  
  91. #calculates averages of times for low tide, high tide
  92. avg_low_tide_time = sum(low_tide_times) / float(len(low_tide_times))
  93. avg_high_tide_time = sum(high_tide_times) / float(len(high_tide_times))
  94.  
  95. #prints answer
  96. print("Over the full period, on average the lowest sand highest tides occured at %.2f and %.2f hours after midnight" % (round(avg_low_tide_time, 2), round(avg_high_tide_time, 2)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement