📖 Scenario: You are programming a group of delivery drones. Each drone can carry out tasks like delivering packages to different locations. To work efficiently, drones need to decide which tasks to take based on their current battery level and task distance.
🎯 Goal: Build a simple program that assigns tasks to drones based on their battery level and the distance of each task. The program will create a list of tasks each drone can handle without running out of battery.
📋 What You'll Learn
Create a dictionary of drones with their battery levels
Create a dictionary of tasks with their distances
Set a battery usage rate per distance unit
Assign tasks to drones if the drone has enough battery for the task
Print the assigned tasks for each drone
💡 Why This Matters
🌍 Real World
Delivery companies use distributed task allocation to assign packages to drones efficiently, saving battery and time.
💼 Career
Understanding task allocation algorithms is important for roles in robotics programming, logistics automation, and drone fleet management.
Progress0 / 4 steps
1
Create drones and tasks data
Create a dictionary called drones with these exact entries: 'DroneA': 100, 'DroneB': 80, 'DroneC': 50 representing battery levels. Also create a dictionary called tasks with these exact entries: 'Task1': 30, 'Task2': 60, 'Task3': 40 representing task distances.
Drone Programming
Hint
Use curly braces to create dictionaries. Put the drone names as keys and battery levels as values. Do the same for tasks with distances.
2
Set battery usage rate per distance
Create a variable called battery_usage_per_km and set it to 1 to represent battery used per distance unit.
Drone Programming
Hint
This variable will help calculate if a drone has enough battery for a task.
3
Assign tasks to drones based on battery
Create an empty dictionary called assigned_tasks. Use a for loop with variables drone and battery to iterate over drones.items(). Inside it, create an empty list for each drone in assigned_tasks. Then use another for loop with variables task and distance to iterate over tasks.items(). If battery is greater than or equal to distance * battery_usage_per_km, append task to the drone's list in assigned_tasks.
Drone Programming
Hint
Use nested loops to check each task for each drone. Use the battery check to decide if the drone can do the task.
4
Print the assigned tasks for each drone
Use a for loop with variables drone and tasks_list to iterate over assigned_tasks.items(). Inside the loop, print the string f"{drone} can do tasks: {tasks_list}".
Drone Programming
Hint
Use a loop to print each drone's name and the list of tasks it can do.
Practice
(1/5)
1. What is the main goal of distributed task allocation in drone programming?
easy
A. To share work fairly among drones without a central controller
B. To make one drone do all the tasks quickly
C. To randomly assign tasks without any order
D. To stop drones from communicating with each other
Solution
Step 1: Understand distributed task allocation
It means dividing tasks so all drones get work fairly.
Step 2: Identify the goal in drone programming
The goal is to avoid a single controller and let drones share tasks efficiently.
Final Answer:
To share work fairly among drones without a central controller -> Option A
Quick Check:
Distributed task allocation = fair work sharing [OK]
Hint: Remember: share tasks fairly, no single boss drone [OK]
Common Mistakes:
Thinking one drone does all tasks
Assuming tasks are assigned randomly
Believing drones don't communicate
2. Which of the following is the correct way to assign tasks to drones in a round-robin style using Python?
easy
A. task_index * len(drones)
B. len(drones) % task_index
C. task_index + len(drones)
D. task_index % len(drones)
Solution
Step 1: Understand round-robin assignment
Round-robin cycles through drones by using modulo with drone count.
Step 2: Apply modulo operator correctly
Using task_index % len(drones) cycles task indices over drone list indices.
Final Answer:
task_index % len(drones) -> Option D
Quick Check:
Modulo with drone count cycles tasks [OK]
Hint: Use modulo (%) with drone count to cycle tasks [OK]
Common Mistakes:
Reversing modulo operands
Adding or multiplying instead of modulo
Using modulo with wrong variable
3. What will be the output of this code snippet?
tasks = ['scan', 'deliver', 'map', 'charge']
drones = ['droneA', 'droneB']
assignments = {}
for i, task in enumerate(tasks):
drone = drones[i % len(drones)]
assignments.setdefault(drone, []).append(task)
print(assignments)
medium
A. {'droneA': ['scan', 'map'], 'droneB': ['deliver', 'charge']}
B. {'droneA': ['scan', 'deliver'], 'droneB': ['map', 'charge']}
C. {'droneA': ['scan'], 'droneB': ['deliver', 'map', 'charge']}
D. {'droneA': ['charge'], 'droneB': ['scan', 'deliver', 'map']}
Solution
Step 1: Assign tasks using modulo
Tasks are assigned to drones cycling through drones list by i % 2.
{'droneA': ['scan', 'map'], 'droneB': ['deliver', 'charge']} -> Option A
Quick Check:
Modulo cycles tasks evenly [OK]
Hint: Match task index modulo drone count to assign tasks [OK]
Common Mistakes:
Mixing task order in assignments
Ignoring modulo cycling
Appending tasks to wrong drone
4. Identify the error in this drone task allocation code:
drones = ['D1', 'D2', 'D3']
tasks = ['photo', 'survey', 'deliver']
assignments = {}
for i in range(len(tasks)):
drone = drones[i // len(drones)]
assignments.setdefault(drone, []).append(tasks[i])
print(assignments)
medium
A. assignments.setdefault is used incorrectly
B. Using range(len(tasks)) is incorrect for looping
C. Using integer division (//) instead of modulo (%) assigns all tasks to the first drone
D. The drones list should be empty
Solution
Step 1: Check drone index calculation
Using i // len(drones) results in index 0 for i=0,1,2 (len(drones)=3), assigning all tasks to 'D1' instead of cycling round-robin.
Step 2: Identify correct operator
Modulo (%) should be used to cycle through drone indices safely.
Final Answer:
Using integer division (//) instead of modulo (%) assigns all tasks to the first drone -> Option C
Quick Check:
Use modulo (%) to cycle indices [OK]
Hint: Use % not // to cycle through drone indices [OK]
Common Mistakes:
Confusing // with %
Thinking range(len(tasks)) is wrong
Misusing setdefault method
5. You have 3 drones and 7 tasks. You want to assign tasks so each drone gets tasks in a round-robin way but skip any task named 'wait'. Which code snippet correctly implements this?
hard
A. assignments = {}
for i, task in enumerate(tasks):
if task == 'wait':
continue
drone = drones[len(drones) % i]
assignments.setdefault(drone, []).append(task)
B. assignments = {}
for i, task in enumerate(tasks):
drone = drones[i % len(drones)]
if task != 'wait':
assignments.setdefault(drone, []).append(task)
C. assignments = {}
for task in tasks:
if task == 'wait':
continue
drone = drones[tasks.index(task) % len(drones)]
assignments.setdefault(drone, []).append(task)
D. assignments = {}
for i in range(len(tasks)):
if tasks[i] == 'wait':
continue
drone = drones[i // len(drones)]
assignments.setdefault(drone, []).append(tasks[i])
Solution
Step 1: Filter out 'wait' tasks correctly
Check task != 'wait' before assigning to drones.
Step 2: Assign tasks using modulo for round-robin
Use i % len(drones) to cycle drones correctly.
Step 3: Avoid using index() inside loop to prevent errors with duplicates
Using tasks.index(task) can cause wrong drone assignment if tasks repeat.
Final Answer:
assignments = {}
for i, task in enumerate(tasks):
drone = drones[i % len(drones)]
if task != 'wait':
assignments.setdefault(drone, []).append(task) -> Option B
Quick Check:
Filter first, then assign with modulo [OK]
Hint: Check task before assign; use modulo for drone cycling [OK]
Common Mistakes:
Using integer division instead of modulo
Using tasks.index() causing wrong assignments
Not skipping 'wait' tasks properly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.