Skip to content

Commit c002adb

Browse files
authored
Merge pull request #9 from tajirdev/exercises/05_functions
finished adding function exercises
2 parents 47ccccc + d31d0bc commit c002adb

5 files changed

Lines changed: 283 additions & 0 deletions

File tree

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Task 1: Pilot Identification
2+
def greet_pilot(name):
3+
4+
pass
5+
6+
7+
# Task 2: Fuel Consumption Calculator
8+
def calculate_fuel(distance, burn_rate):
9+
pass
10+
11+
12+
# Task 3: Trip Cost with Standard Fuel Rate
13+
def calculate_trip_cost(fuel_needed, price_per_unit=2.5):
14+
pass
15+
16+
17+
# Task 4: Manifest Broadcast
18+
def display_manifest(cargo_item, quantity, priority="STANDARD"):
19+
pass
20+
21+
22+
# Task 5: Dispatch Summary Pipeline
23+
def create_dispatch_summary(pilot_name, distance, burn_rate=1.5, price_per_unit=2.5):
24+
pass
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Task 1: Pilot Identification
2+
def greet_pilot(name):
3+
return f"Welcome aboard, Commander {name}. Flight systems ready."
4+
5+
6+
# Task 2: Fuel Consumption Calculator
7+
def calculate_fuel(distance, burn_rate):
8+
Consumption = distance * burn_rate
9+
return float(Consumption)
10+
11+
12+
# Task 3: Trip Cost with Standard Fuel Rate
13+
def calculate_trip_cost(fuel_needed, price_per_unit=2.5):
14+
fuel_Rate = fuel_needed * price_per_unit
15+
return float(fuel_Rate)
16+
17+
18+
# Task 4: Manifest Broadcast
19+
def display_manifest(cargo_item, quantity, priority="STANDARD"):
20+
print(f"[CARGO] {quantity}x {cargo_item}")
21+
print(f"[STATUS] Priority: {priority}")
22+
23+
24+
# Task 5: Dispatch Summary Pipeline
25+
def create_dispatch_summary(pilot_name, distance, burn_rate=1.5, price_per_unit=2.5):
26+
fuel_needed = calculate_fuel(distance, burn_rate)
27+
total_cost = calculate_trip_cost(fuel_needed, price_per_unit)
28+
return (
29+
f"Dispatch Plan for {pilot_name}: {fuel_needed} fuel units required. "
30+
f"Total cost: {total_cost} credits."
31+
)
32+
33+
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from functions_solution import (
2+
calculate_fuel,
3+
calculate_trip_cost,
4+
create_dispatch_summary,
5+
display_manifest,
6+
greet_pilot,
7+
)
8+
9+
10+
def test_task1():
11+
assert greet_pilot("Alfred") == "Welcome aboard, Commander Alfred. Flight systems ready."
12+
assert greet_pilot("Sarah") == "Welcome aboard, Commander Sarah. Flight systems ready."
13+
14+
15+
def test_task2():
16+
assert calculate_fuel(10, 2.5) == 25.0
17+
assert calculate_fuel(40, 1.5) == 60.0
18+
19+
20+
def test_task3():
21+
assert calculate_trip_cost(10) == 25.0
22+
assert calculate_trip_cost(10, 3.0) == 30.0
23+
24+
25+
def test_task4(capsys):
26+
result = display_manifest("Titanium", 50, "HIGH")
27+
captured = capsys.readouterr()
28+
assert captured.out == "[CARGO] 50x Titanium\n[STATUS] Priority: HIGH\n"
29+
assert result is None
30+
31+
result_default = display_manifest("Minerals", 10)
32+
captured_default = capsys.readouterr()
33+
assert captured_default.out == "[CARGO] 10x Minerals\n[STATUS] Priority: STANDARD\n"
34+
assert result_default is None
35+
36+
37+
def test_task5():
38+
assert (
39+
create_dispatch_summary("Alex", 100, 2.0, 3.0)
40+
== "Dispatch Plan for Alex: 200.0 fuel units required. Total cost: 600.0 credits."
41+
)
42+
assert (
43+
create_dispatch_summary("Elena", 100)
44+
== "Dispatch Plan for Elena: 150.0 fuel units required. Total cost: 375.0 credits."
45+
)
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import pytest
2+
3+
from functions import (
4+
calculate_fuel,
5+
calculate_trip_cost,
6+
create_dispatch_summary,
7+
display_manifest,
8+
greet_pilot,
9+
)
10+
11+
12+
def test_task1():
13+
assert greet_pilot("Alex") == "Welcome aboard, Commander Alex. Flight systems ready."
14+
assert greet_pilot("Sarah") == "Welcome aboard, Commander Sarah. Flight systems ready."
15+
16+
17+
@pytest.mark.skip(reason="Finish task 1 first")
18+
def test_task2():
19+
assert calculate_fuel(10, 2.5) == 25.0
20+
assert calculate_fuel(40, 1.5) == 60.0
21+
22+
23+
@pytest.mark.skip(reason="Finish task 2 first")
24+
def test_task3():
25+
assert calculate_trip_cost(10) == 25.0
26+
assert calculate_trip_cost(10, 3.0) == 30.0
27+
28+
29+
@pytest.mark.skip(reason="Finish task 3 first")
30+
def test_task4(capsys):
31+
result = display_manifest("Titanium", 50, "HIGH")
32+
captured = capsys.readouterr()
33+
assert captured.out == "[CARGO] 50x Titanium\n[STATUS] Priority: HIGH\n"
34+
assert result is None
35+
36+
result_default = display_manifest("Minerals", 10)
37+
captured_default = capsys.readouterr()
38+
assert captured_default.out == "[CARGO] 10x Minerals\n[STATUS] Priority: STANDARD\n"
39+
assert result_default is None
40+
41+
42+
@pytest.mark.skip(reason="Finish task 4 first")
43+
def test_task5():
44+
assert (
45+
create_dispatch_summary("Alex", 100, 2.0, 3.0)
46+
== "Dispatch Plan for Alex: 200.0 fuel units required. Total cost: 600.0 credits."
47+
)
48+
assert (
49+
create_dispatch_summary("Elena", 100)
50+
== "Dispatch Plan for Elena: 150.0 fuel units required. Total cost: 375.0 credits."
51+
)

0 commit comments

Comments
 (0)