I want to type a search function which takes the value x and in the ordered sequence and gives back the position which is assumed Should the X be in the list again in the re-run list through elements of the sequence starting with the first element, it should be the first position that it is less than or equal to the next element in the list.
Example: & gt; & Gt; & Gt; Search (-5, (1, 5, 10)) - 0> & gt; & Gt; Search (3, (1, 5, 10)) - 1
In the building list The list of each item will be a bit of a waste of resources when there is a large gap, instead you can do just that through the list items unless the input value is larger than that.
Your code -
DIF search (input, input list): In the range I (lane (input list)): If input list [i] & gt ; Input: return I len (inputList) print search (-5, (1, 5, 10)) # Results: 0 Print search (3, (1, 5, 10)) # Results: 1
Put it in the list, it will work, I split the list into 2 based on the index and add value to the middle.
def insert (input, inputlist): index = search (input, inputList) # where new input = [input] + list (input list [index:]) should be inserted, if index Add the end of the list to input: newInput = list (inputList [: index]) + Add new beginning # start of the list if index 0 returns new input print is not included (-5, (1, 5, 10)) # References: Insert a print (-5, 1, 5, 10) (3, (1, 5, 10)) # Result: (1, 3, 5, 10)
No comments:
Post a Comment