From 0bb4a6dd5c3929beb8ac7752b18bf5a9a0d604a1 Mon Sep 17 00:00:00 2001 From: Doodle Date: Thu, 27 Oct 2022 20:29:54 +0530 Subject: [PATCH 1/5] Create josephus problem.py --- dynamic_programming/josephus problem.py | 27 +++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 dynamic_programming/josephus problem.py diff --git a/dynamic_programming/josephus problem.py b/dynamic_programming/josephus problem.py new file mode 100644 index 000000000000..d1ad26e8411b --- /dev/null +++ b/dynamic_programming/josephus problem.py @@ -0,0 +1,27 @@ +def josephus(person, k, index): + + # when only one person is left + if len(person) == 1: + print(person[0]) + return + + # find the index of first person which will die + index = ((index+k)%len(person)) + + # remove the first person which is going to be killed + person.pop(index) + + # recursive call for n-1 persons + Josh(person,k,index) + +n = 14 +k = 2 +k-=1 + +index = 0 + +person=[] +for i in range(1,n+1): + person.append(i) + +josephus(person,k,index) \ No newline at end of file From f775794d180648a7e326aa488bd8c9bf7771cd99 Mon Sep 17 00:00:00 2001 From: Doodle Date: Thu, 9 Oct 2025 12:47:24 +0530 Subject: [PATCH 2/5] Create ip_address.py code for valid ip addresses --- backtracking/ip_address.py | 45 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 backtracking/ip_address.py diff --git a/backtracking/ip_address.py b/backtracking/ip_address.py new file mode 100644 index 000000000000..b5cbd5f13ce4 --- /dev/null +++ b/backtracking/ip_address.py @@ -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. + + We are given with a string containing only digits , return all possible valid IP Addresses that + 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): + 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): + 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]) + + backtrack(0, []) + return res From 3dcbd626e334f4dec842522ffb5c3e97a7cb51a8 Mon Sep 17 00:00:00 2001 From: Doodle Date: Thu, 9 Oct 2025 15:40:13 +0530 Subject: [PATCH 3/5] Delete josephus problem.py --- dynamic_programming/josephus problem.py | 27 ------------------------- 1 file changed, 27 deletions(-) delete mode 100644 dynamic_programming/josephus problem.py diff --git a/dynamic_programming/josephus problem.py b/dynamic_programming/josephus problem.py deleted file mode 100644 index d1ad26e8411b..000000000000 --- a/dynamic_programming/josephus problem.py +++ /dev/null @@ -1,27 +0,0 @@ -def josephus(person, k, index): - - # when only one person is left - if len(person) == 1: - print(person[0]) - return - - # find the index of first person which will die - index = ((index+k)%len(person)) - - # remove the first person which is going to be killed - person.pop(index) - - # recursive call for n-1 persons - Josh(person,k,index) - -n = 14 -k = 2 -k-=1 - -index = 0 - -person=[] -for i in range(1,n+1): - person.append(i) - -josephus(person,k,index) \ No newline at end of file From c6540aa9bce81d711db50c03acff8378ed168526 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 9 Oct 2025 10:18:47 +0000 Subject: [PATCH 4/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- backtracking/ip_address.py | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/backtracking/ip_address.py b/backtracking/ip_address.py index b5cbd5f13ce4..6a58ac4341ec 100644 --- a/backtracking/ip_address.py +++ b/backtracking/ip_address.py @@ -1,19 +1,19 @@ """ - 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. - - We are given with a string containing only digits , return all possible valid IP Addresses that - 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"] +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. + +We are given with a string containing only digits , return all possible valid IP Addresses that +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"] """ @@ -33,10 +33,10 @@ def backtrack(start, path): for l in range(1, 4): if start + l > len(s): break - segment = s[start:start + l] + segment = s[start : start + l] # Skip invalid parts (leading zeros or > 255) - if (segment.startswith('0') and len(segment) > 1) or int(segment) > 255: + if (segment.startswith("0") and len(segment) > 1) or int(segment) > 255: continue backtrack(start + l, path + [segment]) From a8ad6998685eebdbbfced429211c41d87f7e6253 Mon Sep 17 00:00:00 2001 From: zirea3l Date: Thu, 9 Oct 2025 10:19:21 +0000 Subject: [PATCH 5/5] updating DIRECTORY.md --- DIRECTORY.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 36acb3b97f1e..c7b0050fd8d6 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -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) @@ -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