Skip to content

Instantly share code, notes, and snippets.

@AntiKnot
Created April 2, 2020 05:37
Show Gist options
  • Select an option

  • Save AntiKnot/31fa00ac82ff9ad88cc5853dfd09e7b8 to your computer and use it in GitHub Desktop.

Select an option

Save AntiKnot/31fa00ac82ff9ad88cc5853dfd09e7b8 to your computer and use it in GitHub Desktop.
zhihu上回答一个问题
test_data = "1,2,3-7,9,13,15-19"
def foo(some_array_string: str) -> list:
res = []
items = some_array_string.split(',')
for item in items:
try:
number = int(item)
res.append(number)
except ValueError:
start, end = item.split('-')
res += list(range(int(start), int(end)+1)) # here
return res
if __name__ == '__main__':
res = foo(test_data)
print(res)
@AntiKnot
Copy link
Copy Markdown
Author

AntiKnot commented Apr 2, 2020

[here] 的地方因为没有+1导致右边界错误

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment