Skip to content
This repository was archived by the owner on Nov 18, 2022. It is now read-only.
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
15 changes: 15 additions & 0 deletions Python/Recursion/Binary_Search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
def binarySearch(a,x,si,ei):
if si>ei:
return -1
mid = (si+ei)//2
if a[mid]==x:
return mid
elif a[mid]>x:
return binarySearch(a,x,si,mid-1)
else:
return binarySearch(a,x,mid+1,ei)

a=[1,4,5,6,8,9,12,14,15,16,17,19]
x=15
ei = len(a)-1
print(binarySearch(a, x, 0, ei))
11 changes: 11 additions & 0 deletions Python/Recursion/Char_replace_in_string.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
def replace(s,a,b):
if len(s)==0:
return s
smallOutput = replace(s[1:],a,b)
if s[0] == a:
return b + smallOutput
else:
return s[0] + smallOutput

s = "bananana"
print(replace(s,"a","b"))
11 changes: 11 additions & 0 deletions Python/Recursion/CheckListSorted.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
def isSorted(a, si):
# si = start index
l = len(a)
if si == l-1 or si==l:
return True
if a[si]>a[si+1]:
return False
return isSorted(a, si+1)

a = [1,2,3,4,5]
print(isSorted(a, 0))
11 changes: 11 additions & 0 deletions Python/Recursion/Count_zero.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
def count_zero(n):
if n == 0:
return 0

if n%10==0:
return 1 + count_zero(int(n/10))
else:
return count_zero(int(n/10))

n = 405004020
print(count_zero(n))
7 changes: 7 additions & 0 deletions Python/Recursion/Factorial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
def fact(n):
if n==0:
return 1
return n * fact(n-1)

n = 5
print(fact(n))
8 changes: 8 additions & 0 deletions Python/Recursion/Fibonacci_Series.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
def fib(n):
if n==1 or n==2:
return 1
output = fib(n-1) + fib(n-2)
return output

n=6
print(fib(n))
11 changes: 11 additions & 0 deletions Python/Recursion/Find_element_in_arr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
def findarr(a,x):
l = len(a)
if l==0:
return False
if a[0]==x:
return True
return findarr(a[1:],x)

a = [1,2,3,4,5,6]
x = 6
print(findarr(a,x))
11 changes: 11 additions & 0 deletions Python/Recursion/First_index_occurrence.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
def firstindex(a, x, si):
l = len(a)
if si==l:
return -1
if a[si]==x:
return si
return firstindex(a,x,si+1)

a = [1,4,3,7,5,4,6]
x = 4
print(firstindex(a, x))
7 changes: 7 additions & 0 deletions Python/Recursion/Geometric_sum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
def geosum(k):
if k==0:
return 1
return (1/2**k) + geosum(k-1)

k = 3
print(geosum(k))
16 changes: 16 additions & 0 deletions Python/Recursion/Last_index_occurrence.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
def lastindex(a, x, si):
l = len(a)
if si==l:
return -1
smallerListOutput = lastindex(a, x, si+1)
if smallerListOutput != -1:
return smallerListOutput
else:
if a[si]==x:
return si
else:
return -1

a = [1,2,4,6,7,8,4,7,6]
x = 4
print(lastindex(a,x,0))
39 changes: 39 additions & 0 deletions Python/Recursion/Merge_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
def merge(a1,a2,a):
i = 0
j = 0
k = 0
while i<len(a1) and j<len(a2):
if a1[i]<a2[j]:
a[k] = a1[i]
k= k+1
i = i+1
else:
a[k]=a2[j]
k=k+1
j=j+1

while i<len(a1):
a[k] = a1[i]
k= k+1
i = i+1

while j<len(a2):
a[k]=a2[j]
k=k+1
j=j+1

def merge_sort(a):
if len(a)==0 or len(a)==1:
return
mid = len(a)//2
a1 = a[0:mid]
a2 = a[mid:]

merge_sort(a1)
merge_sort(a2)

merge(a1,a2,a)

a = [7,3,9,4,1,8,5,2,6]
merge_sort(a)
print(a)
7 changes: 7 additions & 0 deletions Python/Recursion/Multiplication.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
def mul(m,n):
if n==0:
return 0
a = mul(m,n-1)
return a + m

print(mul(5,4))
11 changes: 11 additions & 0 deletions Python/Recursion/Palindrome.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
def palindrome(s,si,ei):
if si>=ei:
return True
if s[si]==s[ei]:
return True
else:
return False
palindrome(s,si+1,ei-1)

s = "level"
print(palindrome(s,0,len(s)-1))
8 changes: 8 additions & 0 deletions Python/Recursion/Power.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
def power(x,n):
if n==0:
return 1
return (x*power(x,n-1))

x = int(input())
n = int(input())
print(power(x,n))
9 changes: 9 additions & 0 deletions Python/Recursion/Print_1_to_n.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def print_1_to_n(n):
if n==0:
return
print_1_to_n(n-1)
print(n)
return

n = 4
print(print_1_to_n(n))
8 changes: 8 additions & 0 deletions Python/Recursion/Print_n_to_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
def print_n_to_1(n):
if n==0:
return
print(n)
print_n_to_1(n-1)

n = 5
print(print_n_to_1(n))
34 changes: 34 additions & 0 deletions Python/Recursion/Quick_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
def partition(a,si,ei):
pivot = a[si]
c=0
for i in range(si,ei+1):
if a[i]<pivot:
c=c+1
a[si+c],a[si] = a[si],a[si+c]
pivot_index = si+c

i=si
j=ei

while i<j:
if a[i]<pivot:
i=i+1
elif a[j]>pivot:
j = j-1
else:
a[i],a[j] = a[j],a[i]
i=i+1
j=j-1

return pivot_index

def quicksort(a,si,ei):
if si>=ei:
return
pivot_index = partition(a, si, ei)
quicksort(a,si,pivot_index-1)
quicksort(a,pivot_index+1,ei)

a = [10,7,8,9,1,5]
quicksort(a,0,len(a)-1)
print(a)
11 changes: 11 additions & 0 deletions Python/Recursion/Remove.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
def remove(s,a):
if len(s)==0:
return s
smallOutput = remove(s[1:],a)
if s[0]==a:
return smallOutput
else:
return s[0] + smallOutput

s = "xaxxb"
print(remove(s, 'x'))
8 changes: 8 additions & 0 deletions Python/Recursion/Rempve_duplicate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
def duplicate(s):
if len(s)==0 or len(s)==1:
return s
if s[0]==s[1]:
return duplicate(s[1:])
return s[0] + duplicate(s[1:])

print(duplicate("xyxxtttzzwwa"))
13 changes: 13 additions & 0 deletions Python/Recursion/Replace_pi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
def replacePi(s):
if len(s)==0 or len(s)==1:
return s

if s[0]=="p" and s[1]=="i":
smalloutput = replacePi(s[2:])
return "3.14" + smalloutput

else:
smalloutput = replacePi(s[1:])
return s[0] + smalloutput

print(replacePi("abcpipipipppddfpi"))
11 changes: 11 additions & 0 deletions Python/Recursion/String_to_integer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
def strtoint(s):
if len(s)==1:
return ord(s[0]) - ord('0')
y = strtoint(s[1:])
x = ord(s[0]) - ord('0')
x = x* (10**(len(s) - 1)) + y
return x

s = "12345"
print(strtoint(s))
print(type(strtoint(s)))
9 changes: 9 additions & 0 deletions Python/Recursion/Sum_of_arr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def arrsum(a):
l = len(a)
if l==1:
return a[0]
else:
return a[0] + arrsum(a[1:])

a = [9,8,9]
print(arrsum(a))
7 changes: 7 additions & 0 deletions Python/Recursion/Sum_of_given_int.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
def sum_int(n):
if n==0:
return 0
return (n%10 + sum_int(int(n/10)))

n =12345
print(sum_int(n))
7 changes: 7 additions & 0 deletions Python/Recursion/Sum_of_n.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
def sum_n(n):
if n==0:
return 0
return n + sum_n(n-1)

n = 5
print(sum_n(n))
13 changes: 13 additions & 0 deletions Python/Recursion/Tower_of_Hanoi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
def towerofhanoi(n,source,destination,auxiliary):
if n==1:
print(source,destination)
return
towerofhanoi(n-1,source,auxiliary,destination)
print(source,auxiliary)
towerofhanoi(n-1,auxiliary,destination,source)

n = 3
source = "a"
auxiliary = "b"
destination = "c"
print(towerofhanoi(n,source,auxiliary,destination))