I think it would be a good live coding question: the problem isn't hard to understand, but there are a few different ways you could approach it, and different traps to fall into. I wouldn't judge people on whether they came up with a monstrosity like this code!
Same here. While I like brief code, being able to quickly grok what it's doing several years later is more important to me. I'd rather be clear in an extra line or two than save a few keystrokes.
Other than that, it's not too compact for my taste, and it has a few nice tricks. I learned about partition from one of your earlier post and already found a use case for it instead of split. Thanks for that btw
I don't think it's too compact. Comprehensions are really fast, so they're good to use.
I agree with what @browniebroke.com said about how you formatted the comprehension to make it more readable. And if this were checked into prod code, I'd probably have explanatory comments in the code/commit.
Comments
I love how your formatting hints at the actual order of execution, that's great. Would be nice if it was the default formatting from black/ruff.
if you check your implementation against it's unit tests, please report back here if they all passed!
although I'm not so fond of multi-line list-comprehensions as imho it's not as clear as a normal loop.
I agree with what @browniebroke.com said about how you formatted the comprehension to make it more readable. And if this were checked into prod code, I'd probably have explanatory comments in the code/commit.
Do you have any other renditions of this function with different assignments?
def expand(s):
return chain.from_iterable(
range(int(a), int(b or a) + 1)
for p in s.split(",")
for a, _, b in [p.partition("-")]
)