Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- for the input of 7,1
- the range should be (1,1) and (7,7)
- input 1,7,2 ---> output (1,2) (7,7)
- input 1,7,2,3 ---> output (1,3) (7,7)
- input 1,7,2,3,4 ---> output (1,4)(7,7)
- input 1,7,2,3,4,9 ---> output (1,4)(7,7)(9,9)
- input 1,7,2,3,4,9,8 ---> output (1,4)(7,9)
- input 1,7,2,3,4,9,8,5 ---> output (1,5)(7,9)
- input 1,7,2,3,4,9,8,5,6---> output (1,9)
- def inRange(i):
- for first,second in ranges.copy().items():
- if i in range(first,second+1):
- return True
- elif i > first and second+1 == i:
- ranges[first] = i
- return True
- elif i < first and i+1 == first:
- ranges[i] = second
- del ranges[first]
- return True
- return False
- input = [7,1,2,6,4,8]
- ranges = {}
- for i in input:
- if not ranges or not inRange(i):
- ranges[i] = i
- print(ranges)
- {1: 2, 6: 8, 4: 4}
- {1: 3, 6: 8, 4: 4}
Add Comment
Please, Sign In to add comment