Compare commits

...

6 Commits

Author SHA1 Message Date
Cicero Tiago
e61de49c7f
Merge 124fe8d629ade1b210c22329e24c378b8df84cd6 into 338cbafe0d5b07d57f83060ea0f9ba3a6c1155e7 2025-02-10 17:13:46 +08: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
cicerotcv
124fe8d629 feat: insert the algorithms into Directory.md 2023-10-31 09:59:12 -03:00
cicerotcv
c680fecd54 chore: fix file trailing line 2023-10-30 19:14:23 -03:00
cicerotcv
a62b5e5c97 docs: adds module's author, description and references 2023-10-30 18:53:04 -03:00
cicerotcv
c493d23dda feat: implements a hex to rgb conversion function 2023-10-30 18:22:22 -03:00
3 changed files with 87 additions and 5 deletions

View File

@ -160,6 +160,7 @@
* [Energy Conversions](conversions/energy_conversions.py)
* [Excel Title To Column](conversions/excel_title_to_column.py)
* [Hex To Bin](conversions/hex_to_bin.py)
* [Hex To Rgb](conversions/hex_to_rgb.py)
* [Hexadecimal To Decimal](conversions/hexadecimal_to_decimal.py)
* [Ipv4 Conversion](conversions/ipv4_conversion.py)
* [Length Conversion](conversions/length_conversion.py)

79
conversions/hex_to_rgb.py Normal file
View File

@ -0,0 +1,79 @@
"""
* Author: Cicero Tiago Carneiro Valentim (https://github.com/cicerotcv)
* Description: Convert hexadecimal (#FF2000) color to RGB (rgb(255, 32, 0)).
References:
https://www.w3schools.com/colors/colors_rgb.asp
https://www.w3schools.com/colors/colors_hexadecimal.asp
"""
def hex_to_rgb(hex_color: str) -> str:
"""
Converts a hexadecimal color code to RGB values.
Args:
hex_color (str): A hexadecimal color code, e.g., "#RGB" or "#RRGGBB".
Returns:
str: A string representation of a rgb value "rgb(r, g, b)" containing
three integers.
Raises:
ValueError: If the input hex_color is not a valid hexadecimal color code.
Examples:
>>> hex_to_rgb("#FF0000")
'rgb(255, 0, 0)'
>>> hex_to_rgb("#00FF00")
'rgb(0, 255, 0)'
>>> hex_to_rgb("#123")
'rgb(17, 34, 51)'
>>> hex_to_rgb("#000000")
'rgb(0, 0, 0)'
>>> hex_to_rgb("#FFFFFF")
'rgb(255, 255, 255)'
>>> hex_to_rgb("#0088FF")
'rgb(0, 136, 255)'
>>> hex_to_rgb("#0032ccAA")
Traceback (most recent call last):
...
ValueError: Invalid hex color code
>>> hex_to_rgb("#0032")
Traceback (most recent call last):
...
ValueError: Invalid hex color code
Note:
- The function supports both 6-digit and 3-digit hex color codes.
"""
hex_color = hex_color.lstrip("#")
# Check if the input is a valid hex color code
if not (len(hex_color) == 6 or len(hex_color) == 3):
raise ValueError("Invalid hex color code")
# Expand 3-digit hex codes to 6 digits (e.g., "#123" to "#112233")
if len(hex_color) == 3:
hex_color = "".join([char * 2 for char in hex_color])
# Parse the hex values to integers
red = int(hex_color[0:2], 16)
green = int(hex_color[2:4], 16)
blue = int(hex_color[4:6], 16)
return f"rgb({red}, {green}, {blue})"
if __name__ == "__main__":
import doctest
doctest.testmod()

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