[Tutor] python programmin problem
alan.gauld at yahoo.co.uk
Wed Jul 20 20:26:59 EDT 2016
On 21/07/16 00:14, monikajg at netzero.net wrote:
> IM not able to figure out algorithm to find the runs.
> Here is the code I have:
OK, Forget about code for now. just focus on what is being asked.
> > The first question to ask is can you do it without a computer?
> > In other words given
> > input [1,7,2,3,5,4,6]
> > Can you first of all produce a list of all valid runs?
Lets try it manually. Start with 1
run = 
1 > run[-1] so add it to the run -> 
1 is followed by 7 which >run[-1] so add it to the run -> [1,7]
7 is followed by 2 which <run[-1] so delete 7 from the run -> 
2 is now greater than run[-1] so add it to the run -> [1,2]
2 is followed by 3 which is > run[-1] so add it to the run -> [1,2,3]
3 is followed by 5 which is > run[-1] so add it to the run -> [1,2,3,5]
5 is followed by 4 which is <run[-1] so delete run[-1] - [1,2,3]
4 is now >run[-1] so add it to the run -> [1,2,3,4]
4 is followed by 6 which is > run[-1] so add it to the run -> [1,2,3,4,6]
6 is not followed by anything, run complete.
Can you see an algorithm there that you can code?
Its not quite complete because it never catches the [1,2,3,5,6] case
but its a start. And there are probably more efficient algorithms too,
but we are interested in easy to code here not speed.
Author of the Learn to Program web site
Follow my photo-blog on Flickr at:
More information about the Tutor