可以举一个rearrange() 函数的例子吗
时间: 2024-05-09 22:18:09 浏览: 51
回调函数举例
Sure! Here's an example of a rearrange() function:
```
def rearrange(arr):
# sort the array
arr.sort()
# create a new array with the same length as the input array
result = [0] * len(arr)
# iterate over the input array and fill the new array with the values in a rearranged order
for i in range(len(arr)):
if i % 2 == 0:
result[i] = arr[i//2]
else:
result[i] = arr[len(arr)-1-i//2]
# return the rearranged array
return result
```
This function takes an array as input and rearranges the elements in a specific order. It first sorts the input array and then creates a new array with the same length. Then, it iterates over the input array and fills the new array with the values in a rearranged order. Finally, it returns the rearranged array.
阅读全文