修复在列表列中放置连续元素的逻辑

问题描述 投票:0回答:1

我需要创建一个Python程序,按照以下逻辑将

highest_priority_order = "Order B"
放入
schedule_table
中:

这意味着我需要在

schedule_table
的“机器3”栏中添加连续2个小时,

然后在

schedule_table
的“机器2”栏中添加连续2个小时,

然后在

schedule_table
的“机器4”栏中添加连续3小时,

然后在

schedule_table
的“机器1”栏中添加1小时。

j_column_order = [['Machine 3', 2], ['Machine 2', 2], ['Machine 4', 3], ['Machine 1', 1]]

以及其他必要的输入...

# What should be filled into the cells of the schedule_table
highest_priority_order = "Order B"

# The table where the cells should be filled in the available spaces
schedule_table = [
['Hour', 'Machine 1', 'Machine 2', 'Machine 3', 'Machine 4', 'Deadline'],
[1, 'Order A', None, None, None, None],
[2, 'Order A', None, None, None, None],
[3, 'Order A', None, None, None, None],
[4, None, 'Order A', None, None, None],
[5, None, 'Order A', None, None, None],
[6, None, None, 'Order A', None, None],
[7, None, None, 'Order A', None, None],
[8, None, None, None, 'Order A', None],
[9, None, None, None, None, None],
[10, None, None, None, None, 'Order A'],
[11, None, None, None, None, 'Order B'],
[12, None, None, None, None, None],
[13, None, None, None, None, 'Order C'],
[14, None, None, None, None, 'Order D'],
[15, None, None, None, None, None]
]

将小时数放置在 j_column_order 中指示的相应机器列中没有“None”的单元格中,确保每个指示的小时数可以连续放置在指示的列中。

schedule_table = [
['Hour', 'Machine 1', 'Machine 2', 'Machine 3', 'Machine 4', 'Deadline'],
[1, 'Order A', None, 'Order B', None, None],
[2, 'Order A', None, 'Order B', None, None],
[3, 'Order A', None, None, None, None],
[4, None, 'Order A', None, None, None],
[5, None, 'Order A', None, None, None],
[6, None, 'Order B', 'Order A', None, None],
[7, None, 'Order B', 'Order A', None, None],
[8, None, None, None, 'Order A', None],
[9, None, None, None, 'Order B', None],
[10, None, None, None, 'Order B', 'Order A'],
[11, None, None, None, 'Order B', 'Order B'],
[12, None, None, None, None, None],
[13, None, None, None, None, 'Order C'],
[14, None, None, None, None, 'Order D'],
[15, None, None, None, None, None]
]

这是我的代码:

# Definition of the order in which the order should be added
machine_column_order = [['Machine 3', 2], ['Machine 2', 2], ['Machine 4', 3], ['Machine 1', 1]]

def place_order(table, machine_column_order, order):
    current_hour = 1  # Start from the first row after the header

    for machine, hours_needed in machine_column_order:
        column = table[0].index(machine)  # Find the index of the machine column
        
        assigned_hours = 0  # Counter for consecutive assigned hours
        while assigned_hours < hours_needed and current_hour < len(table):
            # If the cell is empty at that hour, assign the order
            if table[current_hour][column] is None:
                table[current_hour][column] = order
                assigned_hours += 1

            # Move to the next hour
            current_hour += 1

# Call the function to place "Order B"
place_order(schedule_table, machine_column_order, priority_order)

# Print the updated schedule table
for row in schedule_table: print(row)

它给出的输出是不正确的,因为虽然它可以防止

"Order B"
同时分配给不同的机器,但每个订单的小时数并不连续。例如,它应该将“订单 B”放置在
"Machine 2"
列中连续
2
小时
['Machine 2', 2]
,但这里它们不是连续的:

[3, 'Order A', 'Order B', None, None, None]
[4, None, 'Order A', None, None, None]
[5, None, 'Order A', None, None, None]
[6, None, 'Order B', 'Order A', None, None]
python arrays python-3.x list sorting
1个回答
0
投票

问题在于,在分配后续时间之前,您没有对该列执行任何类型的检查来查看后续时间是否空闲。您只需选择一列并将订单分配给一台机器,所需时间不限,跳过机器已预留的任何时间。

在为表赋值之前,您应该搜索所需的连续槽位。

由于这是星期五下午,这听起来可能是一个家庭作业问题,我不会为此编写所有代码,但我认为上述信息将为您指明正确的方向。最后你的函数看起来更像这样:

def place_order(table, machine_column_order, order):

    for machine, hours_needed in machine_column_order:
        column = table[0].index(machine)  # Find the index of the machine column
    
        # Iterate over table to find enough consecutive empty slots in the given column.
        #
        # (You may need to handle the case where there is not enough room in the schedule.)
        #
        # If enough consecutive empty slots are found, set current_hour = the first of those
        # consecutive empty slots and continue executing the code below.
                
        assigned_hours = 0  # Counter for consecutive assigned hours
        while assigned_hours < hours_needed:
            table[current_hour][column] = order
            assigned_hours += 1

            # Move to the next hour
            current_hour += 1

希望有帮助。

附注请不要在没有警告的情况下在发布问题后立即对其进行大量编辑。正准备发表答案,问题突然变了……

© www.soinside.com 2019 - 2024. All rights reserved.