Created
April 2, 2020 05:37
-
-
Save AntiKnot/31fa00ac82ff9ad88cc5853dfd09e7b8 to your computer and use it in GitHub Desktop.
zhihu上回答一个问题
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
[here] 的地方因为没有+1导致右边界错误