1. There is a sorted list. Now input a number and insert it into the list according to the original rule
Analysis: the sorted list may be in ascending or descending order. You need to determine the sorting method of the list first. Take ascending order as an example. You need to consider four main situations:
Case1: insert the first value if it is less than the first value of the list
If s=[2,3,4], insert 1, the result should be [1,2,3,4], and the insertion position of 1 should be 0
Case2: in interval
If s=[2,4,5] inserts 3, then the result should be [2,3,4,5], 2 inserts between 2 and 3, index between s[0] and s[1]
Case3: greater than last value of list
If s=[2,4,5] inserts 6, then the result should be [2,4,5,6], 6 should be inserted at the end, and the index is s[len(s)]
Case4: there are duplicate values in the list and they are the same as the inserted values
For example, if s=[2,4,4,5] inserts 4, the result should be [2,4,4,4,5], and the insertion position of 4 should be between two 4. Therefore, based on case 2, we need to consider the equality based on the previous three cases
def insert_num_in_order(s,num):
result=[]
if s[0]<s[-1]:
#If it's in ascending order,It's better not to use it here s[0]And s[1]Compare, because there may be equality, use the head and tail for comparison
for i in range(len(s)):
if i ==0 and num<s[i]:
#Corresponding Case1 Scene
result.append(num)
result.append(s[i])
elif len(s)-1>i and num>=s[i] and num <s[i+1]:
#The writing interval is required here, otherwise, it will be executed twice in a specific case, such as inserting 3 into the sequence of 2, 3, 3, if the writing num>=s[i] and num <=s[i+1]The condition will be executed twice
#Corresponding Case2,Case4 Scene, i At 0 to len(s)-1 Between, if i=len(s)The situation is s[i+1]Will exceed range
#Here we also need to consider a case where [0,0,0,1,2] i s inserted into 0, so in the elif condition, it should not be written as len (s) - 1 > I > 0, but as len (s) - 1 > I
result.append(s[i])
result.append(num)
elif i ==len(s)-1 and num>=s[i]:
#Corresponding here case3,case4 Scene
result.append(s[i])
result.append(num)
else:
#Other scenes
result.append(s[i])
#print('result',result)
else:
#In descending order
for i in range(len(s)):
if i==0 and num>s[i]:
result.append(num)
result.append(s[i])
elif len(s)-1>i and num<=s[i] and num >s[i+1]:
result.append(s[i])
result.append(num)
elif i ==len(s)-1 and num<=s[i]:
result.append(s[i])
result.append(num)
else:
result.append(s[i])
return result
One thing to pay special attention to: because there are more than two identical values in the sequence, and they are the same as the inserted values, take the descending order as an example. When writing conditions, len (s) - 1 > I and num < = s [i] and num > s [I + 1] are indispensable. Otherwise, there will be multiple executions
#Check in ascending order
s = [0,0,1,2,3,4,5,6,7,8,9,10,10]
print(s)
print(insert_num_in_order(s,9))
#results of enforcement
#[0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 9, 10, 10]
print(insert_num_in_order(s,0))
#results of enforcement
#[0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10]
print(insert_num_in_order(s,10))
#results of enforcement
#[0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10]
#Check in descending order
s1 = [10,10,9,8,7,6,5,4,3,2,1,1]
print(s1)
print(insert_num_in_order(s1,9))
#results of enforcement
#[10, 10, 9, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1]
print(insert_num_in_order(s1,1))
#results of enforcement
#[10, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1, 1]
print(insert_num_in_order(s1,10))
#results of enforcement
#[10, 10, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1]