I want to iterate a list of non-repeating numbers in "increasing resolution" (sorry, don't know the name or exactly how to call this). I will pose an example, suppose the list of numbers [1,2,3,4,5,6,7,8,9]
. I want to iterate this list in the following way [1,9,5,3,7,2,4,6,8]
. The criterion is as follows: in each iteration select the list elements that are (approximately) halfway from the previously selected elements, and append those to the list. Maybe the following scheme helps:
In the zeroth iteration always select those in the extremes. Then the first iteration selects the element in the middle. In the next iteration two elements are picked, those in the middle of the previously selected. And so on. In this way, each iteration increases the resolution in some sense (thinking of this as an image for example).
How is this iteration criterion/algorithm called? Is there a function in Python that already does this for a list of numbers?
CodePudding user response:
I think interlacing is at least similar to it, see GIF Interlaced slow motion example.
You could just code the process as you described it:
result = [lst[0], lst[-1]]
ranges = [(1, len(lst) - 1)]
for start, stop in ranges:
if start < stop:
middle = (start stop) // 2
result.append(lst[middle])
ranges = (start, middle), (middle 1, stop)
Result (Try it online!):
[1, 9, 5, 3, 7, 2, 4, 6, 8]
CodePudding user response:
Maybe this work
def rareSort(inpArr):
#first Els
outputList = [inpArr[0],inpArr[-1]]
subLists =[inpArr[1:-1]]
while len(subLists):
auxSubLists = []
for el in subLists :
if el != []:
mid= int((len(el))/2)
outputList.append(el[mid])
auxSubLists = [el[0:mid], el[mid 1:]]
else:
pass
subLists = auxSubLists
return outputList
print(rareSort(inpArr))