def create_list_of_allowed_hours(prices, amount_of_hours_to_be_blocked, max_amount_of_continuos_blocked_hours):
"""
Create a list of hours to be blocked based on the given constraints.
Parameters:
- prices (list of float): A list of 24 hourly electricity prices.
- amount_of_hours_to_be_blocked (int): Number of hours to block.
- max_amount_of_continuos_blocked_hours (int): Maximum number of continuous hours that can be blocked.
Returns:
- list of int: A list of indices representing the hours to block.
"""
if len(prices) != 24:
raise ValueError("The prices list should have 24 elements.")
indexed_prices = [(price, index) for index, price in enumerate(prices)]
indexed_prices.sort(key=lambda x: x[0], reverse=True)
blocked_hours = []
for _, index in indexed_prices:
if can_block_hour(blocked_hours, index, max_amount_of_continuos_blocked_hours):
blocked_hours.append(index)
if len(blocked_hours) == amount_of_hours_to_be_blocked:
break
return [i for i in range(24) if i not in blocked_hours]
def can_block_hour(blocked_hours, hour, max_continuous):
for i in range(1, max_continuous + 1):
if (hour - i) % 24 not in blocked_hours and (hour + i) % 24 not in blocked_hours:
return True
return False
def print_allowed_hours_info(prices, allowed_hours):
"""
Print the price for each hour, indicating which hours are selected and which are blocked.
Parameters:
- prices (list of float): A list of 24 hourly electricity prices.
- allowed_hours (list of int): A list of indices representing the hours that are allowed.
"""
total_price = 0
print("Hour\tPrice\tSelected\tBlocked")
print("------------------------------------")
for hour in range(24):
selected = "SELECTED" if hour in allowed_hours else ""
blocked = "BLOCKED" if hour not in allowed_hours else ""
print("{}\t{}\t{}\t{}".format(hour, prices[hour], selected, blocked))
if hour in allowed_hours:
total_price += prices[hour]
average_price_for_day = sum(prices) / 24
average_price_for_allowed_hours = total_price / len(allowed_hours)
print("------------------------------------")
print("Total price for allowed hours: {}".format(total_price))
print("Average price for allowed hours: {:.2f}".format(average_price_for_allowed_hours))
print("Average price for the day: {:.2f}".format(average_price_for_day))
# Example usage:
prices = [50, 45, 40, 35, 30, 80, 90, 100, 85, 55, 50, 45, 40, 35, 30, 80, 90, 100, 85, 55, 50, 45, 40, 35]
allowed_hours = create_list_of_allowed_hours(prices, 10, 3)
print_allowed_hours_info(prices, allowed_hours)
Average price for the day: 57.00