Skip to content

Commit a1aa631

Browse files
konsoleSampre-commit-ci[bot]MaximSmolskiy
authored
Adding time and a half pay calculator algorithm to financial folder (#12662)
* Create time&half-pay.py * Update time&half-pay.py * Update time&half-pay.py * Rename time&half-pay.py to time_and_half_pay.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update time_and_half_pay.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update time_and_half_pay.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update time_and_half_pay.py * Update time_and_half_pay.py * Update time_and_half_pay.py --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Maxim Smolskiy <[email protected]>
1 parent c585cb1 commit a1aa631

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

Diff for: financial/time_and_half_pay.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""
2+
Calculate time and a half pay
3+
"""
4+
5+
6+
def pay(hours_worked: float, pay_rate: float, hours: float = 40) -> float:
7+
"""
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
11+
12+
>>> pay(41, 1)
13+
41.5
14+
>>> pay(65, 19)
15+
1472.5
16+
>>> pay(10, 1)
17+
10.0
18+
"""
19+
# Check that all input parameters are float or integer
20+
assert isinstance(hours_worked, (float, int)), (
21+
"Parameter 'hours_worked' must be of type 'int' or 'float'"
22+
)
23+
assert isinstance(pay_rate, (float, int)), (
24+
"Parameter 'pay_rate' must be of type 'int' or 'float'"
25+
)
26+
assert isinstance(hours, (float, int)), (
27+
"Parameter 'hours' must be of type 'int' or 'float'"
28+
)
29+
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
34+
35+
36+
if __name__ == "__main__":
37+
# Test
38+
import doctest
39+
40+
doctest.testmod()

0 commit comments

Comments
 (0)