@@ -1276,6 +1276,8 @@ def predecessor(
1276
1276
* ,
1277
1277
strict : bool = True ,
1278
1278
key : Optional [Callable [[Any ], Any ]] = None ,
1279
+ min_length : int = 0 ,
1280
+ max_length : Optional [int ] = None ,
1279
1281
) -> Optional [str ]:
1280
1282
"""
1281
1283
Returns the first string accepted by the DFA that comes before
@@ -1291,6 +1293,10 @@ def predecessor(
1291
1293
key : Optional[Callable], default: None
1292
1294
Function for defining custom lexicographical ordering. Defaults to using
1293
1295
the standard string ordering.
1296
+ min_length : int, default: 0
1297
+ Limits generation to words with at least the given length.
1298
+ max_length : Optional[int], default: None
1299
+ Limits generation to words with at most the given length.
1294
1300
1295
1301
Returns
1296
1302
------
@@ -1303,7 +1309,13 @@ def predecessor(
1303
1309
Raised if the language accepted by self is infinite, as we cannot
1304
1310
generate predecessors in this case.
1305
1311
"""
1306
- for word in self .predecessors (input_str , strict = strict , key = key ):
1312
+ for word in self .predecessors (
1313
+ input_str ,
1314
+ strict = strict ,
1315
+ key = key ,
1316
+ min_length = min_length ,
1317
+ max_length = max_length ,
1318
+ ):
1307
1319
return word
1308
1320
return None
1309
1321
@@ -1313,6 +1325,8 @@ def predecessors(
1313
1325
* ,
1314
1326
strict : bool = True ,
1315
1327
key : Optional [Callable [[Any ], Any ]] = None ,
1328
+ min_length : int = 0 ,
1329
+ max_length : Optional [int ] = None ,
1316
1330
) -> Generator [str , None , None ]:
1317
1331
"""
1318
1332
Generates all strings that come before the input string
@@ -1328,6 +1342,10 @@ def predecessors(
1328
1342
key : Optional[Callable], default: None
1329
1343
Function for defining custom lexicographical ordering. Defaults to using
1330
1344
the standard string ordering.
1345
+ min_length : int, default: 0
1346
+ Limits generation to words with at least the given length.
1347
+ max_length : Optional[int], default: None
1348
+ Limits generation to words with at most the given length.
1331
1349
1332
1350
Returns
1333
1351
------
@@ -1341,14 +1359,23 @@ def predecessors(
1341
1359
Raised if the language accepted by self is infinite, as we cannot
1342
1360
generate predecessors in this case.
1343
1361
"""
1344
- yield from self .successors (input_str , strict = strict , reverse = True , key = key )
1362
+ yield from self .successors (
1363
+ input_str ,
1364
+ strict = strict ,
1365
+ reverse = True ,
1366
+ key = key ,
1367
+ min_length = min_length ,
1368
+ max_length = max_length ,
1369
+ )
1345
1370
1346
1371
def successor (
1347
1372
self ,
1348
1373
input_str : Optional [str ],
1349
1374
* ,
1350
1375
strict : bool = True ,
1351
1376
key : Optional [Callable [[Any ], Any ]] = None ,
1377
+ min_length : int = 0 ,
1378
+ max_length : Optional [int ] = None ,
1352
1379
) -> Optional [str ]:
1353
1380
"""
1354
1381
Returns the first string accepted by the DFA that comes after
@@ -1364,13 +1391,23 @@ def successor(
1364
1391
key : Optional[Callable], default: None
1365
1392
Function for defining custom lexicographical ordering. Defaults to using
1366
1393
the standard string ordering.
1394
+ min_length : int, default: 0
1395
+ Limits generation to words with at least the given length.
1396
+ max_length : Optional[int], default: None
1397
+ Limits generation to words with at most the given length.
1367
1398
1368
1399
Returns
1369
1400
------
1370
1401
str
1371
1402
The first string accepted by the DFA lexicographically before input_string.
1372
1403
"""
1373
- for word in self .successors (input_str , strict = strict , key = key ):
1404
+ for word in self .successors (
1405
+ input_str ,
1406
+ strict = strict ,
1407
+ key = key ,
1408
+ min_length = min_length ,
1409
+ max_length = max_length ,
1410
+ ):
1374
1411
return word
1375
1412
return None
1376
1413
@@ -1381,6 +1418,8 @@ def successors(
1381
1418
strict : bool = True ,
1382
1419
key : Optional [Callable [[Any ], Any ]] = None ,
1383
1420
reverse : bool = False ,
1421
+ min_length : int = 0 ,
1422
+ max_length : Optional [int ] = None ,
1384
1423
) -> Generator [str , None , None ]:
1385
1424
"""
1386
1425
Generates all strings that come after the input string
@@ -1398,6 +1437,10 @@ def successors(
1398
1437
the standard string ordering.
1399
1438
reverse : bool, default: False
1400
1439
If True, then predecessors will be generated instead of successors.
1440
+ min_length : int, default: 0
1441
+ Limits generation to words with at least the given length.
1442
+ max_length : Optional[int], default: None
1443
+ Limits generation to words with at most the given length.
1401
1444
1402
1445
Returns
1403
1446
------
@@ -1446,6 +1489,8 @@ def successors(
1446
1489
if (
1447
1490
not reverse
1448
1491
and should_yield
1492
+ and min_length <= len (char_stack )
1493
+ and (max_length is None or len (char_stack ) <= max_length )
1449
1494
and candidate == first_symbol
1450
1495
and state in self .final_states
1451
1496
):
@@ -1456,7 +1501,9 @@ def successors(
1456
1501
else self ._get_next_current_state (state , candidate )
1457
1502
)
1458
1503
# Traverse to child if candidate is viable
1459
- if candidate_state in coaccessible_nodes :
1504
+ if candidate_state in coaccessible_nodes and (
1505
+ max_length is None or len (char_stack ) < max_length
1506
+ ):
1460
1507
state_stack .append (candidate_state )
1461
1508
char_stack .append (cast (str , candidate ))
1462
1509
candidate = first_symbol
@@ -1465,6 +1512,8 @@ def successors(
1465
1512
if (
1466
1513
reverse
1467
1514
and should_yield
1515
+ and min_length <= len (char_stack )
1516
+ and (max_length is None or len (char_stack ) <= max_length )
1468
1517
and candidate is None
1469
1518
and state in self .final_states
1470
1519
):
@@ -1480,6 +1529,8 @@ def successors(
1480
1529
if (
1481
1530
reverse
1482
1531
and should_yield
1532
+ and min_length <= len (char_stack )
1533
+ and (max_length is None or len (char_stack ) <= max_length )
1483
1534
and candidate is None
1484
1535
and state in self .final_states
1485
1536
):
0 commit comments