[Tutor] alphabetizing a file by lines
Brian van den Broek
bvande at po-box.mcgill.ca
Sun Jul 18 23:37:43 CEST 2004
orbitz said unto the world upon 18/07/2004 16:25:
> Dragonfirebane seems to want to write his code in the way he has given
> under the guise of learning. I'm not sure if others would agree or
> disagree with me here, but I think doing such a thing is probably
> counter productive with python.
> I'd like to hear others suggestions but here are my thoughts.
> In python, we should generally try to avoid duplicating as much code as
> possible. This means making use of the standard library and 3rd party
> libraries where we can. In dragonfirebane's situation he most likely
> wants to learn about sorting. which is fine, however I think he'd have
> more success if he took a simple case of say, implementing a high school
> level sorting algorithm on a simple list to get the idea of how sorting
> is done, but for something like this just using what python offers.
> IMO, using large relatively complex programs to learn about relatively
> simplistic problems is counter productive because you spend too much
> time in silly details of implementation rather than focusing on what you
> are trying to learn.
>
>
> Dragonfirebane at aol.com wrote:
>
>> Hello all,
>>
>> I'm trying to write a program that alphabetizes a file by the first
>> letter on each line. I'm having some problems because as soon as the
>> program finds a letter, it looks for the next letter, ignoring
>> subsequent appearances of that letter. I can think of a couple ways to
>> fix this but none of them seem to work. The first of these would be to
>> add a special character to lines that have already been alphabetized,
>> but file.write() writes to the end of a file and i would need to write
>> the character at the current position in the file. This might be
>> circumvented by creating a file for each line that is alphabetized,
>> but that seems a bit extreme . . . The code is below. Any suggestions
>> would be appreciated. Future concerns will be alphabetizing past the
>> first letter.
>>
>> ##############
>> def linum():
<SNIP>
>> Thanks in advance,
>> Orri
>>
>> Email: dragonfirebane at aol.com
>> AIM: singingxduck
>> Programming Python for the fun of it.
>> ------------------------------------------------------------------------
Hi all,
as I'm still learning, I doubt that it was my thoughts on this that orbitz
was seeking :-)
But I spent a bit of time re-implementing a few library functions just to
learn how to do it (I did my own cmp() and sort() for instance). I thought
I learned something useful from the effort.
I wouldn't use them again -- in fact, once they worked, I trashed them.
(They'd served their learning purpose.) But for learning, the duplication
of some of the built-in and lib functions seems almost inevitable to me.
Learners want to try central, basic, and fairly small tasks at first.
Central and basic tasks seem those likely to have an implementation in
Python already ;-)
(I absolutely agree that once past learning, it would be daft not to rely
on the lib when what was there did what you want.)
What I find odd about dragonfirebane's approach though is the use of
regular expressions to accomplish the sorting task. I felt like I was
learning by taking something central and re-doing it in basic building
blocks. By using r.e. I feel like dfb is re-doing the central and fairly
simple in terms of the considerably more complex and I at least have
doubts about the learning value of that approach. (But what do I know ;-)
Best to all,
Brian vdB
More information about the Tutor
mailing list