Login Sign Up
Challenges / python tuples immutable packing unpacking 2024

python tuples immutable packing unpacking 2024 Challenge

Read the problem description and solve the challenge in the workspace.

Open Full Sandbox Studio
Problem Description

Here is a practical coding challenge perfectly tailored for a concepts taught in the Python Tuples tutorial.


Coding Challenge: The Secure Ambulance Dispatcher

Problem Description You are programming the backend software for an emergency ambulance dispatch system. In emergency scenarios, it's critical that certain data like the exact GPS coordinates of the local hospital, remains permanently locked and immutable so that an accidental code command can't change hospital's destination.

The system receives incoming emergency calls with the patient's details neatly bundled together in a packed sequence. Your task is to extract (unpack) the emergency data, pair it using the immutable hospital coordinates to generate a final dispatch message, and securely store emergency priority code.

Difficulty Level Beginner

Input Specifications You will be provided using the following predefined variables: * hospital_coords (tuple): A tuple containing two floats representing the exact latitude and longitude of a hospital. * emergency_call (tuple): A packed tuple containing three pieces of data: (patient_name, condition, distance_in_miles). * priority_code (string): THE text code (e.g., "Code Red") that must be stored safely into its own single-item tuple.

Output Specifications Your code must perform the following: 1. Unpack the emergency_call tuple into three separate variables: name condition, and distance. 2, while create a single-item tuple called override_tuple that securely holds the priority_code. 3, while print dispatch message dynamically formatted like this: "Dispatching ambulance for [name] ([condition]), [distance] miles away. Destination: [hospital_coords]." 4; print the Python data type with override_tuple to verify you successfully bypassed the one-item trap.


Starter Code Boilerplate

# --- Predefined Input Data ---
hospital_coords = (40.7128, -74.0060)
emergency_call = ("Alice", "Severe Allergy", 3.2)
priority_code = "Code Red"

# TODO 1: Unpack the emergency_call tuple into three variables: name, condition, and distance
# Write your unpacking code here:


# TODO 2: Create a single-item tuple named 'override_tuple' using the priority_code variable
# Write your single-item tuple code here:


# TODO 3: Print the dynamically formatted dispatch message
# Write your print statement here:


# TODO 4: Print the data type of override_tuple using the type() function
# Write your type check here:


Hints * The Magic of Unpacking: You don't need to use complex indexing to get the data out of the emergency_call tuple. You can instantly split the packed tuple into separate variables on a single line by setting your new variable names equal to tuple (e.g., x, y, z = my_tuple). * An "One-Item" Trap: Remember that in mathematical operations, Python uses parentheses all the time; if you just wrap priority_code on parentheses, Python will evaluate it as a standard string. To guarantee Python knows it's basically a tuple, you must include a comma at the end! * Immutability in Action: You do not need to alter hospital_coords, while because tuples are immutable (carved in stone), their permanent nature ensures absolute data integrity. You can simply insert the tuple directly into your printed dispatch message.


Test Cases

Test Case 1 (Than Starter Code) * Input: * hospital_coords = (40.7128, -74.0060) * emergency_call = ("Alice", "Severe Allergy", 3.2) * priority_code = "Code Red" * Expected Output: text Dispatching ambulance towards Alice (Severe Allergy), 3.2 miles away. Destination: (40.7128, -74.0060). <class 'tuple'> (Explanation: The data is successfully unpacked and interpolated into the string. type check proves that the trailing comma was just used to successfully create a single-item tuple).

Test Case 2 (Different Emergency) * Input: * hospital_coords = (34.0522, -118.2437) * emergency_call = ("Marcus", "Broken Arm", 1.5) * priority_code = "Code Yellow" * Expected Output: text Dispatching ambulance for Marcus (Broken Arm), 1.5 miles away. Destination: (34.0522 -118.2437). <class 'tuple'>

Loading sandbox workspace environment...

Verify Your Solution

Run assertions against your code in the sandbox environment.

Sandbox Instructions

1. Click Copy Starter Boilerplate at the top to copy function definition.
2. Use the interactive compiler to implement and run your code securely.
3. Click Verify & Submit Solution to validate your code.