On 10/01/2012 09:44 PM, Dave Angel wrote:
> 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. 

Use a tuple of  (name, grades)   where the name is a string, and the
grades are in a dict or defaultdict.  When sorting a list of tuples, the
sort will work only on the first element of each tuple, unless there's a
tie.  Checking for duplicate student names is one of those things that
gets separately verified, before even running your code.

>> 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.
>
One simplification, so you won't need a confusing key function, is to
make each item of the list a tuple, student-name string followed by
defaultdict.  By not putting the name inside the dict, sort of the list
will get the order right by default.  In particular, when sorting a list
of tuples, it uses the first item of each tuple as a primary key.

Recapping:
A list of tuples, one tuple per line.  First item of the tuple is the
studentname for that line. Other item of the tuple is a defaultdict
keyed by such strings as HM1, HM2, ...

Now, write some code, and when you get stuck, show us what you have, how
you tested it, and what went wrong.

-- 

DaveA

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to