Search code examples
pythonarrayspython-3.xlistsorting

Fixing logic toplace consecutive elements in a List Table Column


I need to create a Python program that places highest_priority_order = "Order B" into schedule_table following this logic:

It means that I need to add 2 consecutive hours in the "Machine 3" column of the schedule_table,

then add 2 consecutive hours in the "Machine 2" column of the schedule_table,

then add 3 consecutive hours in the "Machine 4" column of the schedule_table,

and then add 1 hour in the "Machine 1" column of the schedule_table.

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

and the other necessary inputs...

# 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]
]

Place the hours in the cells where there is None in the corresponding machine columns indicated in j_column_order, ensuring that the hours for each indication can be placed consecutively in the indicated column.

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]
]

This is my code:

# 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)

The output it gives is incorrect, because although it prevents "Order B" from being assigned to different machines at the same time, the hours for each order are not consecutive. For example, it was supposed to place "Order B" in the "Machine 2" column for 2 consecutive hours ['Machine 2', 2], but here they are not consecutive:

[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]

Solution

  • The problem is that you're not performing any kind of check on the column to see whether subsequent hours are free before you assign them. You're just picking a column and assigning the order to a machine for as many hours as you need skipping any hours that the machine is already reserved for.

    You should search for the consecutive slots you need before assigning values to the table.

    Since it's a Friday afternoon and this sounds like it might be a homework problem, I'm not going to write out all the code for this, but I think the above information will point you in the right direction. In the end your function will look something more like this:

    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
    

    Hope that helps.

    P.S. Please don't make substantial edits to your question right after posting it without warning. I was about to post an answer when the problem suddenly changed...