Compare commits

...

8 Commits

Author SHA1 Message Date
mertmarik
22bff1d8b9
Merge 455749a9eb839eb836cfe63478eb43cf0f83340c into 338cbafe0d5b07d57f83060ea0f9ba3a6c1155e7 2025-02-09 21:06:43 +01:00
lighting9999
338cbafe0d
Improve power.py (#12567)
* Fix And Add power.py

To fix the inaccuracies and allow handling of negative exponents and bases, the key issue lies in how negative numbers are handled in the power calculation, especially when dividing.
## Example Output:
```python
>>> power(4, 6)
4096
>>> power(2, 3)
8
>>> power(-2, 3)
-8
>>> power(2, -3)
0.125
>>> power(-2, -3)
-0.125
```

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update power.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update power.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update power.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update power.py

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Maxim Smolskiy <mithridatus@mail.ru>
2025-02-09 20:51:18 +03:00
pre-commit-ci[bot]
455749a9eb [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2025-01-29 22:04:49 +00:00
Merdo
297da1f274 input added 2025-01-30 01:04:10 +03:00
Merdo
5a61613dd0 Merge branch 'master' of https://github.com/mertmarik/Python 2025-01-30 01:03:44 +03:00
Merdo
0de43acf24 example input added 2025-01-30 00:59:01 +03:00
pre-commit-ci[bot]
0175a2be86 [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2025-01-29 11:03:38 +00:00
Merdo
db38f0e514 Solution for Bridge and Torch Problem 2025-01-29 13:57:34 +03:00
2 changed files with 37 additions and 5 deletions

View File

@ -1,4 +1,4 @@
def actual_power(a: int, b: int):
def actual_power(a: int, b: int) -> int:
"""
Function using divide and conquer to calculate a^b.
It only works for integer a,b.
@ -19,10 +19,12 @@ def actual_power(a: int, b: int):
"""
if b == 0:
return 1
half = actual_power(a, b // 2)
if (b % 2) == 0:
return actual_power(a, int(b / 2)) * actual_power(a, int(b / 2))
return half * half
else:
return a * actual_power(a, int(b / 2)) * actual_power(a, int(b / 2))
return a * half * half
def power(a: int, b: int) -> float:
@ -43,9 +45,9 @@ def power(a: int, b: int) -> float:
-0.125
"""
if b < 0:
return 1 / actual_power(a, b)
return 1 / actual_power(a, -b)
return actual_power(a, b)
if __name__ == "__main__":
print(power(-2, -3))
print(power(-2, -3)) # output -0.125

View File

@ -0,0 +1,30 @@
person_pace = [1, 2, 5, 10, 12, 16]
person_pace.sort() # Sort the array for optimal pairing
people = len(person_pace)
# Base cases
if people == 1:
print("Min Time to Cross is:", person_pace[0])
elif people == 2:
print("Min Time to Cross is:", person_pace[1])
else:
total_time = 0
while people > 3:
# Strategy 1: Send the two fastest first, then the fastest returns
option1 = person_pace[1] + person_pace[0] + person_pace[-1] + person_pace[1]
# Strategy 2: Send the two slowest, then the fastest returns
option2 = person_pace[-1] + person_pace[0] + person_pace[-2] + person_pace[0]
# Choose the minimum of the two strategies
total_time += min(option1, option2)
# Remove the two slowest people who have crossed
person_pace = person_pace[:-2]
# Handle the last 2 or 3 people
if len(person_pace) == 3:
total_time += person_pace[2] + person_pace[0] + person_pace[1]
elif len(person_pace) == 2:
total_time += person_pace[1]
print("Min Time to Cross is:", total_time)