Open In App

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

Last Updated : 01 Apr, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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:




# 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))


Output:

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

Example 2:




# 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))


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:




# 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))


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:




# 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


Output:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads