Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit cbbe4f3

Browse files
committed
refactored and removed unwated/unused code and made it simpler.
1 parent 136990b commit cbbe4f3

File tree

1 file changed

+32
-16
lines changed

1 file changed

+32
-16
lines changed

DynamicProgramming/wordBreak.py

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from typing import Any
2222

2323

24-
def word_break(string: str, words: list[str]) -> bool:
24+
def word_break(givenInputString: str, words: list[str]) -> bool:
2525
"""
2626
Return True if numbers have opposite signs False otherwise.
2727
@@ -51,7 +51,7 @@ def word_break(string: str, words: list[str]) -> bool:
5151
ValueError: the words should be a list of non-empty strings
5252
"""
5353

54-
if not isinstance(string, str) or len(string) == 0:
54+
if not isinstance(givenInputString, str) or len(givenInputString) == 0:
5555
raise ValueError("the string should be not empty string")
5656

5757
if not isinstance(words, list) or not all(
@@ -61,8 +61,21 @@ def word_break(string: str, words: list[str]) -> bool:
6161

6262
word_keeper_key = "WORD_KEEPER"
6363

64-
# Helper function to build the trie
64+
6565
def build_trie(words: list[str]) -> dict[str, Any]:
66+
"""
67+
Builds a trie (prefix tree) from a list of words.
68+
Args:
69+
words (list[str]): A list of words to be inserted into the trie.
70+
Returns:
71+
dict[str, Any]: The root of the trie data structure.
72+
Example:
73+
>>> words = ["apple", "app", "banana"]
74+
>>> trie = build_trie(words)
75+
>>> print(trie)
76+
{'a': {'p': {'p': {'l': {'e': {'word_keeper_key': True}}, 'word_keeper_key': True}}}, 'b': {'a': {'n': {'a': {'n': {'a': {'word_keeper_key': True}}}}}}}
77+
"""
78+
6679
trie: dict[str, Any] = {}
6780

6881
for word in words:
@@ -74,25 +87,28 @@ def build_trie(words: list[str]) -> dict[str, Any]:
7487
trie_node[word_keeper_key] = True
7588

7689
return trie
90+
91+
92+
"""
93+
The functools.cache decorator is used to cache the results of the function it decorates. This means that when the function is called with the same arguments, the cached result is returned instead of recomputing the result. This can significantly improve performance, especially for recursive functions like is_breakable in your code.
94+
95+
In your word_break function, functools.cache is used to cache the results of the is_breakable function. This helps avoid redundant calculations and speeds up the process of checking if the string can be segmented into words from the given list.
7796
78-
# Build trie
79-
trie = build_trie(words)
80-
strLength = len(string)
97+
Here's a brief explanation of how it works in your code:
8198
82-
# Dynamic programming method
99+
When is_breakable is called with a specific index, the result is computed and stored in the cache.
100+
If is_breakable is called again with the same index, the cached result is returned immediately, avoiding the need to recompute the result.
101+
This caching mechanism helps optimize the recursive calls, making the function more efficient.
102+
"""
83103
@functools.cache
84104
def is_breakable(index: int) -> bool:
85-
"""
86-
>>> string = 'a'
87-
>>> is_breakable(1)
88-
True
89-
"""
90-
if index == strLength:
105+
106+
if index == len(givenInputString):
91107
return True
92108

93-
trie_node = trie
94-
for letter in range(index, strLength):
95-
trie_node = trie_node.get(string[letter], None)
109+
trie_node = build_trie(words)
110+
for letter in range(index, len(givenInputString)):
111+
trie_node = trie_node.get(givenInputString[letter], None)
96112

97113
if trie_node is None:
98114
return False

0 commit comments

Comments
 (0)