Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- Task 3
- a) make table to hold time of highest and lowest tides eg
- [’Wednesday 17 January’,22.37,0.06,4.52,0.70]
- [’Thursday 18 January’,23.08,0.05,5.23,0.73]
- b) Compute the average time (in hours since midnight) that
- the highest and lowest tides occur at over the full
- month of data values eg.
- Over the full period, on average the lowest and highest tides
- occurred at XX.XX and YY.YY hours after midnight
- '''
- tides = open("Tides.txt")
- tides_data = []
- empty_list = []
- '''all functions here'''
- #makes a list of all the data and stores it into tides_data
- for line in tides:
- line = line.replace(" meters", "")
- line = line.rstrip()
- line = line.split(",")
- line[1] = float(line[1])
- line[2] = float(line[2])
- empty_list = line
- tides_data.append(empty_list[:])
- #function that returns list of lists of max and min of tides_list
- #and the time:
- def max_min(my_list):
- values = [] #list for each day
- daily_data = [] #main list of all days
- current_day = my_list[0][0]
- current_low = my_list[0][2]
- current_high = my_list[0][2]
- current_time_low = my_list[0][1]
- current_time_high = my_list[0][1]
- for index, value in enumerate(my_list):
- #resets day and values when day changes
- if value[0] != current_day:
- values = [current_day, current_time_low, current_low, current_time_high, current_high]
- daily_data.append(values[:])
- current_day = value[0]
- current_low = value[2]
- current_high = value[2]
- current_time_low = value[1]
- current_time_high = value[1]
- #if day does not change, finds min and max tides/times
- elif value[2] < current_low:
- current_low = value[2]
- current_time_low = value[1]
- if value[2] > current_high:
- current_high = value[2]
- current_time_high = value[1]
- return daily_data
- #function that takes as
- # input: a list of lists (LIST),
- # inner_index of -> LIST[outer_index][inner_index
- # output: new list with all values list[][n]
- def new_list(list_of_lists, index):
- new_list = []
- for inside_list_index, inside_list in enumerate(list_of_lists):
- new_list.append(inside_list[index])
- return new_list
- '''calling functions'''
- #makes list of only dates, high/low tides, and times
- new_tides_data = max_min(tides_data)
- #creates a list with the low tide times (index 1)
- # low tide (index 2)
- # high tide times (index 3)
- # high tide (index 4)
- low_tide_times = new_list(new_tides_data, 1)
- low_tide = new_list(new_tides_data, 2)
- high_tide_times = new_list(new_tides_data, 3)
- high_tide = new_list(new_tides_data, 4)
- #calculates averages of times for low tide, high tide
- avg_low_tide_time = sum(low_tide_times) / float(len(low_tide_times))
- avg_high_tide_time = sum(high_tide_times) / float(len(high_tide_times))
- #prints answer
- 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