Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Codility Frog River One
#3
You are given a list of random numbers that represent the position of leaves falling in a river. The index of the leaf is the time when that leaf falls. For the frog to cross the river, there must be a leaf in every position. The challenge is to find the earliest time where this is true. So essentially you have a list of slots that must be filled, and the result is the index in the leaf list that fills the last slot.

Since the index of of the first leaf that completes a bridge is the value returned by the function, you cannot use set. Order must be maintained.

I am surprised that the posted code is a valid solution. From reading the description on Codality I thought the solution was supposed to return the "time", not print the time.

As for speed, there are several unnecessary and repeated function calls. I also think building a bridge list is a waste of time. At the start of the function you know how many "slots" you need to find. Can you come up with a more efficient way of recording how many "slots" have been filled that does not involve using list.append() which is known to be slow?

Think about the requirements for solving the problem.
1. You need to keep track of what "positions" are filled. Can you think of another way of doing this that is more efficient than building a list and repeatedly searching the list to see if it already contains a leaf?

2. You need a way to know when you have found a leaf for each position. Can you think of another way to do that that does not require checking the length of a list over and over and over and over and over......
Reply


Messages In This Thread
Codility Frog River One - by Westerner - Jan-09-2021, 06:49 AM
RE: Codility Frog River One - by ndc85430 - Jan-09-2021, 07:27 AM
RE: Codility Frog River One - by deanhystad - Jan-09-2021, 06:35 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question Frog Puzzle Random To Custom MoreMoney 4 655 Mar-26-2024, 08:38 AM
Last Post: MoreMoney
  Codility Binary Gap Exercise Westerner 2 1,886 Jan-08-2021, 09:20 PM
Last Post: Westerner

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020