Skip to content

add word_break dynamic approach up -> down. #8039

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Dec 30, 2022
Merged
Show file tree
Hide file tree
Changes from 10 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
7 changes: 2 additions & 5 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

## Audio Filters
* [Butterworth Filter](audio_filters/butterworth_filter.py)
* [Equal Loudness Filter](audio_filters/equal_loudness_filter.py)
* [Iir Filter](audio_filters/iir_filter.py)
* [Show Response](audio_filters/show_response.py)

Expand Down Expand Up @@ -79,6 +78,7 @@
* [A1Z26](ciphers/a1z26.py)
* [Affine Cipher](ciphers/affine_cipher.py)
* [Atbash](ciphers/atbash.py)
* [Autokey](ciphers/autokey.py)
* [Baconian Cipher](ciphers/baconian_cipher.py)
* [Base16](ciphers/base16.py)
* [Base32](ciphers/base32.py)
Expand Down Expand Up @@ -328,6 +328,7 @@
* [Subset Generation](dynamic_programming/subset_generation.py)
* [Sum Of Subset](dynamic_programming/sum_of_subset.py)
* [Viterbi](dynamic_programming/viterbi.py)
* [Word Break](dynamic_programming/word_break.py)

## Electronics
* [Builtin Voltage](electronics/builtin_voltage.py)
Expand Down Expand Up @@ -475,8 +476,6 @@
* [Decision Tree](machine_learning/decision_tree.py)
* Forecasting
* [Run](machine_learning/forecasting/run.py)
* [Gaussian Naive Bayes](machine_learning/gaussian_naive_bayes.py)
* [Gradient Boosting Regressor](machine_learning/gradient_boosting_regressor.py)
* [Gradient Descent](machine_learning/gradient_descent.py)
* [K Means Clust](machine_learning/k_means_clust.py)
* [K Nearest Neighbours](machine_learning/k_nearest_neighbours.py)
Expand All @@ -490,8 +489,6 @@
* [Lstm Prediction](machine_learning/lstm/lstm_prediction.py)
* [Multilayer Perceptron Classifier](machine_learning/multilayer_perceptron_classifier.py)
* [Polymonial Regression](machine_learning/polymonial_regression.py)
* [Random Forest Classifier](machine_learning/random_forest_classifier.py)
* [Random Forest Regressor](machine_learning/random_forest_regressor.py)
* [Scoring Functions](machine_learning/scoring_functions.py)
* [Self Organizing Map](machine_learning/self_organizing_map.py)
* [Sequential Minimum Optimization](machine_learning/sequential_minimum_optimization.py)
Expand Down
107 changes: 107 additions & 0 deletions dynamic_programming/word_break.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
"""
Author : Alexander Pantyukhin
Date : December 12, 2022

Task:
Given a string s and a dictionary of strings wordDict,
return true if s can be segmented into a space-separated
sequence of one or more dictionary words.

Note that the same word in the dictionary may be reused
multiple times in the segmentation.

Implementation notes: Trie + Dynamic programming up -> down.
The Trie keeps all wordDict words. It will be useful for scanning
available words for the current position in the string.

Leetcode:
https://leetcode.com/problems/word-break/description/

Runtime: O(n * n)
Space: O(n)
"""

from functools import lru_cache
from typing import Any


def word_break(string: str, word_dict: list[str]) -> bool:
"""
Return True if numbers have opposite signs False otherwise.

>>> word_break("applepenapple", ["apple","pen"])
True
>>> word_break("catsandog", ["cats","dog","sand","and","cat"])
False
>>> word_break("cars", ["car","ca","rs"])
True
>>> word_break('abc', [])
False
>>> word_break(123, ['a'])
Traceback (most recent call last):
...
ValueError: the string should be not empty string
>>> word_break('', ['a'])
Traceback (most recent call last):
...
ValueError: the string should be not empty string
>>> word_break('abc', [123])
Traceback (most recent call last):
...
ValueError: the word_dict should a list of non empty string
>>> word_break('abc', [''])
Traceback (most recent call last):
...
ValueError: the word_dict should a list of non empty string
"""

# Validation
if not isinstance(string, str) or len(string) == 0:
raise ValueError("the string should be not empty string")

if not isinstance(word_dict, list) or not all(
isinstance(item, str) and len(item) > 0 for item in word_dict
):
raise ValueError("the word_dict should a list of non empty string")

# Build trie
trie: dict[str, Any] = {}
word_keeper_key = "WORD_KEEPER"

for word in word_dict:
trie_node = trie
for c in word:
if c not in trie_node:
trie_node[c] = {}

trie_node = trie_node[c]

trie_node[word_keeper_key] = True

len_string = len(string)

# Dynamic programming method
@lru_cache(maxsize=None)
def is_breakable(index: int) -> bool:

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 dynamic_programming/word_break.py, please provide doctest for the function is_breakable

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 dynamic_programming/word_break.py, please provide doctest for the function is_breakable

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 dynamic_programming/word_break.py, please provide doctest for the function is_breakable

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 dynamic_programming/word_break.py, please provide doctest for the function is_breakable

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 dynamic_programming/word_break.py, please provide doctest for the function is_breakable

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 dynamic_programming/word_break.py, please provide doctest for the function is_breakable

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 dynamic_programming/word_break.py, please provide doctest for the function is_breakable

if index == len_string:
return True

trie_node = trie
for i in range(index, len_string):
trie_node = trie_node.get(string[i], None)

if trie_node is None:
return False

if trie_node.get(word_keeper_key, False) and is_breakable(i + 1):
return True

return False

return is_breakable(0)


if __name__ == "__main__":
import doctest

doctest.testmod()