Skip to content

Commit 3c43cda

Browse files
committed
feat(day 55): implement stellar classification logic with full unittest coverage in the SpaceWeek folder
1 parent 431486a commit 3c43cda

1 file changed

Lines changed: 99 additions & 0 deletions

File tree

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
"""
2+
Space Week Day 1: Stellar Classification
3+
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+
import unittest
24+
class Test(unittest.TestCase):
25+
26+
def test1(self):
27+
self.assertEqual(classification(5778),"G")
28+
29+
def test2(self):
30+
self.assertEqual(classification(2400),"M")
31+
32+
def test3(self):
33+
self.assertEqual(classification(9999),"A")
34+
35+
def test4(self):
36+
self.assertEqual(classification(3700),"K")
37+
38+
def test5(self):
39+
self.assertEqual(classification(3699),"M")
40+
41+
def test6(self):
42+
self.assertEqual(classification(210000),"O")
43+
44+
def test7(self):
45+
self.assertEqual(classification(6000),"F")
46+
47+
def test8(self):
48+
self.assertEqual(classification(11432),"B")
49+
50+
51+
52+
53+
54+
55+
56+
57+
58+
def classification(temp):
59+
60+
if temp <= 3699:
61+
temp = 'M'
62+
elif 3700 <= temp <= 5199:
63+
temp = 'K'
64+
elif 5200 <= temp <= 5999:
65+
temp = 'G'
66+
elif 6000 <= temp <= 7499:
67+
temp = 'F'
68+
elif 7500 <= temp <= 9999:
69+
temp ='A'
70+
elif 10000 <= temp <= 29999:
71+
temp = 'B'
72+
else:
73+
temp = 'O'
74+
75+
return temp
76+
77+
# Alternate method
78+
def classify_star(temp):
79+
80+
if temp >= 30000:
81+
return 'O'
82+
elif temp >= 10000:
83+
return 'B'
84+
elif temp >= 7500:
85+
return 'A'
86+
elif temp >= 6000:
87+
return 'F'
88+
elif temp >= 5200:
89+
return 'G'
90+
elif temp >= 3700:
91+
return 'K'
92+
elif temp >= 0:
93+
return 'M'
94+
else:
95+
raise ValueError("Temperature must be a non-negative numder.")
96+
97+
if __name__ == "__main__":
98+
print(classification(5778))
99+
unittest.main()

0 commit comments

Comments
 (0)