Friday 8 October 2021

[Recursive] How to Reverse Array or String In Golang | Javascript | Python

In this article we are going to cover up how we can reverse an array or string using recursive function .

Before Writing any code let's explore about what recursion is:

What is Recursion?

Recursion is a process in which a function calls itself as a subroutine. This allows the function to be repeated several times, since it calls itself during its execution. Functions that incorporate recursion are called recursive functions.

Recursion is often seen as an efficient method of programming since it requires the least amount of code to perform the necessary functions. However, recursion must be incorporated carefully, since it can lead to an infinite loop if no condition is met that will terminate the function.


1. Golang

Before Writing code let's check how we're going to achieve that, We're going to use depth via using depth code can track each index and then we're going to minus 1 from each index from the length of the array , until depth value becomes -1 after getting depth value -1 we're going to return the data. Let's see how to write the golang code for reversing array using recursive function.


OUTPUT: reverse array in golang






2. Python

same flow of the logic we're going to implement over here, using depth and subtracting 1 from length of the index rest becomes depth.

OUTPUT:






3. Javascript

The left language to test recursion on is javascript before writing code I'll explain you one things which is important.

In javascript for appending a array into an existing array which methods do you use comment below, So the method for appending array into another array we're going to use spread operator or you can call triple dot method (...array).

Spread syntax (...) allows an iterable such as an array expression or string to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected, or an object expression to be expanded in places where zero or more key-value pairs (for object literals) are expected.

Spread syntax can be used when all elements from an object or array need to be included in a list of some kind. 

Let's Write Some Code Now 

OUTPUT:

Checkout For More Articles.

Labels: , , , , ,

0 Comments:

Post a Comment

If have any queries lemme know

Subscribe to Post Comments [Atom]

<< Home