Efficient way to keep numbers in order

This is a problem that can be applied to any language, but I will use python to display it.

Say you have a list of numbers, ls = [0,100,200,300,400]

You can insert an item in any index, but the items should always be in order. Duplicates are not allowed.

For example, ls.insert(2, 150)leads to ls = [0,100,150,200,300,400]. The items are in the correct order, so this is correct.

However ls.insert(3, 190)leads to ls = [0,100,200,190,300,400]. This is not true.

For any index i, what is the best number xto use in ls.insert(i,x)to minimize the number of varieties?

My first intuition was to add half the difference between the previous and next numbers to the previous one. Thus, to insert a number with an index of 3, it xwill be 200 + (300-200) or 250. However, this approaches the asymptote too quickly. When the differences get too close to 0, I could restore the differences by cycling through and changing each number to get a big difference. I want to choose the best number for x, to minimize the number of times I need to reset.

EDIT

, , - iOS . , orderingValue. (- -), , . , orderingValue ListItem.

, - , , , .

+4
2

, , . , ( , , "a").

:

def get_key_str(low="a", high="z"):
    if low == "":
        low = "a"
    assert(low < high)
    for i, (a, b) in enumerate(zip(low, high)):
        if a < b:
            mid = chr((ord(a) + ord(b))//2) # get the character half-way between a and b
            if mid != a:
                return low[:i] + mid
            else:
                return low[:i+1] + get_key_str(low[i+1:], "z")
    return low + get_key_str("a", high[len(low):])

s , "a" <= low < s < high <= "z". "a" "z" , , .

get_key_str([lst[i-1], lst[i]), i. lst.insert(i, get_key_str(lst[i-1], lst[i])). , .

, , . get_key_str(high=lst[0]), , get_key_str(lst[-1]), , . "a" low "z" high, . "m", .

, , , , . , .

:

>>> import random
>>> lst = []
>>> for _ in range(10):
    index = random.randint(0, len(lst))
    print("inserting at", index)
    if index == 0:
        low = "a"
    else:
        low = lst[index-1]
    if index == len(lst):
        high = "z"
    else:
        high = lst[index]
    lst.insert(index, get_key_str(low, high))
    print(lst)


inserting at 0
['m']
inserting at 1
['m', 's']
inserting at 2
['m', 's', 'v']
inserting at 2
['m', 's', 't', 'v']
inserting at 2
['m', 's', 'sm', 't', 'v']
inserting at 0
['g', 'm', 's', 'sm', 't', 'v']
inserting at 3
['g', 'm', 's', 'sg', 'sm', 't', 'v']
inserting at 2
['g', 'm', 'p', 's', 'sg', 'sm', 't', 'v']
inserting at 2
['g', 'm', 'n', 'p', 's', 'sg', 'sm', 't', 'v']
inserting at 3
['g', 'm', 'n', 'o', 'p', 's', 'sg', 'sm', 't', 'v']

, :

>>> for _ in range(10):
    lst.insert(0, get_key_str(high=lst[0]))  # start
    lst.insert(len(lst), get_key_str(low=lst[-1])) # end
    print(lst)


['d', 'g', 'm', 'n', 'o', 'p', 's', 'sg', 'sm', 't', 'v', 'x']
['b', 'd', 'g', 'm', 'n', 'o', 'p', 's', 'sg', 'sm', 't', 'v', 'x', 'y']
['am', 'b', 'd', 'g', 'm', 'n', 'o', 'p', 's', 'sg', 'sm', 't', 'v', 'x', 'y', 'ym']
['ag', 'am', 'b', 'd', 'g', 'm', 'n', 'o', 'p', 's', 'sg', 'sm', 't', 'v', 'x', 'y', 'ym', 'ys']
['ad', 'ag', 'am', 'b', 'd', 'g', 'm', 'n', 'o', 'p', 's', 'sg', 'sm', 't', 'v', 'x', 'y', 'ym', 'ys', 'yv']
['ab', 'ad', 'ag', 'am', 'b', 'd', 'g', 'm', 'n', 'o', 'p', 's', 'sg', 'sm', 't', 'v', 'x', 'y', 'ym', 'ys', 'yv', 'yx']
['aam', 'ab', 'ad', 'ag', 'am', 'b', 'd', 'g', 'm', 'n', 'o', 'p', 's', 'sg', 'sm', 't', 'v', 'x', 'y', 'ym', 'ys', 'yv', 'yx', 'yy']
['aag', 'aam', 'ab', 'ad', 'ag', 'am', 'b', 'd', 'g', 'm', 'n', 'o', 'p', 's', 'sg', 'sm', 't', 'v', 'x', 'y', 'ym', 'ys', 'yv', 'yx', 'yy', 'yym']
['aad', 'aag', 'aam', 'ab', 'ad', 'ag', 'am', 'b', 'd', 'g', 'm', 'n', 'o', 'p', 's', 'sg', 'sm', 't', 'v', 'x', 'y', 'ym', 'ys', 'yv', 'yx', 'yy', 'yym', 'yys']
['aab', 'aad', 'aag', 'aam', 'ab', 'ad', 'ag', 'am', 'b', 'd', 'g', 'm', 'n', 'o', 'p', 's', 'sg', 'sm', 't', 'v', 'x', 'y', 'ym', 'ys', 'yv', 'yx', 'yy', 'yym', 'yys', 'yyv']

, a s, y s.

+2

"" , . .

, , - ( , ) , .

, ls.insert(2,150),

ls[1] = ls[1] - (ls[1] - ls[0])/2
ls[3] = ls[3] + (ls[4] - ls[3])/2

, , , .

, , 1, , , .

+1

All Articles