-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution
50 lines (46 loc) · 1.39 KB
/
solution
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from random import randint
"""Create four random points:
A in upper left quadrant
B in lower left quadrant
C in lower right quadrant
D in upper right quandrant
Calculate slopes
Return False if non-convexness detected"""
def run_once():
ax = float(randint(1,99999)) #Random x-value for A
ay = float(randint(1,99999) + 100000) #Random y-value, pushed to upper half
bx = float(randint(1,99999))
by = float(randint(1,99999))
cx = float(randint(1,99999) + 100000)
cy = float(randint(1,99999))
dx = float(randint(1,99999) + 100000)
dy = float(randint(1,99999) + 100000)
if ax == bx: #Making sure we don't divide by zero
abslope = 999999.0
else:
abslope = (ay - by) / (ax - bx)
adslope = (dy - ay) / (dx - ax)
if cx == dx:
cdslope = 999999.0
else:
cdslope = (dy - cy) / (dx - cx)
bcslope = (by - cy) / (bx - cx)
if abslope > 0 and adslope > abslope: #Check corner in A quadrant
return False
elif cdslope < 0 and adslope < cdslope:
return False
elif cdslope > 0 and bcslope > cdslope:
return False
elif abslope < 0 and bcslope < abslope:
return False
else:
return True
def run_a_lot(times):
count = 0
for i in range(times):
if run_once():
count += 1.0
return count / times
print run_a_lot(1000000)
print run_a_lot(1000000)
print run_a_lot(1000000)