Skip to content

Adding time and a half pay calculator algorithm to financial folder #12662

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Apr 17, 2025
Prev Previous commit
Next Next commit
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Apr 8, 2025
commit c4be33ee44ee204f93af9468d9d26f556f5f175d
59 changes: 34 additions & 25 deletions financial/time_and_half_pay.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,42 @@
Calculate time and a half pay

"""


def pay(hours_worked: float, pay_rate: float, hours: float = 40) -> float:
"""
hours_worked = The total hours worked
pay_rate = Ammount of money per hour
hours = Number of hours that must be worked before you recieve time and a half
>>> pay(41, 1)
41.5
>>> pay(65, 19)
1472.5
>>> pay(10, 1)
10.0
"""
# Check that all input parameters are float or integer
assert (type(hours_worked) is float or type(hours_worked) is int), "Parameter 'hours_worked' must be of type 'int' or 'float'"
assert (type(pay_rate) is float or type(pay_rate) is int), "Parameter 'hours_worked' must be of type 'int' or 'float'"
assert (type(hours) is float or type(hours) is int), "Parameter 'hours_worked' must be of type 'int' or 'float'"
"""
hours_worked = The total hours worked
pay_rate = Ammount of money per hour
hours = Number of hours that must be worked before you recieve time and a half
>>> pay(41, 1)
41.5
>>> pay(65, 19)
1472.5
>>> pay(10, 1)
10.0
"""
# Check that all input parameters are float or integer
assert type(hours_worked) is float or type(hours_worked) is int, (
"Parameter 'hours_worked' must be of type 'int' or 'float'"
)
assert type(pay_rate) is float or type(pay_rate) is int, (
"Parameter 'hours_worked' must be of type 'int' or 'float'"
)
assert type(hours) is float or type(hours) is int, (
"Parameter 'hours_worked' must be of type 'int' or 'float'"
)

normal_pay = hours_worked * pay_rate
over_time = hours_worked - hours
# Another way
"""over_time_pay = ((over_time * ((over_time + (over_time ** 2) ** 0.5) / (2 * over_time))) / 2) * pay_rate"""
over_time_pay = (max(0, over_time) / 2) * pay_rate
total_pay = normal_pay + over_time_pay
return total_pay
normal_pay = hours_worked * pay_rate
over_time = hours_worked - hours
# Another way
"""over_time_pay = ((over_time * ((over_time + (over_time ** 2) ** 0.5) / (2 * over_time))) / 2) * pay_rate"""

Check failure on line 33 in financial/time_and_half_pay.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

financial/time_and_half_pay.py:33:89: E501 Line too long (114 > 88)
over_time_pay = (max(0, over_time) / 2) * pay_rate
total_pay = normal_pay + over_time_pay
return total_pay


if __name__ == "__main__":
# Test
import doctest
doctest.testmod()
# Test
import doctest

doctest.testmod()
Loading