-
Notifications
You must be signed in to change notification settings - Fork 608
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
piyushP7pravin
wants to merge
1
commit into
prabhupant:master
Choose a base branch
from
piyushP7pravin:Add_recursion
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add Recursion #383
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: | ||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 ;)