Reverse array elements in Julia – reverse(), reverse!() and reverseind() Methods

The reverse() is an inbuilt function in julia which is used to reverse the specified vector v from start to stop.

Syntax:
reverse(v, start, stop)
or
reverse(A; dims::Integer)

Parameters:

  • v: Specified vector.
  • start: Specified starting value.
  • stop: Specified stopping value.
  • A: Specified array.
  • dims::Integer: Specified dimensions.

Returns: It returns a reversed copy of the vector.

Example 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

# Julia program to illustrate 
# the use of reverse() method
   
# Getting the reversed copy of 
# the specified vector.
A = Vector(2:8)
println(reverse(A))
println(reverse(A, 3, 6))
println(reverse(A, 7, 4))

chevron_right


Output:

[8, 7, 6, 5, 4, 3, 2]
[2, 3, 7, 6, 5, 4, 8]
[2, 3, 4, 5, 6, 7, 8]

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Julia program to illustrate 
# the use of reverse() method
   
# Getting reversed array in 
# the specified dimension
A = [5 10; 15 20]
println(reverse(A, dims = 1))
println(reverse(A, dims = 2))

chevron_right


Output:

reverse!()

The reverse!() is an inbuilt function in julia which is In-place version of reverse() function.

Syntax:
reverse!(v, start, stop)

Parameters:

  • v: Specified vector.
  • start: Specified starting value.
  • stop: Specified stopping value.

Returns: It returns a reversed copy of the vector.

Example:



filter_none

edit
close

play_arrow

link
brightness_4
code

# Julia program to illustrate 
# the use of reverse !() method
   
# Getting the reversed copy of 
# the specified vector.
A = Vector(2:8)
println(reverse !(A))
println(reverse !(A, 3, 6))

chevron_right


Output:

[8, 7, 6, 5, 4, 3, 2]
[8, 7, 3, 4, 5, 6, 2]

reverseind

The reverseind() is an inbuilt function in julia which is used to return the corresponding index in v so that v[reverseind(v, i)] == reverse(v)[i], where i is the given index.

Syntax:
reverseind(v, i)

Parameters:

  • v: Specified string.
  • i: Specified index.

Returns: It returns the corresponding index in v so that v[reverseind(v, i)] == reverse(v)[i].

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Julia program to illustrate 
# the use of reverseind() method
   
# Getting the corresponding index in v
# so that v[reverseind(v, i)] == reverse(v)[i]
V = reverse("Geeks")
for i in 1:length(r)
    print(V[reverseind("Geeks", i)])
end

chevron_right


Output:




My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.