Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions backtracking/ip_address.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""
Restore IP Addresses
-> IP Address consists of exactly four integers seperated by single dots.
-> Each integer is between 0 and 255.
-> Exempli Gratia - 192.168.1.1 is valid ip address but 192.168@1.1 is an invalid ip address.

Check failure on line 5 in backtracking/ip_address.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

backtracking/ip_address.py:5:89: E501 Line too long (97 > 88)

Check failure on line 5 in backtracking/ip_address.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

backtracking/ip_address.py:5:89: E501 Line too long (97 > 88)

We are given with a string containing only digits , return all possible valid IP Addresses that

Check failure on line 7 in backtracking/ip_address.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

backtracking/ip_address.py:7:100: W291 Trailing whitespace

Check failure on line 7 in backtracking/ip_address.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

backtracking/ip_address.py:7:89: E501 Line too long (100 > 88)

Check failure on line 7 in backtracking/ip_address.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

backtracking/ip_address.py:7:100: W291 Trailing whitespace

Check failure on line 7 in backtracking/ip_address.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

backtracking/ip_address.py:7:89: E501 Line too long (100 > 88)
can be formed by inserting dots in the string.
--> Not allowed to reorder or remove any digits in the string.
--> Return valid IP addresses in any order.

Check failure on line 11 in backtracking/ip_address.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

backtracking/ip_address.py:11:1: W293 Blank line contains whitespace

Check failure on line 11 in backtracking/ip_address.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

backtracking/ip_address.py:11:1: W293 Blank line contains whitespace
Topics covered: Backtracking

Check failure on line 13 in backtracking/ip_address.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

backtracking/ip_address.py:13:1: W293 Blank line contains whitespace

Check failure on line 13 in backtracking/ip_address.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

backtracking/ip_address.py:13:1: W293 Blank line contains whitespace
Example:
Input: s = "25525511135"
Output: ["255.255.11.135","255.255.111.35"]

"""


class Solution:
def restoreIpAddresses(self, s: str):

Check failure on line 22 in backtracking/ip_address.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N802)

backtracking/ip_address.py:22:9: N802 Function name `restoreIpAddresses` should be lowercase

Check failure on line 22 in backtracking/ip_address.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N802)

backtracking/ip_address.py:22:9: N802 Function name `restoreIpAddresses` should be lowercase
res = []

def backtrack(start, path):
# Base case: if we have 4 parts and used all digits
if len(path) == 4:
if start == len(s):
res.append(".".join(path))
return

# Try segments of length 1 to 3
for l in range(1, 4):

Check failure on line 33 in backtracking/ip_address.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E741)

backtracking/ip_address.py:33:17: E741 Ambiguous variable name: `l`

Check failure on line 33 in backtracking/ip_address.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E741)

backtracking/ip_address.py:33:17: E741 Ambiguous variable name: `l`
if start + l > len(s):
break
segment = s[start:start + l]

# Skip invalid parts (leading zeros or > 255)
if (segment.startswith('0') and len(segment) > 1) or int(segment) > 255:
continue

backtrack(start + l, path + [segment])

Check failure on line 42 in backtracking/ip_address.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (RUF005)

backtracking/ip_address.py:42:38: RUF005 Consider `[*path, segment]` instead of concatenation

Check failure on line 42 in backtracking/ip_address.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (RUF005)

backtracking/ip_address.py:42:38: RUF005 Consider `[*path, segment]` instead of concatenation

backtrack(0, [])
return res
Loading