 Having trouble getting my brain wrapped around this one (Minimum number of hops needed to reach a flower)

Hello everyone!

I’m new to Python and having trouble just understanding what exactly this question is asking me to do:

"A grasshopper is trying to reach a flower that’s N feet away by hopping to it. However, there are some restrictions on how it can hop.

It can only hop forward, not backward. If it passes the flower, it can’t come back.

At any given point, it can hop either M feet, or just 1 foot.

On a single line of input, you’re given 2 integer values: N and M.

Determine the minimum number of hops it will take the grasshopper to reach the flower exactly."

I am not even at the point of writing the code for it, just trying to understand what equation this is asking me to solve. I’m just braindead staring at it. Can anyone help?

1 Like

Hello, @net9085133669.

Say that the flower is 10 feet away, so N = 10.
The grasshopper can only hop one of two distances. Either 1 foot or whatever the value of M is. For this example, we’ll say that M = 3.

Now, we need to know the fewest number of hops the grasshopper has to make to get to the flower.
Should the first hop be 1 foot or 3 feet? Since we are trying to determine the fewest number of hops, we’ll need to take as many 3 foot hops as we can without passing the flower.

So, if the grasshopper takes three 3-foot hops, that will be a total of 9 feet leaving only 1 foot to go. The last foot can be covered with a 1-foot hop. The total number of hops taken is 4.

You’ll have to write a function that can determine how many hops of M feet can be taken without passing the flower located N feet away, and then add additional 1-foot hops to finish the journey.

4 Likes

Thank you for clarifying that!

This seems to be suited to a while loop. I think I get that, if M < N, then += 1 (the last step).

This is what I have so far:

def number_of_hops(n, m):
while m < n:
n = n + m
if m > n:
break

Now trying to think of how to write out adding 1-step increments as necessary…

1 Like

n is the distance to be traversed, so adding m to n here: n = n + m is going to result in an infinite loop. To do is this way, you’ll want to add m to m until it is greater than n. Then you’ll have to remove the last addition of m and add the single hops until it equals n.

P.S. There is a much simpler way to accomplish this using a combination of the / division operator and the % modulo operator.

2 Likes

Could it be something like:

def num_of_hops(n, m):
while m < m / n % 1:
m += 1
if m > n:
break
1 Like

See my comment in your code. Your while statement will work as you originally had intended if you leave it at while m < n:, but the code inside the while block would still need work.

Try thinking of the problem slightly differently. What is an easy way to determine how many 3’s will go into 10? Answer: 10 / 3. That will result in a repeating decimal: 3.33333..., but Python has a couple of built in methods we can use to get the integer value of 3. (Possibly: int() or math.floor()). Then, after we know how many big hops we can take, we can use the modulo operator % to get the remaining 1’s.
10 % 3 = 1 (10 / 3 is 3 with a remainder of 1. The modulo operator returns the remainder.). If we add the results of those two operations together We’ll have our answer: 3 + 1 = 4.

If you’d rather continue with your while statement, you’ll need a separate variable to count the hops.
Something like:

hops = 0
while hops < n:
hops += m #repeat this until hops is greater than or equal to n
#etc..
1 Like

Hah, I second-guessed myself but originally had the same as you. Thank you, this helped a ton translating this from English to operators.

2 Likes

You’re most welcome!

2 Likes