Thread: Logic problem

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2005
    Posts
    2
    Rep Power
    0

    Logic problem


    the code: http://paste.phpfi.com/46555
    a small bit of 'full.txt': http://www.fulsic.com/full.txt

    The code works until the last line (where it adds all the information to a dictionary - fDict). I've spent the past hour staring at the screen but can't seem to see the problem.

    The dictionary should contain the student ID - newLine[1] - and the subjects the student selects. The text file has only one subject per line, so I'm thinking that I need a way to 'append' to the dictionary? (The subject code is newLine[3] or subject. The actual time the subject runs is identified by subject[-2] - the second last character in the subject variable).

    Sample of expected output:
    {'040179': ('11MA211', '11PH221', '11SD231', '', '11ES251', '', '11ENA271', '')}
    Any index left out identifies that the student does not have a class on that time.

    It worked for one attempt since then it hasn't and I can't remember what I did.

    Any help would be appreciated.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Location
    Regensburg, Germany
    Posts
    147
    Rep Power
    17
    The problem is that the "line" array is initialised for every line you read from the file. Thus it will contain no more than one non empty elements:
    Code:
    full = open('full.txt')
    fList = []
    fDict = {}
    for fline in full:
        newLine = fline[:-1]
        lines = ['','','','','','','','']
        newLine = newLine.split(',')
        subject = newLine[3]
        # print "Student id: " + str(newLine[1]) + " " + str(newLine[3])
        if subject[-2] == '1':
            lines[0] = subject
        if subject[-2] == '2':
            lines[1] = subject
        if subject[-2] == '3':
            lines[2] = subject
        if subject[-2] == '4':
            lines[3] = subject
        if subject[-2] == '5':
            lines[4] = subject
        if subject[-2] == '6':
            lines[5] = subject
        if subject[-2] == '7':
            lines[6] = subject
        if subject[-2] == '8':
            lines[7] = subject
        stuID = str(newLine[1])
        fDict[stuID] = lines[0],lines[1],lines[2],lines[3],lines[4],lines[5],lines[6],lines[7]
    The "lines" array should be initialised only if the student id changes during line processing.

    A more robust approach which does not rely on the sort order of the input file would be to create an empy data set in fDict for every new student id and update the entries afterwards. You may want to try this:
    Code:
    full = open('full.txt', 'r')
    fList = []
    fDict = {}
    for fline in full:
        newLine = fline[:-1]
        newLine = newLine.split(',')
        subject = newLine[3]
        stuID = str(newLine[1])
        # print "Student id: " + str(newLine[1]) + " " + str(newLine[3])
        if subject[-2] in '12345678':
            if not fDict.has_key(stuID):
                # new student
                ftDict[stuID] = ['','','','','','','','']
    	# update student
    	# index from 0 to 7
    	index = int(subject[-2]) - 1
            fDict[stuID][index] = subject
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2005
    Posts
    2
    Rep Power
    0
    Thank you, it works.

IMN logo majestic logo threadwatch logo seochat tools logo