bin-dec-conversions 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of bin-dec-conversions might be problematic. Click here for more details.
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: bin-dec-conversions
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A Python package for binary and decimal conversions implemented from scratch.
|
|
5
|
+
Author: Marco Salvatore Tornabene
|
|
6
|
+
Requires-Python: >=3.10
|
|
7
|
+
Provides-Extra: dev
|
|
8
|
+
Requires-Dist: pytest; extra == "dev"
|
|
9
|
+
Requires-Dist: ruff; extra == "dev"
|
|
10
|
+
Requires-Dist: black; extra == "dev"
|
|
11
|
+
Requires-Dist: build; extra == "dev"
|
|
12
|
+
Requires-Dist: twine; extra == "dev"
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
conversions/__init__.py,sha256=eqlnbB4oqhDEdViaHE7E8qK5paKzxgpDDou32uPKRwA,116
|
|
2
|
+
conversions/conversions.py,sha256=6ycH4IapZ102dA2h4B5U6BNZfskXP6O7VHtDtC1plUA,555
|
|
3
|
+
bin_dec_conversions-0.1.0.dist-info/METADATA,sha256=nBcodT5FHe6kdixm0YSpQlX4KcPd4DAUBl1L535cNrY,413
|
|
4
|
+
bin_dec_conversions-0.1.0.dist-info/WHEEL,sha256=YVMoNqKzERt-wjUZwJ33xBGAwnFl-4cqbYkTtWa4itE,91
|
|
5
|
+
bin_dec_conversions-0.1.0.dist-info/top_level.txt,sha256=UGHjyMezN0xgDu568ihDaGywxEhblMXdd-jDdbKNiDk,12
|
|
6
|
+
bin_dec_conversions-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
conversions
|
conversions/__init__.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import re
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def binary_to_decimal(binary: str) -> int:
|
|
5
|
+
if not re.fullmatch(r"[01]+", binary):
|
|
6
|
+
raise ValueError
|
|
7
|
+
|
|
8
|
+
decimal = 0
|
|
9
|
+
reversed_binary = binary[::-1]
|
|
10
|
+
for i, bit in enumerate(reversed_binary):
|
|
11
|
+
if bit == "1":
|
|
12
|
+
decimal += 2**i
|
|
13
|
+
|
|
14
|
+
return decimal
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def decimal_to_binary(decimal: int) -> str:
|
|
18
|
+
if decimal < 0:
|
|
19
|
+
raise ValueError
|
|
20
|
+
|
|
21
|
+
if decimal == 0:
|
|
22
|
+
return "0"
|
|
23
|
+
|
|
24
|
+
bits = []
|
|
25
|
+
|
|
26
|
+
while decimal > 0:
|
|
27
|
+
bits.append(str(decimal % 2))
|
|
28
|
+
decimal //= 2
|
|
29
|
+
|
|
30
|
+
return "".join(bits[::-1])
|