Skip to content

Commit 222a27a

Browse files
committed
feat(day 60): determine moon phase using 28-day lunar cycle and reference new moon date
1 parent d09e718 commit 222a27a

1 file changed

Lines changed: 65 additions & 0 deletions

File tree

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
"""
2+
Space Week Day 6: Moon Phase
3+
For day six of Space Week, you will be given a date in the format "YYYY-MM-DD" and need to determine the phase of the moon for that day using the following rules:
4+
5+
Use a simplified lunar cycle of 28 days, divided into four equal phases:
6+
7+
"New": days 1 - 7
8+
"Waxing": days 8 - 14
9+
"Full": days 15 - 21
10+
"Waning": days 22 - 28
11+
After day 28, the cycle repeats with day 1, a new moon.
12+
13+
Use "2000-01-06" as a reference new moon (day 1 of the cycle) to determine the phase of the given day.
14+
You will not be given any dates before the reference date.
15+
Return the correct phase as a string.
16+
"""
17+
18+
import unittest
19+
from datetime import datetime
20+
21+
class SpaceWeek6Test(unittest.TestCase):
22+
23+
def test1(self):
24+
self.assertEqual(moon_phase("2000-01-12"),"New")
25+
26+
def test2(self):
27+
self.assertEqual(moon_phase("2000-01-13"),"Waxing")
28+
29+
def test3(self):
30+
self.assertEqual(moon_phase("2014-10-15"),"Full")
31+
32+
def test4(self):
33+
self.assertEqual(moon_phase("2012-10-21"),"Waning")
34+
35+
def test5(self):
36+
self.assertEqual(moon_phase("2022-12-14"),"New")
37+
38+
39+
40+
41+
42+
def moon_phase(date_string):
43+
44+
reference_date = datetime(2000,1,6)
45+
given_date = datetime.strptime(date_string,"%Y-%m-%d")
46+
days_passed = (given_date - reference_date).days
47+
cycle_day = (days_passed % 28) + 1 # Day 1 to 28
48+
49+
50+
if 1<= cycle_day <= 7:
51+
return "New"
52+
elif 8 <= cycle_day <= 14:
53+
return "Waxing"
54+
elif 15 <= cycle_day <= 21:
55+
return "Full"
56+
else:
57+
return "Waning"
58+
59+
60+
61+
62+
if __name__ == "__main__":
63+
print(moon_phase("2000-01-06"))
64+
print(moon_phase("2000-01-13"))
65+
unittest.main()

0 commit comments

Comments
 (0)