Python algorithm error when trying to find the next largest value

I wrote an algorithm that scans the "ID" file and compares that value with the integer value i (I converted the integer to a string for comparison and I truncated the "\ n" from the string). The algorithm compares these values ​​for each line in the file (each identifier). If they are equal, the algorithm increments i by 1 and uses reccurtion with the new i. If the value is not equal, it compares it to the next line in the file. It does this until it has a value for i that is not in the file, and then returns that value to use as the next record identifier.

My problem is that I have a file ID, list 1,3,2, when I deleted the record with id 2 and then created a new record. This shows that the algorithm is working correctly, as it gave the new record an ID of 2, which was previously deleted. However, when I create a new record, the next ID is 3, which will display my ID: 1,3,2,3

instead 1,3,2,4

. Bellow is my algorithm with command results print()

. I can see where this is going wrong, but cannot figure out why. Any ideas?

Algorithm:

def _getAvailableID(iD):
        i = iD
        f = open(IDFileName,"r")
        lines = f.readlines()
        for line in lines:
            print("%s,%s,%s"%("i=" + str(i), "ID=" + line[:-1], (str(i) == line[:-1])))
            if str(i) == line[:-1]:
                i += 1
                f.close()
                _getAvailableID(i)
        return str(i)

      

Output: (Result when the algorithm was run to find the matching ID for the record, which should have ID 4):

i=1,ID=1,True
i=2,ID=1,False
i=2,ID=3,False
i=2,ID=2,True
i=3,ID=1,False
i=3,ID=3,True
i=4,ID=1,False
i=4,ID=3,False
i=4,ID=2,False
i=4,ID=2,False
i=2,ID=3,False
i=2,ID=2,True
i=3,ID=1,False
i=3,ID=3,True
i=4,ID=1,False
i=4,ID=3,False
i=4,ID=2,False
i=4,ID=2,False

      

+3


source to share


2 answers


I think your program is failing because you need to change:

_getAvailableID(i)

      

to

 return _getAvailableID(i)

      



(At the moment, the recursive function finds the correct answer, which is discarded.)

However, it would probably be better to just put all the identifiers you saw in the set to make the program more efficient.

eg. in pseudocode:

S = set()
loop over all items and S.add(int(line.rstrip()))
i = 0
while i in S:
   i += 1
return i

      

+6


source


If you're just looking for the max id in the file and then want to return the next available value:

def _getAvailableID(IDFileName):
    iD = '0'
    with open(IDFileName,"r") as f:
        for line in f:
            print("ID=%s, line=%s" % (iD, line))
            if line > iD:
                iD = line

    return str(int(iD)+1)

print(_getAvailableID("IDs.txt"))

      

with an input file containing

1
3
2

      



deduces

ID=1, line=1

ID=1
, line=3

ID=3
, line=2

4

      

However, we can solve this in a more pythonic way:

def _getAvailableID(IDFileName):
    with open(IDFileName,"r") as f:
        mx_id = max(f, key=int)
    return int(mx_id)+1

      

+1


source







All Articles