Skip to content

Commit 73d7e11

Browse files
committed
Fix: Correct time-and-a-half pay calculation logic
1 parent a1aa631 commit 73d7e11

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

Diff for: financial/time_and_half_pay.py

+17-8
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,15 @@
55

66
def pay(hours_worked: float, pay_rate: float, hours: float = 40) -> float:
77
"""
8-
hours_worked = The total hours worked
9-
pay_rate = Amount of money per hour
10-
hours = Number of hours that must be worked before you receive time and a half
8+
Calculate total pay including time-and-a-half for overtime.
9+
10+
Parameters:
11+
hours_worked: Total number of hours worked.
12+
pay_rate: Pay rate per hour.
13+
hours: Number of hours to work before overtime applies (default 40).
14+
15+
Returns:
16+
Total pay including overtime compensation.
1117
1218
>>> pay(41, 1)
1319
41.5
@@ -27,14 +33,17 @@ def pay(hours_worked: float, pay_rate: float, hours: float = 40) -> float:
2733
"Parameter 'hours' must be of type 'int' or 'float'"
2834
)
2935

30-
normal_pay = hours_worked * pay_rate
31-
over_time = max(0, hours_worked - hours)
32-
over_time_pay = over_time * pay_rate / 2
33-
return normal_pay + over_time_pay
36+
regular_hours = min(hours_worked, hours)
37+
overtime_hours = max(0, hours_worked - hours)
38+
39+
regular_pay = regular_hours * pay_rate
40+
overtime_pay = overtime_hours * pay_rate * 1.5
41+
42+
return regular_pay + overtime_pay
3443

3544

3645
if __name__ == "__main__":
37-
# Test
46+
# Run doctests
3847
import doctest
3948

4049
doctest.testmod()

0 commit comments

Comments
 (0)