"Simulation escalated. You now have the complete architectural toolkit: data structures, control flows, and modular subroutines. This gauntlet will test your ability to synthesize these components into complex, unbreakable logic."
These challenges require combining everything you have learned: variables, dictionaries, lists, loops, conditionals, and advanced function parameters (*args, **kwargs, global, and recursion). Do not use any external libraries. Pure MicroPython logic only.
Objective: Master dictionary manipulation (.get()), list operations, loops, and conditionals.
The Scenario: You are receiving a raw, unreliable stream of data payloads from a network of sensors. Some payloads are missing keys, and some are throwing errors.
raw_payloads = [
{"id": 1, "temp": 22.5, "status": "OK"},
{"id": 2, "status": "ERROR", "error_code": 404},
{"id": 3, "temp": 25.1, "status": "OK"},
{"id": 4, "temp": -99, "status": "OK"}, # Faulty reading
{"id": 5, "status": "OK"} # Missing temp key entirely
]Your Task:
Write a function sanitize_payloads(payload_list) that iterates through the list.
- If the
"status"is not"OK", skip the payload entirely. - Use
.get()to extract the"temp". If the key doesn't exist, default it to0.0. - If the temperature is valid (between
0.0and50.0), append it to a newclean_tempslist. - Return the sorted
clean_tempslist in descending order.
Objective: Handle variable scopes (global), arbitrary arguments (*args, **kwargs), and list mapping.
The Scenario: You have a global system calibration multiplier that occasionally updates, and a logging function that must handle an unpredictable number of incoming readings.
Your Task:
- Define a global variable:
CALIBRATION_FACTOR = 1.5 - Write a function
process_telemetry(*readings, **config). - Inside the function, multiply every reading in
*readingsby theCALIBRATION_FACTOR. (Hint: You can use aforloop ormap()). - Check the
**configdictionary. Ifconfig.get("strict_mode")isTrue, filter out any calibrated readings that exceed100.0. - Return the final list of processed readings.
Test your function with: process_telemetry(10, 50, 80, 25, strict_mode=True, location="Sector 7")
Objective: Safely implement a recursive function, combine it with type() checking, and manage lists.
The Scenario: A critical data packet arrived heavily fragmented and nested within multiple layers of arrays.
fragmented_data = [1, [2, 3], [[4], 5], 6, [[[7]]]]Your Task:
Write a recursive function flatten_data(nested_list) that breaks this down into a single, flat list: [1, 2, 3, 4, 5, 6, 7].
- Hint: Create an empty list
flat = []inside the function. Loop through each item innested_list. Useif type(item) is list:to check if the current item is another list. If it is, recursively callflatten_data(item)and.extend()the result intoflat. Otherwise, just.append()the item.
Objective: Write highly condensed, functional code using lambda, filter(), map(), and the ternary operator.
The Scenario: CPU cycles are critical, and you have exactly two lines of code to clean a raw array of voltage spikes.
raw_voltages = [-5.0, 12.5, 105.0, 8.2, 0.0]Your Task:
Without using any def functions or for loops:
- Create a variable
positive_onlyand assign it the result of usingfilter()and alambdato strip out any values less than or equal to0.0. - Create a variable
capped_voltagesand assign it the result of usingmap()and alambdawith a ternary operator (x if condition else y) to cap the remaining values at24.0(i.e., if a value is greater than 24.0, change it to 24.0). - Print
capped_voltagesas a list.