Skip to content

Commit 2346774

Browse files
committed
feat(day 56): implement exoplanet detection using luminosity thresholds with unittest coverage
1 parent 03a2b14 commit 2346774

1 file changed

Lines changed: 54 additions & 0 deletions

File tree

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
"""
2+
Space Week Day 2: Exoplanet Search
3+
For the second day of Space Week, you are given a string where each character represents the luminosity reading of a star. Determine if the readings have detected an exoplanet using the transit method. The transit method is when a planet passes in front of a star, reducing its observed luminosity.
4+
5+
Luminosity readings only comprise of characters 0-9 and A-Z where each reading corresponds to the following numerical values:
6+
Characters 0-9 correspond to luminosity levels 0-9.
7+
Characters A-Z correspond to luminosity levels 10-35.
8+
A star is considered to have an exoplanet if any single reading is less than or equal to 80% of the average of all readings. For example, if the average luminosity of a star is 10, it would be considered to have a exoplanet if any single reading is 8 or less.
9+
10+
"""
11+
import unittest
12+
13+
class SpaceWeekTest(unittest.TestCase):
14+
15+
def test1(self):
16+
self.assertEqual(has_exoplanet("665544554"),False)
17+
18+
def test2(self):
19+
self.assertEqual(has_exoplanet("FGFFCFFGG"),True)
20+
21+
def test3(self):
22+
self.assertEqual(has_exoplanet("MONOPLONOMONPLNOMPNOMP"),False)
23+
24+
def test4(self):
25+
self.assertEqual(has_exoplanet("FREECODECAMP"),True)
26+
27+
def test5(self):
28+
self.assertEqual(has_exoplanet("9AB98AB9BC98A"),False)
29+
30+
def test6(self):
31+
self.assertEqual(has_exoplanet("ZXXWYZXYWYXZEGZXWYZXYGEE"),True)
32+
33+
34+
35+
36+
def has_exoplanet(readings):
37+
38+
def char_to_luminus(c):
39+
if c.isdigit():
40+
return int(c)
41+
else:
42+
return ord(c.upper()) - ord('A') + 10
43+
44+
values = [char_to_luminus(c) for c in readings]
45+
average = sum(values) / len(values)
46+
threshold = 0.8 * average
47+
48+
return any( v <= threshold for v in values)
49+
50+
51+
if __name__ == "__main__":
52+
53+
print(has_exoplanet("665544554"))
54+
unittest.main()

0 commit comments

Comments
 (0)