Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- def convert(inp):
- """
- * Get the input from user.
- * Parse the input to extract numbers
- ** Split by comma
- *** Each item in the list will then be split by '-'
- **** Populate the number between a-b using range(a,b)
- >>> convert("")
- []
- >>> convert("1")
- [1]
- >>> convert("1,2")
- [1, 2]
- >>> convert("1,2-5")
- [1, 2, 3, 4, 5]
- >>> convert("1-3,2-5,8,10,15-20")
- [1, 2, 3, 2, 3, 4, 5, 8, 10, 15, 16, 17, 18, 19, 20]
- """
- if not inp: # empty string must return an empty list
- return []
- pages = []
- comma_separated = []
- comma_separated = inp.split(",") # split the input string based on comma delimitation
- for item in comma_separated:
- if "-" in item:
- a = item.split("-")
- pages.extend(range(int(a[0]),int(a[1])+1))
- else:
- pages.append(int(item))
- return pages
- if __name__ == '__main__' :
- import doctest
- doctest.testmod()
Add Comment
Please, Sign In to add comment