-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsomethin
93 lines (74 loc) · 3.3 KB
/
somethin
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
"""
User enters the Table %, Depth %, length, and width numbers
to get diamond quality.
Quality: Excellent, Very Good, Good, Fair, Poor
Table%: 53-63, 52 or 64-65, 51 or 66-68, 50 or 69-70, <50 or >70
Depth%: 58-62, 56-57.9 or 62.1-66, 53-55.9 or 66.1-71, 50-52.9 or 71.1-74, <50 or >74
Len/Wid: 1.35-1.50, 1.30-1.34 or 1.51-1.55, 1.25-1.29 or 1.56-1.60, 1.20-1.24 or 1.61-1.65, <1.20 or >1.65
Reference: https://www.diamonds.pro/education/oval-cut/
Creator: Kenny Hoang
MIT License
"""
import math
import unittest
from datetime import datetime
def binary_search(metric_list, value):
length = len(metric_list)
low, mid, high = 0, length//2, length - 1
while high - low > 1:
quality_value = metric_list[mid][1]
if value > quality_value:
low = mid
# edge case where a very high value (poor quality)
# doesn't print because mid will never = high value if
# mid = ((high - low) // 2) + low
mid = high - ((high - low) // 2)
elif value < quality_value:
high = mid
mid = ((high - low) // 2) + low
else:
break
return metric_list[mid][0]
def get_diamond_quality(answer=None):
start_time = datetime.now()
poor, fair, good, very_good, excellent = "Poor", "Fair", "Good", "Very Good", "Excellent"
reference = {
"table": [
(poor, 50.0), (fair, 51.0), (good, 52.0), (very_good, 53.0), (excellent, 63.0),
(very_good, 65.0), (good, 68.0), (fair, 70.0), (poor, float("inf"))],
"depth": [
(poor, 50.0), (fair, 52.9), (good, 55.9), (very_good, 57.9), (excellent, 62.0),
(very_good, 66.0), (good, 71.0), (fair, 74.0), (poor, float("inf"))],
"length_width": [
(poor, 1.2), (fair, 1.24), (good, 1.29), (very_good, 1.34), (excellent, 1.5),
(very_good, 1.55), (good, 1.6), (fair, 1.65), (poor, float("inf"))]
}
questions = ("What is the Table Percentage? ", "What is the Depth Percentage? ",
"What is the length of diamond? ", "What is the width of diamond? ")
if not answer:
for q in questions:
while True:
try:
value = float(input(q))
assert value or math.sqrt(value) < 10.0
answer.append(value)
break
except (AssertionError, ValueError):
print("Please enter a number between 0 and 100")
answer[2] = answer[2]/answer[3]
print("\nScale: Excellent, Very Good, Good, Fair, Poor")
for ans, (diamond_prop, metric_list) in zip(answer[:3], reference.items()):
quality_level = binary_search(metric_list, ans)
print("{} is {}, {}".format(diamond_prop, quality_level, ans))
print("Execution time:", datetime.now()-start_time)
if __name__ == '__main__':
get_diamond_quality()
class TestStringMethods(unittest.TestCase):
def test_upper(self):
self.assertEqual('foo'.upper(), 'FOO')
def test_main():
start = datetime.now()
values = ([0, 0, 0, 1], [50, 50, 1.2, 1], [53, 57.9, 1.34, 1], [63, 62, 1.5, 1], [100, 100, 1001, 1])
for v in values:
main(v)
print("time:", datetime.now()-start)