[Tutor] python help?

Dave Angel d at davea.name
Tue Oct 2 03:44:47 CEST 2012

On 09/30/2012 02:02 AM, patrick Howard wrote:
> I have to write a program that takes an input file, with students names and 
> various grades.
> My vindictive teacher also added grades that are not supposed to count, so I 
> need to pick the grades that are 'HM1-4"; not HW, or TEST or anything else.
> Then when these are listed in order with that persons name, sort the list 
> alphabetically. I know that this is '.sort;
> But, how do I get it to sort only names, AND keep the right grades with them. 
> Below is a list of parameters… Can you help me please?

What part are you stuck on? It's hard to give advice when we don't know
what part of the assignment matters. First question is what language are
you to write this in, and on what OS. Assuming it's Python, then what's
the version?

I'm a little surprised you call the teacher vindictive. Bad data is
quite common in the real world. And in the real world, I'd be writing a
separate program to "cleanse" the data before processing.

Anyway, first you need to be able to open the specified file and read
and parse the text lines into some form of data structure.

Second you manipulate that structure, so that the output can be produced
in a straightforward manner.

Third, you produce the output, which now should be pretty straightforward.

Write them as three (at least) separate functions, and you can debug
them separately. Much easier than trying to make it a monolith which
either works or is hopeless.

So the first question is what kind of structure can hold all that data.
I have no idea what level of Python you've attained, but I'll guess you
don't know how to write your own classes or generators. So you have to
build the structures from whatever's in the standard library.

Outermost structure is a list, one per student. Each item of the list is
a defaultdict, keyed by name, and by the various HMn fields. Values of
each of those dict items are floats.



More information about the Tutor mailing list