priority = [ 2, 6, 1, 7, 10, 8, 5]
time = [30,200,20,70,120,60, 150]
#
# define number of tasks: don't hardwire this but rather use the len() command
n_tasks= len(priority)
max_time = 480
print ( f"TASK PRIORITY TIME TO COMPLETE")
# you need to add an appropriate for loop here and modify the next print statement so that you can print out all tasks
for item in range (0,n_tasks):
print (f" {1+item} {priority[item]} {time[item]}")
TASK PRIORITY TIME TO COMPLETE
1 2 30
2 6 200
3 1 20
4 7 70
5 10 120
6 8 60
7 5 150
test_list = [ 4, 5, 3, 7, 11, 2]
max_value = max(test_list) # use function max() to find maximum value in list
max_index = test_list.index(max_value) # use method list.index () to find the index where this max occurs
print(f'The maximum value {max_value} occurs at index {max_index}') # print out results in formatted statement
The maximum value 11 occurs at index 4
test_list = [ 4, 5, 3, 7, 11, 2]
time_list = [30, 45, 20, 120, 90, 70 ]
max_value = max(test_list)
max_index = test_list.index(max_value)
task_time = time_list[max_index] # input the time it takes to complete this task
print(f'The maximum value {max_value} occurs at index {max_index} and this task takes {task_time}') # print out values as in Step #2 plus the time it takes to complete task
The maximum value 11 occurs at index 4 and this task takes 90
max_time = 6 * 60 # this is not the maximum time available for your example but rather for testing
time_used = 3 * 60
#
# Following 6 statements are from Step 3
test_list = [ 4, 5, 3, 7, 11, 2]
time_list = [30, 45, 20, 120, 90, 70 ]
max_value = max(test_list)
max_index = test_list.index(max_value)
task_time = time_list[max_index]
print(f'The maximum value {max_value} occurs at index {max_index} and this task takes {task_time}')
#
# Add conditional to see if this task can be completed in the allotted time
if (time_used+task_time<=max_time):
print(f"Adding item number {max_index} makes the current time used to {task_time+time_used}")
The maximum value 11 occurs at index 4 and this task takes 90
Adding item number 4 makes the current time used to 270
# Input problem specific data and print it out from Step 1
priority = [ 2, 6, 1, 7, 10, 8, 5]
time = [30,200,20,70,120,60, 150]
n_tasks = len(priority)
max_time = 480
#
# Initialize any variables before loop
cur_priority=0.
cur_time=0.
#
# add for loop to go through all tasks
for item in range(0,n_tasks):
item_priority=max(priority)
max_index=priority.index(item_priority)
item_time=time[max_index]
print(f"Try item {max_index+1}")
if(cur_time+item_time<=max_time):
print(f"Add item number {max_index+1} to schedule with time {item_time}")
cur_time=cur_time+item_time
cur_priority=cur_priority+item_priority
print(f"Current time on schedule is {cur_time} and current priority is {cur_priority}")
else:
print(f"Item number {max_index+1} does not fit so discard")
priority[max_index]=0
print(f"Current time on schedule is {cur_time}")
Try item 5
Add item number 5 to schedule with time 120
Current time on schedule is 120.0 and current priority is 10.0
Try item 6
Add item number 6 to schedule with time 60
Current time on schedule is 180.0 and current priority is 18.0
Try item 4
Add item number 4 to schedule with time 70
Current time on schedule is 250.0 and current priority is 25.0
Try item 2
Add item number 2 to schedule with time 200
Current time on schedule is 450.0 and current priority is 31.0
Try item 7
Item number 7 does not fit so discard
Try item 1
Add item number 1 to schedule with time 30
Current time on schedule is 480.0 and current priority is 33.0
Try item 3
Item number 3 does not fit so discard
Current time on schedule is 480.0
# Input problem specific data and print it out from Step 1
priority = [ 2, 6, 1, 7, 10, 8, 5]
time = [30,200,20,70,120,60, 150]
n_tasks = len(priority)
max_time = 480
#
# Initialize any variables before loop
cur_priority=0.
cur_time=0.
#
# add for loop to go through all tasks
#
for item in range(0,n_tasks):
item_priority=max(priority)
max_index=priority.index(item_priority)
item_time=time[max_index]
print(f"Try item {max_index+1}")
if(cur_time+item_time<=max_time):
print(f"Add item number {max_index+1} to schedule with time {item_time}")
cur_time=cur_time+item_time
cur_priority=cur_priority+item_priority
print(f"Current time on schedule is {cur_time} and current priority of schedule is {cur_priority}")
if(cur_time==max_time):
break
else:
print(f"Item number {max_index+1} does not fit so discard")
priority[max_index]=0
Try item 5
Add item number 5 to schedule with time 120
Current time on schedule is 120.0 and current priority of schedule is 10.0
Try item 6
Add item number 6 to schedule with time 60
Current time on schedule is 180.0 and current priority of schedule is 18.0
Try item 4
Add item number 4 to schedule with time 70
Current time on schedule is 250.0 and current priority of schedule is 25.0
Try item 2
Add item number 2 to schedule with time 200
Current time on schedule is 450.0 and current priority of schedule is 31.0
Try item 7
Item number 7 does not fit so discard
Try item 1
Add item number 1 to schedule with time 30
Current time on schedule is 480.0 and current priority of schedule is 33.0
# Input problem specific data
time = [30,200,20,70,120,60, 150]
n_tasks = len(priority)
max_time = 480
#
# Create new priority list containing ratios
#
priority= [.06, .03, .05, .1, .083, .13, .033] # initialize the list which contains the new priorities which are original priority divided by
# time it takes to complete this task
cur_priority=0.
cur_time=0.
for item in range(0,n_tasks):
item_priority=max(priority)
max_index=priority.index(item_priority)
item_time=time[max_index]
print(f"Try item {max_index+1}")
if(cur_time+item_time<=max_time):
print(f"Add item number {max_index+1} to schedule with time {item_time}")
cur_time=cur_time+item_time
cur_priority=cur_priority+item_priority
print(f"Current time on schedule is {cur_time} and current priority of schedule is {cur_priority}")
if(cur_time==max_time):
break
else:
print(f"Item number {max_index+1} does not fit so discard")
priority[max_index]=0
Try item 6
Add item number 6 to schedule with time 60
Current time on schedule is 60.0 and current priority of schedule is 0.13
Try item 4
Add item number 4 to schedule with time 70
Current time on schedule is 130.0 and current priority of schedule is 0.23
Try item 5
Add item number 5 to schedule with time 120
Current time on schedule is 250.0 and current priority of schedule is 0.313
Try item 1
Add item number 1 to schedule with time 30
Current time on schedule is 280.0 and current priority of schedule is 0.373
Try item 3
Add item number 3 to schedule with time 20
Current time on schedule is 300.0 and current priority of schedule is 0.423
Try item 7
Add item number 7 to schedule with time 150
Current time on schedule is 450.0 and current priority of schedule is 0.45599999999999996
Try item 2
Item number 2 does not fit so discard