Challenge - 5 Problems
Swarm Collision Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of simple collision avoidance logic
Consider a swarm of drones where each drone checks the distance to its neighbors and adjusts its velocity to avoid collisions. What is the output of the following code snippet simulating one drone's velocity adjustment?
Drone Programming
neighbors = [(2, 3), (5, 5), (1, 1)] current_position = (3, 3) velocity = [1, 1] for nx, ny in neighbors: dx = current_position[0] - nx dy = current_position[1] - ny dist_sq = dx*dx + dy*dy if dist_sq < 5: velocity[0] += dx velocity[1] += dy print(tuple(velocity))
Attempts:
2 left
💡 Hint
Check which neighbors are within the distance threshold and how velocity changes.
✗ Incorrect
The drone checks neighbors within distance squared less than 5. Only neighbor at (2,3) is close enough (dist_sq=(3-2)^2 + (3-3)^2 = 1 < 5). Neighbors at (5,5) and (1,1) have dist_sq=20 >=5. Velocity is adjusted by adding dx=1, dy=0 to [1,1], resulting in (2,1).
🧠 Conceptual
intermediate1:30remaining
Key principle behind collision avoidance in swarms
What is the main principle that allows drones in a swarm to avoid collisions while moving together?
Attempts:
2 left
💡 Hint
Think about how drones keep safe space around themselves.
✗ Incorrect
Collision avoidance relies on drones sensing neighbors and adjusting their velocity to keep a safe distance, preventing crashes while moving cohesively.
🔧 Debug
advanced2:30remaining
Identify the error causing collision in swarm simulation
The following code is intended to prevent collisions by adjusting drone velocities. However, collisions still occur. What is the bug?
Drone Programming
def avoid_collision(position, neighbors): velocity = [0, 0] for nx, ny in neighbors: dx = position[0] - nx dy = position[1] - ny dist_sq = dx*dx + dy*dy if dist_sq < 4: velocity[0] -= dx velocity[1] -= dy return velocity # Drone at (3,3) with neighbors print(avoid_collision((3,3), [(2,3), (4,4), (3,5)]))
Attempts:
2 left
💡 Hint
Check how dx and dy are calculated and used to update velocity.
✗ Incorrect
The code calculates dx and dy as position minus neighbor, but then subtracts dx and dy from velocity, which reverses the intended avoidance direction. This causes drones to move toward neighbors instead of away, causing collisions.
📝 Syntax
advanced1:30remaining
Syntax error in collision avoidance velocity update
Which option contains the correct syntax for updating a drone's velocity to avoid neighbors within a threshold?
Drone Programming
neighbors = [(1,2), (3,4)] position = (2,2) velocity = [0,0] for nx, ny in neighbors: dx = position[0] - nx dy = position[1] - ny dist_sq = dx*dx + dy*dy if dist_sq < 5 velocity[0] += dx velocity[1] += dy print(velocity)
Attempts:
2 left
💡 Hint
Check the syntax of the if statement.
✗ Incorrect
Python requires a colon at the end of an if statement. Missing colon causes SyntaxError. Other options cause syntax or logic errors.
🚀 Application
expert3:00remaining
Calculate number of drones avoiding collision in a swarm
Given a swarm of 5 drones at positions [(0,0), (1,1), (3,3), (5,5), (1,0)], each drone avoids neighbors closer than distance 2 by adjusting velocity. How many drones will adjust their velocity in one update cycle?
Attempts:
2 left
💡 Hint
Check pairs of drones closer than distance 2 and count how many have at least one neighbor that close.
✗ Incorrect
Drones at (0,0), (1,1), and (1,0) have neighbors within distance 2, so they adjust velocity. Drones at (3,3) and (5,5) are too far from others to adjust. So 3 drones adjust velocity.