[Tutor] python programmin problem
alan.gauld at yahoo.co.uk
Fri Jul 22 02:30:31 EDT 2016
Forwarding to list. Please use reply-all when responding to tutor messages.
As Danny suggested this is quite a complex problem. I wasn't sure whether
it was just the programming or the bigger algorithm issue you were stuck on.
For now I'd stick with the code you have and think about how to use that
to build up a list of runs and then find the longest. Hint: Start
progressive items in your initial sequence.
Hopefully somebody else can pick this up as I have to go away for a fewys...
On 22/07/16 06:02, monikajg at netzero.net wrote:
> Thank you so much for your suggestion. I was able to turn it into the code but Im stuck on catching the [1,2,3,5,6] case case. Could you please be so kind and give me a hint? I apologize for not having much experience on this but things like that were never taught in any of the classes I took and I have nowhere else to ask.
> Thank you so much.
> Here is my code:
> run = 
> for i in range(len(items)):
> if i == 0:
> run += [items[i]]
> if items[i - 1] < items[i]:
> run +=[ items[i]]
> elif items[i - 1] > items[i]:
> del run[-1]
> run += [items[i]]
> print run
> ---------- Original Message ----------
> From: Alan Gauld via Tutor <tutor at python.org>
> To: "monikajg at netzero.net" <monikajg at netzero.net>
> Cc: tutor at python.org
> Subject: Re: [Tutor] python programmin problem
> Date: Thu, 21 Jul 2016 01:26:59 +0100
> 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