mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-23 21:11:08 +00:00
fix(mypy): type annotations for conversions algorithms (#4314)
* fix(mypy): type annotations for conversions algorithms * refactor(CI): include conversions algorithms for mypy tests
This commit is contained in:
parent
536fb4bca4
commit
20c7518028
2
.github/workflows/build.yml
vendored
2
.github/workflows/build.yml
vendored
|
@ -23,7 +23,7 @@ jobs:
|
||||||
python -m pip install mypy pytest-cov -r requirements.txt
|
python -m pip install mypy pytest-cov -r requirements.txt
|
||||||
# FIXME: #4052 fix mypy errors in the exclude directories and remove them below
|
# FIXME: #4052 fix mypy errors in the exclude directories and remove them below
|
||||||
- run: mypy --ignore-missing-imports
|
- run: mypy --ignore-missing-imports
|
||||||
--exclude '(conversions|data_structures|digital_image_processing|dynamic_programming|graphs|linear_algebra|maths|matrix|other|project_euler|scripts|searches|strings*)/$' .
|
--exclude '(data_structures|digital_image_processing|dynamic_programming|graphs|linear_algebra|maths|matrix|other|project_euler|scripts|searches|strings*)/$' .
|
||||||
- name: Run tests
|
- name: Run tests
|
||||||
run: pytest --doctest-modules --ignore=project_euler/ --ignore=scripts/ --cov-report=term-missing:skip-covered --cov=. .
|
run: pytest --doctest-modules --ignore=project_euler/ --ignore=scripts/ --cov-report=term-missing:skip-covered --cov=. .
|
||||||
- if: ${{ success() }}
|
- if: ${{ success() }}
|
||||||
|
|
|
@ -28,7 +28,7 @@ def bin_to_octal(bin_string: str) -> str:
|
||||||
bin_string = "0" + bin_string
|
bin_string = "0" + bin_string
|
||||||
bin_string_in_3_list = [
|
bin_string_in_3_list = [
|
||||||
bin_string[index : index + 3]
|
bin_string[index : index + 3]
|
||||||
for index, value in enumerate(bin_string)
|
for index in range(len(bin_string))
|
||||||
if index % 3 == 0
|
if index % 3 == 0
|
||||||
]
|
]
|
||||||
for bin_group in bin_string_in_3_list:
|
for bin_group in bin_string_in_3_list:
|
||||||
|
|
|
@ -28,9 +28,9 @@ def decimal_to_binary(num: int) -> str:
|
||||||
TypeError: 'str' object cannot be interpreted as an integer
|
TypeError: 'str' object cannot be interpreted as an integer
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if type(num) == float:
|
if isinstance(num, float):
|
||||||
raise TypeError("'float' object cannot be interpreted as an integer")
|
raise TypeError("'float' object cannot be interpreted as an integer")
|
||||||
if type(num) == str:
|
if isinstance(num, str):
|
||||||
raise TypeError("'str' object cannot be interpreted as an integer")
|
raise TypeError("'str' object cannot be interpreted as an integer")
|
||||||
|
|
||||||
if num == 0:
|
if num == 0:
|
||||||
|
@ -42,7 +42,7 @@ def decimal_to_binary(num: int) -> str:
|
||||||
negative = True
|
negative = True
|
||||||
num = -num
|
num = -num
|
||||||
|
|
||||||
binary = []
|
binary: list[int] = []
|
||||||
while num > 0:
|
while num > 0:
|
||||||
binary.insert(0, num % 2)
|
binary.insert(0, num % 2)
|
||||||
num >>= 1
|
num >>= 1
|
||||||
|
|
|
@ -21,7 +21,7 @@ values = {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def decimal_to_hexadecimal(decimal):
|
def decimal_to_hexadecimal(decimal: float) -> str:
|
||||||
"""
|
"""
|
||||||
take integer decimal value, return hexadecimal representation as str beginning
|
take integer decimal value, return hexadecimal representation as str beginning
|
||||||
with 0x
|
with 0x
|
||||||
|
@ -58,6 +58,7 @@ def decimal_to_hexadecimal(decimal):
|
||||||
True
|
True
|
||||||
"""
|
"""
|
||||||
assert type(decimal) in (int, float) and decimal == int(decimal)
|
assert type(decimal) in (int, float) and decimal == int(decimal)
|
||||||
|
decimal = int(decimal)
|
||||||
hexadecimal = ""
|
hexadecimal = ""
|
||||||
negative = False
|
negative = False
|
||||||
if decimal < 0:
|
if decimal < 0:
|
||||||
|
|
|
@ -17,14 +17,14 @@ def decimal_to_octal(num: int) -> str:
|
||||||
counter = 0
|
counter = 0
|
||||||
while num > 0:
|
while num > 0:
|
||||||
remainder = num % 8
|
remainder = num % 8
|
||||||
octal = octal + (remainder * math.pow(10, counter))
|
octal = octal + (remainder * math.floor(math.pow(10, counter)))
|
||||||
counter += 1
|
counter += 1
|
||||||
num = math.floor(num / 8) # basically /= 8 without remainder if any
|
num = math.floor(num / 8) # basically /= 8 without remainder if any
|
||||||
# This formatting removes trailing '.0' from `octal`.
|
# This formatting removes trailing '.0' from `octal`.
|
||||||
return f"0o{int(octal)}"
|
return f"0o{int(octal)}"
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main() -> None:
|
||||||
"""Print octal equivalents of decimal numbers."""
|
"""Print octal equivalents of decimal numbers."""
|
||||||
print("\n2 in octal is:")
|
print("\n2 in octal is:")
|
||||||
print(decimal_to_octal(2)) # = 2
|
print(decimal_to_octal(2)) # = 2
|
||||||
|
|
|
@ -59,10 +59,12 @@ def convert_si_prefix(
|
||||||
1000
|
1000
|
||||||
"""
|
"""
|
||||||
if isinstance(known_prefix, str):
|
if isinstance(known_prefix, str):
|
||||||
known_prefix: SI_Unit = SI_Unit[known_prefix.lower()]
|
known_prefix = SI_Unit[known_prefix.lower()]
|
||||||
if isinstance(unknown_prefix, str):
|
if isinstance(unknown_prefix, str):
|
||||||
unknown_prefix: SI_Unit = SI_Unit[unknown_prefix.lower()]
|
unknown_prefix = SI_Unit[unknown_prefix.lower()]
|
||||||
unknown_amount = known_amount * (10 ** (known_prefix.value - unknown_prefix.value))
|
unknown_amount: float = known_amount * (
|
||||||
|
10 ** (known_prefix.value - unknown_prefix.value)
|
||||||
|
)
|
||||||
return unknown_amount
|
return unknown_amount
|
||||||
|
|
||||||
|
|
||||||
|
@ -85,10 +87,10 @@ def convert_binary_prefix(
|
||||||
1024
|
1024
|
||||||
"""
|
"""
|
||||||
if isinstance(known_prefix, str):
|
if isinstance(known_prefix, str):
|
||||||
known_prefix: Binary_Unit = Binary_Unit[known_prefix.lower()]
|
known_prefix = Binary_Unit[known_prefix.lower()]
|
||||||
if isinstance(unknown_prefix, str):
|
if isinstance(unknown_prefix, str):
|
||||||
unknown_prefix: Binary_Unit = Binary_Unit[unknown_prefix.lower()]
|
unknown_prefix = Binary_Unit[unknown_prefix.lower()]
|
||||||
unknown_amount = known_amount * (
|
unknown_amount: float = known_amount * (
|
||||||
2 ** ((known_prefix.value - unknown_prefix.value) * 10)
|
2 ** ((known_prefix.value - unknown_prefix.value) * 10)
|
||||||
)
|
)
|
||||||
return unknown_amount
|
return unknown_amount
|
||||||
|
|
|
@ -29,7 +29,7 @@ REFERENCES :
|
||||||
-> Wikipedia reference: https://en.wikipedia.org/wiki/Dalton_(unit)
|
-> Wikipedia reference: https://en.wikipedia.org/wiki/Dalton_(unit)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
KILOGRAM_CHART = {
|
KILOGRAM_CHART: dict[str, float] = {
|
||||||
"kilogram": 1,
|
"kilogram": 1,
|
||||||
"gram": pow(10, 3),
|
"gram": pow(10, 3),
|
||||||
"milligram": pow(10, 6),
|
"milligram": pow(10, 6),
|
||||||
|
@ -42,7 +42,7 @@ KILOGRAM_CHART = {
|
||||||
"atomic-mass-unit": 6.022136652e26,
|
"atomic-mass-unit": 6.022136652e26,
|
||||||
}
|
}
|
||||||
|
|
||||||
WEIGHT_TYPE_CHART = {
|
WEIGHT_TYPE_CHART: dict[str, float] = {
|
||||||
"kilogram": 1,
|
"kilogram": 1,
|
||||||
"gram": pow(10, -3),
|
"gram": pow(10, -3),
|
||||||
"milligram": pow(10, -6),
|
"milligram": pow(10, -6),
|
||||||
|
|
Loading…
Reference in New Issue
Block a user