Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
namedtuple solution in Clear category for Shooting Range by Sim0000
from collections import namedtuple
def shot(wall1, wall2, shot_point, later_point):
Point = namedtuple('Point', 'x y')
w1 = Point(*wall1)
w2 = Point(*wall2)
sp = Point(*shot_point)
lp = Point(*later_point)
f = lambda a, b, c, d: (b.y - a.y)*(d.x - c.x) - (b.x - a.x)*(d.y - c.y)
d = f(sp, lp, w1, w2)
n = f(sp, lp, w1, lp)
k = f(w1, w2, w1, sp)
if d == 0 or n/d < 0 or n/d > 1 or k/d < 0: return -1
return round(min(200*n/d, 200 - 200*n/d))
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert shot((2, 2), (5, 7), (11, 2), (8, 3)) == 100, "1st case"
assert shot((2, 2), (5, 7), (11, 2), (7, 2)) == 0, "2nd case"
assert shot((2, 2), (5, 7), (11, 2), (8, 4)) == 29, "3th case"
assert shot((2, 2), (5, 7), (11, 2), (9, 5)) == -1, "4th case"
assert shot((2, 2), (5, 7), (11, 2), (10.5, 3)) == -1, "4th case again"
Aug. 27, 2014
Comments: