MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / reverse

Function reverse

data_structures/arrays/rotate_array.py:32–61  ·  view source on GitHub ↗

Reverses a portion of the list in place from index start to end. Parameters: start (int): Starting index of the portion to reverse. end (int): Ending index of the portion to reverse. Returns: None Examples: >>> example = [1, 2, 3, 4

(start: int, end: int)

Source from the content-addressed store, hash-verified

30 steps += n
31
32 def reverse(start: int, end: int) -> None:
33 """
34 Reverses a portion of the list in place from index start to end.
35
36 Parameters:
37 start (int): Starting index of the portion to reverse.
38 end (int): Ending index of the portion to reverse.
39
40 Returns:
41 None
42
43 Examples:
44 >>> example = [1, 2, 3, 4, 5]
45 >>> def reverse_test(arr, start, end):
46 ... while start < end:
47 ... arr[start], arr[end] = arr[end], arr[start]
48 ... start += 1
49 ... end -= 1
50 >>> reverse_test(example, 0, 2)
51 >>> example
52 [3, 2, 1, 4, 5]
53 >>> reverse_test(example, 2, 4)
54 >>> example
55 [3, 2, 5, 4, 1]
56 """
57
58 while start < end:
59 arr[start], arr[end] = arr[end], arr[start]
60 start += 1
61 end -= 1
62
63 reverse(0, n - 1)
64 reverse(0, steps - 1)

Callers 1

rotate_arrayFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected