DeaD_EyE

sequence homework 2

Apr 9th, 2018
112
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from contextlib import suppress
  2.  
  3.  
  4. def adv_range(seq):
  5.     for element in seq.split(','):
  6.         with suppress(ValueError):
  7.             if '-' in element:
  8.                 start, _, stop = element.partition('-')
  9.                 start, stop = int(start), int(stop)
  10.                 direction = 1 if start <= stop else -1
  11.                 yield from range(start, stop + direction, direction)
  12.             else:
  13.                 yield int(element)
  14.  
  15.  
  16. def safe_adv_range(seq, constructor=set):
  17.     retval = constructor(adv_range(seq))
  18.     return sorted(retval)
RAW Paste Data