-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6dfe0a9
commit 41ec2a3
Showing
2 changed files
with
147 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import re | ||
|
||
|
||
#Matches the lower_bound and upwards. | ||
def generate_regex_from_lower_bound(lower_bound): | ||
# Constructing the regex pattern dynamically based on the interval | ||
pattern = "((" | ||
current_factor = 0 | ||
num_len = len(str(lower_bound)) | ||
lower_bound_str = str(lower_bound) | ||
for digit in lower_bound_str: | ||
digit = int(digit) | ||
digit_diff = 9 - int(digit) | ||
upper_range_in_factor = (digit+digit_diff) | ||
if current_factor == 0: | ||
pattern += "[{}-{}]".format(digit, upper_range_in_factor) | ||
else: | ||
if current_factor == num_len -1: | ||
pattern += "[{}-9]".format(digit if digit == 9 else digit+1, upper_range_in_factor) | ||
else: | ||
pattern += "[{}-9]".format(digit, upper_range_in_factor) | ||
current_factor += 1 | ||
pattern += ")" | ||
|
||
larger_part = "" | ||
for i in range(num_len): | ||
if i == 0: | ||
larger_part += "|([1-9]" | ||
larger_part += "[0-9]" | ||
larger_part += "+" | ||
larger_part += "))(\.\d\+)?" | ||
|
||
full_pattern = f"{pattern + larger_part}$" | ||
|
||
full_pattern = r'^' + full_pattern | ||
|
||
# Creating the regex object | ||
regex = re.compile(full_pattern) | ||
|
||
return regex | ||
|
||
# Test cases | ||
def test_generate_regex_from_lower_bound(): | ||
# Test with lower bound 100 | ||
regex_100 = generate_regex_from_lower_bound(100) | ||
assert regex_100.match("101") | ||
assert regex_100.match("199") | ||
assert regex_100.match("999") | ||
assert not regex_100.match("99") | ||
assert not regex_100.match("0") | ||
assert not regex_100.match("15") | ||
assert regex_100.match("1000") | ||
|
||
# Test with lower bound 5000 | ||
regex_5000 = generate_regex_from_lower_bound(5000) | ||
assert regex_5000.match("5001") | ||
assert regex_5000.match("5999") | ||
assert regex_5000.match("9999") | ||
assert not regex_5000.match("4999") | ||
assert not regex_5000.match("4") | ||
assert not regex_5000.match("440") | ||
assert regex_5000.match("10000") | ||
|
||
# Test with lower bound 1 | ||
regex_1 = generate_regex_from_lower_bound(1) | ||
assert regex_1.match("2") | ||
assert regex_1.match("9") | ||
assert regex_1.match("99") | ||
assert not regex_1.match("0") | ||
assert regex_1.match("100") | ||
|
||
regex_0 = generate_regex_from_lower_bound(0) | ||
assert regex_0.match("1") | ||
assert regex_0.match("9") | ||
assert regex_0.match("99") | ||
assert not regex_0.match("-1") | ||
assert regex_0.match("100") | ||
|
||
# Test with lower bound 999 | ||
regex_999 = generate_regex_from_lower_bound(999) | ||
assert regex_999.match("1000") | ||
assert regex_999.match("1099") | ||
assert regex_999.match("1999") | ||
assert not regex_999.match("998") | ||
assert not regex_999.match("1") | ||
assert not regex_999.match("88") | ||
assert regex_999.match("999") | ||
assert regex_999.match("9999") | ||
|
||
# Test with lower bound 100000 | ||
regex_100000 = generate_regex_from_lower_bound(100000) | ||
assert regex_100000.match("100001") | ||
assert regex_100000.match("100999") | ||
assert regex_100000.match("199999") | ||
assert not regex_100000.match("99999") | ||
assert regex_100000.match("1000000") | ||
|
||
print("All tests passed successfully.") | ||
|
||
# Run the tests | ||
test_generate_regex_from_lower_bound() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,54 @@ | ||
import re | ||
|
||
def generate_regex_from_lower_bound(lower_bound): | ||
# Constructing the regex pattern dynamically based on the interval | ||
pattern = "((" | ||
current_factor = 0 | ||
num_len = len(str(lower_bound)) | ||
def generate_regex_from_interval(lower_bound, upper_bound): | ||
# Convert bounds to strings | ||
lower_bound_str = str(lower_bound) | ||
for digit in lower_bound_str: | ||
digit = int(digit) | ||
digit_diff = 9 - int(digit) | ||
upper_range_in_factor = (digit+digit_diff) | ||
if current_factor == 0: | ||
pattern += "[{}-{}]".format(digit, upper_range_in_factor) | ||
upper_bound_str = str(upper_bound) | ||
|
||
# Ensure the upper bound has the same number of digits as the lower bound | ||
if len(upper_bound_str) != len(lower_bound_str): | ||
raise ValueError("Upper bound must have the same number of digits as the lower bound.") | ||
|
||
# Initialize pattern | ||
pattern = "^" | ||
|
||
# Iterate over each digit position | ||
for lower_digit, upper_digit in zip(lower_bound_str, upper_bound_str): | ||
# If digits are the same, add them to pattern | ||
if lower_digit == upper_digit: | ||
pattern += lower_digit | ||
else: | ||
if current_factor == num_len -1: | ||
pattern += "[{}-9]".format(digit+1, upper_range_in_factor) | ||
else: | ||
pattern += "[{}-9]".format(digit, upper_range_in_factor) | ||
current_factor += 1 | ||
pattern += ")" | ||
|
||
larger_part = "" | ||
for i in range(num_len): | ||
if i == 0: | ||
larger_part += "|([1-9]" | ||
larger_part += "[0-9]" | ||
larger_part += "))(\.\d+)?" | ||
|
||
full_pattern = f"{pattern + larger_part}$" | ||
|
||
full_pattern = r'^' + full_pattern | ||
# If digits are different, create a range | ||
pattern += f"[{lower_digit}-{upper_digit}]" | ||
|
||
# Add optional digits and decimal part | ||
pattern += r"\d*\.?\d*$" | ||
|
||
# Compile regex | ||
regex = re.compile(pattern) | ||
|
||
# Creating the regex object | ||
regex = re.compile(full_pattern) | ||
|
||
return regex | ||
|
||
# Example usage: | ||
regex = generate_regex_from_lower_bound(100) | ||
# Test cases | ||
def test_generate_regex_from_interval(): | ||
# Test with interval 100-199 | ||
regex_100_199 = generate_regex_from_interval(100, 199) | ||
assert regex_100_199.match("100") | ||
assert regex_100_199.match("101") | ||
assert regex_100_199.match("199") | ||
assert not regex_100_199.match("99") | ||
assert not regex_100_199.match("1999") | ||
assert not regex_100_199.match("200") | ||
|
||
# Test with interval 5000-5999 | ||
regex_5000_5999 = generate_regex_from_interval(5000, 5999) | ||
assert regex_5000_5999.match("5000") | ||
assert regex_5000_5999.match("5001") | ||
assert regex_5000_5999.match("5999") | ||
assert not regex_5000_5999.match("4999") | ||
assert not regex_5000_5999.match("6000") | ||
|
||
print("All tests passed successfully.") | ||
|
||
# Test the regex | ||
test_number = "101" | ||
if regex.match(test_number): | ||
print(f"{test_number} is larger and thus allowed") | ||
else: | ||
print(f"{test_number} is not larger and thus allowed") | ||
# Run the tests | ||
test_generate_regex_from_interval() |