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
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.
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.
0 commit comments