Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Basic Array Questions #27

Merged
merged 8 commits into from
Oct 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions recursion/Basic Array Questions/Display_arr_reverse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Program to print an array in reverse order

def reverse(a,n):
if n==0: # Base Case when size of array is 0 then it returns
return
else:
print(a[n-1],end=" ") # Print the last element of the array
reverse(a,n-1) # Function calling itself

a = [1, 42, 45, 60, 50, 10, 2] # Taking an array
n = len(a)
reverse(a,n)

# Output
# 2 10 50 60 45 42 1
29 changes: 29 additions & 0 deletions recursion/Basic Array Questions/Display_array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Program to Display Array Elements Using Recursion

def display_arr(a,i,n):
if i>=n: # Base Case if value of i become greater or equal to size of an array then it returns
return
print(a[i],end=" ") # Print the array at ith position (as the value of i increases elements get print accordingly)
display_arr(a,i+1,n) # Function calling itself

arr = [] # Taking an Array
n = int(input("Enter the size of the array: ")) # Initializing the size of an array
print("Enter the elements of the array: ")
for i in range(0,n):
num = int(input()) # Taking input from user
arr.append(num)

print("Elements of Array are: ")
display_arr(arr,0,n) # Printing the elements of an array through recursion

# Example

# Input:
# 1
# 2
# 3
# 4
# 5

# Ouput:
# 1 2 3 4 5
18 changes: 18 additions & 0 deletions recursion/Basic Array Questions/First_Index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Program to find first index of occurrence of an element in an array
# Using start index approach, in this we don't have to make the copy of an array


def firstindex(a, x, si):
l = len(a)
if si==l: # Base Case if size of array is 1 then it returns -1
return -1
if a[si]==x: # If element at the index si is equal to the value of x
return si # Then it will return index si
return firstindex(a,x,si+1) # Function calling itself with the increment of si

a = [1,4,3,7,5,4,6] # Taking an Example
x = 4 # Find the first index of occurrence of 4
print(firstindex(a, x,0))

# Ouput:
# 1
25 changes: 25 additions & 0 deletions recursion/Basic Array Questions/Last_Index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Program to find last index of occurrence of an element in an array
# We will use start index approach

def lastindex(a, x, si):
l = len(a)
if si==l: # Base Case if size of array is 1 then it returns -1
return -1

smallerListOutput = lastindex(a, x, si+1) # calling function itself

if smallerListOutput != -1: # if smallerListOuput returns -1 then it will return smallerListOuput itself
return smallerListOutput # means when in smallerListOuput, value of si becomes equal to length of array
# it means no element found then it will return -1
else:
if a[si]==x: # if element at si is equal to x then it will return index si
return si
else:
return -1 # else return -1

a = [1,2,4,6,7,8,4,7,6] # Taking an array for example
x = 4 # Find the last index of occurrence of 4
print(lastindex(a,x,0))

# Output:
# 6
19 changes: 19 additions & 0 deletions recursion/Basic Array Questions/Max_arr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Program to find maximum element in an array using recursion

def max_arr_rec(a,n):
if n==1: # Base Case if array containing only one element then return it
return a[0]
else:
previous = max_arr_rec(a,n-1) # calling function itself and storing in variable called prevoius
current = a[n-1] # that means previous element from the sequence and check the value with the next element in the sequence which is current
if previous > current: # comparing both and returing the max of these values
return previous
else:
return current

a = [1, 42, 45, 60, 50, 10, 2] # Taking an array for example
n = len(a) # length of the array
print(max_arr_rec(a,n))

# Output:
# 60