This is a python question for recursion The evil Team Rocket has invented an inc
ID: 3848869 • Letter: T
Question
This is a python question for recursion
The evil Team Rocket has invented an incredible new space-ship! The ship works as follows:
• If the distance to the destination is greater than 1 meter, the ship will "fold space" and "jump" to a position
exactly half way to the destination. It takes exactly one minute to perform this process.
• Any distance of 1 meter or less will take exactly one minute, using normal impulse rockets.
For example, if the ship has to travel 10 meters, it will jump 5 meters after the first minute, 2.5 meters the second
minute, 1.25 meters the 3rd minute, and 0.625 meters the fourth minute. Finally, the remaining 0.625 meters takes
one more minute. Thus the total time to travel 10 meters is 5 minutes. (That seems very slow, and it is for small
distances; the true value of this method only reveals itself for large distances.)
Write a recursive function called spaceTime() that calculates the time needed for Team Rocket’s new ship to travel
a given distance (in meters). In the same file, write code that asks the user for a distance in meters (console input),
and then calls your spaceTime() function to calculate the time required to travel the given distance.
Test your function out on all of the following examples:
• Distance from Team Rocket’s base to the nearest Poke-Stop : 37 meters.
• Distance for a round-trip around the entire planet earth: 40075000 meters.
• Average distance between our earth and our sun: 1.49e11 meters (about 150 million km).
• Approximate distance between the sun and the closest star: 4.0e16 meters (about 4 light-years).
• Size of the observable universe: 8.8e26 meters (about 93 Giga-light-years)
Explanation / Answer
SPACETIME.py
def spaceTime(distance, time=0):
if distance <= 1:
time += 1
return int(time)
else:
time += 1
distance = distance/2 # distance /= 2
return spaceTime(distance, time)
d = int(input('Enter the distance in meters '))
a = spaceTime(d)
print('Time taken by team rocket to reach the destination is ',a,' minutes')
# Test conditions
print(' Distance for a round-trip around the entire planet earth: 40075000 meters.')
d = 40075000
a = spaceTime(d)
print('Time taken for a round trip of the planet earth ',a,' minutes')
# Test 2
print('• Average distance between our earth and our sun: 1.49e11 meters (about 150 million km);')
d = 150000000
a = spaceTime(d)
print('Time taken to reach the sun from earth ',a,' minutes')
# Test 3
print('Approximate distance between the sun and the closest star: 4.0e16 meters (about 4 light-years).')
d = float(4.0e16)
a = spaceTime(d)
print('Time taken to reach the closest star is ',a,' minutes')
# Test 4
print('Size of the observable universe: 8.8e26 meters (about 93 Giga-light-years')
d = float(8.8e26)
a = spaceTime(d)
print('To travel the universe it would take ',a,' minutes')
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.