Web M&M
https://mam.matfyz.cz
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
35 lines
1.0 KiB
35 lines
1.0 KiB
#!/bin/python3
|
|
import unittest
|
|
|
|
class RangeSet:
|
|
def __init__(self, rangespec):
|
|
self.ranges = self.parse_spec(rangespec)
|
|
def __contains__(self, num):
|
|
return any(num in r for r in self.ranges)
|
|
def parse_spec(self, spec: str) -> list[range]:
|
|
result = []
|
|
parts = spec.split(',')
|
|
parts = map(str.strip, parts)
|
|
for part in parts:
|
|
if '-' in part:
|
|
start, end = part.split('-')
|
|
result.append(range(int(start), int(end)+1))
|
|
else:
|
|
n = int(part)
|
|
result.append(range(n, n+1))
|
|
return result
|
|
# TODO: iteration. Will require deduplication.
|
|
# TODO: simplification of inner ranges
|
|
# TODO: Set operations: union, intersection, difference?
|
|
# TODO: Comparison to classic range objects (only to check that it represents the same numbers)
|
|
|
|
class TestRangeSetBasic(unittest.TestCase):
|
|
def test_number_sequence(self):
|
|
rs = RangeSet('1,3,4,5')
|
|
for x in [1,3,4,5]:
|
|
self.assertIn(x, rs)
|
|
self.assertNotIn(2, rs)
|
|
|
|
if __name__ == '__main__':
|
|
print('This is a library. Runing the testsuite instead.')
|
|
unittest.main()
|
|
|