You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
October 4th marks the beginning of World Space Week. The next seven days will bring you astronomy-themed coding challenges.
4
+
5
+
For today's challenge, you are given the surface temperature of a star in Kelvin (K) and need to determine its stellar classification based on the following ranges:
6
+
7
+
"O": 30,000 K or higher
8
+
9
+
"B": 10,000 K - 29,999 K
10
+
11
+
"A": 7,500 K - 9,999 K
12
+
13
+
"F": 6,000 K - 7,499 K
14
+
15
+
"G": 5,200 K - 5,999 K
16
+
17
+
"K": 3,700 K - 5,199 K
18
+
19
+
"M": 0 K - 3,699 K
20
+
21
+
Return the classification of the given star.
22
+
"""
23
+
importunittest
24
+
classTest(unittest.TestCase):
25
+
26
+
deftest1(self):
27
+
self.assertEqual(classification(5778),"G")
28
+
29
+
deftest2(self):
30
+
self.assertEqual(classification(2400),"M")
31
+
32
+
deftest3(self):
33
+
self.assertEqual(classification(9999),"A")
34
+
35
+
deftest4(self):
36
+
self.assertEqual(classification(3700),"K")
37
+
38
+
deftest5(self):
39
+
self.assertEqual(classification(3699),"M")
40
+
41
+
deftest6(self):
42
+
self.assertEqual(classification(210000),"O")
43
+
44
+
deftest7(self):
45
+
self.assertEqual(classification(6000),"F")
46
+
47
+
deftest8(self):
48
+
self.assertEqual(classification(11432),"B")
49
+
50
+
51
+
52
+
53
+
54
+
55
+
56
+
57
+
58
+
defclassification(temp):
59
+
60
+
iftemp<=3699:
61
+
temp='M'
62
+
elif3700<=temp<=5199:
63
+
temp='K'
64
+
elif5200<=temp<=5999:
65
+
temp='G'
66
+
elif6000<=temp<=7499:
67
+
temp='F'
68
+
elif7500<=temp<=9999:
69
+
temp='A'
70
+
elif10000<=temp<=29999:
71
+
temp='B'
72
+
else:
73
+
temp='O'
74
+
75
+
returntemp
76
+
77
+
# Alternate method
78
+
defclassify_star(temp):
79
+
80
+
iftemp>=30000:
81
+
return'O'
82
+
eliftemp>=10000:
83
+
return'B'
84
+
eliftemp>=7500:
85
+
return'A'
86
+
eliftemp>=6000:
87
+
return'F'
88
+
eliftemp>=5200:
89
+
return'G'
90
+
eliftemp>=3700:
91
+
return'K'
92
+
eliftemp>=0:
93
+
return'M'
94
+
else:
95
+
raiseValueError("Temperature must be a non-negative numder.")
0 commit comments