I'm trying to advance my abilities and learn generator pipeline. I manage to solve a few problems with it but I feel that i have a basic gap in my understanding.
for example:
I tried to build a function that returns all numbers within a range. The range given as a string of few ranges.
so an example for the string received: "1-2,4-4,8-10"
.
So the first generator should return a list of int couples: [1, 2] [4, 4] [8, 10]
and second generator should use the 1st_gen[0]
as start and the 1st_gen[1]
as a stop in a range function and return all numbers in range: 1 2 4 8 9 10
There is my code, I'll be happy for tips to improve my skills:
def parse_ranges(range_string):
temp_list = (c.replace("-", ",") for c in list(range_string.split(",")))
generator2 = (i for start, stop in temp_list for i in range(int(start), int(stop) + 1))
for i in generator2:
print(i)
print(parse_ranges("1-2,4-4,8-10"))
print(parse_ranges("0-0,4-8,20-21,43-45"))