Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- class RangeParser:
- def __init__(self, ranges_):
- self.ranges_str = ranges_
- self.result = []
- self.parse_ranges()
- def parse_ranges(self):
- pattern = re.compile(r'(\d+)-(\d+)')
- ranges_list = self.ranges_str.split(',')
- for record in ranges_list:
- match = pattern.match(record)
- if match:
- start, end = int(match.group(1)), int(match.group(2))
- self.add_range(start, end)
- def add_range(self, start, end):
- for num in range(start, end + 1):
- self.result.append(num)
- def get_result(self):
- return self.result
- ranges_str = '1-2,8-15,23-25'
- parser = RangeParser(ranges_str)
- print(parser.get_result())
- # [1, 2, 8, 9, 10, 11, 12, 13, 14, 15, 23, 24, 25]
Advertisement
Comments
-
- class ParseRange:
- def __init__(self, ranges_string):
- self.ranges_string = ranges_string
- self.numbers = []
- self.ranges = self.ranges_string.split(',')
- def parse_ranges(self):
- for r in self.ranges:
- start, end = map(int, r.split('-'))
- self.numbers.extend(range(start, end + 1))
- return self.numbers
- parser = ParseRange('1-2,8-15,23-25')
- print(parser.parse_ranges())
- parser = ParseRange('0-0, 4-8, 20-20, 43-45')
- print(parser.parse_ranges())
Add Comment
Please, Sign In to add comment
Advertisement