Advertisement
Guest User

Untitled

a guest
Feb 9th, 2016
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. import math, check
  2.  
  3. def distance (x1,y1, x2,y2):
  4. return math.sqrt( ((x1-x2) ** 2) + ((y1-y2) ** 2))
  5. # min_dist(x1, y1, x2, y2, x3, y3) consumes 6 float values
  6. # (x1 to x3, and y1 to y3) where each pair of xy with the same numbers
  7. # represents a coordinate of the house's condition. The function returns
  8. # the closest house from the two houses
  9. # min_dist: Float Float Float Float Float Float
  10. # -> (anyof "House1" "House2" "House3")
  11. # Examples:
  12.  
  13. def min_dist(x1, y1, x2, y2, x3, y3):
  14. h1_h2 = distance(x1, y1, x2, y2)
  15. h1_h3 = distance(x1, y1, x3, y3)
  16. h2_h3 = distance(x2, y2, x3, y3)
  17. sum_h1 = h1_h2 + h1_h3
  18. sum_h2 = h1_h2 + h2_h3
  19. sum_h3 = h1_h3 + h2_h3
  20. print(sum_h1, sum_h2, sum_h3)
  21. if min(sum_h1, sum_h2, sum_h3) == sum_h1:
  22. return "House 1"
  23. elif min(sum_h1, sum_h2, sum_h3) == sum_h2:
  24. return "House 2"
  25. elif min(sum_h1, sum_h2, sum_h3) == sum_h3:
  26. return "House 3"
  27. elif sum_h1 == sum_h2 and sum_h2 == sum_h3:
  28. return "House 1"
  29.  
  30.  
  31. # Test 8
  32. check.expect("Q1T8", min_dist(0, 0, 0.5, 0.5 * math.sqrt(3), 1, 0), "House 1")
  33.  
  34. # Test 9
  35. check.expect("Q1T9", min_dist(0.5, 0.5 * math.sqrt(3), 0, 0, 1, 0), "House 1")
  36.  
  37. # Test 8
  38. check.expect("Q1T8", min_dist(1, 0, 0.5, 0.5 * math.sqrt(3), 0, 0), "House 1")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement