rainfall data coding practice

rainfall data coding practice

Jul 28, 2025

Fun exercise that I did in class today

# each row represents one day of predicted rainfall data from Monday to Sunday
# cols represent predicted rainfall for each hour, beginning at midnight
rainfall_data = [
    [0, 0, 0, 0, 0, 1, 2, 5, 10, 12, 15, 8, 6, 4, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0],
    [1, 1, 1, 0, 0, 0, 2, 4, 7, 9, 11, 13, 14, 13, 10, 8, 5, 3, 2, 1, 1, 1, 0, 0],
    [0, 0, 0, 1, 2, 3, 4, 6, 8, 12, 16, 20, 18, 15, 10, 8, 5, 3, 2, 1, 0, 0, 0, 0],
    [2, 2, 2, 2, 3, 5, 7, 10, 14, 17, 20, 22, 19, 15, 12, 9,  7, 5, 3, 2, 2, 2, 2, 2],
    [0, 0, 0, 0, 0, 0, 0, 1, 2,  2,  2,  3,  3, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [5, 4, 3, 2, 2, 3, 6, 9, 12, 15, 18, 20, 17, 14, 10, 7, 5, 3, 2, 1, 0, 0, 0, 0],
    [0, 0, 1, 1, 1, 1, 2, 3, 5, 7, 10, 12, 11, 9, 7,  5, 3, 1, 0, 0, 0, 0, 0, 0],
]
days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]


def total_rainfall_per_day(rainfall_data):
    """
    takes in the rainfall data and returns a dict with days as keys and total as value
    """
    output = {}
    for day_index, day_data in enumerate(rainfall_data):
        output[days[day_index]] = sum(day_data)
    
    return output


# return day name e.g. Thursday
def day_with_most_rainfall(rainfall_data, days):
    index = 0
    most = 0
    
    for day_index, day in enumerate(rainfall_data):
        if sum(day) > most:
            most = sum(day)
            index = day_index
    
    return days[index]


def total_weekly_rainfall(rainfall_data):
    total = 0
    
    for day in rainfall_data:
        total += sum(day)
    
    return total


#find the hour (0-23) whose sum of the predicted rainfall is the smallest

# 22 or 11?
def least_rainfall_hour(rainfall_data):
    least_hour = 0
    least_total = float("inf")
    
    for hour in range(24):
        total = 0
        for day in rainfall_data:
            total += day[hour]
        if total < least_total:
            least_hour = hour
            least_total = total
    
    return least_hour
        

# return a list containing the average rainfall for each day
def average_daily_rainfall(rainfall_data):
    output = []
    for day_data in rainfall_data:
        output.append(sum(day_data) / 24)
    
    return output


# find the highest number of consecutive hours where the predicted rainfall is 0
def longest_dry_period(rainfall_data):
    # flatten 2d list so it is easier to go across days when looping
    flattened_data = []
    for day in rainfall_data:
        for hour in day:
            flattened_data.append(hour)
    
    highest = 0
    current = 0
    for entry in flattened_data:
        if entry == 0:
            current += 1
        else:
            current = 0
        if current > highest:
            highest = current
    
    return highest


# given num_hours e.g. 2, find the window of time of that size e.g. 1100 to 1300 that has the least rainfall
def least_rain_window(rainfall_data, num_hours):
    # flatten 2d list so it is easier to go across days when looping
    flattened_data = []
    for day in rainfall_data:
        for hour in day:
            flattened_data.append(hour)
    
    lowest = float("inf")
    lowest_index = 0
    for window_start in range(len(flattened_data) - num_hours):  # end early to avoid index error
        window = sum(flattened_data[window_start:window_start + num_hours])
        # print(window_start, window)
        if window < lowest:
            lowest_index = window_start
            lowest = window
    
    # turn lowest index into day and 24 hr time
    day = days[lowest_index // 24]
    time_start = lowest_index - 24 * (lowest_index // 24)
    time_end = lowest_index + num_hours
    day_end = days[time_end // 24]
    time_end = time_end - 24 * (time_end // 24)
    
    # big fancy f-string stuff to look good for printing
    time_start_str = f'{"0" if time_start < 10 else ""}{time_start}00'
    time_end_str = f'{"0" if time_end < 10 else ""}{time_end}00'

    time = f'{day} {time_start_str}  -- {day_end} {time_end_str}'
    return time


# testing
print(total_rainfall_per_day(rainfall_data))
print(day_with_most_rainfall(rainfall_data, days))
print(total_weekly_rainfall(rainfall_data))
print(least_rainfall_hour(rainfall_data))
print(average_daily_rainfall(rainfall_data))
print(longest_dry_period(rainfall_data))
print(least_rain_window(rainfall_data, 27))
image

Here's a cool pic from Warframe as a bonus!

Vous aimez cette publication ?

Achetez un café à unseeyou

Plus de unseeyou

ConfidentialitéConditionsSignaler