diff --git a/Python/Recursion/Binary_Search.py b/Python/Recursion/Binary_Search.py new file mode 100644 index 0000000..5610324 --- /dev/null +++ b/Python/Recursion/Binary_Search.py @@ -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)) \ No newline at end of file diff --git a/Python/Recursion/Char_replace_in_string.py b/Python/Recursion/Char_replace_in_string.py new file mode 100644 index 0000000..5690808 --- /dev/null +++ b/Python/Recursion/Char_replace_in_string.py @@ -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")) \ No newline at end of file diff --git a/Python/Recursion/CheckListSorted.py b/Python/Recursion/CheckListSorted.py new file mode 100644 index 0000000..287458f --- /dev/null +++ b/Python/Recursion/CheckListSorted.py @@ -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)) \ No newline at end of file diff --git a/Python/Recursion/Count_zero.py b/Python/Recursion/Count_zero.py new file mode 100644 index 0000000..2468e37 --- /dev/null +++ b/Python/Recursion/Count_zero.py @@ -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)) \ No newline at end of file diff --git a/Python/Recursion/Factorial.py b/Python/Recursion/Factorial.py new file mode 100644 index 0000000..ce281b9 --- /dev/null +++ b/Python/Recursion/Factorial.py @@ -0,0 +1,7 @@ +def fact(n): + if n==0: + return 1 + return n * fact(n-1) + +n = 5 +print(fact(n)) \ No newline at end of file diff --git a/Python/Recursion/Fibonacci_Series.py b/Python/Recursion/Fibonacci_Series.py new file mode 100644 index 0000000..c3b7a9e --- /dev/null +++ b/Python/Recursion/Fibonacci_Series.py @@ -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)) \ No newline at end of file diff --git a/Python/Recursion/Find_element_in_arr.py b/Python/Recursion/Find_element_in_arr.py new file mode 100644 index 0000000..156c480 --- /dev/null +++ b/Python/Recursion/Find_element_in_arr.py @@ -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)) \ No newline at end of file diff --git a/Python/Recursion/First_index_occurrence.py b/Python/Recursion/First_index_occurrence.py new file mode 100644 index 0000000..37f5fa2 --- /dev/null +++ b/Python/Recursion/First_index_occurrence.py @@ -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)) \ No newline at end of file diff --git a/Python/Recursion/Geometric_sum.py b/Python/Recursion/Geometric_sum.py new file mode 100644 index 0000000..8ce3b14 --- /dev/null +++ b/Python/Recursion/Geometric_sum.py @@ -0,0 +1,7 @@ +def geosum(k): + if k==0: + return 1 + return (1/2**k) + geosum(k-1) + +k = 3 +print(geosum(k)) \ No newline at end of file diff --git a/Python/Recursion/Last_index_occurrence.py b/Python/Recursion/Last_index_occurrence.py new file mode 100644 index 0000000..3bcac6e --- /dev/null +++ b/Python/Recursion/Last_index_occurrence.py @@ -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)) \ No newline at end of file diff --git a/Python/Recursion/Merge_sort.py b/Python/Recursion/Merge_sort.py new file mode 100644 index 0000000..6c099b1 --- /dev/null +++ b/Python/Recursion/Merge_sort.py @@ -0,0 +1,39 @@ +def merge(a1,a2,a): + i = 0 + j = 0 + k = 0 + while i=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)) \ No newline at end of file diff --git a/Python/Recursion/Power.py b/Python/Recursion/Power.py new file mode 100644 index 0000000..c805efe --- /dev/null +++ b/Python/Recursion/Power.py @@ -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)) \ No newline at end of file diff --git a/Python/Recursion/Print_1_to_n.py b/Python/Recursion/Print_1_to_n.py new file mode 100644 index 0000000..096e140 --- /dev/null +++ b/Python/Recursion/Print_1_to_n.py @@ -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)) \ No newline at end of file diff --git a/Python/Recursion/Print_n_to_1.py b/Python/Recursion/Print_n_to_1.py new file mode 100644 index 0000000..1d072bf --- /dev/null +++ b/Python/Recursion/Print_n_to_1.py @@ -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)) \ No newline at end of file diff --git a/Python/Recursion/Quick_sort.py b/Python/Recursion/Quick_sort.py new file mode 100644 index 0000000..ecb8407 --- /dev/null +++ b/Python/Recursion/Quick_sort.py @@ -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: + 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) \ No newline at end of file diff --git a/Python/Recursion/Remove.py b/Python/Recursion/Remove.py new file mode 100644 index 0000000..328ece6 --- /dev/null +++ b/Python/Recursion/Remove.py @@ -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')) \ No newline at end of file diff --git a/Python/Recursion/Rempve_duplicate.py b/Python/Recursion/Rempve_duplicate.py new file mode 100644 index 0000000..81fb3bc --- /dev/null +++ b/Python/Recursion/Rempve_duplicate.py @@ -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")) \ No newline at end of file diff --git a/Python/Recursion/Replace_pi.py b/Python/Recursion/Replace_pi.py new file mode 100644 index 0000000..37ed4ab --- /dev/null +++ b/Python/Recursion/Replace_pi.py @@ -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")) \ No newline at end of file diff --git a/Python/Recursion/String_to_integer.py b/Python/Recursion/String_to_integer.py new file mode 100644 index 0000000..535c039 --- /dev/null +++ b/Python/Recursion/String_to_integer.py @@ -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))) \ No newline at end of file diff --git a/Python/Recursion/Sum_of_arr.py b/Python/Recursion/Sum_of_arr.py new file mode 100644 index 0000000..3e3352a --- /dev/null +++ b/Python/Recursion/Sum_of_arr.py @@ -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)) \ No newline at end of file diff --git a/Python/Recursion/Sum_of_given_int.py b/Python/Recursion/Sum_of_given_int.py new file mode 100644 index 0000000..960222a --- /dev/null +++ b/Python/Recursion/Sum_of_given_int.py @@ -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)) \ No newline at end of file diff --git a/Python/Recursion/Sum_of_n.py b/Python/Recursion/Sum_of_n.py new file mode 100644 index 0000000..cc51549 --- /dev/null +++ b/Python/Recursion/Sum_of_n.py @@ -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)) \ No newline at end of file diff --git a/Python/Recursion/Tower_of_Hanoi.py b/Python/Recursion/Tower_of_Hanoi.py new file mode 100644 index 0000000..1aa8e9a --- /dev/null +++ b/Python/Recursion/Tower_of_Hanoi.py @@ -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)) \ No newline at end of file