In this Kata you are to implement a function that parses a string which is composed from tokens of the form 'n1-n2,n3,n4-n5:n6' where 'nX' is a positive integer. Each token represent a different range:

'n1-n2' represents the range n1 to n2 (inclusive in both ends).
'n3' represents the single integer n3.
'n4-n5:n6' represents the range n4 to n5 (inclusive in both ends) but only includes every other n6 integer.

Notes:
1. The input string doesn't not have to include all the token types.
2. All integers are assumed to be positive.
3. Tokens may be separated by `','`, `', '` or ` , `.

Some examples:

'1-10,14, 20-25:2' -> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 14, 20, 22, 24]

'5-10' -> [5, 6, 7, 8, 9, 10]

'2' -> [2]

The output should be a list of integers.

Source: codewars | Difficulty: EASY
Tags: String algorithms

Fundamentals

Starter code: def range_parser(string):
	

Solutions:
def range_parser(string):
	res = []
	for range_ in string.split(','):
		(first_last, _, step) = range_.partition(':')
		(first, _, last) = first_last.partition('-')
		res += range(int(first), int(last or first) + 1, int(step or 1))
	return res


import re

def range_parser(s):
	return [x for r in re.split(', *', s) for (start, end, step) in re.findall('(\\d+)-?(\\d*):?(\\d*)', r) for x in range(int(start), int(end or start) + 1, int(step or '1'))]


def range_parser(inp):
	res = []
	for r in inp.split(','):
		r = list(map(int, r.replace(':', '-').split('-')))
		if len(r) > 1:
			r[1] += 1
			res += range(*r)
		else:
			res.append(r[0])
	return res


import re

def range_parser(string):
	r = []
	for i in [i.strip() for i in string.split(',')]:
		if bool(re.match('^\\d+$', i)):
			r.append(int(i))
		elif bool(re.match('^\\d+-\\d+$', i)):
			x = i.split('-')
			r.extend([j for j in range(int(x[0]), int(x[1]) + 1)])
		elif bool(re.match('^\\d+-\\d+:\\d+$', i)):
			x = i.split('-')
			y = x[1].split(':')
			r.extend([j for j in range(int(x[0]), int(y[0]) + 1, int(y[1]))])
	print(r)
	return r


import re

def range_parser(string):
	result = []
	for match in re.finditer('(\\d+)(?:-(\\d+)(?::(\\d+))?)?', string):
		start = int(match.group(1))
		end = int(match.group(2) or start) + 1
		step = int(match.group(3) or 1)
		result.extend(list(range(start, end, step)))
	return result


def range_parser(stg):
	result = []
	for rg in stg.split(','):
		(rg, _, s) = rg.partition(':')
		(i, _, j) = rg.partition('-')
		(i, j, s) = (int(i), int(j or i) + 1, int(s or 1))
		result.extend(range(i, j, s))
	return result


def range_parser(string):

	def f():
		for x in string.split(','):
			if '-' in x:
				if ':' in x:
					(x, step) = x.split(':')
					step = int(step)
				else:
					step = 1
				(a, b) = map(int, x.split('-'))
				yield from range(a, b + 1, step)
			else:
				yield int(x)
	return list(f())


def clean(string):
	return string.replace("'", '')

def range_parser(string):
	l = []
	for elem in clean(string).split(','):
		if '-' in elem:
			l.extend(parse_range(elem))
		else:
			l.append(int(elem))
	return l

def parse_range(interval):
	step = 1
	if ':' in interval:
		(interval, step) = interval.split(':')
	(start, stop) = [int(e) for e in interval.split('-')]
	return list(range(start, stop + 1, int(step)))


import re

def range_parser(string):
	return sum((list(range(int(m.group(1)), int(m.group(2) or int(m.group(1))) + 1, int(m.group(3) or 1))) for m in re.finditer('(\\d+)(?:-(\\d+)(?::(\\d+))?)?', string)), [])


import re

def range_parser(string):
	return sum((list(range(int(start), int(end or start) + 1, int(step or 1))) for (start, end, step) in re.findall('(\\d+)(?:-(\\d+)(?::(\\d+))?)?', string)), [])