patternpythonMinor
Reorder list such that each element is followed by the value closest to it
Viewed 0 times
suchtheeachreorderfollowedvaluethatelementclosestlist
Problem
I just started playing with Python and was hoping to get some feedback regarding the quality of the following snippet. Does this look like proper Python? What would you change? Is this small script well structured?
Quick description of functional goal: reorder list such that each element is followed by the value closest to it, starting from 20 (AKA shortest-seek-first algorithm).
example:
becomes:
better example:
becomes:
Quick description of functional goal: reorder list such that each element is followed by the value closest to it, starting from 20 (AKA shortest-seek-first algorithm).
example:
[15, 24, 12, 13, 48, 56, 2]becomes:
[24, 15, 13, 12, 2, 48, 56]better example:
[22, 19, 23, 18, 17, 16]becomes:
[19, 18, 17, 16, 22, 23]fcfs_working = [15, 24, 12, 13, 48, 56, 2]
fcfs_track = []
track_number = 20
while len(fcfs_working) > 0:
track_number = min(fcfs_working, key=lambda x:abs(x-track_number))
fcfs_working.remove(track_number)
fcfs_track.append(track_number)Solution
From the algorithmic perspective, I'd like to suggest another solution (regardless to Python as a language).
Here goes:
let's assume that all items in the given array are different. the other case has a similar solution, so let's focus on the algorithm itself.
Demonstration:
original array:
[22, 19, 23, 18, 17, 8]
sorted:
[8, 17, 18, 19, 22, 23]
first element is 19, since abs(20-19) = 1 is the nearest distance to 20.
let's find 19 in the sorted array:
[8, 17, 18, 19, 22, 23]
^
now the next element would be either 18 or 22, since the array is already sorted:
[8, 17, 18, 19, 22, 23]
? ^ ?
so now we have those inner-left and inner-right indexes, let's label them as "il" and "ir".
[8, 17, 18, 19, 22, 23]
il ^ ir
result list: [19]
since 18 is closer to 19 than 22, we take it, and shift il to the left:
[8, 17, 18, 19, 22, 23]
il ^ ir
result list: [19, 18]
again, now 17 is closer to 18 than 22
[8, 17, 18, 19, 22, 23]
il ^ ir
result list: [19, 18, 17]
now 22 is closer to 17 than 8, so we'd shift the right-index:
[8, 17, 18, 19, 22, 23]
il ^ ir
result list: [19, 18, 17, 22]
and the rest is obvious.
Again, this has nothing to do with Python in particular. It's purely an algorithm to be implemented in any language.
This algorithm takes O(n log(n)) in time, while the one suggested by the original poster takes O(n^2) in time.
Here goes:
let's assume that all items in the given array are different. the other case has a similar solution, so let's focus on the algorithm itself.
- Find the nearest number, in terms of distance, from the array to the given external number (20).
- sort the given array.
- find the value in (1) in the array. it can be done using a binary search.
- now, to the main point: the next value would be either to the left of the current value or to the right of the current value, since the distance is now between the next value to the current one, and the array is sorted (!).
- so all you have to do is to hold two additional indexes: inner-left and inner-right. these indexes represent the inner boundaries of the "hole" that is being created while constructing the new list.
Demonstration:
original array:
[22, 19, 23, 18, 17, 8]
sorted:
[8, 17, 18, 19, 22, 23]
first element is 19, since abs(20-19) = 1 is the nearest distance to 20.
let's find 19 in the sorted array:
[8, 17, 18, 19, 22, 23]
^
now the next element would be either 18 or 22, since the array is already sorted:
[8, 17, 18, 19, 22, 23]
? ^ ?
so now we have those inner-left and inner-right indexes, let's label them as "il" and "ir".
[8, 17, 18, 19, 22, 23]
il ^ ir
result list: [19]
since 18 is closer to 19 than 22, we take it, and shift il to the left:
[8, 17, 18, 19, 22, 23]
il ^ ir
result list: [19, 18]
again, now 17 is closer to 18 than 22
[8, 17, 18, 19, 22, 23]
il ^ ir
result list: [19, 18, 17]
now 22 is closer to 17 than 8, so we'd shift the right-index:
[8, 17, 18, 19, 22, 23]
il ^ ir
result list: [19, 18, 17, 22]
and the rest is obvious.
Again, this has nothing to do with Python in particular. It's purely an algorithm to be implemented in any language.
This algorithm takes O(n log(n)) in time, while the one suggested by the original poster takes O(n^2) in time.
Context
StackExchange Code Review Q#5548, answer score: 3
Revisions (0)
No revisions yet.