Skip to content
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

Successors length limits #230

Merged
merged 1 commit into from
Jun 28, 2024
Merged
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
59 changes: 55 additions & 4 deletions automata/fa/dfa.py
Original file line number Diff line number Diff line change
Expand Up @@ -1287,6 +1287,8 @@ def predecessor(
*,
strict: bool = True,
key: Optional[Callable[[Any], Any]] = None,
min_length: int = 0,
max_length: Optional[int] = None,
) -> Optional[str]:
"""
Returns the first string accepted by the DFA that comes before
Expand All @@ -1302,6 +1304,10 @@ def predecessor(
key : Optional[Callable], default: None
Function for defining custom lexicographical ordering. Defaults to using
the standard string ordering.
min_length : int, default: 0
Limits generation to words with at least the given length.
max_length : Optional[int], default: None
Limits generation to words with at most the given length.

Returns
------
Expand All @@ -1314,7 +1320,13 @@ def predecessor(
Raised if the language accepted by self is infinite, as we cannot
generate predecessors in this case.
"""
for word in self.predecessors(input_str, strict=strict, key=key):
for word in self.predecessors(
input_str,
strict=strict,
key=key,
min_length=min_length,
max_length=max_length,
):
return word
return None

Expand All @@ -1324,6 +1336,8 @@ def predecessors(
*,
strict: bool = True,
key: Optional[Callable[[Any], Any]] = None,
min_length: int = 0,
max_length: Optional[int] = None,
) -> Generator[str, None, None]:
"""
Generates all strings that come before the input string
Expand All @@ -1339,6 +1353,10 @@ def predecessors(
key : Optional[Callable], default: None
Function for defining custom lexicographical ordering. Defaults to using
the standard string ordering.
min_length : int, default: 0
Limits generation to words with at least the given length.
max_length : Optional[int], default: None
Limits generation to words with at most the given length.

Returns
------
Expand All @@ -1352,14 +1370,23 @@ def predecessors(
Raised if the language accepted by self is infinite, as we cannot
generate predecessors in this case.
"""
yield from self.successors(input_str, strict=strict, reverse=True, key=key)
yield from self.successors(
input_str,
strict=strict,
reverse=True,
key=key,
min_length=min_length,
max_length=max_length,
)

def successor(
self,
input_str: Optional[str],
*,
strict: bool = True,
key: Optional[Callable[[Any], Any]] = None,
min_length: int = 0,
max_length: Optional[int] = None,
) -> Optional[str]:
"""
Returns the first string accepted by the DFA that comes after
Expand All @@ -1375,13 +1402,23 @@ def successor(
key : Optional[Callable], default: None
Function for defining custom lexicographical ordering. Defaults to using
the standard string ordering.
min_length : int, default: 0
Limits generation to words with at least the given length.
max_length : Optional[int], default: None
Limits generation to words with at most the given length.

Returns
------
str
The first string accepted by the DFA lexicographically before input_string.
"""
for word in self.successors(input_str, strict=strict, key=key):
for word in self.successors(
input_str,
strict=strict,
key=key,
min_length=min_length,
max_length=max_length,
):
return word
return None

Expand All @@ -1392,6 +1429,8 @@ def successors(
strict: bool = True,
key: Optional[Callable[[Any], Any]] = None,
reverse: bool = False,
min_length: int = 0,
max_length: Optional[int] = None,
) -> Generator[str, None, None]:
"""
Generates all strings that come after the input string
Expand All @@ -1409,6 +1448,10 @@ def successors(
the standard string ordering.
reverse : bool, default: False
If True, then predecessors will be generated instead of successors.
min_length : int, default: 0
Limits generation to words with at least the given length.
max_length : Optional[int], default: None
Limits generation to words with at most the given length.

Returns
------
Expand Down Expand Up @@ -1457,6 +1500,8 @@ def successors(
if (
not reverse
and should_yield
and min_length <= len(char_stack)
and (max_length is None or len(char_stack) <= max_length)
and candidate == first_symbol
and state in self.final_states
):
Expand All @@ -1467,7 +1512,9 @@ def successors(
else self._get_next_current_state(state, candidate)
)
# Traverse to child if candidate is viable
if candidate_state in coaccessible_nodes:
if candidate_state in coaccessible_nodes and (
max_length is None or len(char_stack) < max_length
):
state_stack.append(candidate_state)
char_stack.append(cast(str, candidate))
candidate = first_symbol
Expand All @@ -1476,6 +1523,8 @@ def successors(
if (
reverse
and should_yield
and min_length <= len(char_stack)
and (max_length is None or len(char_stack) <= max_length)
and candidate is None
and state in self.final_states
):
Expand All @@ -1491,6 +1540,8 @@ def successors(
if (
reverse
and should_yield
and min_length <= len(char_stack)
and (max_length is None or len(char_stack) <= max_length)
and candidate is None
and state in self.final_states
):
Expand Down
11 changes: 11 additions & 0 deletions tests/test_dfa.py
Original file line number Diff line number Diff line change
Expand Up @@ -1887,6 +1887,9 @@ def test_predecessor(self, as_partial: bool) -> None:
actual = list(dfa.predecessors("010", strict=False))

self.assertEqual(dfa.predecessor("000"), "00")
self.assertEqual(dfa.predecessor("000", max_length=1), "0")
self.assertEqual(dfa.predecessor("0", min_length=2), None)
self.assertEqual(dfa.predecessor("0000", min_length=2, max_length=3), "000")
self.assertEqual(dfa.predecessor("0100"), "010")
self.assertEqual(dfa.predecessor("1"), "010101111111101011010100")
self.assertEqual(
Expand Down Expand Up @@ -1923,6 +1926,10 @@ def test_successor(self, as_partial: bool) -> None:
self.assertIsNone(dfa.successor("110"))
self.assertIsNone(dfa.successor("111111110101011"))

self.assertEqual(dfa.successor("", min_length=3), "000")
self.assertEqual(dfa.successor("", min_length=4), "010101111111101011010100")
self.assertEqual(dfa.successor("010", max_length=6), "100")

infinite_dfa = DFA.from_nfa(NFA.from_regex("0*1*"))
self.assertEqual(infinite_dfa.successor(""), "0")
self.assertEqual(infinite_dfa.successor("0"), "00")
Expand All @@ -1933,6 +1940,10 @@ def test_successor(self, as_partial: bool) -> None:
self.assertEqual(infinite_dfa.successor("1"), "11")
self.assertEqual(infinite_dfa.successor(100 * "0"), 101 * "0")
self.assertEqual(infinite_dfa.successor(100 * "1"), 101 * "1")
self.assertEqual(infinite_dfa.successor("", min_length=5), "00000")
self.assertEqual(infinite_dfa.successor("000", min_length=5), "00000")
self.assertEqual(infinite_dfa.successor("1", min_length=5), "11111")
self.assertEqual(infinite_dfa.successor("1111", max_length=4), None)

@params(True, False)
def test_successor_and_predecessor(self, as_partial: bool) -> None:
Expand Down
Loading