[Tutor] extracting numbers from a list
Danny Yoo
dyoo at hkn.eecs.berkeley.edu
Wed Oct 18 22:18:14 CEST 2006
On Wed, 18 Oct 2006, kumar s wrote:
> I am not sure if I got your opinion about the way I solved.
Hi Kumar,
I was replying to your question:
http://mail.python.org/pipermail/tutor/2006-October/050064.html
in which you showed a program that had problems. I'm assuming that you
haven't figured out what's wrong with the code in your post. I've
presented an approach that shows how one might try to approach the problem
without being brilliant. Did you understand every step in the program in:
http://mail.python.org/pipermail/tutor/2006-October/050121.html
or was there something there that you didn't understand? If so, please
ask for clarification, because everything there should have been
approachable to you.
> do you mean that there is something wrong with the way i solved it.
Well... how do I put this gently? Your program doesn't work, so I can't
really give a positive or negative opinion about its effectiveness until
it does. I can sorta see what the code tries to do, but it's going about
it in a complicated fashion.
I can give my opinion that:
###################
fx = a[0]
fy = a[1]
b = a[2:]
ai = iter(b)
last = ai.next()
for j in ai:
print fy+1,last
last = j
###################
seems to be juggling too many variables, too much state. I think the
conceptual bug here is that the code fumbles around, trying to do
something toward a goal, but it loses intentionality. I don't understand
the intent of 'fx', since that variable is never used. I don't understand
the expression 'fy+1' in the print statement, since 'fy' never changes.
There's just a lot of little small things there that seem irrelevant to
the problem at hand.
I haven't tried to fix your program; rather, I've tried to show you an
approach that can systematic attack these kinds of problems without much
trouble. The problem I see that you've been having is that you're getting
stuck as soon as the complexity of a problem reaches a threshold. I'm
trying to show you techniques to compensate when problems get hard. I
could just as easily have just fixed the above code, but how would this
really help you? For you, it would be as if I just pulled that program
out of thin air!
But just to satisfy you, such a program would have looked like this:
#################
x = a[0]
for y in a[1:]:
print x, y
x = y + 1
#################
The relationship between this and your original program is somewhat there.
But again, if I just showed this to you, how does that really help you to
program better? The program here is written to conserve memory, but at
the expense of being harder to develop, understand, and explain. And, if
I were doing this as a computer scientist, I'd have to develop a loop
invariant argument to feel satisfied that it was doing the right thing.
In comparison, the inductive definition has the force of a proof behind
it.
> I am not sure If I explained the problem correctly in terms of exons,
> transcripts. If not I would be happy to send you a pdf file with a
> figure.
That's ok. I don't think it is necessary as long as you clearly explain
the problem you're trying to solve. You should be able to communicate
what you're trying to do.
(And just as background: I've worked in the past as a bioinformatician at
arabidopsis.org. The terminology that you've been using has not been an
issue.)
More information about the Tutor
mailing list