fix(mypy): type annotations for conversions algorithms (#4314) · csvila/TheAlgorithms-Python@20c7518 · GitHub
Skip to content

Commit 20c7518

Browse files
authored
fix(mypy): type annotations for conversions algorithms (TheAlgorithms#4314)
* fix(mypy): type annotations for conversions algorithms * refactor(CI): include conversions algorithms for mypy tests
1 parent 536fb4b commit 20c7518

7 files changed

Lines changed: 19 additions & 16 deletions

File tree

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion

conversions/binary_to_octal.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def bin_to_octal(bin_string: str) -> str:
2828
bin_string = "0" + bin_string
2929
bin_string_in_3_list = [
3030
bin_string[index : index + 3]
31-
for index, value in enumerate(bin_string)
31+
for index in range(len(bin_string))
3232
if index % 3 == 0
3333
]
3434
for bin_group in bin_string_in_3_list:

conversions/decimal_to_binary.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ def decimal_to_binary(num: int) -> str:
2828
TypeError: 'str' object cannot be interpreted as an integer
2929
"""
3030

31-
if type(num) == float:
31+
if isinstance(num, float):
3232
raise TypeError("'float' object cannot be interpreted as an integer")
33-
if type(num) == str:
33+
if isinstance(num, str):
3434
raise TypeError("'str' object cannot be interpreted as an integer")
3535

3636
if num == 0:
@@ -42,7 +42,7 @@ def decimal_to_binary(num: int) -> str:
4242
negative = True
4343
num = -num
4444

45-
binary = []
45+
binary: list[int] = []
4646
while num > 0:
4747
binary.insert(0, num % 2)
4848
num >>= 1

conversions/decimal_to_hexadecimal.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
}
2222

2323

24-
def decimal_to_hexadecimal(decimal):
24+
def decimal_to_hexadecimal(decimal: float) -> str:
2525
"""
2626
take integer decimal value, return hexadecimal representation as str beginning
2727
with 0x
@@ -58,6 +58,7 @@ def decimal_to_hexadecimal(decimal):
5858
True
5959
"""
6060
assert type(decimal) in (int, float) and decimal == int(decimal)
61+
decimal = int(decimal)
6162
hexadecimal = ""
6263
negative = False
6364
if decimal < 0:

conversions/decimal_to_octal.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ def decimal_to_octal(num: int) -> str:
1717
counter = 0
1818
while num > 0:
1919
remainder = num % 8
20-
octal = octal + (remainder * math.pow(10, counter))
20+
octal = octal + (remainder * math.floor(math.pow(10, counter)))
2121
counter += 1
2222
num = math.floor(num / 8) # basically /= 8 without remainder if any
2323
# This formatting removes trailing '.0' from `octal`.
2424
return f"0o{int(octal)}"
2525

2626

27-
def main():
27+
def main() -> None:
2828
"""Print octal equivalents of decimal numbers."""
2929
print("\n2 in octal is:")
3030
print(decimal_to_octal(2)) # = 2

conversions/prefix_conversions.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,12 @@ def convert_si_prefix(
5959
1000
6060
"""
6161
if isinstance(known_prefix, str):
62-
known_prefix: SI_Unit = SI_Unit[known_prefix.lower()]
62+
known_prefix = SI_Unit[known_prefix.lower()]
6363
if isinstance(unknown_prefix, str):
64-
unknown_prefix: SI_Unit = SI_Unit[unknown_prefix.lower()]
65-
unknown_amount = known_amount * (10 ** (known_prefix.value - unknown_prefix.value))
64+
unknown_prefix = SI_Unit[unknown_prefix.lower()]
65+
unknown_amount: float = known_amount * (
66+
10 ** (known_prefix.value - unknown_prefix.value)
67+
)
6668
return unknown_amount
6769

6870

@@ -85,10 +87,10 @@ def convert_binary_prefix(
8587
1024
8688
"""
8789
if isinstance(known_prefix, str):
88-
known_prefix: Binary_Unit = Binary_Unit[known_prefix.lower()]
90+
known_prefix = Binary_Unit[known_prefix.lower()]
8991
if isinstance(unknown_prefix, str):
90-
unknown_prefix: Binary_Unit = Binary_Unit[unknown_prefix.lower()]
91-
unknown_amount = known_amount * (
92+
unknown_prefix = Binary_Unit[unknown_prefix.lower()]
93+
unknown_amount: float = known_amount * (
9294
2 ** ((known_prefix.value - unknown_prefix.value) * 10)
9395
)
9496
return unknown_amount

conversions/weight_conversion.py

Lines changed: 2 additions & 2 deletions

0 commit comments

Comments
 (0)