Guest User

Untitled

a guest
Jun 21st, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. #!/usr/bin/env python
  2. def convert(inp):
  3. """
  4. * Get the input from user.
  5. * Parse the input to extract numbers
  6. ** Split by comma
  7. *** Each item in the list will then be split by '-'
  8. **** Populate the number between a-b using range(a,b)
  9. >>> convert("")
  10. []
  11. >>> convert("1")
  12. [1]
  13. >>> convert("1,2")
  14. [1, 2]
  15. >>> convert("1,2-5")
  16. [1, 2, 3, 4, 5]
  17. >>> convert("1-3,2-5,8,10,15-20")
  18. [1, 2, 3, 2, 3, 4, 5, 8, 10, 15, 16, 17, 18, 19, 20]
  19. """
  20. if not inp: # empty string must return an empty list
  21. return []
  22. pages = []
  23. comma_separated = []
  24. comma_separated = inp.split(",") # split the input string based on comma delimitation
  25. for item in comma_separated:
  26. if "-" in item:
  27. a = item.split("-")
  28. pages.extend(range(int(a[0]),int(a[1])+1))
  29. else:
  30. pages.append(int(item))
  31. return pages
  32. if __name__ == '__main__' :
  33. import doctest
  34. doctest.testmod()
Add Comment
Please, Sign In to add comment