1.
def reverse_array_two_pointers(arr):
left = 0
right = len(arr) - 1
while left < right:
arr[left], arr[right] = arr[right], arr[left]
left += 1
right -= 1
return arr
2.
def reverse_array_recursive(arr):
if len(arr) <= 1:
else:
return reverse_array_recursive(arr[1:]) + [arr[0]]
3.
def reverse_array_single_pointer(arr):
start = 0
end = len(arr) - 1
while start < end:
arr[start], arr[end] = arr[end], arr[start]
start += 1
end -= 1
4.
def reverse_array_stack(arr):
stack = []
for item in arr:
stack.append(item)
reversed_arr = []
while stack:
reversed_arr.append(stack.pop())
return reversed_arr
Na wejściu do funkcji podajesz tablicę :D
" Life is not a problem to be solved but a reality to be experienced! "
© Copyright 2013 - 2024 KUDO.TIPS - All rights reserved.
1.
def reverse_array_two_pointers(arr):
left = 0
right = len(arr) - 1
while left < right:
arr[left], arr[right] = arr[right], arr[left]
left += 1
right -= 1
return arr
2.
def reverse_array_recursive(arr):
if len(arr) <= 1:
return arr
else:
return reverse_array_recursive(arr[1:]) + [arr[0]]
3.
def reverse_array_single_pointer(arr):
start = 0
end = len(arr) - 1
while start < end:
arr[start], arr[end] = arr[end], arr[start]
start += 1
end -= 1
return arr
4.
def reverse_array_stack(arr):
stack = []
for item in arr:
stack.append(item)
reversed_arr = []
while stack:
reversed_arr.append(stack.pop())
return reversed_arr
Na wejściu do funkcji podajesz tablicę :D