Guest User

Untitled

a guest
Dec 15th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. for the input of 7,1
  2. the range should be (1,1) and (7,7)
  3. input 1,7,2 ---> output (1,2) (7,7)
  4. input 1,7,2,3 ---> output (1,3) (7,7)
  5. input 1,7,2,3,4 ---> output (1,4)(7,7)
  6. input 1,7,2,3,4,9 ---> output (1,4)(7,7)(9,9)
  7. input 1,7,2,3,4,9,8 ---> output (1,4)(7,9)
  8. input 1,7,2,3,4,9,8,5 ---> output (1,5)(7,9)
  9. input 1,7,2,3,4,9,8,5,6---> output (1,9)
  10.  
  11. def inRange(i):
  12. for first,second in ranges.copy().items():
  13. if i in range(first,second+1):
  14. return True
  15. elif i > first and second+1 == i:
  16. ranges[first] = i
  17. return True
  18. elif i < first and i+1 == first:
  19. ranges[i] = second
  20. del ranges[first]
  21. return True
  22. return False
  23.  
  24. input = [7,1,2,6,4,8]
  25. ranges = {}
  26. for i in input:
  27. if not ranges or not inRange(i):
  28. ranges[i] = i
  29. print(ranges)
  30.  
  31. {1: 2, 6: 8, 4: 4}
  32.  
  33. {1: 3, 6: 8, 4: 4}
Add Comment
Please, Sign In to add comment