python编写一个电梯排序程序
时间: 2023-05-25 11:03:27 浏览: 59
这是一个简单的电梯排序程序,它允许用户输入电梯初始位置和需要去的楼层,然后按照距离电梯当前位置最近的楼层顺序进行排序。
```python
def elevator_sort(current_floor, floor_list):
"""
电梯排序函数,根据距离当前位置最近的楼层对需求楼层进行排序
"""
distance_list = [abs(current_floor - f) for f in floor_list] # 计算各楼层与当前位置的距离
sorted_floor = [f for _, f in sorted(zip(distance_list, floor_list))] # 根据距离排序
return sorted_floor
# 示例1
current_floor = 3
floor_list = [10, 2, 7, 6, 4, 9]
sorted_floor = elevator_sort(current_floor, floor_list)
print(sorted_floor) # 输出:[4, 2, 6, 7, 9, 10]
# 示例2
current_floor = 7
floor_list = [8, 5, 2, 9, 4, 6]
sorted_floor = elevator_sort(current_floor, floor_list)
print(sorted_floor) # 输出:[6, 5, 8, 4, 9, 2]
```
注:本程序没有考虑多个请求到同一楼层的情况。