From 5af0ee10bed053e4fe09f99c8e4df56a8dbcb482 Mon Sep 17 00:00:00 2001 From: Tapas Singhal <98687345+Shocker-lov-t@users.noreply.github.com> Date: Thu, 26 Oct 2023 23:06:29 +0530 Subject: [PATCH 1/6] Create ipconversion.py --- conversions/ipconversion.py | 59 +++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 conversions/ipconversion.py diff --git a/conversions/ipconversion.py b/conversions/ipconversion.py new file mode 100644 index 000000000000..396cf906e4e8 --- /dev/null +++ b/conversions/ipconversion.py @@ -0,0 +1,59 @@ +# https://www.geeksforgeeks.org/convert-ip-address-to-integer-and-vice-versa/ + +def ip_to_decimal(ip_address: str) -> int: + """ + Convert an IPv4 address to its decimal representation. + + Args: + ip_address (str): A string representing an IPv4 address (e.g., "192.168.0.1"). + + Returns: + int: The decimal representation of the IP address. + + >>> ip_to_decimal("192.168.0.1") + 3232235521 + >>> ip_to_decimal("10.0.0.255") + 167772415 + """ + + ip_parts = ip_address.split('.') + if len(ip_parts) != 4: + raise ValueError("Invalid IPv4 address format") + + decimal_ip = 0 + for part in ip_parts: + decimal_ip = (decimal_ip << 8) + int(part) + + return decimal_ip + +def decimal_to_ip(decimal_ip: int) -> str: + """ + Convert a decimal representation of an IP address to its IPv4 format. + + Args: + decimal_ip (int): An integer representing the decimal IP address. + + Returns: + str: The IPv4 representation of the decimal IP address. + + >>> decimal_to_ip(3232235521) + '192.168.0.1' + >>> decimal_to_ip(167772415) + '10.0.0.255' + """ + + if not (0 <= decimal_ip <= 4294967295): + raise ValueError("Invalid decimal IP address") + + ip_parts = [] + for _ in range(4): + ip_parts.append(str(decimal_ip & 255)) + decimal_ip >>= 8 + + ip_parts.reverse() + return '.'.join(ip_parts) + +if __name__ == "__main__": + import doctest + + doctest.testmod() From 8cb1561f2cda9455073015f4d726388a4a429cfb Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 26 Oct 2023 17:50:46 +0000 Subject: [PATCH 2/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- conversions/ipconversion.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/conversions/ipconversion.py b/conversions/ipconversion.py index 396cf906e4e8..f59cbc5f9079 100644 --- a/conversions/ipconversion.py +++ b/conversions/ipconversion.py @@ -1,5 +1,6 @@ # https://www.geeksforgeeks.org/convert-ip-address-to-integer-and-vice-versa/ + def ip_to_decimal(ip_address: str) -> int: """ Convert an IPv4 address to its decimal representation. @@ -16,7 +17,7 @@ def ip_to_decimal(ip_address: str) -> int: 167772415 """ - ip_parts = ip_address.split('.') + ip_parts = ip_address.split(".") if len(ip_parts) != 4: raise ValueError("Invalid IPv4 address format") @@ -26,6 +27,7 @@ def ip_to_decimal(ip_address: str) -> int: return decimal_ip + def decimal_to_ip(decimal_ip: int) -> str: """ Convert a decimal representation of an IP address to its IPv4 format. @@ -51,7 +53,8 @@ def decimal_to_ip(decimal_ip: int) -> str: decimal_ip >>= 8 ip_parts.reverse() - return '.'.join(ip_parts) + return ".".join(ip_parts) + if __name__ == "__main__": import doctest From 5bf714680f4f0d4f2d5eb296c26c0b67b2585ac4 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Thu, 26 Oct 2023 23:53:53 +0200 Subject: [PATCH 3/6] Update conversions/ipconversion.py --- conversions/ipconversion.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conversions/ipconversion.py b/conversions/ipconversion.py index f59cbc5f9079..48270f43a86b 100644 --- a/conversions/ipconversion.py +++ b/conversions/ipconversion.py @@ -17,7 +17,7 @@ def ip_to_decimal(ip_address: str) -> int: 167772415 """ - ip_parts = ip_address.split(".") + octets = ip_address.split(".") if len(ip_parts) != 4: raise ValueError("Invalid IPv4 address format") From 079dc13f4cf2e5ef2bc2311a8d6147717b74cb5a Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 29 Oct 2023 00:37:19 +0200 Subject: [PATCH 4/6] Update ipconversion.py --- conversions/ipconversion.py | 65 +++++++++++++++++++++++++------------ 1 file changed, 44 insertions(+), 21 deletions(-) diff --git a/conversions/ipconversion.py b/conversions/ipconversion.py index 48270f43a86b..862309b7251e 100644 --- a/conversions/ipconversion.py +++ b/conversions/ipconversion.py @@ -1,59 +1,82 @@ # https://www.geeksforgeeks.org/convert-ip-address-to-integer-and-vice-versa/ -def ip_to_decimal(ip_address: str) -> int: +def ipv4_to_decimal(ipv4_address: str) -> int: """ Convert an IPv4 address to its decimal representation. Args: - ip_address (str): A string representing an IPv4 address (e.g., "192.168.0.1"). + ip_address: A string representing an IPv4 address (e.g., "192.168.0.1"). Returns: int: The decimal representation of the IP address. - >>> ip_to_decimal("192.168.0.1") + >>> ipv4_to_decimal("192.168.0.1") 3232235521 - >>> ip_to_decimal("10.0.0.255") + >>> ipv4_to_decimal("10.0.0.255") 167772415 + >>> ipv4_to_decimal("10.0.255") + Traceback (most recent call last): + ... + ValueError: Invalid IPv4 address format + >>> ipv4_to_decimal("10.0.0.256") + Traceback (most recent call last): + ... + ValueError: Invalid IPv4 octet 256 """ - octets = ip_address.split(".") - if len(ip_parts) != 4: + octets = [int(octet) for octet in ipv4_address.split(".")] + if len(octets) != 4: raise ValueError("Invalid IPv4 address format") - decimal_ip = 0 - for part in ip_parts: - decimal_ip = (decimal_ip << 8) + int(part) + decimal_ipv4 = 0 + for octet in octets: + if not 0 <= octet <= 255: + raise ValueError(f"Invalid IPv4 octet {octet}") # noqa: EM102 + decimal_ipv4 = (decimal_ipv4 << 8) + int(octet) - return decimal_ip + return decimal_ipv4 -def decimal_to_ip(decimal_ip: int) -> str: +def alt_ipv4_to_decimal(ipv4_address: str) -> int: + """ + >>> alt_ipv4_to_decimal("192.168.0.1") + 3232235521 + >>> alt_ipv4_to_decimal("10.0.0.255") + 167772415 + """ + return int("0x" + "".join(f"{int(i):02x}" for i in ipv4_address.split(".")), 16) + + +def decimal_to_ipv4(decimal_ipv4: int) -> str: """ Convert a decimal representation of an IP address to its IPv4 format. Args: - decimal_ip (int): An integer representing the decimal IP address. + decimal_ipv4: An integer representing the decimal IP address. Returns: - str: The IPv4 representation of the decimal IP address. + The IPv4 representation of the decimal IP address. - >>> decimal_to_ip(3232235521) + >>> decimal_to_ipv4(3232235521) '192.168.0.1' - >>> decimal_to_ip(167772415) + >>> decimal_to_ipv4(167772415) '10.0.0.255' + >>> decimal_to_ipv4(-1) + Traceback (most recent call last): + ... + ValueError: Invalid decimal IPv4 address """ - if not (0 <= decimal_ip <= 4294967295): - raise ValueError("Invalid decimal IP address") + if not (0 <= decimal_ipv4 <= 4294967295): + raise ValueError("Invalid decimal IPv4 address") ip_parts = [] for _ in range(4): - ip_parts.append(str(decimal_ip & 255)) - decimal_ip >>= 8 + ip_parts.append(str(decimal_ipv4 & 255)) + decimal_ipv4 >>= 8 - ip_parts.reverse() - return ".".join(ip_parts) + return ".".join(reversed(ip_parts)) if __name__ == "__main__": From aa280cc476c5536b4b3ae42d3a3bbc97d4932159 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 29 Oct 2023 00:40:51 +0200 Subject: [PATCH 5/6] Rename ipconversion.py to ipv4_conversion.py --- conversions/{ipconversion.py => ipv4_conversion.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename conversions/{ipconversion.py => ipv4_conversion.py} (100%) diff --git a/conversions/ipconversion.py b/conversions/ipv4_conversion.py similarity index 100% rename from conversions/ipconversion.py rename to conversions/ipv4_conversion.py From 586fef389831097f864dd5b9b5673f2242081b5f Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 29 Oct 2023 00:44:27 +0200 Subject: [PATCH 6/6] forward_propagation(32, 450_000) # Was 10_000_000 --- neural_network/simple_neural_network.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/neural_network/simple_neural_network.py b/neural_network/simple_neural_network.py index f2a3234873b5..8751a38908cf 100644 --- a/neural_network/simple_neural_network.py +++ b/neural_network/simple_neural_network.py @@ -28,7 +28,7 @@ def sigmoid_function(value: float, deriv: bool = False) -> float: def forward_propagation(expected: int, number_propagations: int) -> float: """Return the value found after the forward propagation training. - >>> res = forward_propagation(32, 10000000) + >>> res = forward_propagation(32, 450_000) # Was 10_000_000 >>> res > 31 and res < 33 True