Skip to content
Closed
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* [Generate Parentheses](backtracking/generate_parentheses.py)
* [Generate Parentheses Iterative](backtracking/generate_parentheses_iterative.py)
* [Hamiltonian Cycle](backtracking/hamiltonian_cycle.py)
* [Ip Address](backtracking/ip_address.py)
* [Knight Tour](backtracking/knight_tour.py)
* [Match Word Pattern](backtracking/match_word_pattern.py)
* [Minimax](backtracking/minimax.py)
Expand Down Expand Up @@ -195,6 +196,7 @@
* [Permutations](data_structures/arrays/permutations.py)
* [Prefix Sum](data_structures/arrays/prefix_sum.py)
* [Product Sum](data_structures/arrays/product_sum.py)
* [Rotate Array](data_structures/arrays/rotate_array.py)
* [Sparse Table](data_structures/arrays/sparse_table.py)
* [Sudoku Solver](data_structures/arrays/sudoku_solver.py)
* Binary Tree
Expand Down
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 (93 > 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 (E501)

backtracking/ip_address.py:7:89: E501 Line too long (95 > 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.

Topics covered: Backtracking

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file backtracking/ip_address.py, please provide doctest for the function restoreIpAddresses

Please provide return type hint for the function: restoreIpAddresses. If the function does not return a value, please provide the type hint as: def function() -> None:

Variable and function names should follow the snake_case naming convention. Please update the following name accordingly: restoreIpAddresses

Please provide descriptive name for the parameter: s

res = []

def backtrack(start, path):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file backtracking/ip_address.py, please provide doctest for the function backtrack

Please provide return type hint for the function: backtrack. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: start

Please provide type hint for the parameter: 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`
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

backtrack(0, [])
return res
Loading