Bird
Raised Fist0
Drone Programmingprogramming~20 mins

Setting geofence boundaries in Drone Programming - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Geofence Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
1:30remaining
Output of geofence boundary check
What is the output of this code that checks if a drone is inside a rectangular geofence boundary?
Drone Programming
geofence = {'min_lat': 10.0, 'max_lat': 20.0, 'min_lon': 30.0, 'max_lon': 40.0}
drone_position = {'lat': 15.0, 'lon': 35.0}
inside = (geofence['min_lat'] <= drone_position['lat'] <= geofence['max_lat']) and (geofence['min_lon'] <= drone_position['lon'] <= geofence['max_lon'])
print('Inside geofence' if inside else 'Outside geofence')
ASyntaxError
BOutside geofence
CInside geofence
DKeyError
Attempts:
2 left
💡 Hint
Check if the drone's latitude and longitude are within the min and max values of the geofence.
🧠 Conceptual
intermediate
1:00remaining
Understanding geofence polygon boundaries
Which data structure is best to represent a polygon geofence boundary for a drone?
AA string describing the area name
BA single latitude and longitude point representing the center
CA dictionary with min and max latitude only
DA list of latitude and longitude coordinate pairs defining the polygon vertices
Attempts:
2 left
💡 Hint
A polygon needs multiple points to define its shape.
🔧 Debug
advanced
1:30remaining
Fix the geofence boundary check error
This code is supposed to check if a drone is inside a geofence but raises an error. What is the error?
Drone Programming
geofence = {'min_lat': 10.0, 'max_lat': 20.0, 'min_lon': 30.0, 'max_lon': 40.0}
drone_position = {'lat': 25.0, 'lon': 35.0}
inside = (geofence['min_lat'] <= drone_position['lat'] <= geofence['max_lat']) and (geofence['min_lon'] <= drone_position['lon'] <= geofence['max_lon'])
print('Inside geofence' if inside else 'Outside geofence')
ASyntaxError due to missing colon
BNo error; output is 'Outside geofence'
CTypeError due to comparing string with float
DKeyError because 'lat' key is missing
Attempts:
2 left
💡 Hint
Check the drone's latitude value and the geofence limits.
📝 Syntax
advanced
1:00remaining
Identify the syntax error in geofence code
Which option contains a syntax error in defining a geofence dictionary?
Ageofence = {'min_lat': 10.0, 'max_lat': 20.0, 'min_lon': 30.0, 'max_lon' 40.0}
Bgeofence = {'min_lat': 10.0, 'max_lat': 20.0, 'min_lon': 30.0, 'max_lon': 40.0}
Cgeofence = dict(min_lat=10.0, max_lat=20.0, min_lon=30.0, max_lon=40.0)
D}0.04 :'nol_xam' ,0.03 :'nol_nim' ,0.02 :'tal_xam' ,0.01 :'tal_nim'{ = ecnefoeg
Attempts:
2 left
💡 Hint
Look for missing punctuation in dictionary syntax.
🚀 Application
expert
2:30remaining
Calculate if drone is inside a polygon geofence
Given a polygon geofence defined by vertices and a drone position, which option correctly determines if the drone is inside the polygon using the ray casting algorithm?
Drone Programming
def is_inside_polygon(point, polygon):
    x, y = point
    inside = False
    n = len(polygon)
    p1x, p1y = polygon[0]
    for i in range(n + 1):
        p2x, p2y = polygon[i % n]
        if y > min(p1y, p2y):
            if y <= max(p1y, p2y):
                if x <= max(p1x, p2x):
                    if p1y != p2y:
                        xinters = (y - p1y) * (p2x - p1x) / (p2y - p1y) + p1x
                    if p1x == p2x or x <= xinters:
                        inside = not inside
        p1x, p1y = p2x, p2y
    return inside

polygon = [(0,0), (0,10), (10,10), (10,0)]
drone = (5,5)
print(is_inside_polygon(drone, polygon))
ATrue
BFalse
CSyntaxError
DIndexError
Attempts:
2 left
💡 Hint
The drone is inside the square polygon defined by the vertices.

Practice

(1/5)
1.

What is the main purpose of setting geofence boundaries for a drone?

easy
A. To improve the drone's camera quality
B. To increase the drone's speed
C. To keep the drone flying within a safe area
D. To reduce the drone's battery usage

Solution

  1. Step 1: Understand geofence boundaries

    Geofence boundaries define a virtual area where the drone is allowed to fly.
  2. Step 2: Identify the purpose of geofencing

    The main goal is to keep the drone safe by preventing it from flying outside this area.
  3. Final Answer:

    To keep the drone flying within a safe area -> Option C
  4. Quick Check:

    Geofence = safe flying area [OK]
Hint: Geofence means safe zone for drone flight [OK]
Common Mistakes:
  • Confusing geofence with speed control
  • Thinking geofence improves camera
  • Assuming geofence saves battery
2.

Which of the following is the correct way to define a geofence boundary in code using minimum and maximum latitude and longitude?

geofence = {
    'min_lat': 34.0,
    'max_lat': 35.0,
    'min_lon': -118.5,
    'max_lon': -117.5
}
easy
A. geofence = [34.0, 35.0, -118.5, -117.5]
B. geofence = '34.0,35.0,-118.5,-117.5'
C. geofence = (min_lat=34.0, max_lat=35.0, min_lon=-118.5, max_lon=-117.5)
D. geofence = {'min_lat': 34.0, 'max_lat': 35.0, 'min_lon': -118.5, 'max_lon': -117.5}

Solution

  1. Step 1: Identify correct data structure for geofence

    A dictionary with keys for min and max latitude and longitude is clear and correct.
  2. Step 2: Check syntax correctness

    geofence = {'min_lat': 34.0, 'max_lat': 35.0, 'min_lon': -118.5, 'max_lon': -117.5} uses a dictionary with proper key-value pairs and correct syntax.
  3. Final Answer:

    geofence = {'min_lat': 34.0, 'max_lat': 35.0, 'min_lon': -118.5, 'max_lon': -117.5} -> Option D
  4. Quick Check:

    Dictionary with keys = geofence = {'min_lat': 34.0, 'max_lat': 35.0, 'min_lon': -118.5, 'max_lon': -117.5} [OK]
Hint: Use dictionary with descriptive keys for boundaries [OK]
Common Mistakes:
  • Using list or tuple without keys
  • Using string instead of structured data
  • Incorrect syntax for tuples
3.

Given the following code snippet, what will be the output?

geofence = {'min_lat': 10.0, 'max_lat': 20.0, 'min_lon': 30.0, 'max_lon': 40.0}

current_position = {'lat': 15.0, 'lon': 35.0}

inside = (geofence['min_lat'] <= current_position['lat'] <= geofence['max_lat']) and \
         (geofence['min_lon'] <= current_position['lon'] <= geofence['max_lon'])

print(inside)
medium
A. False
B. True
C. SyntaxError
D. None

Solution

  1. Step 1: Check latitude condition

    15.0 is between 10.0 and 20.0, so latitude condition is True.
  2. Step 2: Check longitude condition

    35.0 is between 30.0 and 40.0, so longitude condition is True.
  3. Step 3: Combine conditions

    Both conditions are True, so inside is True.
  4. Final Answer:

    True -> Option B
  5. Quick Check:

    Position inside geofence = True [OK]
Hint: Check if lat and lon are within min and max [OK]
Common Mistakes:
  • Mixing up latitude and longitude
  • Using wrong comparison operators
  • Forgetting to combine both conditions
4.

Identify the error in the following geofence check code and select the fix:

geofence = {'min_lat': 5.0, 'max_lat': 15.0, 'min_lon': 25.0, 'max_lon': 35.0}

current_position = {'lat': 10.0, 'lon': 40.0}

if geofence['min_lat'] <= current_position['lat'] <= geofence['max_lat'] and 
   geofence['min_lon'] <= current_position['lon'] <= geofence['max_lon']:
    print("Inside geofence")
else:
    print("Outside geofence")
medium
A. Add parentheses around the if condition
B. Replace 'and' with 'or' in the if condition
C. Swap min_lon and max_lon values in geofence
D. Change current_position['lon'] to 30.0 to be inside geofence

Solution

  1. Step 1: Identify the syntax error

    The if condition is split across lines without parentheses or backslash, causing SyntaxError.
  2. Step 2: Understand the required fix

    Parentheses around the condition allow multi-line expressions.
  3. Step 3: Confirm logic after fix

    With syntax fixed, lat inside but lon 40.0 > 35.0 outside, prints correctly "Outside geofence".
  4. Final Answer:

    Add parentheses around the if condition -> Option A
  5. Quick Check:

    if (cond1 and cond2): syntax OK [OK]
Hint: Wrap multi-line if conditions in parentheses [OK]
Common Mistakes:
  • Using 'or' instead of 'and' in condition
  • Swapping min and max values incorrectly
  • Changing data instead of fixing syntax
5.

You want to create a geofence that excludes a small no-fly zone inside a larger allowed area. Which approach correctly sets this using nested geofence boundaries?

# Outer geofence
outer = {'min_lat': 10.0, 'max_lat': 20.0, 'min_lon': 30.0, 'max_lon': 40.0}

# Inner no-fly zone
no_fly = {'min_lat': 14.0, 'max_lat': 16.0, 'min_lon': 34.0, 'max_lon': 36.0}

# Function to check if position is inside a geofence

Which code snippet correctly returns True only if the position is inside the outer geofence but outside the no-fly zone?

hard
A. return (outer['min_lat'] <= lat <= outer['max_lat'] and outer['min_lon'] <= lon <= outer['max_lon']) and not (no_fly['min_lat'] <= lat <= no_fly['max_lat'] and no_fly['min_lon'] <= lon <= no_fly['max_lon'])
B. return (outer['min_lat'] <= lat <= outer['max_lat'] and outer['min_lon'] <= lon <= outer['max_lon']) or (no_fly['min_lat'] <= lat <= no_fly['max_lat'] and no_fly['min_lon'] <= lon <= no_fly['max_lon'])
C. return not (outer['min_lat'] <= lat <= outer['max_lat'] and outer['min_lon'] <= lon <= outer['max_lon']) and (no_fly['min_lat'] <= lat <= no_fly['max_lat'] and no_fly['min_lon'] <= lon <= no_fly['max_lon'])
D. return (outer['min_lat'] >= lat >= outer['max_lat'] and outer['min_lon'] >= lon >= outer['max_lon']) and not (no_fly['min_lat'] >= lat >= no_fly['max_lat'] and no_fly['min_lon'] >= lon >= no_fly['max_lon'])

Solution

  1. Step 1: Check position inside outer geofence

    Use conditions to confirm latitude and longitude are within outer boundaries.
  2. Step 2: Exclude position inside no-fly zone

    Use 'not' to ensure position is outside the inner no-fly zone boundaries.
  3. Step 3: Combine conditions correctly

    Use 'and' to require both conditions: inside outer and outside no-fly zone.
  4. Final Answer:

    return (outer['min_lat'] <= lat <= outer['max_lat'] and outer['min_lon'] <= lon <= outer['max_lon']) and not (no_fly['min_lat'] <= lat <= no_fly['max_lat'] and no_fly['min_lon'] <= lon <= no_fly['max_lon']) -> Option A
  5. Quick Check:

    Inside outer and outside inner = return (outer['min_lat'] <= lat <= outer['max_lat'] and outer['min_lon'] <= lon <= outer['max_lon']) and not (no_fly['min_lat'] <= lat <= no_fly['max_lat'] and no_fly['min_lon'] <= lon <= no_fly['max_lon']) [OK]
Hint: Use 'and' with 'not' to exclude inner no-fly zone [OK]
Common Mistakes:
  • Using 'or' instead of 'and' to combine conditions
  • Incorrect comparison operators (>= instead of <=)
  • Not excluding the no-fly zone properly