Consider a system where drones pick tasks based on their battery level and distance. What is the output of the following code snippet that assigns tasks to drones?
drones = [{'id': 'D1', 'battery': 80, 'distance': 5}, {'id': 'D2', 'battery': 50, 'distance': 3}, {'id': 'D3', 'battery': 90, 'distance': 10}]
tasks = ['Survey', 'Delivery', 'Mapping']
assignments = {}
for task, drone in zip(tasks, sorted(drones, key=lambda d: (d['distance'], -d['battery']))):
assignments[drone['id']] = task
print(assignments)Look at how drones are sorted by distance ascending and battery descending.
The drones are sorted first by distance (lowest first), then by battery (highest first). So order is D2 (3,50), D1 (5,80), D3 (10,90). Tasks are assigned in this order.
In a distributed drone network, what is the main purpose of a consensus algorithm during task allocation?
Consensus means agreement among multiple parties.
Consensus algorithms help all drones agree on task assignments to avoid conflicts or duplicated work.
What error will this code produce when run in a distributed drone system?
tasks = ['Inspect', 'Deliver']
drones = [{'id': 'D1'}, {'id': 'D2'}]
assignments = {}
for i in range(len(tasks)):
assignments[drones[i]['id']] = tasks[i]
assignments[drones[2]['id']] = 'Charge'
print(assignments)Check the index used to access drones in the last assignment.
The drones list has only 2 elements (indices 0 and 1). Accessing drones[2] causes IndexError.
Which option contains the correct syntax to create a dictionary mapping drone IDs to their assigned tasks?
tasks = ['Scan', 'Deliver']
drones = [{'id': 'D1'}, {'id': 'D2'}]Dictionary comprehensions use colon ':' to separate keys and values.
Option C uses correct syntax with colon ':' between key and value. Others use invalid assignment or arrow or missing colon.
Given the following code simulating distributed task allocation, what is the total number of tasks assigned after execution?
tasks = ['A', 'B', 'C', 'D']
drones = [{'id': 'D1', 'capacity': 2}, {'id': 'D2', 'capacity': 1}, {'id': 'D3', 'capacity': 3}]
assignments = {d['id']: [] for d in drones}
task_index = 0
while task_index < len(tasks):
for drone in drones:
if len(assignments[drone['id']]) < drone['capacity'] and task_index < len(tasks):
assignments[drone['id']].append(tasks[task_index])
task_index += 1
if task_index >= len(tasks):
break
count = sum(len(v) for v in assignments.values())
print(count)Count how many tasks are assigned before all tasks run out.
The code assigns tasks in round-robin until all 4 tasks are assigned. Total assigned is 4.