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

Add Recursion #383

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
49 changes: 49 additions & 0 deletions data_structures/Recursion/Check AB.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"""
---------------------------------------------- Check AB ----------------------------------------

Suppose you have a string, S, made up of only 'a's and 'b's. Write a recursive function that checks
if the string was generated using the following rules:
a. The string begins with an 'a'
b. Each 'a' is followed by nothing or an 'a' or "bb"
c. Each "bb" is followed by nothing or an 'a'
If all the rules are followed by the given string, return true otherwise return false.

#### Input format :
String S

#### Output format :
'true' or 'false'

#### Constraints :
0 <= |S| <= 1000
where |S| represents length of string S.

#### Sample Input 1 :
abb
#### Sample Output 1 :
true

#### Sample Input 2 :
abababa
#### Sample Output 2 :
false
"""

def checkAB(str) :
if(len(str) == 0):
return True

if(str[0] == 'a') :
if(len(str[1:]) > 1 and str[1:3] == 'bb') :
return checkAB(str[3:])
else:
return checkAB(str[1:])
else :
return False

# Main
str=input()
if(checkAB(str)):
print("true")
else:
print("false")
52 changes: 52 additions & 0 deletions data_structures/Recursion/Check Number in Array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""
------------------------------------ Check Number in Array ---------------------------------------------

Given an array of length N and an integer x, you need to find if x is present in the array or not.
Return true or false.

Do this recursively.

#### Input Format :
Line 1 : An Integer N i.e. size of array
Line 2 : N integers which are elements of the array, separated by spaces
Line 3 : Integer x

#### Output Format :
'true' or 'false'

#### Constraints :
1 <= N <= 10^3

#### Sample Input 1 :
3
9 8 10
8
#### Sample Output 1 :
true

#### Sample Input 2 :
3
9 8 10
2
#### Sample Output 2 :
false
"""

def checkNumber(arr, x):
size=len(arr)
if size == 1:
return x==arr[0]

smallAns = checkNumber(arr[:size-1], x)
return smallAns or (x==arr[size-1])

# Main
from sys import setrecursionlimit
setrecursionlimit(11000)
n=int(input())
arr=list(int(i) for i in input().strip().split(' '))
x=int(input())
if checkNumber(arr, x):
print('true')
else:
print('false')
42 changes: 42 additions & 0 deletions data_structures/Recursion/Check Palindrome (recursive).py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""
---------------------------- Check Palindrome (recursive) -----------------------------

Check whether a given String S is a palindrome using recursion. Return true or false.

#### Input Format :
String S

#### Output Format :
'true' or 'false'

#### Constraints :
0 <= |S| <= 1000
where |S| represents length of string S.

#### Sample Input 1 :
racecar
#### Sample Output 1:
true

#### Sample Input 2 :
ninja
#### Sample Output 2:
false
"""

def RcheckPalindrome(str):
size=len(str)
if size <= 1:
return True
if str[0]!=str[size-1]:
return False
return RcheckPalindrome(str[1:-1])

# Main
from sys import setrecursionlimit
setrecursionlimit(11000)
str=input()
if RcheckPalindrome(str):
print('true')
else:
print('false')
42 changes: 42 additions & 0 deletions data_structures/Recursion/Count Zeros.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""
-------------------------------------- Count Zeros -------------------------------------------

Given an integer N, count and return the number of zeros that are present in the given integer using recursion.

#### Input Format :
Integer N

#### Output Format :
Number of zeros in N

#### Constraints :
0 <= N <= 10^9

#### Sample Input 1 :
10204
#### Sample Output 1 :
2

#### Sample Input 2 :
708000
#### Sample Output 2 :
4
"""

def countZeros(n):
if n<0:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can have an space between n < 10 as in the next line you have n == 0 with spaces ;)

n *= -1 # Make n positive
if n<10:
if n == 0:
return 1
return 0
smallAns = countZeros(n // 10)
if n%10==0:
smallAns += 1
return smallAns

# Main
from sys import setrecursionlimit
setrecursionlimit(11000)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better would be set_recursion_limit name

n=int(input())
print(countZeros(n))
47 changes: 47 additions & 0 deletions data_structures/Recursion/First Index of Number.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""
-------------------------------------- First Index of Number -----------------------------------------------

Given an array of length N and an integer x, you need to find and return the first index of integer x present
in the array. Return -1 if it is not present in the array.

First index means, the index of first occurrence of x in the input array.
Do this recursively. Indexing in the array starts from 0.

#### Input Format :
Line 1 : An Integer N i.e. size of array
Line 2 : N integers which are elements of the array, separated by spaces
Line 3 : Integer x

#### Output Format :
first index or -1

#### Constraints :
1 <= N <= 10^3

#### Sample Input :
4
9 8 10 8
8
#### Sample Output :
1
"""

def firstIndex(arr, x):
size=len(arr)
if size <= 0:
return -1
if x==arr[0]:
return 0
smallAns = firstIndex(arr[1:], x)
if smallAns == -1:
return -1
else:
return smallAns+1

# Main
from sys import setrecursionlimit
setrecursionlimit(11000)
n=int(input())
arr=list(int(i) for i in input().strip().split(' '))
x=int(input())
print(firstIndex(arr, x))
34 changes: 34 additions & 0 deletions data_structures/Recursion/Geometric Sum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""
----------------------- Geometric Sum ---------------------------

Given k, find the geometric sum i.e.
1 + 1/2 + 1/4 + 1/8 + ... + 1/(2^k)
using recursion.

#### Input format :
Integer k

#### Output format :
Geometric sum

#### Constraints :
0 <= k <= 1000

#### Sample Input 1 :
3
#### Sample Output 1 :
1.875

#### Sample Input 2 :
4
#### Sample Output 2 :
1.93750
"""
def gp(k):
if k<0:
return 0
return 1/(2**k) + gp(k-1)

# Main
k=int(input())
print("%.5f"%gp(k))
53 changes: 53 additions & 0 deletions data_structures/Recursion/Last Index Of Number.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""
----------------------------------------- Last Index Of Number ---------------------------------------------

Given an array of length N and an integer x, you need to find and return the last index of integer x present in
the array. Return -1 if it is not present in the array.
Last index means - if x is present multiple times in the array, return the index at which x comes last in the
array.

You should start traversing your array from 0, not from (N - 1).
Do this recursively. Indexing in the array starts from 0.

#### Input Format :
Line 1 : An Integer N i.e. size of array
Line 2 : N integers which are elements of the array, separated by spaces
Line 3 : Integer x

#### Output Format :
last index or -1

#### Constraints :
1 <= N <= 10^3

#### Sample Input :
4
9 8 10 8
8
#### Sample Output :
3
"""

def lastIndex(arr, x):

size=len(arr)
if size <= 0:
return -1

smallAns = lastIndex(arr[1:], x)

if smallAns == -1:
if x==arr[0]:
return 0
else:
return -1
else:
return smallAns+1

# Main
from sys import setrecursionlimit
setrecursionlimit(11000)
n=int(input())
arr=list(int(i) for i in input().strip().split(' '))
x=int(input())
print(lastIndex(arr, x))
46 changes: 46 additions & 0 deletions data_structures/Recursion/Multiplication (Recursive).py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
""""
----------------------------------- Multiplication (Recursive) -------------------------------------------

Given two integers M & N, calculate and return their multiplication using recursion. You can only use subtraction
and addition for your calculation. No other operators are allowed.

#### Input format :
Line 1 : Integer M
Line 2 : Integer N

#### Output format :
M x N

#### Constraints :
0 <= M <= 1000
0 <= N <= 1000

#### Sample Input 1 :
3
5
#### Sample Output 1 :
15

#### Sample Input 2 :
4
0
#### Sample Output 2 :
0
"""

def multiplyNumbers(m, n):
if m==0 or n==0:
return 0
if n>0:
smallAns = multiplyNumbers(m,n-1)
return smallAns + m
else:
smallAns = multiplyNumbers(m,n+1)
return smallAns - m

# Main
from sys import setrecursionlimit
setrecursionlimit(11000)
m=int(input())
n=int(input())
print(multiplyNumbers(m,n))
Loading