The following function converts a list of non-unique random numbers into a list of unique random numbers using a helper list of sequential integers:
def get_d(rlist):
# t[] is a list of ints 0..n-1 where n is big
# enough not to cause issues when deleting items
t = list(range( len(rlist) + max(rlist) ))
d = [] # the output list we will populate
for r in rlist:
d.append(t[r]) # add r'th remaining item in t[] to d[]
t.pop(r) # delete that item from t[]
return d
e.g.
get_d([3, 2, 3, 1, 5]) = [3, 2, 5, 1, 9]
I would like to get the same output using a counting method or other method. This is my attempt, which has some edge case or perhaps more fundamental issues:
def get_e(r):
e = []
for i,t in enumerate(r):
c = 0
# track numbers <= the current one
for j in range(i):
if r[j] <= t:
c+=1
# track numbers <= the current one when virtual deletions are accounted for
for j in range(i):
if r[j] > t and r[j]-c <= t:
c+=1
e.append(t+c)
return e
Here is a test:
for r in [ [3,2,3,1,5], [0,0,1,0,0]]:
d = get_d(r)
e = get_e(r)
print('match: ' if d==e else 'mismatch:', r, ' : ', d, ' ', e)
Output:
match: [3, 2, 3, 1, 5] : [3, 2, 5, 1, 9] [3, 2, 5, 1, 9]
mismatch: [0, 0, 1, 0, 0] : [0, 1, 3, 2, 4] [0, 1, 3, 3, 4]
Aucun commentaire:
Enregistrer un commentaire