+ 3
Trip planner problem. Why both give the correct result?
Hi! Anybody, please, explain the difference. I solve the travel planner problem, where you need to find the travel time when you know the speed and distance. why do both expressions give the correct result? minutes = distance / speed *60 minutes = distance/(40/60)
27 odpowiedzi
+ 8
This post inspired me to create this code which is relevant to this discussion. 😂🤣
https://code.sololearn.com/ca4A19a13a69/?ref=app
+ 5
oh no!
my bad...
a/(b/c) == a/b*c
+ 2
Yaroslav Vernigora , what do you mean? Speed is known by the task description. What are your doubts?
+ 2
speed === 40
+ 2
because:
a/(b/c) == a*(c/b)
+ 2
not VERY good...
but I have my basics ;)
+ 2
Yaroslav Vernigora distance/speed *60=distance*60/speed
And distance /(40/60)=distance/40/60=distance/40*60
+ 1
does the trip planner code coach have only once test?
if then, that mean that the value used for speed*60 should be equals to 40/60 ^^
+ 1
that works also if speed is a constant and there are many tests ;P
+ 1
yes, you are right, we have one constant - speed. it is equal to 40
+ 1
🤭
+ 1
visph no, wait... let's look at the first expression you wrote. I did not change the constant speed of 40 from 60. in one case, we multiply, in the second, we divide... and both expressions are correct! how can this be?
+ 1
Simba you wrote
a/b/c = a*c/b
but I have written:
a / b * c == a / (b / c)
+ 1
your first expression is a/b*c wich is evaluated as c*a/b coz left to right precedence (same precedence for / and *)
your second one is a/(b/c) wich is equal to a*(c/b) and equal to a*c/b or c*a/b or a/b*c
+ 1
thank you very much! I'll need more time to figure this out. and repeat the priority of rolling out the operators
+ 1
Thank y'all!
+ 1
this is just maths ;)
n x * n
x * ------ == ------------
d d
and left to right precedence when same operators precedences
+ 1
there is also the result in the correct answer in parentheses. if you remove them, the answer will be incorrect
+ 1
I see, you very good know math! 👏👍
+ 1
distance = 5
speed = 40
Example 1:
t = 5/40 × 60
1) 5/40 = 1/8
2) 1/8 × 60 = 60/8 = 15/2 = 7.5
Example 2:
t = 5 ÷ 40/60
1) 40/60 = 4/6 = 2/3
2) 5 ÷ 2/3 = 5 × 3/2 = 15/2 = 7.5
Check:
distance = 7.5 × 40 ÷ 60
1) 7.5 × 40 = 300
2) 300 ÷ 60 = 5
Conclusion:
example1 == example2