promotion image of download ymail app
Promoted

Python help?

Define a function count_hops_to_last(...) that receives a non-empty list lst of non-negative integers as parameter, and returns the number of "hops" it takes to get from the first to the last position in the list.

The list lst will have values which are numbers between 0 and len(lst)-1 (inclusive). Those values can be understood as positions/indices in lst, in any order. For example possible lists coming into the function could be [1,3,0,2] and [7,6,1,10,9,5,3,2,0,8,4].

A "hop" means moving from position i to another position, which is the value at position i in lst. "The hopping" starts happening always at position 0 and will end in the last position. This function should return the number of hops that were needed to go from the first position to the last.

 

In the list [7,6,1,10,9,5,3,2,0,8,4] , it takes six hops to reach the last position from the first position:

0->7->2->1->6->3->10

You can assume that the input list lst does not contain any cycles. For example, the list [1,0,2] is not allowed (and therefore it will not be a test case) as it would result in an infinite chain of hops: 

0->1->0->1->0->1->... 

As an example, the following code fragment:

print(count_hops_to_last([1,3,0,2]))

should produce the output:

2

1 Answer

Relevance
  • 2 months ago

    Hi, Struggling Student.

    I have solved this, but why should I just give you the solution to this, and your other questions? Are we bringing up a generation which can't think for themselves, can't apply logical thought processes to arrive at a solution? A generation of cheats? Wouldn't you rather have the satisfaction of being able to say "I solved that myself"?

    So, although in the past I would "hand you the answer on a plate" as it were, I will let you figure this out, and your other code, for yourself.

    The very best of luck!

    • As someone native to the Mathematics and Science section, I see people like @StrugglingStudent all the time. I 100% agree with your response (despite being in the said generation) and appreciate your thoughtful answer; it makes me want to reflect on my lack of efforts as well

    • Commenter avatarLogin to reply the answers
Still have questions? Get your answers by asking now.