Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.02 KB | None | 0 0
  1. def create_map(heights):
  2. max_height = max(heights)
  3. return '\n'.join(''.join('##' if i >= h else ' ' for i in heights)
  4. for h in range(max_height, 0, -1))
  5.  
  6.  
  7. def method_a(heights):
  8. water = 0
  9. left = 0
  10. right = len(heights) - 1
  11. left_max = heights[0]
  12. right_max = heights[-1]
  13.  
  14. #print '[%i<->%i][%i<->%i] -> %i' % (left, right, left_max, right_max, water)
  15. while left < right:
  16.  
  17. if heights[left] > left_max:
  18. left_max = heights[left]
  19.  
  20. if heights[right] > right_max:
  21. right_max = heights[right]
  22.  
  23. if left_max >= right_max:
  24. water += right_max - heights[right]
  25. right -= 1
  26. else:
  27. water += left_max - heights[left]
  28. left += 1
  29.  
  30. #print '[%i<->%i][%i<->%i] -> %i' % (left, right, left_max, right_max, water)
  31.  
  32. return water
  33.  
  34.  
  35. def method_b(heights):
  36. stack = []
  37. local_max = 0
  38. water = 0
  39.  
  40. for i in heights:
  41. if i >= local_max:
  42. water_now = sum([local_max - h for h in stack])
  43. #print '>>>', water_now, local_max, stack
  44. local_max = i
  45. water += water_now
  46. stack = []
  47. else:
  48. stack.append(i)
  49. #print local_max, stack
  50.  
  51. if stack:
  52. water += method_b(stack[::-1] + [local_max])
  53.  
  54. return water
  55.  
  56. def method_c(heights):
  57. if len(heights) < 3:
  58. return 0
  59.  
  60. left_max = heights[0]
  61. right_max = heights[-1]
  62.  
  63. left = 1
  64. right = len(heights) - 2
  65.  
  66. total = 0
  67.  
  68. while left <= right:
  69. while left <= right and heights[left] >= left_max:
  70. left_max = heights[left]
  71. left += 1
  72.  
  73. while left <= right and heights[right] >= right_max:
  74. right_max = heights[right]
  75. right -= 1
  76.  
  77. if left <= right:
  78. while left <= right and left_max >= right_max and heights[right] <= right_max:
  79. total += right_max - heights[right]
  80. right -= 1
  81.  
  82. while left <= right and right_max >= left_max and heights[left] <= left_max:
  83. total += left_max - heights[left]
  84. left += 1
  85.  
  86. return total
  87.  
  88.  
  89. def measure_water(heights, debug=False):
  90. """
  91. >>> measure_water([2, 5, 1, 2, 3, 4, 7, 7, 6])
  92. 10
  93. >>> measure_water([2, 5, 1, 3, 1, 2, 1, 7, 7, 6])
  94. 17
  95. >>> measure_water([5, 1, 3, 6, 1, 6, 1, 3, 1, 4])
  96. 18
  97. >>> measure_water([1, 2, 3, 4, 5, 4, 3, 2, 1])
  98. 0
  99. >>> measure_water([1, 2, 3, 4, 5])
  100. 0
  101. >>> measure_water([5, 1, 4, 2, 3])
  102. 4
  103. >>> measure_water([3, 4, 7, 3, 4, 7, 6, 7, 2, 4])
  104. 10
  105. >>> measure_water([6, 1, 5, 2, 1, 4])
  106. 9
  107. >>> measure_water([7, 1, 1, 1, 7, 1, 1, 1, 1, 7])
  108. 42
  109. >>> measure_water([1,3,1,2,5,1,2,1])
  110. 4
  111. """
  112.  
  113. if debug:
  114. print create_map(heights)
  115.  
  116. return method_c(heights)
  117.  
  118.  
  119. if __name__ == '__main__':
  120. import doctest
  121. doctest.testmod()
  122.  
  123. from time import clock as time
  124.  
  125. def test(method):
  126. assert method([2, 5, 1, 2, 3, 4, 7, 7, 6]) == 10
  127. assert method([2, 5, 1, 3, 1, 2, 1, 7, 7, 6]) == 17
  128. assert method([5, 1, 3, 6, 1, 6, 1, 3, 1, 4]) == 18
  129. assert method([1, 2, 3, 4, 5, 4, 3, 2, 1]) == 0
  130. assert method([1, 2, 3, 4, 5]) == 0
  131. assert method([5, 1, 4, 2, 3]) == 4
  132. assert method([3, 4, 7, 3, 4, 7, 6, 7, 2, 4]) == 10
  133. assert method([6, 1, 5, 2, 1, 4]) == 9
  134. assert method([7, 1, 1, 1, 7, 1, 1, 1, 1, 7]) == 42
  135. assert measure_water([1,3,1,2,5,1,2,1]) == 4
  136.  
  137. count = 10
  138. num = 10000
  139. methods = {
  140. method_a: 0,
  141. method_b: 0,
  142. method_c: 0,
  143. }
  144.  
  145. for _ in xrange(count):
  146. for method in methods:
  147. begin = time()
  148. for _ in xrange(num):
  149. test(method)
  150. elapsed = time() - begin
  151. methods[method] += elapsed
  152. print method, elapsed
  153.  
  154. print 'Totals:'
  155. for method, elapsed in methods.iteritems():
  156. print method, elapsed / count
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement