0
Roadrunner code quality
I used the following code for the Roadrunner code coach. It passed and I was pleasantly surprised! I was wondering how others approached this puzzle. I felt like my solution was convoluted. safety = int(input()) + 50 rrspeed = int(input()) coyspeed = int(input()) rrdistance = 50 coydistance = 0 time = 0 while rrdistance - coydistance > 0: rrdistance = (rrspeed * time) + 50 coydistance = (coyspeed * time) time += 1 if rrdistance >= safety: is_safe = True break elif coydistance >= rrdistance: is_safe = False break if is_safe: print("Meep Meep") else: print("Yum")
3 Answers
+ 4
Ok I found it in the code coach.
I solved it with a different approach.
If you learned some elementary physics, you should be familiar with the formula:
v = s / t
Speed is distance divided by time.
From this equation, we know the speed and we know the distance, for both the coyote and the roadrunner, hence we can calculate how much time it takes for each of them to cover that distance (reach safety).
t = s / v
If the coyote would get there first (his time is less), he is faster so he can eat the bird.
+ 1
I am not sure where in the course is this task, but I would definitely put the calculation in a function. This fuction could take the dynamic parameters as argument (speeds and safety) and return the string that must be printed.
Using the while loop is a good idea. But it seems to me your while condition is checking the same thing as your elif.
Also, you never set is_safe before the while loop. Python is very lax on this, but in a serious language this would get you a compilation error, because there could be a case when is_safe does not exist when you want to print the result. Returning a string from a function, would make this boolean unnecessary too.
0
Can you share your code using a function? I tend to have a hard time implementing them when I'm solving problems and end up with garbled messes list I show above