|
| 1 | +# Exercise 05 - Orbital Cargo Dispatch (Functions) |
| 2 | + |
| 3 | +## Description |
| 4 | +Welcome aboard the Orbital Freight Dispatch Center! Freight shuttles constantly arrive and depart between orbital stations and asteroid mining hubs. Up until now, flight coordinators have been manually copying and pasting repetitive calculation blocks for each flight manifest. |
| 5 | + |
| 6 | +In this exercise, you will bundle these operations into reusable, modular **functions** using the `def` keyword, parameters, default argument values, and `return` statements, adhering to the **DRY (Don't Repeat Yourself)** principle. |
| 7 | + |
| 8 | +--- |
| 9 | + |
| 10 | +## Tasks |
| 11 | + |
| 12 | +### Task 1: Pilot Identification (`def`, parameter, string return) |
| 13 | +Write a function named `greet_pilot(name)` that takes a pilot's name as a parameter and returns a formal flight greeting. |
| 14 | +* **Parameters**: `name` (a string) |
| 15 | +* **Returns**: A formatted string: `"Welcome aboard, Commander {name}. Flight systems ready."` |
| 16 | +* **Example**: |
| 17 | + ```python |
| 18 | + greet_pilot("Alfred") |
| 19 | + # Returns: "Welcome aboard, Commander Alfred. Flight systems ready." |
| 20 | + ``` |
| 21 | + |
| 22 | +> **Next step:** Run `python check.py 05`. Once Task 1 passes, open `test_functions.py` and remove the `@pytest.mark.skip` line above `test_task2`. |
| 23 | +
|
| 24 | +--- |
| 25 | + |
| 26 | +### Task 2: Fuel Consumption Calculator (Multiple parameters, arithmetic, return) |
| 27 | +Write a function named `calculate_fuel(distance, burn_rate)` that calculates the total fuel needed for a transit jump. |
| 28 | +* **Parameters**: |
| 29 | + * `distance` (int or float) — distance in light-sectors |
| 30 | + * `burn_rate` (int or float) — fuel units consumed per sector |
| 31 | +* **Returns**: The total fuel units required as a `float` (`distance * burn_rate`). |
| 32 | +* **Example**: |
| 33 | + ```python |
| 34 | + calculate_fuel(10, 2.5) |
| 35 | + # Returns: 25.0 |
| 36 | + ``` |
| 37 | + |
| 38 | +> **Next step:** Run tests. If Task 2 passes, open `test_functions.py` and remove the `@pytest.mark.skip` line above `test_task3`. |
| 39 | +
|
| 40 | +--- |
| 41 | + |
| 42 | +### Task 3: Trip Cost with Standard Fuel Rate (Default parameter values) |
| 43 | +Write a function named `calculate_trip_cost(fuel_needed, price_per_unit=2.5)` that computes the credit cost for fueling a shuttle. |
| 44 | +* **Parameters**: |
| 45 | + * `fuel_needed` (int or float) — amount of fuel required |
| 46 | + * `price_per_unit` (int or float, default `2.5`) — cost in credits per fuel unit |
| 47 | +* **Returns**: Total credit cost as a `float` (`fuel_needed * price_per_unit`). |
| 48 | +* **Example**: |
| 49 | + ```python |
| 50 | + # Using default fuel price (2.5) |
| 51 | + calculate_trip_cost(10) |
| 52 | + # Returns: 25.0 |
| 53 | + |
| 54 | + # Specifying custom fuel price (3.0) |
| 55 | + calculate_trip_cost(10, 3.0) |
| 56 | + # Returns: 30.0 |
| 57 | + ``` |
| 58 | + |
| 59 | +> **Next step:** Run tests. If Task 3 passes, open `test_functions.py` and remove the `@pytest.mark.skip` line above `test_task4`. |
| 60 | +
|
| 61 | +--- |
| 62 | + |
| 63 | +### Task 4: Manifest Broadcast (`print()` vs. `return`) |
| 64 | +Write a function named `display_manifest(cargo_item, quantity, priority="STANDARD")` that outputs cargo telemetry to the terminal screen. |
| 65 | +* **Parameters**: |
| 66 | + * `cargo_item` (string) — the name of the cargo item (e.g. `"Minerals"`) |
| 67 | + * `quantity` (int) — number of cargo crates |
| 68 | + * `priority` (string, default `"STANDARD"`) — shipment priority level |
| 69 | +* **Output**: Must print exactly two lines to the console: |
| 70 | + ```text |
| 71 | + [CARGO] {quantity}x {cargo_item} |
| 72 | + [STATUS] Priority: {priority} |
| 73 | + ``` |
| 74 | +* **Return value**: This function must **not** return anything (it should implicitly return `None`). Remember: `print()` displays text on the screen for humans to read, while `return` hands data back to the program. |
| 75 | +* **Example**: |
| 76 | + ```python |
| 77 | + result = display_manifest("Titanium", 50, "HIGH") |
| 78 | + # Console output: |
| 79 | + # [CARGO] 50x Titanium |
| 80 | + # [STATUS] Priority: HIGH |
| 81 | + # result is None |
| 82 | + ``` |
| 83 | + |
| 84 | +> **Next step:** Run tests. If Task 4 passes, open `test_functions.py` and remove the `@pytest.mark.skip` line above `test_task5`. |
| 85 | +
|
| 86 | +--- |
| 87 | + |
| 88 | +### Task 5: Dispatch Summary Pipeline (Function Composition & DRY) |
| 89 | +Bring your modular functions together to generate a complete flight dispatch summary without duplicating math calculations. |
| 90 | +Write a function named `create_dispatch_summary(pilot_name, distance, burn_rate=1.5, price_per_unit=2.5)`. |
| 91 | +* **Parameters**: |
| 92 | + * `pilot_name` (string) |
| 93 | + * `distance` (int or float) |
| 94 | + * `burn_rate` (int or float, default `1.5`) |
| 95 | + * `price_per_unit` (int or float, default `2.5`) |
| 96 | +* **Logic**: |
| 97 | + 1. Call `calculate_fuel(distance, burn_rate)` to compute fuel needed. |
| 98 | + 2. Call `calculate_trip_cost(fuel_needed, price_per_unit)` to compute total cost. |
| 99 | + 3. Return a formatted string: |
| 100 | + `"Dispatch Plan for {pilot_name}: {fuel_needed} fuel units required. Total cost: {total_cost} credits."` |
| 101 | +* **Example**: |
| 102 | + ```python |
| 103 | + create_dispatch_summary("Alex", 100, 2.0, 3.0) |
| 104 | + # Returns: "Dispatch Plan for Alex: 200.0 fuel units required. Total cost: 600.0 credits." |
| 105 | + |
| 106 | + # Using default rates: |
| 107 | + create_dispatch_summary("Alex", 100) |
| 108 | + # Returns: "Dispatch Plan for Alex: 150.0 fuel units required. Total cost: 375.0 credits." |
| 109 | + ``` |
| 110 | + |
| 111 | +> **Next step:** Run tests. If Task 5 passes, congratulations! You have completed Exercise 05. |
| 112 | +
|
| 113 | +--- |
| 114 | + |
| 115 | +## How to Run the Tests |
| 116 | +Run the following command from the repository root: |
| 117 | +```bash |
| 118 | +python check.py 05 |
| 119 | +``` |
| 120 | + |
| 121 | +Or run directly with pytest: |
| 122 | +```bash |
| 123 | +pytest exercises/foundations/05_functions/test_functions.py -v |
| 124 | +``` |
| 125 | + |
| 126 | +--- |
| 127 | + |
| 128 | +## Resources & Notes |
| 129 | +Review the concepts covered in this exercise in: |
| 130 | +* [The Python Ledger Curriculum: Functions](https://thepythonledger.github.io/Docusaurus-engine/lessons/code-organization/functions) |
0 commit comments