PIXELBANKv9.1.0
Menu

Problem Statement

Convert a dotted-quad IPv4 address string to its 32-bit integer value.

Background

Each of the four octets contributes 8 bits: the integer is a*2563 + b256**2 + c256 + d**, i.e. the big-endian packing of the four bytes.

Your Task

def ip_to_int(ip):

Return the integer value of the address.

Input Format

  • ip (str): "a.b.c.d" with each octet 0..255.

Output Format

  • A single int.

Sample

print(ip_to_int("192.168.1.1"))

Output:

3232235777

Example:

Input:
print(ip_to_int("192.168.1.1"))
Output:
3232235777
Reasoning:
  • Split the input string "192.168.1.1" by dots to extract the four octets: 192192, 168168, 11, 11.
  • Initialize the accumulator to 00 and process the first octet (192192): 0×256+192=1920 \times 256 + 192 = 192.
  • Process the second octet (168168) by shifting the previous value left by 8 bits (multiplying by 256) and adding the new byte: 192×256+168=49152+168=49320192 \times 256 + 168 = 49152 + 168 = 49320.
  • Process the third octet (11): 49320×256+1=12625920+1=1262592149320 \times 256 + 1 = 12625920 + 1 = 12625921.
  • Process the fourth octet (11): 12625921×256+1=3232235776+1=323223577712625921 \times 256 + 1 = 3232235776 + 1 = 3232235777.
  • The final output is 3232235777

Constraints:

  • Four octets, each 0..255.
  • Big-endian: leftmost octet is most significant.
  • Return an int.
🔒

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.