Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

[pull] master from TheAlgorithms:master #109

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 2 commits into from
Apr 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions financial/time_and_half_pay.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""
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 = Amount of money per hour
hours = Number of hours that must be worked before you receive 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 isinstance(hours_worked, (float, int)), (
"Parameter 'hours_worked' must be of type 'int' or 'float'"
)
assert isinstance(pay_rate, (float, int)), (
"Parameter 'pay_rate' must be of type 'int' or 'float'"
)
assert isinstance(hours, (float, int)), (
"Parameter 'hours' must be of type 'int' or 'float'"
)

normal_pay = hours_worked * pay_rate
over_time = max(0, hours_worked - hours)
over_time_pay = over_time * pay_rate / 2
return normal_pay + over_time_pay


if __name__ == "__main__":
# Test
import doctest

doctest.testmod()
Empty file.
63 changes: 63 additions & 0 deletions project_euler/problem_136/sol1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
"""
Project Euler Problem 136: https://projecteuler.net/problem=136

Singleton Difference

The positive integers, x, y, and z, are consecutive terms of an arithmetic progression.
Given that n is a positive integer, the equation, x^2 - y^2 - z^2 = n,
has exactly one solution when n = 20:
13^2 - 10^2 - 7^2 = 20.

In fact there are twenty-five values of n below one hundred for which
the equation has a unique solution.

How many values of n less than fifty million have exactly one solution?

By change of variables

x = y + delta
z = y - delta

The expression can be rewritten:

x^2 - y^2 - z^2 = y * (4 * delta - y) = n

The algorithm loops over delta and y, which is restricted in upper and lower limits,
to count how many solutions each n has.
In the end it is counted how many n's have one solution.
"""


def solution(n_limit: int = 50 * 10**6) -> int:
"""
Define n count list and loop over delta, y to get the counts, then check
which n has count == 1.

>>> solution(3)
0
>>> solution(10)
3
>>> solution(100)
25
>>> solution(110)
27
"""
n_sol = [0] * n_limit

for delta in range(1, (n_limit + 1) // 4 + 1):
for y in range(4 * delta - 1, delta, -1):
n = y * (4 * delta - y)
if n >= n_limit:
break
n_sol[n] += 1

ans = 0
for i in range(n_limit):
if n_sol[i] == 1:
ans += 1

return ans


if __name__ == "__main__":
print(f"{solution() = }")