0
0
Drone Programmingprogramming~20 mins

Distributed task allocation in Drone Programming - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Distributed Task Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of task assignment in distributed drones

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?

Drone Programming
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)
A{"D2": "Survey", "D3": "Delivery", "D1": "Mapping"}
B{"D2": "Survey", "D1": "Delivery", "D3": "Mapping"}
C{"D1": "Survey", "D2": "Delivery", "D3": "Mapping"}
D{"D3": "Survey", "D1": "Delivery", "D2": "Mapping"}
Attempts:
2 left
💡 Hint

Look at how drones are sorted by distance ascending and battery descending.

🧠 Conceptual
intermediate
1:30remaining
Understanding consensus in distributed task allocation

In a distributed drone network, what is the main purpose of a consensus algorithm during task allocation?

ATo reduce the number of drones in the network
BTo maximize the battery usage of a single drone
CTo randomly assign tasks without communication
DTo ensure all drones agree on which tasks are assigned to which drones
Attempts:
2 left
💡 Hint

Consensus means agreement among multiple parties.

🔧 Debug
advanced
2:00remaining
Identify the error in task allocation code

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)
AIndexError: list index out of range
BKeyError: 'id'
CTypeError: unsupported operand type(s)
DNo error, prints {'D1': 'Inspect', 'D2': 'Deliver', 'D3': 'Charge'}
Attempts:
2 left
💡 Hint

Check the index used to access drones in the last assignment.

📝 Syntax
advanced
1:30remaining
Syntax error in distributed task allocation code

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'}]
Aassignments = {drone['id'] -> task for drone, task in zip(drones, tasks)}
Bassignments = {drone['id'] = task for drone, task in zip(drones, tasks)}
Cassignments = {drone['id']: task for drone, task in zip(drones, tasks)}
Dassignments = {drone['id'] task for drone, task in zip(drones, tasks)}
Attempts:
2 left
💡 Hint

Dictionary comprehensions use colon ':' to separate keys and values.

🚀 Application
expert
2:30remaining
Calculate total tasks assigned after distributed allocation

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)
A4
B3
C5
D6
Attempts:
2 left
💡 Hint

Count how many tasks are assigned before all tasks run out.