Skip to content

Commit 62e3237

Browse files
committed
feat(day 57): calculate communication time across satellite route with preccise delay logic
1 parent a62120a commit 62e3237

1 file changed

Lines changed: 109 additions & 0 deletions

File tree

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
"""
2+
Space Week Day 3: Phone Home
3+
For day three of Space Week, you are given an array of numbers representing distances (in kilometers) between yourself, satellites, and your home planet in a communication route. Determine how long it will take a message sent through the route to reach its destination planet using the following constraints:
4+
5+
The first value in the array is the distance from your location to the first satellite.
6+
Each subsequent value, except for the last, is the distance to the next satellite.
7+
The last value in the array is the distance from the previous satellite to your home planet.
8+
The message travels at 300,000 km/s.
9+
Each satellite the message passes through adds a 0.5 second transmission delay.
10+
Return a number rounded to 4 decimal places, with trailing zeros removed.
11+
"""
12+
13+
import unittest
14+
15+
class SpaceWeekTest(unittest.TestCase):
16+
17+
def test1(self):
18+
self.assertEqual(send_message([300000,300000]),2.5)
19+
20+
def test2(self):
21+
self.assertEqual(send_message([384400,384400]),3.0627)
22+
23+
def test3(self):
24+
self.assertEqual(send_message([54600000,54600000]),364.5)
25+
26+
def test4(self):
27+
self.assertEqual(send_message([1000000,500000000,1000000]),1674.3333)
28+
29+
def test5(self):
30+
self.assertEqual(send_message([10000, 21339, 50000, 31243, 10000]),2.4086)
31+
32+
def test6(self):
33+
self.assertEqual(send_message([802101, 725994, 112808, 3625770, 481239]),21.1597)
34+
35+
36+
37+
38+
39+
40+
41+
42+
43+
44+
def send_message(route):
45+
46+
47+
total_distance = sum(route)
48+
total_num_of_satellites = route[:-1]
49+
50+
total_time = (total_distance / 300000) + len(total_num_of_satellites) * 0.5
51+
52+
return round(total_time,4)
53+
54+
"""
55+
56+
Why round(total_time, 4) gives 4 decimal places
57+
Yes, round(total_time, 4) returns a float rounded to 4 decimal places. But floats in Python don’t preserve trailing zeros — so round(2.5, 4) gives 2.5, not 2.5000.
58+
To force 4 decimal places, we use:
59+
format(round(total_time, 4), '.4f') # → '2.5000'
60+
61+
62+
Then we strip trailing zeros with:
63+
.rstrip('0') # → '2.5'
64+
65+
66+
67+
❓ Why .rstrip('.') is used
68+
This is a defensive edge-case guard. After stripping trailing zeros, you might end up with a string like:
69+
'2.' # ← not ideal
70+
71+
72+
This happens when the number is something like 2.0000:
73+
- format(..., '.4f') → '2.0000'
74+
- .rstrip('0') → '2.'
75+
- .rstrip('.') → '2'
76+
So .rstrip('.') ensures you don’t return a number with a dangling decimal point.
77+
78+
🧪 Example
79+
total_time = 2.0000
80+
formatted = format(round(total_time, 4), '.4f') # '2.0000'
81+
cleaned = formatted.rstrip('0').rstrip('.') # '2'
82+
83+

84+
85+
🔍 Did the question require it?
86+
No, the prompt didn’t explicitly mention this edge case. But it did say:
87+
“Return a number rounded to 4 decimal places, with trailing zeros removed.”
88+
89+
So .rstrip('.') is a safe polish to prevent awkward outputs like '2.'.
90+
91+
92+
93+
"""
94+
def send_message_edge(route):
95+
speed = 300_000 # km/s
96+
97+
travel_time = sum(route) / speed
98+
satellite_delay = 0.5 * (len(route) - 1)
99+
total_time = travel_time + satellite_delay
100+
101+
# The result is returning the string but we need the number with the decimal places
102+
refined_time = format(round(total_time,4), '.4f').rstrip('0').rstrip('.') if '.' in str(total_time) else str(total_time)
103+
104+
return float(refined_time)
105+
106+
if __name__ == "__main__":
107+
108+
print(send_message([300000,300000]))
109+
unittest.main()

0 commit comments

Comments
 (0)