Hover condition (thrust = weight) in Drone Programming - Time & Space Complexity
We want to understand how the time needed to check if a drone can hover changes as we look at more data points.
Specifically, how the program's work grows when checking thrust against weight repeatedly.
Analyze the time complexity of the following code snippet.
for i in range(len(sensor_readings)):
thrust = calculate_thrust(sensor_readings[i])
weight = get_weight(sensor_readings[i])
if thrust == weight:
hover_status[i] = True
else:
hover_status[i] = False
This code checks for each sensor reading if the drone's thrust equals its weight to decide if it is hovering.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each sensor reading once.
- How many times: Exactly once per sensor reading, so as many times as the number of readings.
Each new sensor reading adds one more check of thrust versus weight.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The work grows directly with the number of sensor readings.
Time Complexity: O(n)
This means the time to check hover condition grows in a straight line with the number of readings.
[X] Wrong: "The program checks all readings instantly, so time does not grow with input size."
[OK] Correct: Each reading must be checked one by one, so more readings mean more work and more time.
Understanding how loops affect time helps you explain how your drone code scales with more data, a useful skill in real projects.
"What if we added a nested loop to compare each reading with every other reading? How would the time complexity change?"
Practice
thrust == weight mean for a drone?Solution
Step 1: Understand thrust and weight
Thrust is the upward force from the drone's propellers, and weight is the downward force due to gravity.Step 2: Analyze the equality condition
When thrust equals weight, the forces balance, so the drone neither rises nor falls.Final Answer:
The drone stays steady in the air without moving up or down. -> Option DQuick Check:
Hover condition means thrust = weight [OK]
- Thinking thrust > weight means hovering
- Confusing thrust with battery power
- Assuming thrust < weight means hovering
Solution
Step 1: Identify the comparison operator
In programming,==checks if two values are equal, while=assigns a value.Step 2: Choose the correct syntax
To check equality, useif thrust == weight:which compares thrust and weight.Final Answer:
if thrust == weight: -> Option AQuick Check:
Use '==' for equality check [OK]
- Using single '=' instead of '==' for comparison
- Using '!=' which means not equal
- Using invalid operators like '=>'
thrust = 10
weight = 10
if thrust == weight:
print('Hovering')
else:
print('Not hovering')Solution
Step 1: Compare thrust and weight values
Both thrust and weight are set to 10, sothrust == weightis True.Step 2: Determine which branch runs
Since the condition is True, the code prints 'Hovering'.Final Answer:
Hovering -> Option AQuick Check:
10 == 10 is True, so print 'Hovering' [OK]
- Confusing assignment '=' with comparison '=='
- Expecting 'Not hovering' when values are equal
- Missing indentation causing syntax errors
if thrust = weight:
print('Hovering')Solution
Step 1: Check the if statement syntax
The code uses=which assigns value, not compares values.Step 2: Correct the operator for comparison
Replace=with==to properly check if thrust equals weight.Final Answer:
Using '=' instead of '==' for comparison -> Option BQuick Check:
Use '==' to compare, '=' assigns [OK]
- Confusing '=' and '=='
- Ignoring missing colon errors
- Assuming indentation error instead of operator error
thrust = 15 weight = 15 # Your code here
Solution
Step 1: Use correct equality check
The condition must use==to check if thrust equals weight.Step 2: Use else for other cases
If thrust is not equal to weight, print 'Adjusting' to indicate change needed.Final Answer:
if thrust == weight: print('Hovering') else: print('Adjusting') -> Option CQuick Check:
Use '==' for equality, else for other cases [OK]
- Using '=' instead of '==' in if condition
- Reversing condition with '!='
- Using '>' which is not hover condition
Ready to prove it?
Pass the test and this pattern moves unknown
